/usr/share/pyshared/PythonCard/log.py is in python-pythoncard 0.8.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """
__version__ = "$Revision: 1.11 $"
__date__ = "$Date: 2004/05/14 18:57:18 $"
"""
import time
import configuration
import sys
class Log :
"""
A simple file logging class. In the future we should
use this class to wrap up log4p.
The name of the log file is specified in PythonCard's
configuration file, 'pythoncard.rsrc.py'
The log file will be created in the current working directory.
Each time PythonCard runs, it deletes the old log contents.
Log has four levels: DEBUG, ERROR, INFO and WARNING.
Using Log:
Call Log.getInstance() to get a reference to
the single Log instance:
log = Log.getInstance()
Logging is initially disabled. To turn logging on:
log.enable()
To turn logging off:
log.disable()
To selectively enable one or more logging levels:
log.enableLevels( [ Log.ERROR, Log.WARNING ] )
To selectively disable one or more logging levels:
log.disableLevels( [ Log.DEBUG, Log.INFO ] )
To write to the Log:
log.debug( 'a debug message' )
log.error( 'an error message' )
log.info( 'an info message' )
log.warning( 'a warning message' )
NOTE: The debug(), error(), info(), and warning() methods
can be called with any number of parameters, and
the parameters can be of any type. For example, if you
want to print a debug message that is a string,
and a list of items you can call:
items = [ 'one', 'two', 3 ]
log.debug( 'the items are: ', items )
"""
# PUBLIC CLASS VARIABLES
DEBUG = 'debug'
ERROR = 'error'
INFO = 'info'
WARNING = 'warning'
# PRIVATE CLASS VARIABLES
instance = None
legalLevels = { DEBUG:DEBUG, ERROR:ERROR, INFO:INFO, WARNING:WARNING }
# PRIVATE INNER CLASSES
class LogSingletonHelper :
"""
A helper class used to implement the Singleton Desing Pattern.
"""
def __call__( self, *args, **kw ) :
# If an instance of Log does not exist,
# create one and assign it to Log.instance.
if Log.instance is None :
Log.instance = Log()
# Return TestSingleton.instance, which should contain
# a reference to the only instance of Log in the system.
return Log.instance
# Create a class level method that must be called to
# get the single instance of TestSingleton.
# PUBLIC CLASS METHODS
getInstance = LogSingletonHelper()
# PUBLIC METHODS
def __init__( self ) :
"""
Initialize a Log instance.
"""
if Log.instance is not None:
raise RuntimeError, 'Only one instance of Log is allowed! use Log.getInstance() to get a reference to a Log'
# The Log is initially disabled.
self.enabled = 0
self.created = 0
#self.fileName = configuration.Configuration().getLogFileName()
self.fileName = configuration.getLogFileName()
self.logToStdout = configuration.getOption('logToStdout')
# Enable all logging levels.
self.errorEnabled = 1
self.warningEnabled = 1
self.debugEnabled = 1
self.infoEnabled = 1
def isEnabled( self ) :
"""
Return true if logging is enabled.
"""
return self.enabled
def enable( self ) :
"""
Enable all logging - levels remain at their current settings.
"""
self.enabled = 1
# If this is the first time logging has been enabled,
# create a new, empty log file.
if not self.created:
if not self.logToStdout:
file = open(self.fileName, 'w')
file.close()
self.created = 1
def disable( self ) :
"""
Disable all logging - levels remain at their current settings.
"""
self.enabled = 0
def enableLevels( self, levels ) :
"""
Enable each logging level listed in 'levels'.
"""
for level in levels :
if self.levelIsLegal( level ) :
exec( 'self.' + level + 'Enabled = 1' )
def disableLevels( self, levels ) :
"""
Disable each logging level listed in 'levels'.
"""
for level in levels :
if self.levelIsLegal( level ) :
exec( 'self.' + level + 'Enabled = 0' )
def error( self, *args ) :
"""
Write an error message to the log.
"""
if self.errorEnabled :
self.write( 'ERROR: ', args )
def warning( self, *args ) :
"""
Write a warning message to the log.
"""
if self.warningEnabled :
self.write( 'WARNING: ', args )
def debug( self, *args ) :
"""
Write a debug message to the log.
"""
if self.debugEnabled :
self.write( 'DEBUG: ', args )
def info( self, *args ) :
"""
Write an info message to the log.
"""
if self.infoEnabled :
self.write( 'INFO: ', args )
# PRIVATE METHODS
def levelIsLegal( self, level ) :
if level not in Log.legalLevels:
print '"', level, '" is not a legal logging level!'
return 0
else :
return 1
def write( self, prefix, argList ) :
"""
This method should wait for the file to come available
in the event that multiple threads are accessing the log file.
"""
if self.enabled :
now = time.localtime( time.time() )
try:
if self.logToStdout:
f = sys.stdout
else:
f = open( self.fileName, 'a' )
f.write( prefix + ': ' + "%s" % time.asctime( now ) + ': ' )
for arg in argList :
f.write( str( arg ) )
f.write( '\n' )
if self.logToStdout:
f = None
else:
f.close()
except:
pass
log = Log()
def isEnabled(): return log.isEnabled()
def enable(): log.enable()
def disable(): log.disable()
def enablelevels(levels): log.enablelevels(levels)
def disablelevels(levels): log.disablelevels(levels)
def error(*args): log.error(*args)
def warning(*args): log.warning(*args)
def debug(*args): log.debug(*args)
def info(*args): log.info(*args)
# Unit Test
if __name__ == '__main__' :
#log = Log.getInstance()
log.info( 'All logging enabled' )
log.enable()
log.debug( 's1', [ 'a', 'list' ] )
log.error( 's2', { 'a':'dictionary' } )
log.info( 's3', 1, 2, 3, 4 )
log.warning( 's4', ' this ', 'is', ' a ', 'string ' )
log.info( 'Disabling error and warning levels' )
log.disableLevels( [ Log.ERROR, Log.WARNING ] )
log.debug( 's1' )
log.error( 's2' )
log.info( 's3' )
log.warning( 's4' )
log.info( 'Enabling error and warning levels' )
log.enableLevels( [ Log.ERROR, Log.WARNING ] )
log.debug( 's1' )
log.error( 's2' )
log.info( 's3' )
log.warning( 's4' )
log.info( 'All logging disabled' )
log.disable()
log.debug( 's1' )
log.error( 's2' )
log.info( 's3' )
log.warning( 's4' )
log.enable()
log.info( 'Attempt to enable an unknown level' )
log.enableLevels( [ 'bogus-level' ] )
|