Package mathbench :: Package lab :: Module fumehood
[hide private]

Source Code for Module mathbench.lab.fumehood

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5  Define a nice way to show the messages sent to be logged. 
  6   
  7  NOTE: not used yet 
  8  """ 
  9   
 10  import wx 
 11   
 12  # Load Python's logging utilities 
 13  import logging 
 14  import logging.handlers 
 15   
 16  from mathbench.basement.logging_utils import CreateLogger 
 17   
 18   
19 -class FumeHoodLogHandler(logging.StreamHandler):
20 """ 21 A log handler that will display the logged messages (above a 22 certain priprity level) in GUI windows. 23 """ 24
25 - def __init__(self) :
26 """ 27 Initialisation 28 """ 29 logging.StreamHandler.__init__(self) 30 # Associate a loglevel to a certain kind of window 31 self.handle_mapping = { 32 logging.CRITICAL: self.TriggerCritical, 33 logging.ERROR : self.TriggerError, 34 logging.WARNING : self.TriggerWarning, 35 logging.INFO : self.TriggerInfo, 36 }
37
38 - def handle(self, record):
39 """ 40 React to the record according to its priority level. 41 """ 42 if self.handle_mapping.has_key(record.levelno): 43 self.handle_mapping[record.levelno](record) 44 logging.StreamHandler.handle(self,record)
45
46 - def TriggerWarning(self,record):
47 """ 48 mouf 49 """ 50 dialog = wx.MessageBox(record.getMessage(), caption = "Warning", style = wx.OK | wx.ICON_WARNING)
51
52 - def TriggerError(self,record):
53 """ 54 Trigger the info contained in a record of level ERROR. 55 """ 56 dialog = wx.MessageBox(record.getMessage(), caption = "Error !", style = wx.OK | wx.ICON_ERROR)
57
58 - def TriggerInfo(self,record):
59 """ 60 Trigger the info contained in a record of level INFO. 61 """ 62 dialog = wx.MessageBox(record.getMessage(), caption = "Information...", style = wx.OK | wx.ICON_INFORMATION)
63
64 - def TriggerCritical(self,record):
65 """ 66 Trigger the info contained in a record of level CRITICAL. 67 """ 68 dialog = wx.MessageBox(record.getMessage(), caption = "Critical Error !", style = wx.OK | wx.ICON_STOP)
69 70 71 72 # The global variables should only be defined when the module is 73 # loaded/executed for the first time 74 if "fumehood" in __name__ or __name__ == "__main__": 75 76 # Create a GUI enabled logger 77 __log_handler = FumeHoodLogHandler() 78 __MB_LOGGER = CreateLogger(__log_handler) 79 80 # (re)define the global functions through which the messages are sent
81 - def debug(text) :
82 return __MB_LOGGER.debug(text)
83
84 - def info(text) :
85 return __MB_LOGGER.info(text)
86
87 - def warn(text) :
88 return __MB_LOGGER.warn(text)
89
90 - def critical(text) :
91 return __MB_LOGGER.critical(text)
92
93 - def log(text) :
94 return __MB_LOGGER.log(text)
95
96 - def error(text) :
97 return __MB_LOGGER.error(text)
98 99
100 -def log_function_call(func):
101 """ 102 Send a message each time the decorated function is called. 103 104 This nifty decorator is very usefull to trace the function calls 105 """ 106 def wrapper(*args, **kwargs): 107 debug("Called func.: %s (defined in %s)" % (func.__name__,func.code.co_filename)) 108 return func(*args, **kwargs)
109 # usefull for automatic doc generation/introspection 110 wrapper.__name__ = func.__name__ 111 wrapper.__doc__ = func.__doc__ 112 wrapper.__dict__.update(func.__dict__) 113 return wrapper 114 115 116 #--- TEST --------------------------------------------------------------------------------- 117 118 if __name__ == "__main__": 119 app = wx.PySimpleApp() 120 debug("debug message") 121 info("info message") 122 warn("warn message") 123 error("error message") 124 critical("critical message") 125 app.MainLoop() 126