-
Notifications
You must be signed in to change notification settings - Fork 15
/
maclient_logging.py
120 lines (103 loc) · 4.28 KB
/
maclient_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# coding:utf-8
import os
import sys
from datetime import datetime, tzinfo, timedelta
import locale
import logging
import logging.handlers
from cross_platform import *
class zh_BJ(tzinfo):
def utcoffset(self, dt):
return timedelta(hours = 8)
def dst(self, dt):
return timedelta(0)
class Logging(object):
# paste from goagent
CRITICAL = 5
FATAL = CRITICAL
ERROR = 4
WARNING = 3
WARN = WARNING
INFO = 2
SLEEP = INFO
DEBUG = 1
NOTSET = 0
def __init__(self, *args, **kwargs):
self.level = self.__class__.INFO
self.__write = __write = lambda x:sys.stdout.write(safestr(x))
self.isatty = getattr(sys.stdout, 'isatty', lambda: False)()
self.__set_error_color = lambda: None
self.__set_warning_color = lambda: None
self.__set_debug_color = lambda: None
self.__set_sleep_color = lambda: None
self.__reset_color = lambda: None
if self.isatty:
if os.name == 'nt':
import ctypes
SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
GetStdHandle = ctypes.windll.kernel32.GetStdHandle
self.__set_error_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x0C)
self.__set_warning_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x06)
self.__set_debug_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x02)
self.__set_sleep_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x08)
self.__set_bright_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x0F)
self.__reset_color = lambda: SetConsoleTextAttribute(GetStdHandle(-11), 0x07)
elif os.name == 'posix':
self.__set_error_color = lambda: __write('\033[31m')
self.__set_warning_color = lambda: __write('\033[33m')
self.__set_debug_color = lambda: __write('\033[32m')
self.__set_sleep_color = lambda: __write('\033[36m')
self.__set_bright_color = lambda: __write('\033[32m')
self.__reset_color = lambda: __write('\033[0m')
self.logfile = None
def setlogfile(self, f):
self.logfile = open(f, 'a')
def logpipe(self, to):
self.__write = to
@classmethod
def getLogger(cls, *args, **kwargs):
return cls(*args, **kwargs)
def basicConfig(self, *args, **kwargs):
self.level = int(kwargs.get('level', self.__class__.INFO))
if self.level > self.__class__.DEBUG:
self.debug = self.dummy
def log(self, level, fmt, *args, **kwargs):
# fmt=du8(fmt)
try:
self.__write(raw_du8('%-5s - [%s] %s\n' % (level, datetime.now(zh_BJ()).strftime('%X'), fmt % args)))
except (ValueError, TypeError):
fmt = fmt.replace('%','%%')
self.__write(raw_du8('%-5s - [%s] %s\n' % (level, datetime.now(zh_BJ()).strftime('%X'), fmt % args)))
sys.stdout.flush()
return '[%s] %s\n' % (datetime.now(zh_BJ()).strftime('%b %d %X'), fmt % args)
def dummy(self, *args, **kwargs):
pass
def debug(self, fmt, *args, **kwargs):
self.__set_debug_color()
self.log('DEBUG', fmt, *args, **kwargs)
self.__reset_color()
def info(self, fmt, *args, **kwargs):
puretext = self.log('INFO', fmt, *args)
if self.logfile:
self.logfile.write(puretext)
def sleep(self, fmt, *args, **kwargs):
self.__set_sleep_color()
self.log('SLEEP', fmt, *args, **kwargs)
self.__reset_color()
def warning(self, fmt, *args, **kwargs):
self.__set_warning_color()
self.log('WARNING', fmt, *args, **kwargs)
self.__reset_color()
def warn(self, fmt, *args, **kwargs):
self.warning(fmt, *args, **kwargs)
def error(self, fmt, *args, **kwargs):
self.__set_error_color()
self.log('ERROR', fmt, *args, **kwargs)
self.__reset_color()
def exception(self, fmt, *args, **kwargs):
self.error(fmt, *args, **kwargs)
traceback.print_exc(file = sys.stderr)
def critical(self, fmt, *args, **kwargs):
self.__set_error_color()
self.log('CRITICAL', fmt, *args, **kwargs)
self.__reset_color()