From 72a796d1bce29b02c2d6e3e9f0709f81dc33070c Mon Sep 17 00:00:00 2001 From: JF Ding Date: Fri, 9 Sep 2011 15:10:03 +0900 Subject: [PATCH] enhance msger module to support logfile --- mic/creator.py | 5 +++++ mic/msger.py | 66 +++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/mic/creator.py b/mic/creator.py index 0d4f268..6db5635 100644 --- a/mic/creator.py +++ b/mic/creator.py @@ -60,6 +60,7 @@ class Creator(cmdln.Cmdln): optparser.add_option('-v', '--verbose', action='store_true', dest='verbose', help=SUPPRESS_HELP) optparser.add_option('-o', '--outdir', type='string', action='store', dest='outdir', default=None, help='output directory') optparser.add_option('', '--local-pkgs-path', type='string', dest='local_pkgs_path', default=None, help='Path for local pkgs(rpms) to be installed') + optparser.add_option('', '--logfile', type='string', dest='logfile', default=None, help='Path of logfile') return optparser def preoptparse(self, argv): @@ -111,6 +112,10 @@ class Creator(cmdln.Cmdln): if self.options.local_pkgs_path is not None: self.configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path + if self.options.logfile: + msger.set_interactive(False) + msger.set_logfile(self.options.logfile) + def main(self, argv=None): if argv is None: argv = sys.argv diff --git a/mic/msger.py b/mic/msger.py index d01a47c..a0bc047 100644 --- a/mic/msger.py +++ b/mic/msger.py @@ -21,7 +21,19 @@ import os,sys import re -__ALL__ = ['set_mode', 'get_loglevel', 'set_loglevel', 'raw' 'debug', 'verbose', 'info', 'warning', 'error', 'ask', 'pause'] +__ALL__ = ['set_mode', + 'get_loglevel', + 'set_loglevel', + 'set_logfile', + 'raw', + 'debug', + 'verbose', + 'info', + 'warning', + 'error', + 'ask', + 'pause', + ] # COLORs in ANSI INFO_COLOR = 32 # green @@ -43,11 +55,19 @@ LOG_LEVELS = { } LOG_LEVEL = 1 -def _color_print(head, color, msg = None, stream = sys.stdout, level = 'normal'): +LOG_FILE_FP = None + +def _general_print(head, color, msg = None, stream = sys.stdout, level = 'normal'): if LOG_LEVELS[level] > LOG_LEVEL: # skip return + if LOG_FILE_FP: + LOG_FILE_FP.write(msg.strip() + '\n') + + _color_print(head, color, msg, stream, level) + +def _color_print(head, color, msg, stream, level): colored = True if color == NO_COLOR or \ not stream.isatty() or \ @@ -80,7 +100,7 @@ def _color_print(head, color, msg = None, stream = sys.stdout, level = 'normal') stream.flush() def _color_perror(head, color, msg, level = 'normal'): - _color_print(head, color, msg, sys.stderr, level) + _general_print(head, color, msg, sys.stderr, level) def _split_msg(head, msg): if isinstance(msg, list): @@ -114,26 +134,23 @@ def set_loglevel(level): LOG_LEVEL = LOG_LEVELS[level] -def set_mode(interactive): +def set_interactive(mode=True): global INTERACTIVE - if interactive: + if mode: INTERACTIVE = True else: INTERACTIVE = False -def raw(msg=None): - if msg: - sys.stdout.write(msg) - sys.stdout.write('\n') - sys.stdout.flush() +def raw(msg=''): + _general_print('', NO_COLOR, msg) def info(msg): head, msg = _split_msg('Info', msg) - _color_print(head, INFO_COLOR, msg) + _general_print(head, INFO_COLOR, msg) def verbose(msg): head, msg = _split_msg('Verbose', msg) - _color_print(head, INFO_COLOR, msg, level = 'verbose') + _general_print(head, INFO_COLOR, msg, level = 'verbose') def warning(msg): head, msg = _split_msg('Warning', msg) @@ -149,7 +166,7 @@ def error(msg): sys.exit(1) def ask(msg, default=True): - _color_print('\rQ', ASK_COLOR, '') + _general_print('\rQ', ASK_COLOR, '') try: if default: msg += '(Y/n) ' @@ -168,11 +185,12 @@ def ask(msg, default=True): # else loop else: - sys.stdout.write('%s ' % msg) if default: - sys.stdout.write('Y\n') + msg += ' Y' else: - sys.stdout.write('N\n') + msg += ' N' + _general_print('', NO_COLOR, msg) + return default except KeyboardInterrupt: sys.stdout.write('\n') @@ -180,8 +198,22 @@ def ask(msg, default=True): def pause(msg=None): if INTERACTIVE: - _color_print('\rQ', ASK_COLOR, '') + _general_print('\rQ', ASK_COLOR, '') if msg is None: msg = 'press to continue ...' raw_input(msg) +def set_logfile(fpath): + global LOG_FILE_FP + + def _closelogf(): + if LOG_FILE_FP: + LOG_FILE_FP.close() + + if LOG_FILE_FP is not None: + warning('duplicate log file configuration') + + LOG_FILE_FP = open(os.path.abspath(os.path.expanduser(fpath)), 'a') + + import atexit + atexit.register(_closelogf) -- 2.7.4