log.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library 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 GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  19. #
  20. '''Qubes logging routines
  21. See also: :py:attr:`qubes.vm.qubesvm.QubesVM.log`
  22. '''
  23. import logging
  24. import logging.handlers
  25. import os
  26. import sys
  27. import fcntl
  28. FORMAT_CONSOLE = '%(message)s'
  29. FORMAT_LOG = '%(asctime)s %(message)s'
  30. FORMAT_DEBUG = '%(asctime)s ' \
  31. '[%(processName)s %(module)s.%(funcName)s:%(lineno)d] %(name)s: %(message)s'
  32. LOGPATH = '/var/log/qubes'
  33. formatter_console = logging.Formatter(FORMAT_CONSOLE)
  34. formatter_log = logging.Formatter(FORMAT_LOG)
  35. formatter_debug = logging.Formatter(FORMAT_DEBUG)
  36. def enable():
  37. '''Enable global logging
  38. Use :py:mod:`logging` module from standard library to log messages.
  39. >>> import qubes.log
  40. >>> qubes.log.enable() # doctest: +SKIP
  41. >>> import logging
  42. >>> logging.warning('Foobar') # doctest: +SKIP
  43. '''
  44. if logging.root.handlers:
  45. return
  46. handler_console = logging.StreamHandler(sys.stderr)
  47. handler_console.setFormatter(formatter_console)
  48. logging.root.addHandler(handler_console)
  49. if os.path.exists('/var/log/qubes'):
  50. log_path = '/var/log/qubes/qubes.log'
  51. else:
  52. # for tests, travis etc
  53. log_path = '/tmp/qubes.log'
  54. old_umask = os.umask(0o007)
  55. try:
  56. handler_log = logging.handlers.WatchedFileHandler(
  57. log_path, 'a', encoding='utf-8')
  58. fcntl.fcntl(handler_log.stream.fileno(),
  59. fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  60. finally:
  61. os.umask(old_umask)
  62. handler_log.setFormatter(formatter_log)
  63. logging.root.addHandler(handler_log)
  64. logging.root.setLevel(logging.INFO)
  65. def enable_debug():
  66. '''Enable debug logging
  67. Enable more messages and additional info to message format.
  68. '''
  69. enable()
  70. logging.root.setLevel(logging.DEBUG)
  71. for handler in logging.root.handlers:
  72. handler.setFormatter(formatter_debug)
  73. def get_vm_logger(vmname):
  74. '''Initialise logging for particular VM name
  75. :param str vmname: VM's name
  76. :rtype: :py:class:`logging.Logger`
  77. '''
  78. logger = logging.getLogger('vm.' + vmname)
  79. if logger.handlers:
  80. return logger
  81. old_umask = os.umask(0o007)
  82. try:
  83. handler = logging.handlers.WatchedFileHandler(
  84. os.path.join(LOGPATH, 'vm-{}.log'.format(vmname)))
  85. fcntl.fcntl(handler.stream.fileno(),
  86. fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  87. finally:
  88. os.umask(old_umask)
  89. handler.setFormatter(formatter_log)
  90. logger.addHandler(handler)
  91. return logger