00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 import sys
00018 import traceback
00019 import time
00020 import threading
00021 import logging
00022 import logging.handlers
00023
00024 logger = None
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 class Logger:
00040 """
00041 """
00042
00043 SILENT = 0
00044 FATAL = 41
00045 ERROR = 40
00046 WARN = 30
00047 INFO = 20
00048 DEBUG = 10
00049 TRACE = 9
00050 VERBOSE = 8
00051 PARANOID = 7
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 def strToLogLevel(self, lv):
00070 if lv == "SILENT":
00071 return Logger.SILENT
00072 elif lv == "FATAL":
00073 return Logger.FATAL
00074 elif lv == "ERROR":
00075 return Logger.ERROR
00076 elif lv == "WARN":
00077 return Logger.WARN
00078 elif lv == "INFO":
00079 return Logger.INFO
00080 elif lv == "DEBUG":
00081 return Logger.DEBUG
00082 elif lv == "TRACE":
00083 return Logger.TRACE
00084 elif lv == "VERBOSE":
00085 return Logger.VERBOSE
00086 elif lv == "PARANOID":
00087 return Logger.PARANOID
00088 else:
00089 return Logger.INFO
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 def __init__(self, *args):
00109 self._mutex = threading.RLock()
00110 self._fhdlr = None
00111
00112
00113 def init(*args):
00114 global logger
00115
00116 if logger is not None:
00117 return logger
00118
00119
00120 logger = Logger()
00121 mode = None
00122 fileName = None
00123
00124 if len(args) == 0:
00125 return
00126 elif len(args) == 2:
00127 name = args[0]
00128 mode = args[1]
00129 elif len(args) == 3:
00130 name = args[0]
00131 mode = args[1]
00132 fileName = args[2]
00133
00134
00135 logging.PARANOID = logging.DEBUG - 3
00136 logging.VERBOSE = logging.DEBUG - 2
00137 logging.TRACE = logging.DEBUG - 1
00138 logging.FATAL = logging.ERROR + 1
00139
00140 logging.addLevelName(logging.PARANOID, "PARANOID")
00141 logging.addLevelName(logging.VERBOSE, "VERBOSE")
00142 logging.addLevelName(logging.TRACE, "TRACE")
00143 logging.addLevelName(logging.FATAL, "FATAL")
00144
00145 """
00146 logging.root.setLevel([logging.NOTSET,
00147 logging.PARANOID,
00148 logging.VERBOSE,
00149 logging.TRACE,
00150 logging.DEBUG,
00151 logging.INFO,
00152 logging.WARNING,
00153 logging.ERROR,
00154 logging.FATAL,
00155 logging.CRITICAL])
00156 """
00157
00158
00159 formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
00160
00161 if mode is None or mode == "FILE":
00162 if fileName:
00163 logger._fhdlr = logging.FileHandler(fileName)
00164 else:
00165 logger._fhdlr = logging.FileHandler('rtcsystem.log')
00166
00167 mhdlr = logging.handlers.MemoryHandler(1024,logging.NOTSET, logger._fhdlr)
00168 logger._fhdlr.setFormatter(formatter)
00169 logging.getLogger("").addHandler(mhdlr)
00170 logging.getLogger("").setLevel(logging.NOTSET)
00171
00172 elif mode == "STDOUT":
00173 ch = logging.StreamHandler()
00174 ch.setLevel(logging.NOTSET)
00175 ch.setFormatter(formatter)
00176 logging.getLogger("").addHandler(ch)
00177
00178 return logger
00179
00180 init = staticmethod(init)
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 def __del__(self):
00199
00200
00201
00202 pass
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 def printf(self, fmt):
00223 return fmt
00224
00225
00226 def addHandler(self, *args):
00227 mode = None
00228 fileName = None
00229
00230 if len(args) == 0:
00231 return
00232 elif len(args) == 1:
00233 mode = args[0]
00234 elif len(args) == 2:
00235 mode = args[0]
00236 fileName = args[1]
00237
00238 formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
00239
00240 if mode is None or mode == "FILE":
00241 if fileName:
00242 self._fhdlr = logging.FileHandler(fileName)
00243 else:
00244 self._fhdlr = logging.FileHandler('rtcsystem.log')
00245
00246 mhdlr = logging.handlers.MemoryHandler(1024,logging.NOTSET, self._fhdlr)
00247 self._fhdlr.setFormatter(formatter)
00248 logging.getLogger("").addHandler(mhdlr)
00249
00250 elif mode.lower() == "stdout":
00251 ch = logging.StreamHandler()
00252 ch.setLevel(logging.NOTSET)
00253 ch.setFormatter(formatter)
00254 logging.getLogger("").addHandler(ch)
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 def print_exception():
00272 if sys.version_info[0:3] >= (2, 4, 0):
00273 return traceback.format_exc()
00274 else:
00275 _exc_list = traceback.format_exception(*sys.exc_info())
00276 _exc_str = "".join(_exc_list)
00277 return _exc_str
00278
00279 print_exception = staticmethod(print_exception)
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 class LogStream:
00296 """
00297 """
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 def __init__(self, *args):
00315 self._LogLock = False
00316 self._log_enable = False
00317 self._loggerObj = None
00318 name = ""
00319
00320 if len(args) == 0:
00321 return
00322 elif len(args) > 0:
00323 name = args[0]
00324
00325 self._loggerObj = Logger.init(*args)
00326 self._log_enable = True
00327 self.logger = logging.getLogger(name)
00328
00329
00330 def __del__(self):
00331 return
00332
00333 def shutdown(self):
00334 logging.shutdown()
00335 return
00336
00337 def addHandler(self, *args):
00338 if self._loggerObj is not None:
00339 self._loggerObj.addHandler(*args)
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 def setLogLevel(self, level):
00356 if level == "INFO":
00357 self.logger.setLevel(logging.INFO)
00358 elif level == "FATAL":
00359 self.logger.setLevel(logging.FATAL)
00360 elif level == "ERROR":
00361 self.logger.setLevel(logging.ERROR)
00362 elif level == "WARN":
00363 self.logger.setLevel(logging.WARNING)
00364 elif level == "DEBUG":
00365 self.logger.setLevel(logging.DEBUG)
00366 elif level == "SILENT":
00367 self.logger.setLevel(logging.NOTSET)
00368 elif level == "TRACE":
00369 self.logger.setLevel(logging.TRACE)
00370 elif level == "VERBOSE":
00371 self.logger.setLevel(logging.VERBOSE)
00372 elif level == "PARANOID":
00373 self.logger.setLevel(logging.PARANOID)
00374 else:
00375 self.logger.setLevel(logging.INFO)
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 def setLogLock(self, lock):
00392 if lock == 1:
00393 self._LogLock = True
00394 elif lock == 0:
00395 self._LogLock = False
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 def enableLogLock(self):
00411 self._LogLock = True
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 def disableLogLock(self):
00427 self._LogLock = False
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 def acquire(self):
00442 if self._LogLock:
00443 self.guard = OpenRTM_aist.ScopedLock(self._mutex)
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 def release(self):
00458 if self._LogLock:
00459 del self.guard
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480 def RTC_LOG(self, LV, msg, opt=None):
00481 if self._log_enable:
00482 self.acquire()
00483
00484 if opt is None:
00485 messages = msg
00486 else:
00487 try:
00488 messages = msg%(opt)
00489 except:
00490 print "RTC_LOG : argument error"
00491 return
00492
00493 self.logger.log(LV,messages)
00494
00495 self.release()
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 def RTC_FATAL(self, msg, opt=None):
00517 if self._log_enable:
00518 self.acquire()
00519
00520 if opt is None:
00521 messages = msg
00522 else:
00523 try:
00524 messages = msg%(opt)
00525 except:
00526 print "RTC_FATAL : argument error"
00527 return
00528
00529 self.logger.log(logging.FATAL,messages)
00530
00531 self.release()
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552 def RTC_ERROR(self, msg, opt=None):
00553 if self._log_enable:
00554 self.acquire()
00555
00556 if opt is None:
00557 messages = msg
00558 else:
00559 try:
00560 messages = msg%(opt)
00561 except:
00562 print "RTC_ERROR : argument error"
00563 return
00564
00565 self.logger.error(messages)
00566
00567 self.release()
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592 def RTC_WARN(self, msg, opt=None):
00593 if self._log_enable:
00594 self.acquire()
00595
00596 if opt is None:
00597 messages = msg
00598 else:
00599 try:
00600 messages = msg%(opt)
00601 except:
00602 print "RTC_WARN : argument error"
00603 return
00604
00605 self.logger.warning(messages)
00606
00607 self.release()
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632 def RTC_INFO(self, msg, opt=None):
00633 if self._log_enable:
00634 self.acquire()
00635
00636 if opt is None:
00637 messages = msg
00638 else:
00639 try:
00640 messages = msg%(opt)
00641 except:
00642 print "RTC_INFO : argument error"
00643 return
00644
00645 self.logger.info(messages)
00646
00647 self.release()
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 def RTC_DEBUG(self, msg, opt=None):
00673 if self._log_enable:
00674 self.acquire()
00675
00676 if opt is None:
00677 messages = msg
00678 else:
00679 try:
00680 messages = msg%(opt)
00681 except:
00682 print "RTC_DEBUG : argument error"
00683 return
00684
00685 self.logger.debug(messages)
00686
00687 self.release()
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712 def RTC_TRACE(self, msg, opt=None):
00713 if self._log_enable:
00714 self.acquire()
00715
00716 if opt is None:
00717 messages = msg
00718
00719 else:
00720 try:
00721 messages = msg%(opt)
00722 except:
00723 print "RTC_TRACE : argument error"
00724 return
00725
00726 self.logger.log(logging.TRACE,messages)
00727
00728 self.release()
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754 def RTC_VERBOSE(self, msg, opt=None):
00755 if self._log_enable:
00756 self.acquire()
00757
00758 if opt is None:
00759 messages = msg
00760 else:
00761 try:
00762 messages = msg%(opt)
00763 except:
00764 print "RTC_VERBOSE : argument error"
00765 return
00766
00767 self.logger.log(logging.VERBOSE,messages)
00768
00769 self.release()
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796 def RTC_PARANOID(self, msg, opt=None):
00797 if self._log_enable:
00798 self.acquire()
00799
00800 if opt is None:
00801 messages = msg
00802 else:
00803 try:
00804 messages = msg%(opt)
00805 except:
00806 print "RTC_PARANOID : argument error"
00807 return
00808
00809 self.logger.log(logging.PARANOID,messages)
00810
00811 self.release()
00812
00813