log.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. '''Qubes logging routines
  24. See also: :py:attr:`qubes.vm.qubesvm.QubesVM.log`
  25. '''
  26. import logging
  27. import os
  28. import sys
  29. import dbus
  30. FORMAT_CONSOLE = '%(message)s'
  31. FORMAT_LOG = '%(asctime)s %(message)s'
  32. FORMAT_DEBUG = '%(asctime)s ' \
  33. '[%(processName)s %(module)s.%(funcName)s:%(lineno)d] %(name)s: %(message)s'
  34. LOGPATH = '/var/log/qubes'
  35. LOGFILE = os.path.join(LOGPATH, 'qubes.log')
  36. formatter_console = logging.Formatter(FORMAT_CONSOLE)
  37. formatter_log = logging.Formatter(FORMAT_LOG)
  38. formatter_debug = logging.Formatter(FORMAT_DEBUG)
  39. class DBusHandler(logging.Handler):
  40. '''Handler which displays records as DBus notifications'''
  41. #: mapping of loglevels to icons
  42. app_icons = {
  43. logging.ERROR: 'dialog-error',
  44. logging.WARNING: 'dialog-warning',
  45. logging.NOTSET: 'dialog-information',
  46. }
  47. def __init__(self, *args, **kwargs):
  48. super(DBusHandler, self).__init__(*args, **kwargs)
  49. self._notify_object = dbus.SessionBus().get_object(
  50. 'org.freedesktop.Notifications', '/org/freedesktop/Notifications')
  51. def emit(self, record):
  52. app_icon = self.app_icons[
  53. max(level for level in self.app_icons if level <= record.levelno)]
  54. try:
  55. # https://developer.gnome.org/notification-spec/#command-notify
  56. self._notify_object.Notify(
  57. 'Qubes', # STRING app_name
  58. 0, # UINT32 replaces_id
  59. app_icon, # STRING app_icon
  60. record.msg, # STRING summary
  61. '', # STRING body
  62. (), # ARRAY actions
  63. {}, # DICT hints
  64. 0, # INT32 timeout
  65. dbus_interface='org.freedesktop.Notifications')
  66. except dbus.DBusException:
  67. pass
  68. def enable():
  69. '''Enable global logging
  70. Use :py:mod:`logging` module from standard library to log messages.
  71. >>> import qubes.log
  72. >>> qubes.log.enable() # doctest: +SKIP
  73. >>> import logging
  74. >>> logging.warning('Foobar') # doctest: +SKIP
  75. '''
  76. if logging.root.handlers:
  77. return
  78. handler_console = logging.StreamHandler(sys.stderr)
  79. handler_console.setFormatter(formatter_console)
  80. logging.root.addHandler(handler_console)
  81. handler_log = logging.FileHandler('log', 'a', encoding='utf-8')
  82. handler_log.setFormatter(formatter_log)
  83. logging.root.addHandler(handler_log)
  84. logging.root.setLevel(logging.INFO)
  85. def enable_debug():
  86. '''Enable debug logging
  87. Enable more messages and additional info to message format.
  88. '''
  89. enable()
  90. logging.root.setLevel(logging.DEBUG)
  91. for handler in logging.root.handlers:
  92. handler.setFormatter(formatter_debug)
  93. def get_vm_logger(vmname):
  94. '''Initialise logging for particular VM name
  95. :param str vmname: VM's name
  96. :rtype: :py:class:`logging.Logger`
  97. '''
  98. logger = logging.getLogger('vm.' + vmname)
  99. handler = logging.FileHandler(
  100. os.path.join(LOGPATH, 'vm-{}.log'.format(vmname)))
  101. handler.setFormatter(formatter_log)
  102. logger.addHandler(handler)
  103. return logger