log.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ########################################################################
  2. # Searx-Qt - Lightweight desktop application for Searx.
  3. # Copyright (C) 2020-2022 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. import os
  23. from datetime import datetime
  24. class LogLevels:
  25. QUIET = 0 # noqa: E221
  26. ERROR = 1 << 1 # noqa: E221
  27. WARNING = 1 << 2 # noqa: E221
  28. INFO = 1 << 3 # noqa: E221
  29. DEBUG = 1 << 4 # noqa: E221
  30. LogLevel = LogLevels.QUIET
  31. # When enviroment variable 'SEARXQT_DEBUG' is set it will output all levels no
  32. # matter what the LogLevel flags are.
  33. DebugMode = True if os.getenv('SEARXQT_DEBUG', None) is not None else False
  34. del os
  35. def outputMessageFactory(name, level, out=sys.stdout):
  36. def decorator(func):
  37. def wrap(msg, cls=None):
  38. if not DebugMode and not (LogLevel & level):
  39. return
  40. if cls is None:
  41. cls = sys._getframe().f_back.f_code.co_name
  42. else:
  43. cls = cls.__module__
  44. print("{0} {1} <{2}>: {3}".format(
  45. datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
  46. name,
  47. cls,
  48. msg
  49. ),
  50. file=out,
  51. flush=DebugMode
  52. )
  53. return wrap
  54. return decorator
  55. @outputMessageFactory('INFO', LogLevels.INFO, out=sys.stdout)
  56. def info(msg, cls=None):
  57. pass
  58. @outputMessageFactory('ERROR', LogLevels.ERROR, out=sys.stderr)
  59. def error(msg, cls=None):
  60. pass
  61. @outputMessageFactory('WARNING', LogLevels.WARNING, out=sys.stderr)
  62. def warning(msg, cls=None):
  63. pass
  64. @outputMessageFactory('DEBUG', LogLevels.DEBUG, out=sys.stderr)
  65. def debug(msg, cls=None):
  66. pass