[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gnue/gnue-common/src GLogger.py
From: |
Jason Cater |
Subject: |
gnue/gnue-common/src GLogger.py |
Date: |
Mon, 22 Oct 2001 18:51:41 -0400 |
CVSROOT: /home/cvs
Module name: gnue
Changes by: Jason Cater <address@hidden> 01/10/22 18:51:41
Modified files:
gnue-common/src: GLogger.py
Log message:
enhanced logging functionality
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-common/src/GLogger.py.diff?cvsroot=OldCVS&tr1=1.1&tr2=1.2&r1=text&r2=text
Patches:
Index: gnue/gnue-common/src/GLogger.py
diff -u gnue/gnue-common/src/GLogger.py:1.1 gnue/gnue-common/src/GLogger.py:1.2
--- gnue/gnue-common/src/GLogger.py:1.1 Mon Oct 22 17:49:03 2001
+++ gnue/gnue-common/src/GLogger.py Mon Oct 22 18:51:41 2001
@@ -25,43 +25,88 @@
# Class that provides logging facilities for GNUe apps.
#
# NOTES:
-#
+# This package implements both a Logger class and a system wide
+# logging instance. If you need just one logging system for your app,
+# you can just use the openlog, closelog, and log methods. If you need
+# more control over your logs or may have several logging destinations,
+# you will want to use the Logger class.
+
import GDebug
import time, string
+
-_filehandle = None
+_logger = None
+
class LogIOError (StandardError):
pass
-def open(logfile):
- global _filehandle
- try:
- _filehandle = open(logfile,'a')
- log ('---- [ Logging started ] ----')
- except IOError, mesg
- raise LogIOError, "[IOError] %s" % (mesg)
+class Logger:
-def close(logfile):
- try:
- log ('---- [ Logging stopped ] ----')
- close _filehandle
- except:
- pass
+ def __init__(self, logfile):
+ try:
+ self._filehandle = open(logfile,'a')
+ self.log ('---- [ Logging started ] ----')
+ except IOError, mesg:
+ raise LogIOError, "[IOError] %s" % (mesg)
-def log(*messages):
- if _filehandle:
- timestamp = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())
-
+ def __del__(self):
try:
- for messageset in messages:
- for message in string.split("%s" % messageset,'\n'):
- _filehandle.write('[%s] %s' % (timestamp, message))
- GDebug.printMesg(2,'>> Logger: [%s] %s' % (timestamp, message))
- except IOError, mesg
- raise LogIOError, "[IOError] %s" % (mesg)
+ self.closelog()
+ except:
+ pass
+
+
+ def closelog(self):
+ try:
+ self.log ('---- [ Logging stopped ] ----')
+ close (self._filehandle)
+ except:
+ pass
+ self._filehandle = None
+
+
+ def log(self, *messages):
+ timestamp = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
+
+ # TODO: Is this thread safe?
+
+ for messageset in messages:
+ for message in string.split("%s" % messageset,'\n'):
+ GDebug.printMesg(2,'>> Logger: [%s] %s' % (timestamp, message))
+ try:
+ self._filehandle.write('[%s] %s\n' % (timestamp, message))
+ except IOError, mesg:
+ raise LogIOError, "[IOError] %s" % (mesg)
+
+
+
+def openlog(logfile):
+ global _logger
+ _logger = Logger(logfile)
+
+
+def closelog():
+ global _logger
+ if _logger:
+ _logger.closelog()
+ _logger = None
+
+
+def log(*messages):
+ if _logger:
+ for message in messages:
+ _logger.log(message)
+
+
+if __name__ == '__main__':
+ openlog('test.log')
+ log ('Message1')
+ log ('Message2')
+ log ('Message3')
+ closelog()