jmunsch

log_queue.py

Jun 5th, 2014
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. from Queue import Queue
  2. import inspect
  3. import traceback
  4. import logging
  5.  
  6. #https://docs.python.org/3.5/howto/logging-cookbook.html
  7. #QueueHandler is new to 3.2, so logging.handlers has been 'patched' and included
  8. # in the dependecies folder just in case
  9.  
  10. class logQueue(object):
  11.     '''
  12.    Class to handle threaded logging ... for example with onPrefetch/wx.Yield
  13.    '''
  14.     def __init__(self, MainFrame):
  15.         super(logQueue, self).__init__()
  16.         self.MainFrame = MainFrame
  17.         self.q = Queue(-1) # no limit on size
  18.         #self.q_handler = QueueHandler(self.q)
  19.         self.run()
  20.  
  21.  
  22.     def enqueue(self,task):
  23.         self.q.put(task)
  24.         self.run()
  25.         return
  26.  
  27.     def run(self):
  28.         print('entering logging q')
  29.         while not self.q.empty():
  30.             try:
  31.                 task = self.q.get()
  32.                 #print(task)
  33.                 task_type = task[0]
  34.                 if task_type is 0:
  35.                     try:
  36.                         logger = logging.getLogger(task[1])
  37.                         HANDLER = task[2]
  38.                         msg = task[3]
  39.                         logger.addHandler(HANDLER)
  40.                         if self.MainFrame.threading is False:
  41.                             logger.info("\n%s" % (msg))
  42.                     except Exception, e:
  43.                         print(e)
  44.                         print(traceback.format_exc())
  45.                         exit()
  46.                 if task_type is 1:
  47.                     try:
  48.                         logger = logging.getLogger(task[1])
  49.                         HANDLER = task[2]
  50.                         func_name = task[3]
  51.                         msg = task[4]
  52.                         debug_info = task[5]
  53.                         logger.addHandler(HANDLER)
  54.                         #called_frame_trace = "\n    ".join(traceback.format_stack()).replace("\n\n","\n")
  55.                         outer_frames = inspect.getouterframes(inspect.currentframe().f_back.f_back)
  56.                         call_fn = outer_frames[3][1]
  57.                         call_ln = outer_frames[3][2]
  58.                         call_func = outer_frames[3][3]
  59.                         caller = outer_frames[3][4]
  60.                         # a string with args and kwargs from log_debug
  61.                         args_kwargs_str = "\n"+str(debug_info).replace(", '","\n, '")
  62.                         if self.MainFrame.threading is False:
  63.                             results = logger.debug("%s\nARGS_KWARGS_STR:%s\ncall_fn: %s\ncall_ln: %i\ncall_func: %s\ncaller: %s\nException:" % (
  64.                                                         msg,
  65.                                                         args_kwargs_str,
  66.                                                         call_fn,
  67.                                                         call_ln,
  68.                                                         call_func,
  69.                                                         caller
  70.                                                         ),exc_info=1)
  71.                         return
  72.                     except Exception, e:
  73.                         print(e)
  74.                         print(traceback.format_exc())
  75.                         return
  76.  
  77.             except Exception,e:
  78.                 import traceback
  79.                 print(e)
  80.                 print(traceback.format_exc())
  81.                 exit()
Advertisement
Add Comment
Please, Sign In to add comment