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
|
#!/usr/bin/python -tt
#
# Copyright 2012 David Sommerseth <davids@redhat.com>
#
# 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
#
# For the avoidance of doubt the "preferred form" of this code is one which
# is in an open unpatent encumbered format. Where cryptographic key signing
# forms part of the process of creating an executable the information
# including keys needed to generate an equivalently functional executable
# are deemed to be part of the source code.
#
import sys
class Log(object):
NONE = 0
ALWAYS = 0
INFO = 1<<0
WARN = 1<<1
ERR = 1<<2
DEBUG = 1<<3
def __init__(self, logfile=None):
if logfile is not None:
self.__logfile = open(logfile, "w")
else:
self.__logfile = sys.stdout
self.__logverb = self.INFO
def __logtype_str(self, ltype):
if ltype == self.ALWAYS:
return ""
if ltype == self.INFO:
return "[INFO] "
elif ltype == self.WARN:
return "[WARNING] "
elif ltype == self.ERR:
return "[ERROR] "
elif ltype == self.DEBUG:
return "[DEBUG] "
def SetLogVerbosity(self, logverb):
self.__logverb = logverb
def log(self, logtype, msg):
if (logtype & self.__logverb) or logtype == self.ALWAYS:
self.__logfile.write("%s%s\n" %
(self.__logtype_str(logtype),
msg)
)
def unit_test(rootdir):
from itertools import takewhile, count
logtypes = (Log.ALWAYS, Log.INFO, Log.WARN, Log.ERR, Log.DEBUG)
logtypes_s = ("ALWAYS", "INFO", "WARN", "ERR", "DEBUG")
def test_log(l, msg):
for lt in logtypes:
l.log(lt, msg)
def run_log_test(l):
for lt in range(min(logtypes), max(logtypes)*2):
test = ", ".join([logtypes_s[logtypes.index(i)] for i in [p for p in takewhile(lambda x: x <= lt, (2**i for i in count())) if p & lt]])
print "Testing verbosity flags set to: (%i) %s" % (lt, test)
msg = "Log entry when verbosity is set to %i [%s]" % (lt, test)
l.SetLogVerbosity(lt)
test_log(l, msg)
print "-"*20
try:
print "** Testing stdout"
l = Log()
run_log_test(l)
print "** Testing file logging - using test.log"
l = Log("test.log")
run_log_test(l)
return 0
except Exception, e:
import traceback
traceback.print_exc(file=sys.stdout)
print "** EXCEPTION %s", str(e)
return 1
if __name__ == '__main__':
unit_test(None)
|