log.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 os
  25. import sys
  26. import fcntl
  27. import dbus
  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. class DBusHandler(logging.Handler):
  37. '''Handler which displays records as DBus notifications'''
  38. #: mapping of loglevels to icons
  39. app_icons = {
  40. logging.ERROR: 'dialog-error',
  41. logging.WARNING: 'dialog-warning',
  42. logging.NOTSET: 'dialog-information',
  43. }
  44. def __init__(self, *args, **kwargs):
  45. super().__init__(*args, **kwargs)
  46. self._notify_object = dbus.SessionBus().get_object(
  47. 'org.freedesktop.Notifications', '/org/freedesktop/Notifications')
  48. def emit(self, record):
  49. app_icon = self.app_icons[
  50. max(level for level in self.app_icons if level <= record.levelno)]
  51. try:
  52. # https://developer.gnome.org/notification-spec/#command-notify
  53. self._notify_object.Notify(
  54. 'Qubes', # STRING app_name
  55. 0, # UINT32 replaces_id
  56. app_icon, # STRING app_icon
  57. record.msg, # STRING summary
  58. '', # STRING body
  59. (), # ARRAY actions
  60. {}, # DICT hints
  61. 0, # INT32 timeout
  62. dbus_interface='org.freedesktop.Notifications')
  63. except dbus.DBusException:
  64. pass
  65. def enable():
  66. '''Enable global logging
  67. Use :py:mod:`logging` module from standard library to log messages.
  68. >>> import qubes.log
  69. >>> qubes.log.enable() # doctest: +SKIP
  70. >>> import logging
  71. >>> logging.warning('Foobar') # doctest: +SKIP
  72. '''
  73. if logging.root.handlers:
  74. return
  75. handler_console = logging.StreamHandler(sys.stderr)
  76. handler_console.setFormatter(formatter_console)
  77. logging.root.addHandler(handler_console)
  78. if os.path.exists('/var/log/qubes'):
  79. log_path = '/var/log/qubes/qubes.log'
  80. else:
  81. # for tests, travis etc
  82. log_path = '/tmp/qubes.log'
  83. old_umask = os.umask(0o007)
  84. try:
  85. handler_log = logging.FileHandler(log_path, 'a', encoding='utf-8')
  86. fcntl.fcntl(handler_log.stream.fileno(),
  87. fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  88. finally:
  89. os.umask(old_umask)
  90. handler_log.setFormatter(formatter_log)
  91. logging.root.addHandler(handler_log)
  92. logging.root.setLevel(logging.INFO)
  93. def enable_debug():
  94. '''Enable debug logging
  95. Enable more messages and additional info to message format.
  96. '''
  97. enable()
  98. logging.root.setLevel(logging.DEBUG)
  99. for handler in logging.root.handlers:
  100. handler.setFormatter(formatter_debug)
  101. def get_vm_logger(vmname):
  102. '''Initialise logging for particular VM name
  103. :param str vmname: VM's name
  104. :rtype: :py:class:`logging.Logger`
  105. '''
  106. logger = logging.getLogger('vm.' + vmname)
  107. if logger.handlers:
  108. return logger
  109. old_umask = os.umask(0o007)
  110. try:
  111. handler = logging.FileHandler(
  112. os.path.join(LOGPATH, 'vm-{}.log'.format(vmname)))
  113. fcntl.fcntl(handler.stream.fileno(),
  114. fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  115. finally:
  116. os.umask(old_umask)
  117. handler.setFormatter(formatter_log)
  118. logger.addHandler(handler)
  119. return logger