File: aglogging.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (375 lines) | stat: -rw-r--r-- 13,833 bytes parent folder | download | duplicates (3)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#----------------------------------------------------------------------------
# Name:         aglogging.py
# Purpose:      Utilities to help with logging
#
# Author:       Jeff Norton
#
# Created:      01/04/05
# CVS-ID:       $Id$
# Copyright:    (c) 2005 ActiveGrid, Inc.
# License:      wxWindows License
#----------------------------------------------------------------------------

import sys
import os
import re
import traceback
import logging
import logging.config
from activegrid.util.lang import *
import activegrid.util.objutils as objutils
import activegrid.util.sysutils as sysutils
import activegrid.util.appdirs as appdirs

LEVEL_FATAL = logging.FATAL
LEVEL_ERROR = logging.ERROR
LEVEL_WARN = logging.WARN
LEVEL_INFO = logging.INFO
LEVEL_DEBUG = logging.DEBUG

EXCEPTION_INFO = 'exceptionInfo'
loggingInitialized = False

LOG_MODE_IDE = 1
LOG_MODE_TESTRUN = 2
LOG_MODE_RUN = 3
def initLogging(mode, force=False):
    global ag_debugLogger, loggingInitialized
    if (force or not loggingInitialized):
        loggingInitialized = True
        configFile = None
        if (mode == LOG_MODE_IDE):
            configFile = os.getenv("AG_LOGCONFIG_IDE")
        elif (mode == LOG_MODE_TESTRUN):
            configFile = os.getenv("AG_LOGCONFIG_PYTESTRUN")
        else:
            configFile = os.getenv("AG_LOGCONFIG_RUN")
        if ((configFile == None) or not os.path.exists(configFile)):
            if (mode == LOG_MODE_IDE):
                configFile = "IDELog"
            elif (mode == LOG_MODE_TESTRUN):
                configFile = "TestRunLog"
            else:
                configFile = "RunLog"
            configFile = os.path.join(appdirs.getSystemDir(appdirs.AG_LOGS_DIR), "py" + configFile + ".ini")
        if (os.path.exists(configFile)):
            print "Using logging configuration file: %s" % configFile
            fileConfig(configFile)
        else:
            print "*** Cannot find logging configuration file (%s) -- setting default logging level to WARN ***" % (configFile)
            defaultStream = sys.stderr
            if (mode == LOG_MODE_RUN):
                defaultStream = sys.stdout
            handler = logging.StreamHandler(defaultStream)
            handler.setLevel(logging.DEBUG)
            handler.setFormatter(logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s"))
            logging.getLogger().addHandler(handler)
            logging.getLogger().setLevel(logging.WARN)
        ag_debugLogger = logging.getLogger("activegrid.debug")
        ag_debugLogger.setLevel(logging.DEBUG)
        return configFile
    
ag_debugLogger = logging.getLogger("activegrid.debug")

def log(logger, level, msg, *params):
    if (logger == None):
        logger = ag_debugLogger
    apply(logger.log, (level, msg) + params)

def fatal(logger, msg, *params):
    apply(logger.fatal, (msg,) + params)

def error(logger, msg, *params):
    apply(logger.error, (msg,) + params)

def warn(logger, msg, *params):
    apply(logger.warn, (msg,) + params)

def info(logger, msg, *params):
    apply(logger.info, (msg,) + params)

def debug(logger, msg, *params):
    if (logger == None):
        logger = ag_debugLogger
    apply(logger.debug, (msg,) + params)
    
def setLevelFatal(logger):
    logger.setLevel(LEVEL_FATAL)
    
def setLevelError(logger):
    logger.setLevel(LEVEL_ERROR)
    
def setLevelWarn(logger):
    logger.setLevel(LEVEL_WARN)
    
def setLevelInfo(logger):
    logger.setLevel(LEVEL_INFO)
    
def setLevelDebug(logger):
    logger.setLevel(LEVEL_DEBUG)
    
def isEnabledForError(logger):
    return logger.isEnabledFor(LEVEL_ERROR)

def isEnabledForWarn(logger):
    return logger.isEnabledFor(LEVEL_WARN)

def isEnabledForInfo(logger):
    return logger.isEnabledFor(LEVEL_INFO)

def isEnabledForDebug(logger):
    return logger.isEnabledFor(LEVEL_DEBUG)

TEST_MODE_NONE = 0
TEST_MODE_DETERMINISTIC = 1
TEST_MODE_NON_DETERMINISTIC = 2

global agTestMode
agTestMode = TEST_MODE_NONE

def setTestMode(mode):
    global agTestMode
    agTestMode = mode
        
def getTestMode():
    global agTestMode
    return agTestMode
    
def testMode(normalObj, testObj=None, nonDeterministicObj=None):
    testMode = getTestMode()
    if testMode > TEST_MODE_NONE:
        if ((nonDeterministicObj != None) and (testMode == TEST_MODE_NON_DETERMINISTIC)):
            return nonDeterministicObj
        return testObj
    return normalObj

pythonFileRefPattern = asString(r'(?<=File ")[^"]*(#[^#]*")(, line )[0-9]*')
phpFileRefPattern = asString(r'( in ).*#([^#]*#[^ ]*)(?= on line )')
pathSepPattern = os.sep
if (pathSepPattern == "\\"):
    pathSepPattern = "\\\\"
pythonFileRefPattern = pythonFileRefPattern.replace("#", pathSepPattern)
pythonFileRefPattern = re.compile(pythonFileRefPattern)
phpFileRefPattern = phpFileRefPattern.replace("#", pathSepPattern)
phpFileRefPattern = re.compile(phpFileRefPattern)

def removeFileRefs(str):
    str = pythonFileRefPattern.sub(_fileNameReplacement, str)
    str = phpFileRefPattern.sub(_fileNameReplacementPHP, str)
    return str
    
def removePHPFileRefs(str):
    str = phpFileRefPattern.sub(_fileNameReplacementPHP, str)
    return str
    
def _fileNameReplacement(match):
    return "...%s" % match.group(1).replace(os.sep, "/")
    
def _fileNameReplacementPHP(match):
    return "%s...%s" % (match.group(1), match.group(2).replace(os.sep, "/"))
    
def formatTraceback(tb=None):
    if (tb == None):
        extype, val, tb = sys.exc_info()
    tbs = "\n" + "".join(traceback.format_tb(tb))
    return tbs

def formatExceptionCause(cause, stacktrace=False):
    if (cause == None):
        return ""
    tbs = ""
    if (stacktrace):
        tbs = formatTraceback()
    return "Caused by %s.%s: %s%s" % (cause.__module__, cause.__class__.__name__, str(cause), tbs)

def addExceptionInfo(e, key, value):
    if not hasattr(e, EXCEPTION_INFO):
        try:
            setattr(e, EXCEPTION_INFO, {})
        except:
            return # Make sure we still report the real exception even if we can't add the extra info
    if not e.exceptionInfo.has_key(key): # Never overwrite exception info since we assume earlier info is more specific
        e.exceptionInfo[key] = value
            
def reportException(exception, out=None, stacktrace=False, diffable=False):
    exstr = exceptionToString(exception, stacktrace, diffable)
    if (out == None):
        print exstr
    else:
        print >> out, exstr

def exceptionToString(exception, stacktrace=False, diffable=False):
    extype = objutils.typeToString(exception)
    val = exception
    if (stacktrace):
        e,v,t = sys.exc_info()
    if (diffable):
        exstr = removeFileRefs(str(val))
    else:
        exstr = str(val)
    if hasattr(val, EXCEPTION_INFO):
        firstTime = True
        for infoKey, infoValue in getattr(val, EXCEPTION_INFO).items():
            if firstTime:
                prefix = " EXTRA INFO:"
                firstTime = False
            else:
                prefix = ","
            exstr += ("%s %s=%s" % (prefix, infoKey, infoValue))
    result = "Got Exception = %s: %s" % (extype, exstr)
    if (stacktrace):
        fmt = traceback.format_exception(extype, val, t)
        for s in fmt:
            if (diffable):
                s = removeFileRefs(s)
            result = result + "\n" + s
    return result
    
def fileConfig(fname, defaults=None):
    """
    This is copied from logging.config so that we could fix the class lookup of
    handlers.  Previously handlers had to be defined in logging.handlers and we
    need to be able to define our own.
    """
    import ConfigParser, string

    cp = ConfigParser.ConfigParser(defaults)
    if hasattr(cp, 'readfp') and hasattr(fname, 'readline'):
        cp.readfp(fname)
    else:
        cp.read(fname)
    #first, do the formatters...
    flist = cp.get("formatters", "keys")
    if len(flist):
        flist = string.split(flist, ",")
        formatters = {}
        for form in flist:
            sectname = "formatter_%s" % form
            opts = cp.options(sectname)
            if "format" in opts:
                fs = cp.get(sectname, "format", 1)
            else:
                fs = None
            if "datefmt" in opts:
                dfs = cp.get(sectname, "datefmt", 1)
            else:
                dfs = None
            f = logging.Formatter(fs, dfs)
            formatters[form] = f
    #next, do the handlers...
    #critical section...
    logging._acquireLock()
    try:
##        try:
            #first, lose the existing handlers...
            logging._handlers.clear()
            #now set up the new ones...
            hlist = cp.get("handlers", "keys")
            if len(hlist):
                hlist = string.split(hlist, ",")
                handlers = {}
                fixups = [] #for inter-handler references
                for hand in hlist:
##                    try:
                        sectname = "handler_%s" % hand
                        classname = cp.get(sectname, "class")
                        opts = cp.options(sectname)
                        if "formatter" in opts:
                            fmt = cp.get(sectname, "formatter")
                        else:
                            fmt = ""
                        klass = None
                        try:
                            klass = eval(classname, vars(logging))
                        except:
                            pass
                        if (klass == None):
                            klass = objutils.classForName(classname)
                        args = cp.get(sectname, "args")
                        args = eval(args, vars(logging))
                        h = apply(klass, args)
                        if "level" in opts:
                            level = cp.get(sectname, "level")
                            h.setLevel(logging._levelNames[level])
                        if len(fmt):
                            h.setFormatter(formatters[fmt])
                        #temporary hack for FileHandler and MemoryHandler.
                        if klass == logging.handlers.MemoryHandler:
                            if "target" in opts:
                                target = cp.get(sectname,"target")
                            else:
                                target = ""
                            if len(target): #the target handler may not be loaded yet, so keep for later...
                                fixups.append((h, target))
                        handlers[hand] = h
##                    except Exception, e:     #if an error occurs when instantiating a handler, too bad
##                        pass    #this could happen e.g. because of lack of privileges
                #now all handlers are loaded, fixup inter-handler references...
                for fixup in fixups:
                    h = fixup[0]
                    t = fixup[1]
                    h.setTarget(handlers[t])
            #at last, the loggers...first the root...
            llist = cp.get("loggers", "keys")
            llist = string.split(llist, ",")
            llist.remove("root")
            sectname = "logger_root"
            root = logging.root
            log = root
            opts = cp.options(sectname)
            if "level" in opts:
                level = cp.get(sectname, "level")
                log.setLevel(logging._levelNames[level])
            for h in root.handlers[:]:
                root.removeHandler(h)
            hlist = cp.get(sectname, "handlers")
            if len(hlist):
                hlist = string.split(hlist, ",")
                for hand in hlist:
                    log.addHandler(handlers[hand])
            #and now the others...
            #we don't want to lose the existing loggers,
            #since other threads may have pointers to them.
            #existing is set to contain all existing loggers,
            #and as we go through the new configuration we
            #remove any which are configured. At the end,
            #what's left in existing is the set of loggers
            #which were in the previous configuration but
            #which are not in the new configuration.
            existing = root.manager.loggerDict.keys()
            #now set up the new ones...
            for log in llist:
                sectname = "logger_%s" % log
                qn = cp.get(sectname, "qualname")
                opts = cp.options(sectname)
                if "propagate" in opts:
                    propagate = cp.getint(sectname, "propagate")
                else:
                    propagate = 1
                logger = logging.getLogger(qn)
                if qn in existing:
                    existing.remove(qn)
                if "level" in opts:
                    level = cp.get(sectname, "level")
                    logger.setLevel(logging._levelNames[level])
                for h in logger.handlers[:]:
                    logger.removeHandler(h)
                logger.propagate = propagate
                logger.disabled = 0
                hlist = cp.get(sectname, "handlers")
                if len(hlist):
                    hlist = string.split(hlist, ",")
                    for hand in hlist:
                        logger.addHandler(handlers[hand])
            #Disable any old loggers. There's no point deleting
            #them as other threads may continue to hold references
            #and by disabling them, you stop them doing any logging.
            for log in existing:
                root.manager.loggerDict[log].disabled = 1
##        except:
##            import traceback
##            ei = sys.exc_info()
##            traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr)
##            del ei
    finally:
        logging._releaseLock()