1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
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 # -----------------------------------------------------------------------
16 import traceback, sys, os, re, threading
17 from osrf.const import *
18 logSema = threading.BoundedSemaphore(value=1)
21 loglevel = OSRF_LOG_DEBUG
22 logtype = OSRF_LOG_TYPE_STDERR
25 def osrfInitLog(level, facility=None, file=None):
26 """Initialize the logging subsystem."""
27 global loglevel, logtype, logfile
35 sys.stderr.write("syslog not found, logging to stderr\n")
38 logtype = OSRF_LOG_TYPE_SYSLOG
39 osrfInitSyslog(facility, level)
43 logtype = OSRF_LOG_TYPE_FILE
47 # -----------------------------------------------------------------------
48 # Define wrapper functions for the log levels
49 # -----------------------------------------------------------------------
50 def osrfLogInternal(s): __osrfLog(OSRF_LOG_INTERNAL,s)
51 def osrfLogDebug(s): __osrfLog(OSRF_LOG_DEBUG,s)
52 def osrfLogInfo(s): __osrfLog(OSRF_LOG_INFO,s)
53 def osrfLogWarn(s): __osrfLog(OSRF_LOG_WARN,s)
54 def osrfLogErr(s): __osrfLog(OSRF_LOG_ERR,s)
57 frgx = re.compile('/.*/')
59 def __osrfLog(level, msg):
60 """Builds the log message and passes the message off to the logger."""
61 global loglevel, logtype
66 if level == OSRF_LOG_ERR:
67 sys.stderr.write('ERR ' + msg)
70 if int(level) > int(loglevel): return
72 # find the caller info for logging the file and line number
73 tb = traceback.extract_stack(limit=3)
77 if level == OSRF_LOG_INTERNAL: lvl = 'INT '
78 if level == OSRF_LOG_INFO: lvl = 'INFO'
79 if level == OSRF_LOG_WARN: lvl = 'WARN'
80 if level == OSRF_LOG_ERR: lvl = 'ERR '
82 file = frgx.sub('',tb[0])
83 msg = '[%s:%d:%s:%s:%s] %s' % (lvl, os.getpid(), file, tb[1], threading.currentThread().getName(), msg)
85 if logtype == OSRF_LOG_TYPE_SYSLOG:
86 __logSyslog(level, msg)
88 if logtype == OSRF_LOG_TYPE_FILE:
91 sys.stderr.write("%s\n" % msg)
93 if level == OSRF_LOG_ERR and logtype != OSRF_LOG_TYPE_STDERR:
94 sys.stderr.write(msg + '\n')
96 def __logSyslog(level, msg):
97 ''' Logs the message to syslog '''
100 slvl = syslog.LOG_DEBUG
101 if level == OSRF_LOG_INTERNAL: slvl=syslog.LOG_DEBUG
102 if level == OSRF_LOG_INFO: slvl = syslog.LOG_INFO
103 if level == OSRF_LOG_WARN: slvl = syslog.LOG_WARNING
104 if level == OSRF_LOG_ERR: slvl = syslog.LOG_ERR
106 sys.stderr.write("logging at level: %s\n" % str(slvl))
107 syslog.syslog(slvl, msg)
110 ''' Logs the message to a file. '''
112 global logfile, logtype
116 f = open(logfile, 'a')
118 sys.stderr.write("cannot open log file for writing: %s\n", logfile)
119 logtype = OSRF_LOG_TYPE_STDERR
123 f.write("%s\n" % msg)
131 def osrfInitSyslog(facility, level):
132 """Connect to syslog and set the logmask based on the level provided."""
137 if facility == 'local0': facility = syslog.LOG_LOCAL0
138 if facility == 'local1': facility = syslog.LOG_LOCAL1
139 if facility == 'local2': facility = syslog.LOG_LOCAL2
140 if facility == 'local3': facility = syslog.LOG_LOCAL3
141 if facility == 'local4': facility = syslog.LOG_LOCAL4
142 if facility == 'local5': facility = syslog.LOG_LOCAL5
143 if facility == 'local6': facility = syslog.LOG_LOCAL6
144 # add other facility maps if necessary...
146 syslog.openlog(sys.argv[0], 0, facility)