logger.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env/python
  2. # -*- coding: utf-8 -*-
  3. ###
  4. ### Author: Chris Iatrou (ichrispa@core-vector.net)
  5. ### Version: rev 13
  6. ###
  7. ### This program was created for educational purposes and has been
  8. ### contributed to the open62541 project by the author. All licensing
  9. ### terms for this source is inherited by the terms and conditions
  10. ### specified for by the open62541 project (see the projects readme
  11. ### file for more information on the LGPL terms and restrictions).
  12. ###
  13. ### This program is not meant to be used in a production environment. The
  14. ### author is not liable for any complications arising due to the use of
  15. ### this program.
  16. ###
  17. from __future__ import print_function
  18. import inspect
  19. ###
  20. ### Tidy logging helpers
  21. ###
  22. LOG_LEVEL_DEBUG = 4
  23. LOG_LEVEL_INFO = 2
  24. LOG_LEVEL_WARN = 1
  25. LOG_LEVEL_ERROR = 0
  26. LOG_LEVEL_SILENT = -1
  27. # Change the following to filter logging output
  28. GLOBAL_LOG_LEVEL = LOG_LEVEL_SILENT
  29. def log(callee, logstr, level=LOG_LEVEL_DEBUG):
  30. prefixes = { LOG_LEVEL_DEBUG : "DBG: ",
  31. LOG_LEVEL_INFO : "INF: ",
  32. LOG_LEVEL_WARN : "WRN: ",
  33. LOG_LEVEL_ERROR : "ERR: ",
  34. LOG_LEVEL_SILENT: ""
  35. }
  36. if level <= GLOBAL_LOG_LEVEL:
  37. if prefixes.has_key(level):
  38. print(str(prefixes[level]) + callee.__class__.__name__ + "." + inspect.stack()[1][3] + "(): " + logstr)
  39. else:
  40. print(callee.__class__.__name__ + "." + inspect.stack()[1][3] + "(): " + logstr)