log.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. FORMAT_CONSOLE = '%(message)s'
  30. FORMAT_LOG = '%(asctime)s %(message)s'
  31. FORMAT_DEBUG = '%(asctime)s ' \
  32. '[%(processName)s %(module)s.%(funcName)s:%(lineno)d] %(name)s: %(message)s'
  33. LOGPATH = '/var/log/qubes'
  34. LOGFILE = os.path.join(LOGPATH, 'qubes.log')
  35. formatter_console = logging.Formatter(FORMAT_CONSOLE)
  36. formatter_log = logging.Formatter(FORMAT_LOG)
  37. formatter_debug = logging.Formatter(FORMAT_DEBUG)
  38. def enable():
  39. '''Enable global logging
  40. Use :py:mod:`logging` module from standard library to log messages.
  41. >>> import qubes.log
  42. >>> qubes.log.enable() # doctest: +SKIP
  43. >>> import logging
  44. >>> logging.warning('Foobar') # doctest: +SKIP
  45. '''
  46. if logging.root.handlers:
  47. return
  48. handler_console = logging.StreamHandler(sys.stderr)
  49. handler_console.setFormatter(formatter_console)
  50. logging.root.addHandler(handler_console)
  51. handler_log = logging.FileHandler('log', 'a', encoding='utf-8')
  52. handler_log.setFormatter(formatter_log)
  53. logging.root.addHandler(handler_log)
  54. logging.root.setLevel(logging.INFO)
  55. def enable_debug():
  56. '''Enable debug logging
  57. Enable more messages and additional info to message format.
  58. '''
  59. enable()
  60. logging.root.setLevel(logging.DEBUG)
  61. for handler in logging.root.handlers:
  62. handler.setFormatter(formatter_debug)
  63. def get_vm_logger(vmname):
  64. '''Initialise logging for particular VM name
  65. :param str vmname: VM's name
  66. :rtype: :py:class:`logging.Logger`
  67. '''
  68. logger = logging.getLogger('vm.' + vmname)
  69. handler = logging.FileHandler(
  70. os.path.join(LOGPATH, 'vm-{}.log'.format(vmname)))
  71. handler.setFormatter(formatter_log)
  72. logger.addHandler(handler)
  73. return logger