GOError.py

Go to the documentation of this file.
00001 ## \package GOError
00002 # Custom error logging.  Can be turned off - in which case RuntimeExceptions are thrown
00003 # rather than errors just being printed out.
00004 # \author mullerb@musc.edu
00005 
00006 import sys, time, os, datetime
00007 
00008 ## Singleton error logging class.  
00009 class GOError:
00010     __state = {}
00011     NONE = 0
00012     FATAL = 1
00013     WARNING = 2
00014     DEBUG = 3
00015 
00016     ## Constructor (makes a new instance, but state is shared across all instances)
00017     # @param loglevel (default of 0):
00018     # \li 0 - turns off logging - raise RuntimeErrors/Warnings for fatal/warning messages (useful for code build on this library)
00019     # \li 1 - print only fatal messages
00020     # \li 2 - print warnings and fatal messages
00021     # \li 3 - debug mode - prints everything
00022     def __init__(self, loglevel=0):
00023         self.__dict__ = GOError.__state
00024         if self.__dict__ == {}:
00025             self.loglevel = int(loglevel)
00026             self.stdout_logging = (os.name == 'posix')
00027 
00028     def timenow(self):
00029         return time.strftime("[%H:%M:%S] ")
00030 
00031     ## Log a debug message
00032     # @param msg Debugging message to log
00033     def debug(self, msg, singleline=False):
00034         if self.loglevel==3:
00035             if self.stdout_logging and singleline:
00036                 print "\r" + self.timenow() + "\033[32;1mdebug:\033[0m " + msg,
00037                 sys.stdout.flush()
00038             elif self.stdout_logging:
00039                 print self.timenow() + "\033[32;1mdebug:\033[0m " + msg
00040             else:
00041                 print self.timenow() + "DEBUG: "+msg
00042 
00043     ## Log a warning message (bad, but not bad enough to stop the program)
00044     # @param error Error message to log
00045     def handleWarning(self, error):
00046         if self.loglevel > 1:
00047             if self.stdout_logging:
00048                 print self.timenow() + "\033[33;1mwarning:\033[0m " + error
00049             else:
00050                 print self.timenow() + "WARNING: "+error
00051         elif self.loglevel == 0:
00052             raise RuntimeWarning, error
00053 
00054 
00055     ## Log a message concerning a fatal error that will cause this program to terminate.  The function then
00056     # terminates the program.
00057     # @param fatal The message containing a description of the fatal conditions.
00058     def handleFatal(self, fatal):
00059         if self.loglevel > 0:
00060             if self.stdout_logging:
00061                 print self.timenow() + "\033[31;1mfatal:\033[0m " + fatal
00062             else:
00063                 print self.timenow() + "FATAL: "+fatal
00064             sys.exit(1)
00065         else:
00066             raise RuntimeError, fatal
00067 
00068                                                                                 
00069 ## Class to handle percent logging to terminal
00070 # p = GOPercentMessage(totalsize)
00071 # p.update() # run many times
00072 # p.finished()
00073 class GOPercentMessage:
00074     def __init__(self, size):
00075         self.size = float(size)
00076         self.start = None
00077         self.error = GOError()
00078         self.current = 0
00079         self.index = 0
00080 
00081 
00082     def update(self):
00083         if self.start is None:
00084             self.start = time.time()
00085         self.current += 1
00086         self.index += 1
00087 
00088         if self.size > 100000 and self.index != 1000:
00089             return
00090         if self.index == 1000:
00091             self.index = 0
00092 
00093         ratio = float(self.current) / self.size
00094         finish = self.prettyTime(float(self.age()) / ratio)
00095         line = "%s percent done at point %i (estimated finish in %s)" % (str(ratio), self.current, finish)
00096         self.error.debug(line,True)
00097 
00098 
00099     def age(self):
00100         return time.time() - self.start
00101 
00102 
00103     def prettyTime(self, seconds):
00104         seconds = int(seconds)
00105         return str(datetime.timedelta(seconds=seconds))
00106 
00107 
00108     def finished(self):
00109         self.error.debug("done")
00110         self.error.debug("finished in %s" % self.prettyTime(self.age()))

Generated on Tue Apr 7 11:34:50 2009 for GOGrapher by  doxygen 1.5.6