Ajout de GroupUserFolder extrait de l'état suivant :
[GroupUserFolder.git] / Log.py
1 # -*- coding: utf-8 -*-
2 ## GroupUserFolder
3 ## Copyright (C)2006 Ingeniweb
4
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 2 of the License, or
8 ## (at your option) any later version.
9
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
14
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; see the file COPYING. If not, write to the
17 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 """
19 One can override the following variables :
20
21 LOG_LEVEL : The log level, from 0 to 5.
22 A Log level n implies all logs from 0 to n.
23 LOG_LEVEL MUST BE OVERRIDEN !!!!!
24
25
26 LOG_NONE = 0 => No log output
27 LOG_CRITICAL = 1 => Critical problems (data consistency, module integrity, ...)
28 LOG_ERROR = 2 => Error (runtime exceptions, ...)
29 LOG_WARNING = 3 => Warning (non-blocking exceptions, ...)
30 LOG_NOTICE = 4 => Notices (Special conditions, ...)
31 LOG_DEBUG = 5 => Debug (Debugging information)
32
33
34 LOG_PROCESSOR : A dictionnary holding, for each key, the data processor.
35 A data processor is a function that takes only one parameter : the data to print.
36 Default : LogFile for all keys.
37 """
38 __version__ = "$Revision: $"
39 # $Source: $
40 # $Id: Log.py 33389 2006-11-11 11:24:41Z shh42 $
41 __docformat__ = 'restructuredtext'
42
43
44
45 LOG_LEVEL = -1
46
47 LOG_NONE = 0
48 LOG_CRITICAL = 1
49 LOG_ERROR = 2
50 LOG_WARNING = 3
51 LOG_NOTICE = 4
52 LOG_DEBUG = 5
53
54 from sys import stdout, stderr, exc_info
55 import time
56 import thread
57 import threading
58 import traceback
59 import os
60 import pprint
61 import string
62
63 LOG_STACK_DEPTH = [-2]
64
65 def Log(level, *args):
66 """
67 Log(level, *args) => Pretty-prints data on the console with additional information.
68 """
69 if LOG_LEVEL and level <= LOG_LEVEL:
70 if not level in LOG_PROCESSOR.keys():
71 raise ValueError, "Invalid log level :", level
72
73 stack = ""
74 stackItems = traceback.extract_stack()
75 for depth in LOG_STACK_DEPTH:
76 stackItem = stackItems[depth]
77 stack = "%s%s:%s:" % (stack, os.path.basename(stackItem[0]), stackItem[1],)
78 pr = "%8s %s%s: " % (
79 LOG_LABEL[level],
80 stack,
81 time.ctime(time.time()),
82 )
83 for data in args:
84 try:
85 if "\n" in data:
86 data = data
87 else:
88 data = pprint.pformat(data)
89 except:
90 data = pprint.pformat(data)
91 pr = pr + data + " "
92
93 LOG_PROCESSOR[level](level, LOG_LABEL[level], pr, )
94
95 def LogCallStack(level, *args):
96 """
97 LogCallStack(level, *args) => View the whole call stack for the specified call
98 """
99 if LOG_LEVEL and level <= LOG_LEVEL:
100 if not level in LOG_PROCESSOR.keys():
101 raise ValueError, "Invalid log level :", level
102
103 stack = string.join(traceback.format_list(traceback.extract_stack()[:-1]))
104 pr = "%8s %s:\n%s\n" % (
105 LOG_LABEL[level],
106 time.ctime(time.time()),
107 stack
108 )
109 for data in args:
110 try:
111 if "\n" in data:
112 data = data
113 else:
114 data = pprint.pformat(data)
115 except:
116 data = pprint.pformat(data)
117 pr = pr + data + " "
118
119 LOG_PROCESSOR[level](level, LOG_LABEL[level], pr, )
120
121
122
123 def FormatStack(stack):
124 """
125 FormatStack(stack) => string
126
127 Return a 'loggable' version of the stack trace
128 """
129 ret = ""
130 for s in stack:
131 ret = ret + "%s:%s:%s: %s\n" % (os.path.basename(s[0]), s[1], s[2], s[3])
132 return ret
133
134
135 def LogException():
136 """
137 LogException () => None
138
139 Print an exception information on the console
140 """
141 Log(LOG_NOTICE, "EXCEPTION >>>")
142 traceback.print_exc(file = LOG_OUTPUT)
143 Log(LOG_NOTICE, "<<< EXCEPTION")
144
145
146 LOG_OUTPUT = stderr
147 def LogFile(level, label, data, ):
148 """
149 LogFile : writes data to the LOG_OUTPUT file.
150 """
151 LOG_OUTPUT.write(data+'\n')
152 LOG_OUTPUT.flush()
153
154
155 import logging
156
157 CUSTOM_TRACE = 5
158 logging.addLevelName('TRACE', CUSTOM_TRACE)
159
160 zLogLevelConverter = {
161 LOG_NONE: CUSTOM_TRACE,
162 LOG_CRITICAL: logging.CRITICAL,
163 LOG_ERROR: logging.ERROR,
164 LOG_WARNING: logging.WARNING,
165 LOG_NOTICE: logging.INFO,
166 LOG_DEBUG: logging.DEBUG,
167 }
168
169 def LogzLog(level, label, data, ):
170 """
171 LogzLog : writes data though Zope's logging facility
172 """
173 logger = logging.getLogger('GroupUserFolder')
174 logger.log(zLogLevelConverter[level], data + "\n", )
175
176
177
178 LOG_PROCESSOR = {
179 LOG_NONE: LogzLog,
180 LOG_CRITICAL: LogzLog,
181 LOG_ERROR: LogzLog,
182 LOG_WARNING: LogzLog,
183 LOG_NOTICE: LogzLog,
184 LOG_DEBUG: LogFile,
185 }
186
187
188 LOG_LABEL = {
189 LOG_NONE: "",
190 LOG_CRITICAL: "CRITICAL",
191 LOG_ERROR: "ERROR ",
192 LOG_WARNING: "WARNING ",
193 LOG_NOTICE: "NOTICE ",
194 LOG_DEBUG: "DEBUG ",
195 }