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
121
|
# vim: set fileencoding=utf-8 :
#
# (C) 2010 Guido Guenther <agx@sigxcpu.org>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""Simple colored logging classes"""
import os
import sys
logger = None
class Logger(object):
DEBUG, INFO, WARNING, ERROR = range(4)
COLOR_NONE = 0
COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW = range(30,34)
COLOR_SEQ = "\033[%dm"
BOLD_SEQ = "\033[1m"
format = ("%(color)s"
"gbp:%(levelname)s: "
"%(message)s"
"%(coloroff)s")
def __init__(self):
self.levels = { self.DEBUG: [ 'debug', self.COLOR_GREEN ],
self.INFO: [ 'info', self.COLOR_GREEN ],
self.WARNING: [ 'warning', self.COLOR_YELLOW ],
self.ERROR: [ 'error', self.COLOR_RED ], }
self.color = False
self.level = self.INFO
self.get_color = self.get_coloroff = self._color_dummy
def set_level(self, level):
self.level = level
def _is_tty(self):
if (os.getenv("EMACS") and
os.getenv("INSIDE_EMACS", "").endswith(",comint")):
return False
if (sys.stderr.isatty() and
sys.stdout.isatty()):
return True
return False
def set_color(self, color):
if type(color) == type(True):
self.color = color
else:
if color.is_on():
self.color = True
elif color.is_auto():
self.color = self._is_tty()
else:
self.color = False
if self.color:
self.get_color = self._color
self.get_coloroff = self._color_off
else:
self.get_color = self.get_coloroff = self._color_dummy
def _color_dummy(self, level=None):
return ""
def _color(self, level):
return self.COLOR_SEQ % (self.levels[level][1])
def _color_off(self):
return self.COLOR_SEQ % self.COLOR_NONE
def log(self, level, message):
if level < self.level:
return
out = [sys.stdout, sys.stderr][level >= self.WARNING]
print >>out, self.format % { 'levelname': self.levels[level][0],
'color': self.get_color(level),
'message': message,
'coloroff': self.get_coloroff()}
def err(msg):
logger.log(Logger.ERROR, msg)
def warn(msg):
logger.log(Logger.WARNING, msg)
def info(msg):
logger.log(Logger.INFO, msg)
def debug(msg):
logger.log(Logger.DEBUG, msg)
def setup(color, verbose):
logger.set_color(color)
if verbose:
logger.set_level(Logger.DEBUG)
if not logger:
logger = Logger()
|