log.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ########################################################################
  2. # Searx-qt - Lightweight desktop application for SearX.
  3. # Copyright (C) 2020 CYBERDEViL
  4. #
  5. # This file is part of Searx-qt.
  6. #
  7. # Searx-qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. import sys
  22. from datetime import datetime
  23. class LogLevels:
  24. QUIET = 0
  25. ERROR = 1 << 1
  26. WARNING = 1 << 2
  27. INFO = 1 << 3
  28. DEBUG = 1 << 4
  29. LogLevel = LogLevels.QUIET
  30. def outputMessageFactory(name, level, out=sys.stdout):
  31. def decorator(func):
  32. def wrap(msg, cls=None):
  33. if not (LogLevel & level):
  34. return
  35. if cls is None:
  36. cls = sys._getframe().f_back.f_code.co_name
  37. else:
  38. cls = cls.__module__
  39. print("{0} {1} <{2}>: {3}".format(
  40. datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
  41. name,
  42. cls,
  43. msg
  44. ), file=out
  45. )
  46. return wrap
  47. return decorator
  48. @outputMessageFactory('INFO', LogLevels.INFO, out=sys.stdout)
  49. def info(msg, cls=None):
  50. pass
  51. @outputMessageFactory('ERROR', LogLevels.ERROR, out=sys.stderr)
  52. def error(msg, cls=None):
  53. pass
  54. @outputMessageFactory('WARNING', LogLevels.WARNING, out=sys.stderr)
  55. def warning(msg, cls=None):
  56. pass
  57. @outputMessageFactory('DEBUG', LogLevels.DEBUG, out=sys.stderr)
  58. def debug(msg, cls=None):
  59. pass