X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/edb90fb159a4bc69cfc2f804a1f858ad46ae2090..53b2bb2bcfedf9c8cb4e94c7ce8cbeb40bf033da:/lib/wizard/command/_base.py diff --git a/lib/wizard/command/_base.py b/lib/wizard/command/_base.py index 2ba6e2f..9303269 100644 --- a/lib/wizard/command/_base.py +++ b/lib/wizard/command/_base.py @@ -12,14 +12,40 @@ def makeLogger(options): logger = logging.getLogger("main") logger.setLevel(logging.INFO) stdout = logging.StreamHandler(sys.stdout) + stdout.setFormatter(logging.Formatter(" " * int(options.indent) + '%(message)s')) logger.addHandler(stdout) - if options.verbose: - logger.verbose = True + if options.log_file: + file = logging.FileHandler(options.log_file) + logger.addHandler(file) + if options.debug: + logger.setLevel(logging.DEBUG) else: - if not options.debug: stdout.setLevel(logging.ERROR) - if options.debug: logger.setLevel(logging.DEBUG) + stdout.setLevel(logging.WARNING) + if options.verbose or options.dry_run: + stdout.setLevel(logging.INFO) + if options.log_file: + file.setLevel(logging.INFO) return logger +def makeBaseArgs(options, **grab): + """Takes parsed options, and breaks them back into a command + line string that we can pass into a subcommand""" + args = [] + grab["log_file"] = "--log-file" + grab["debug"] = "--debug" + grab["verbose"] = "--verbose" + grab["indent"] = "--indent" + #grab["log_db"] = "--log-db" + for k,flag in grab.items(): + value = getattr(options, k) + if not value and k != "indent": continue + args.append(flag) + if type(value) is not bool: + if k == "indent": + value += 4 + args.append(str(value)) + return args + class NullLogHandler(logging.Handler): """Log handler that doesn't do anything""" def emit(self, record): @@ -33,6 +59,10 @@ class WizardOptionParser(optparse.OptionParser): default=False, help="Turns on verbose output") self.add_option("--debug", dest="debug", action="store_true", default=False, help="Turns on debugging output") + self.add_option("--log-file", dest="log_file", + default=None, help="Logs verbose output to file") + self.add_option("--indent", dest="indent", + default=0, help="Indents stdout, useful for nested calls") def parse_all(self, argv, logger): options, numeric_args = self.parse_args(argv) return options, numeric_args, logger and logger or makeLogger(options)