forked from QubesOS/qubes-core-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
149 lines (120 loc) · 4.5 KB
/
log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#
# The Qubes OS Project, https://www.qubes-os.org/
#
# Copyright (C) 2014-2015 Joanna Rutkowska <[email protected]>
# Copyright (C) 2014-2015 Wojtek Porczyk <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
#
'''Qubes logging routines
See also: :py:attr:`qubes.vm.qubesvm.QubesVM.log`
'''
import logging
import os
import sys
import fcntl
import dbus
FORMAT_CONSOLE = '%(message)s'
FORMAT_LOG = '%(asctime)s %(message)s'
FORMAT_DEBUG = '%(asctime)s ' \
'[%(processName)s %(module)s.%(funcName)s:%(lineno)d] %(name)s: %(message)s'
LOGPATH = '/var/log/qubes'
LOGFILE = os.path.join(LOGPATH, 'qubes.log')
formatter_console = logging.Formatter(FORMAT_CONSOLE)
formatter_log = logging.Formatter(FORMAT_LOG)
formatter_debug = logging.Formatter(FORMAT_DEBUG)
class DBusHandler(logging.Handler):
'''Handler which displays records as DBus notifications'''
#: mapping of loglevels to icons
app_icons = {
logging.ERROR: 'dialog-error',
logging.WARNING: 'dialog-warning',
logging.NOTSET: 'dialog-information',
}
def __init__(self, *args, **kwargs):
super(DBusHandler, self).__init__(*args, **kwargs)
self._notify_object = dbus.SessionBus().get_object(
'org.freedesktop.Notifications', '/org/freedesktop/Notifications')
def emit(self, record):
app_icon = self.app_icons[
max(level for level in self.app_icons if level <= record.levelno)]
try:
# https://developer.gnome.org/notification-spec/#command-notify
self._notify_object.Notify(
'Qubes', # STRING app_name
0, # UINT32 replaces_id
app_icon, # STRING app_icon
record.msg, # STRING summary
'', # STRING body
(), # ARRAY actions
{}, # DICT hints
0, # INT32 timeout
dbus_interface='org.freedesktop.Notifications')
except dbus.DBusException:
pass
def enable():
'''Enable global logging
Use :py:mod:`logging` module from standard library to log messages.
>>> import qubes.log
>>> qubes.log.enable() # doctest: +SKIP
>>> import logging
>>> logging.warning('Foobar') # doctest: +SKIP
'''
if logging.root.handlers:
return
handler_console = logging.StreamHandler(sys.stderr)
handler_console.setFormatter(formatter_console)
logging.root.addHandler(handler_console)
if os.path.exists('/var/log/qubes'):
log_path = '/var/log/qubes/qubes.log'
else:
# for tests, travis etc
log_path = '/tmp/qubes.log'
old_umask = os.umask(0o007)
try:
handler_log = logging.FileHandler(log_path, 'a', encoding='utf-8')
fcntl.fcntl(handler_log.stream.fileno(),
fcntl.F_SETFD, fcntl.FD_CLOEXEC)
finally:
os.umask(old_umask)
handler_log.setFormatter(formatter_log)
logging.root.addHandler(handler_log)
logging.root.setLevel(logging.INFO)
def enable_debug():
'''Enable debug logging
Enable more messages and additional info to message format.
'''
enable()
logging.root.setLevel(logging.DEBUG)
for handler in logging.root.handlers:
handler.setFormatter(formatter_debug)
def get_vm_logger(vmname):
'''Initialise logging for particular VM name
:param str vmname: VM's name
:rtype: :py:class:`logging.Logger`
'''
logger = logging.getLogger('vm.' + vmname)
if logger.handlers:
return logger
old_umask = os.umask(0o007)
try:
handler = logging.FileHandler(
os.path.join(LOGPATH, 'vm-{}.log'.format(vmname)))
fcntl.fcntl(handler.stream.fileno(),
fcntl.F_SETFD, fcntl.FD_CLOEXEC)
finally:
os.umask(old_umask)
handler.setFormatter(formatter_log)
logger.addHandler(handler)
return logger