SystemLogger.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
16 
17 import sys
18 import traceback
19 import time
20 import threading
21 import logging
22 import logging.handlers
23 
24 logger = None
25 
26 
27 
39 class Logger:
40  """
41  """
42 
43  SILENT = 0 # ()
44  FATAL = 41 # (FATAL)
45  ERROR = 40 # (FATAL, ERROR)
46  WARN = 30 # (FATAL, ERROR, WARN)
47  INFO = 20 # (FATAL, ERROR, WARN, INFO)
48  DEBUG = 10 # (FATAL, ERROR, WARN, INFO, DEBUG)
49  TRACE = 9 # (FATAL, ERROR, WARN, INFO, DEBUG, TRACE)
50  VERBOSE = 8 # (FATAL, ERROR, WARN, INFO, DEBUG, TRACE, VERBOSE)
51  PARANOID = 7 # (FATAL, ERROR, WARN, INFO, DEBUG, TRACE, VERBOSE, PARA)
52 
53 
54 
69  def strToLogLevel(self, lv):
70  if lv == "SILENT":
71  return Logger.SILENT
72  elif lv == "FATAL":
73  return Logger.FATAL
74  elif lv == "ERROR":
75  return Logger.ERROR
76  elif lv == "WARN":
77  return Logger.WARN
78  elif lv == "INFO":
79  return Logger.INFO
80  elif lv == "DEBUG":
81  return Logger.DEBUG
82  elif lv == "TRACE":
83  return Logger.TRACE
84  elif lv == "VERBOSE":
85  return Logger.VERBOSE
86  elif lv == "PARANOID":
87  return Logger.PARANOID
88  else:
89  return Logger.INFO
90 
91 
92 
93 
108  def __init__(self, *args):
109  self._mutex = threading.RLock()
110  self._fhdlr = None
111 
112 
113  def init(*args):
114  global logger
115 
116  if logger is not None:
117  return logger
118 
119 
120  logger = Logger()
121  mode = None
122  fileName = None
123 
124  if len(args) == 0:
125  return
126  elif len(args) == 2:
127  name = args[0]
128  mode = args[1]
129  elif len(args) == 3:
130  name = args[0]
131  mode = args[1]
132  fileName = args[2]
133 
134 
135  logging.PARANOID = logging.DEBUG - 3
136  logging.VERBOSE = logging.DEBUG - 2
137  logging.TRACE = logging.DEBUG - 1
138  logging.FATAL = logging.ERROR + 1
139 
140  logging.addLevelName(logging.PARANOID, "PARANOID")
141  logging.addLevelName(logging.VERBOSE, "VERBOSE")
142  logging.addLevelName(logging.TRACE, "TRACE")
143  logging.addLevelName(logging.FATAL, "FATAL")
144 
145  """
146  logging.root.setLevel([logging.NOTSET,
147  logging.PARANOID,
148  logging.VERBOSE,
149  logging.TRACE,
150  logging.DEBUG,
151  logging.INFO,
152  logging.WARNING,
153  logging.ERROR,
154  logging.FATAL,
155  logging.CRITICAL])
156  """
157 
158 
159  formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
160 
161  if mode is None or mode == "FILE":
162  if fileName:
163  logger._fhdlr = logging.FileHandler(fileName)
164  else:
165  logger._fhdlr = logging.FileHandler('rtcsystem.log')
166 
167  mhdlr = logging.handlers.MemoryHandler(1024,logging.NOTSET, logger._fhdlr)
168  logger._fhdlr.setFormatter(formatter)
169  logging.getLogger("").addHandler(mhdlr)
170  logging.getLogger("").setLevel(logging.NOTSET)
171 
172  elif mode == "STDOUT":
173  ch = logging.StreamHandler()
174  ch.setLevel(logging.NOTSET)
175  ch.setFormatter(formatter)
176  logging.getLogger("").addHandler(ch)
177 
178  return logger
179 
180  init = staticmethod(init)
181 
182 
183 
184 
198  def __del__(self):
199  #if self._fhdlr is not None:
200  # self._fhdlr.close()
201  # self._fhdler = None
202  pass
203 
204 
222  def printf(self, fmt):
223  return fmt
224 
225 
226  def addHandler(self, *args):
227  mode = None
228  fileName = None
229 
230  if len(args) == 0:
231  return
232  elif len(args) == 1:
233  mode = args[0]
234  elif len(args) == 2:
235  mode = args[0]
236  fileName = args[1]
237 
238  formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
239 
240  if mode is None or mode == "FILE":
241  if fileName:
242  self._fhdlr = logging.FileHandler(fileName)
243  else:
244  self._fhdlr = logging.FileHandler('rtcsystem.log')
245 
246  mhdlr = logging.handlers.MemoryHandler(1024,logging.NOTSET, self._fhdlr)
247  self._fhdlr.setFormatter(formatter)
248  logging.getLogger("").addHandler(mhdlr)
249 
250  elif mode.lower() == "stdout":
251  ch = logging.StreamHandler()
252  ch.setLevel(logging.NOTSET)
253  ch.setFormatter(formatter)
254  logging.getLogger("").addHandler(ch)
255 
256 
257 
272  if sys.version_info[0:3] >= (2, 4, 0):
273  return traceback.format_exc()
274  else:
275  _exc_list = traceback.format_exception(*sys.exc_info())
276  _exc_str = "".join(_exc_list)
277  return _exc_str
278 
279  print_exception = staticmethod(print_exception)
280 
281 
282 
283 
295 class LogStream:
296  """
297  """
298 
299 
314  def __init__(self, *args):
315  self._LogLock = False
316  self._log_enable = False
317  self._loggerObj = None
318  name = ""
319 
320  if len(args) == 0:
321  return
322  elif len(args) > 0:
323  name = args[0]
324 
325  self._loggerObj = Logger.init(*args)
326  self._log_enable = True
327  self.logger = logging.getLogger(name)
328 
329 
330  def __del__(self):
331  return
332 
333  def shutdown(self):
334  logging.shutdown()
335  return
336 
337  def addHandler(self, *args):
338  if self._loggerObj is not None:
339  self._loggerObj.addHandler(*args)
340 
341 
342 
355  def setLogLevel(self, level):
356  if level == "INFO":
357  self.logger.setLevel(logging.INFO)
358  elif level == "FATAL":
359  self.logger.setLevel(logging.FATAL)
360  elif level == "ERROR":
361  self.logger.setLevel(logging.ERROR)
362  elif level == "WARN":
363  self.logger.setLevel(logging.WARNING)
364  elif level == "DEBUG":
365  self.logger.setLevel(logging.DEBUG)
366  elif level == "SILENT":
367  self.logger.setLevel(logging.NOTSET)
368  elif level == "TRACE":
369  self.logger.setLevel(logging.TRACE)
370  elif level == "VERBOSE":
371  self.logger.setLevel(logging.VERBOSE)
372  elif level == "PARANOID":
373  self.logger.setLevel(logging.PARANOID)
374  else:
375  self.logger.setLevel(logging.INFO)
376 
377 
378 
391  def setLogLock(self, lock):
392  if lock == 1:
393  self._LogLock = True
394  elif lock == 0:
395  self._LogLock = False
396 
397 
398 
410  def enableLogLock(self):
411  self._LogLock = True
412 
413 
414 
426  def disableLogLock(self):
427  self._LogLock = False
428 
429 
430 
441  def acquire(self):
442  if self._LogLock:
443  self.guard = OpenRTM_aist.ScopedLock(self._mutex)
444 
445 
446 
457  def release(self):
458  if self._LogLock:
459  del self.guard
460 
461 
462 
480  def RTC_LOG(self, LV, msg, opt=None):
481  if self._log_enable:
482  self.acquire()
483 
484  if opt is None:
485  messages = msg
486  else:
487  try:
488  messages = msg%(opt)
489  except:
490  print "RTC_LOG : argument error"
491  return
492 
493  self.logger.log(LV,messages)
494 
495  self.release()
496 
497 
498 
516  def RTC_FATAL(self, msg, opt=None):
517  if self._log_enable:
518  self.acquire()
519 
520  if opt is None:
521  messages = msg
522  else:
523  try:
524  messages = msg%(opt)
525  except:
526  print "RTC_FATAL : argument error"
527  return
528 
529  self.logger.log(logging.FATAL,messages)
530 
531  self.release()
532 
533 
534 
552  def RTC_ERROR(self, msg, opt=None):
553  if self._log_enable:
554  self.acquire()
555 
556  if opt is None:
557  messages = msg
558  else:
559  try:
560  messages = msg%(opt)
561  except:
562  print "RTC_ERROR : argument error"
563  return
564 
565  self.logger.error(messages)
566 
567  self.release()
568 
569 
570 
592  def RTC_WARN(self, msg, opt=None):
593  if self._log_enable:
594  self.acquire()
595 
596  if opt is None:
597  messages = msg
598  else:
599  try:
600  messages = msg%(opt)
601  except:
602  print "RTC_WARN : argument error"
603  return
604 
605  self.logger.warning(messages)
606 
607  self.release()
608 
609 
610 
632  def RTC_INFO(self, msg, opt=None):
633  if self._log_enable:
634  self.acquire()
635 
636  if opt is None:
637  messages = msg
638  else:
639  try:
640  messages = msg%(opt)
641  except:
642  print "RTC_INFO : argument error"
643  return
644 
645  self.logger.info(messages)
646 
647  self.release()
648 
649 
650 
672  def RTC_DEBUG(self, msg, opt=None):
673  if self._log_enable:
674  self.acquire()
675 
676  if opt is None:
677  messages = msg
678  else:
679  try:
680  messages = msg%(opt)
681  except:
682  print "RTC_DEBUG : argument error"
683  return
684 
685  self.logger.debug(messages)
686 
687  self.release()
688 
689 
690 
712  def RTC_TRACE(self, msg, opt=None):
713  if self._log_enable:
714  self.acquire()
715 
716  if opt is None:
717  messages = msg
718 
719  else:
720  try:
721  messages = msg%(opt)
722  except:
723  print "RTC_TRACE : argument error"
724  return
725 
726  self.logger.log(logging.TRACE,messages)
727 
728  self.release()
729 
730 
731 
754  def RTC_VERBOSE(self, msg, opt=None):
755  if self._log_enable:
756  self.acquire()
757 
758  if opt is None:
759  messages = msg
760  else:
761  try:
762  messages = msg%(opt)
763  except:
764  print "RTC_VERBOSE : argument error"
765  return
766 
767  self.logger.log(logging.VERBOSE,messages)
768 
769  self.release()
770 
771 
772 
773 
796  def RTC_PARANOID(self, msg, opt=None):
797  if self._log_enable:
798  self.acquire()
799 
800  if opt is None:
801  messages = msg
802  else:
803  try:
804  messages = msg%(opt)
805  except:
806  print "RTC_PARANOID : argument error"
807  return
808 
809  self.logger.log(logging.PARANOID,messages)
810 
811  self.release()
812 
813 
def RTC_DEBUG(self, msg, opt=None)
Debug level log output macro.
def RTC_TRACE(self, msg, opt=None)
Trace level log output macro.
def RTC_ERROR(self, msg, opt=None)
Error log output macro.
def RTC_PARANOID(self, msg, opt=None)
Paranoid level log output macro.
def RTC_VERBOSE(self, msg, opt=None)
Verbose level log output macro.
def RTC_WARN(self, msg, opt=None)
Warning log output macro.
def RTC_FATAL(self, msg, opt=None)
Error log output macro.
def printf(self, fmt)
Formatted output like printf.
def __init__(self, args)
constructor.
def RTC_LOG(self, LV, msg, opt=None)
Log output macro.
def print_exception()
Print exception information.
def __init__(self, args)
constructor.
def RTC_INFO(self, msg, opt=None)
Infomation level log output macro.
def __del__(self)
destractor.


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:06