html tool

2014年7月22日星期二

goagent_local源码阅读_class Logging(type(sys))_II

 
      def __init__(self,*args,**kwargs):
        self.level = self.__class__.INFO //1
        self.__set_error_color = lambda: None
        self.__set_warning_color = lambda: None
        self.__set_debug_color = lambda: None
        self.__reset_color = lambda: None
        if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty(): //2
            if os.name == 'nt': //3
                import ctypes //5
                SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute //6
                GetStdHandle = ctypes.windll.kernel32.GetStdHandle //7
                self.__set_error_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x04)//8
                self.__set_warning_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x06) //9
                self.__set_debug_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x002) //10
                self.__reset_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x07) //11
            elif os.name == 'posix': //4
                self.__set_error_color = lambda: sys.stderr.write('\033[31m')//12     
                self.__set_warning_color = lambda: sys.stderr.write('\033[33m')
                self.__set_debug_color = lambda: sys.stderr.write('\033[32m')

                self.__reset_color = lambda: sys.stderr.write('\033[0m')  


12
sys.stderr.write
add:http://stackoverflow.com/questions/6195877/python-colour-printing-with-decorator-in-a-function
The simplest solution would be similar to what Pete suggest. Just print the escape codes before running the function to each of stderr and stdout. However, if both stderr and stdout feed the same terminal, as is usually the case, they will interfere
那就是在输出字符前直接定义颜色就可以了;
而颜色用的是ANSI的color的定义
add:http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
有详细的说明,这里的例子是ls下的看来linux在shell中直接就定义这样使用
]
这个_init__是对不同os下的日志打印颜色做定义用的,日志区分为:
error
warni
debug
reset

级别     

没有评论:

发表评论