1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import sys
- import os
- from datetime import datetime
- class LogLevels:
- QUIET = 0
- ERROR = 1 << 1
- WARNING = 1 << 2
- INFO = 1 << 3
- DEBUG = 1 << 4
- LogLevel = LogLevels.QUIET
- DebugMode = True if os.getenv('SEARXQT_DEBUG', None) is not None else False
- del os
- def outputMessageFactory(name, level, out=sys.stdout):
- def decorator(func):
- def wrap(msg, cls=None):
- if not DebugMode and not (LogLevel & level):
- return
- if cls is None:
- cls = sys._getframe().f_back.f_code.co_name
- else:
- cls = cls.__module__
- print("{0} {1} <{2}>: {3}".format(
- datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
- name,
- cls,
- msg
- ),
- file=out,
- flush=DebugMode
- )
- return wrap
- return decorator
- @outputMessageFactory('INFO', LogLevels.INFO, out=sys.stdout)
- def info(msg, cls=None):
- pass
- @outputMessageFactory('ERROR', LogLevels.ERROR, out=sys.stderr)
- def error(msg, cls=None):
- pass
- @outputMessageFactory('WARNING', LogLevels.WARNING, out=sys.stderr)
- def warning(msg, cls=None):
- pass
- @outputMessageFactory('DEBUG', LogLevels.DEBUG, out=sys.stderr)
- def debug(msg, cls=None):
- pass
|