log.py 4.5 KB

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