log.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation; either version 2.1 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program 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
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. '''Qubes logging routines
  22. See also: :py:attr:`qubes.vm.qubesvm.QubesVM.log`
  23. '''
  24. import logging
  25. import sys
  26. import dbus
  27. FORMAT_CONSOLE = '%(name)s: %(message)s'
  28. FORMAT_LOG = '%(asctime)s %(message)s'
  29. FORMAT_DEBUG = '%(asctime)s ' \
  30. '[%(processName)s %(module)s.%(funcName)s:%(lineno)d] %(name)s: %(message)s'
  31. formatter_console = logging.Formatter(FORMAT_CONSOLE)
  32. formatter_log = logging.Formatter(FORMAT_LOG)
  33. formatter_debug = logging.Formatter(FORMAT_DEBUG)
  34. class DBusHandler(logging.Handler):
  35. '''Handler which displays records as DBus notifications'''
  36. #: mapping of loglevels to icons
  37. app_icons = {
  38. logging.ERROR: 'dialog-error',
  39. logging.WARNING: 'dialog-warning',
  40. logging.NOTSET: 'dialog-information',
  41. }
  42. def __init__(self, *args, **kwargs):
  43. super(DBusHandler, self).__init__(*args, **kwargs)
  44. self._notify_object = dbus.SessionBus().get_object(
  45. 'org.freedesktop.Notifications', '/org/freedesktop/Notifications')
  46. def emit(self, record):
  47. app_icon = self.app_icons[
  48. max(level for level in self.app_icons if level <= record.levelno)]
  49. try:
  50. # https://developer.gnome.org/notification-spec/#command-notify
  51. self._notify_object.Notify(
  52. 'Qubes', # STRING app_name
  53. 0, # UINT32 replaces_id
  54. app_icon, # STRING app_icon
  55. record.msg, # STRING summary
  56. '', # STRING body
  57. (), # ARRAY actions
  58. {}, # DICT hints
  59. 0, # INT32 timeout
  60. dbus_interface='org.freedesktop.Notifications')
  61. except dbus.DBusException:
  62. pass
  63. def enable():
  64. '''Enable global logging
  65. Use :py:mod:`logging` module from standard library to log messages.
  66. >>> import qubes.log
  67. >>> qubes.log.enable() # doctest: +SKIP
  68. >>> import logging
  69. >>> logging.warning('Foobar') # doctest: +SKIP
  70. '''
  71. if logging.root.handlers:
  72. return
  73. handler_console = logging.StreamHandler(sys.stderr)
  74. handler_console.setFormatter(formatter_console)
  75. logging.root.addHandler(handler_console)
  76. logging.root.setLevel(logging.INFO)
  77. def enable_debug():
  78. '''Enable debug logging
  79. Enable more messages and additional info to message format.
  80. '''
  81. enable()
  82. logging.root.setLevel(logging.DEBUG)
  83. for handler in logging.root.handlers:
  84. handler.setFormatter(formatter_debug)