html tool

2014年7月15日星期二

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


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')

[popexizhi:
1.__class__ is the instance’s class ,        self.level = self.__class__.INFO 是引用类的全局变量INFO = 20
2.hasattr(sys.stderr, 'isatty') 检查sys.stderr.isatty是存在的,and sys.stderr.isatty()如果存在这个那就检查一下是否为tty设备
sys.stderr linux中错误的输出流,下面是help的说明
The objects sys.stdin, sys.stdout and sys.stderr are initialized to file objects corresponding to the interpreter’s standard input, output and error streams. See File Objects for complete documentation of file objects
这个是file objects中关于isatty的说明
file.isatty()
Return True if the file is connected to a tty(-like) device, else False.
Note
If a file-like object is not associated with a real file, this method should not  be implemented
go了一下tty文件: 应该是特殊的虚拟终端或端口设备文件的检查
http://oss.org.cn/kernel-book/ldd3/ch18.html
http://flykof.pixnet.net/blog/post/24277709-%5B%E8%BD%89%E8%BC%89%E6%95%B4%E7%90%86%5Dwhat-is-tty%3F
3.“nt”及windows系列的操作系统
4.'posix' 及unix类的操作系统
5.
import ctypes [help]
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python

6. SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute 的介绍
add:http://msdn.microsoft.com/en-us/library/ms686047(VS.85).aspx
Sets the attributes of characters written to the console screen buffer by the WriteFile or WriteConsole function, or echoed by the ReadFile or ReadConsolefunction. This function affects text written after the function call.
这个是windows的文本或控制台写入属性的设置函数c++的函数头如下:
Syntax
C++
BOOL WINAPI SetConsoleTextAttribute(
  _In_  HANDLE hConsoleOutput,
  _In_  WORD wAttributes
);

Parameters
hConsoleOutput [in] :A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see Console Buffer Security and Access Rights.
wAttributes [in] :The character attributes.
地址中有详细的此函数的来源,返回值,参数等等内容,windows提供的内容

7. GetStdHandle = ctypes.windll.kernel32.GetStdHandle
add:http://msdn.microsoft.com/en-us/library/ms683231(v=vs.85).aspx
Retrieves a handle to the specified standard device (standard input, standard output, or standard error).
原来windows也有input,output and error.之前在shell中使用以为只有linux做了相关区分呢,看来自己对win32的系统还真是不熟悉:)
看原文的参数说明
下面的-11使用的就是output [GetStdHandle(-11)]
STD_OUTPUT_HANDLE
(DWORD)-11   :The standard output device. Initially, this is the active console screen buffer, CONOUT$.
[8,9,10,11]
add:http://www.cnblogs.com/morewindows/archive/2011/09/19.html [这个是自己设置用c++的过程]
add:http://openexz.sinaapp.com/2011/02/17/%E8%BD%ACcc%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%87%BA%E6%97%B6%E8%AE%BE%E7%BD%AE%E5%AD%97%E4%BD%93%E5%8F%8A%E8%83%8C%E6%99%AF%E9%A2%9C%E8%89%B2/[这个有详细的颜色定义]
这样翻译过来就是
8  error_color 定义背景色黑色 ,字体red[4]
9 warning_color定义背景色黑色,字体黄色[6]
10 debug_color 定义背景色黑色,字体绿色[2]
11 reset_color 定义背景色黑色,字体蓝色[1]
12[next]

       ]     

没有评论:

发表评论