chiark / gitweb /
python: change license to LGPL 2.1
[elogind.git] / src / python-systemd / journal.py
1 #  -*- Mode: python; indent-tabs-mode: nil -*- */
2 #
3 #  This file is part of systemd.
4 #
5 #  Copyright 2012 David Strauss
6 #
7 #  systemd is free software; you can redistribute it and/or modify it
8 #  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 #  systemd is distributed in the hope that it will be useful, but
13 #  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 License
18 #  along with systemd; If not, see <http://www.gnu.org/licenses/>.
19
20 import traceback as _traceback
21 import os as _os
22 from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
23                     LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
24 from ._journal import sendv, stream_fd
25
26 def _make_line(field, value):
27     if isinstance(value, bytes):
28         return field.encode('utf-8') + b'=' + value
29     else:
30         return field + '=' + value
31
32 def send(MESSAGE, MESSAGE_ID=None,
33          CODE_FILE=None, CODE_LINE=None, CODE_FUNC=None,
34          **kwargs):
35     r"""Send a message to journald.
36
37     >>> journal.send('Hello world')
38     >>> journal.send('Hello, again, world', FIELD2='Greetings!')
39     >>> journal.send('Binary message', BINARY=b'\xde\xad\xbe\xef')
40
41     Value of the MESSAGE argument will be used for the MESSAGE= field.
42
43     MESSAGE_ID can be given to uniquely identify the type of message.
44
45     Other parts of the message can be specified as keyword arguments.
46
47     Both MESSAGE and MESSAGE_ID, if present, must be strings, and will
48     be sent as UTF-8 to journal. Other arguments can be bytes, in
49     which case they will be sent as-is to journal.
50
51     CODE_LINE, CODE_FILE, and CODE_FUNC can be specified to identify
52     the caller. Unless at least on of the three is given, values are
53     extracted from the stack frame of the caller of send(). CODE_FILE
54     and CODE_FUNC must be strings, CODE_LINE must be an integer.
55
56     Other useful fields include PRIORITY, SYSLOG_FACILITY,
57     SYSLOG_IDENTIFIER, SYSLOG_PID.
58     """
59
60     args = ['MESSAGE=' + MESSAGE]
61
62     if MESSAGE_ID is not None:
63         args.append('MESSAGE_ID=' + MESSAGE_ID)
64
65     if CODE_LINE == CODE_FILE == CODE_FUNC == None:
66         CODE_FILE, CODE_LINE, CODE_FUNC = \
67             _traceback.extract_stack(limit=2)[0][:3]
68     if CODE_FILE is not None:
69         args.append('CODE_FILE=' + CODE_FILE)
70     if CODE_LINE is not None:
71         args.append('CODE_LINE={:d}'.format(CODE_LINE))
72     if CODE_FUNC is not None:
73         args.append('CODE_FUNC=' + CODE_FUNC)
74
75     args.extend(_make_line(key, val) for key, val in kwargs.items())
76     return sendv(*args)
77
78 def stream(identifier, priority=LOG_DEBUG, level_prefix=False):
79     r"""Return a file object wrapping a stream to journal.
80
81     Log messages written to this file as simple newline sepearted
82     text strings are written to the journal.
83
84     The file will be line buffered, so messages are actually sent
85     after a newline character is written.
86
87     >>> stream = journal.stream('myapp')
88     >>> stream
89     <open file '<fdopen>', mode 'w' at 0x...>
90     >>> stream.write('message...\n')
91
92     will produce the following message in the journal:
93
94     PRIORITY=7
95     SYSLOG_IDENTIFIER=myapp
96     MESSAGE=message...
97
98     Using the interface with print might be more convinient:
99
100     >>> from __future__ import print_function
101     >>> print('message...', file=stream)
102
103     priority is the syslog priority, one of LOG_EMERG, LOG_ALERT,
104     LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG.
105
106     level_prefix is a boolean. If true, kernel-style log priority
107     level prefixes (such as '<1>') are interpreted. See sd-daemon(3)
108     for more information.
109     """
110
111     fd = stream_fd(identifier, priority, level_prefix)
112     return _os.fdopen(fd, 'w', 1)