[systemd-commits] 5 commits - configure.ac src/analyze src/python-systemd src/shared
Zbigniew JÄdrzejewski-Szmek
zbyszek at kemper.freedesktop.org
Tue Nov 13 02:15:39 PST 2012
configure.ac | 27 ++++++-------
src/analyze/systemd-analyze | 60 ++++++++++-------------------
src/python-systemd/_journal.c | 4 -
src/python-systemd/journal.py | 85 ++++++++++++++++++++++++++++++++++++++++++
src/shared/socket-util.c | 3 -
5 files changed, 123 insertions(+), 56 deletions(-)
New commits:
commit 73c0495f6830ca99c0c6eb15e83c3c7d44096d19
Author: Marti Raudsepp <marti at juffo.org>
Date: Tue Oct 9 18:12:02 2012 +0300
python: add journal backend for the logging framework
Supports Python versions 2.6 through 3.3 (tested on 2.7 and 3.2).
See JournalHandler docstring for usage details.
[zj: - use send() instead of using sendv() directly
- do exception handling like in the logging module
- bumped min version to python2.6, since the module
does not work with python2.5 anyway ]
diff --git a/src/python-systemd/journal.py b/src/python-systemd/journal.py
index 760d2db..516ca1a 100644
--- a/src/python-systemd/journal.py
+++ b/src/python-systemd/journal.py
@@ -19,6 +19,7 @@
import traceback as _traceback
import os as _os
+import logging as _logging
from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
from ._journal import sendv, stream_fd
@@ -114,3 +115,87 @@ def stream(identifier, priority=LOG_DEBUG, level_prefix=False):
fd = stream_fd(identifier, priority, level_prefix)
return _os.fdopen(fd, 'w', 1)
+
+class JournalHandler(_logging.Handler):
+ """Journal handler class for the Python logging framework.
+
+ Please see the Python logging module documentation for an
+ overview: http://docs.python.org/library/logging.html
+
+ To create a custom logger whose messages go only to journal:
+
+ >>> log = logging.getLogger('custom_logger_name')
+ >>> log.propagate = False
+ >>> log.addHandler(journal.JournalHandler())
+ >>> log.warn("Some message: %s", detail)
+
+ Note that by default, message levels INFO and DEBUG are ignored
+ by the logging framework. To enable those log levels:
+
+ >>> log.setLevel(logging.DEBUG)
+
+ To attach journal MESSAGE_ID, an extra field is supported:
+
+ >>> log.warn("Message with ID",
+ >>> extra={'MESSAGE_ID': '22bb01335f724c959ac4799627d1cb61'})
+
+ To redirect all logging messages to journal regardless of where
+ they come from, attach it to the root logger:
+
+ >>> logging.root.addHandler(journal.JournalHandler())
+
+ For more complex configurations when using dictConfig or
+ fileConfig, specify 'systemd.journal.JournalHandler' as the
+ handler class. Only standard handler configuration options
+ are supported: level, formatter, filters.
+
+ The following journal fields will be sent:
+
+ MESSAGE, PRIORITY, THREAD_NAME, CODE_FILE, CODE_LINE,
+ CODE_FUNC, LOGGER (name as supplied to getLogger call),
+ MESSAGE_ID (optional, see above).
+ """
+
+ def emit(self, record):
+ """Write record as journal event.
+
+ MESSAGE is taken from the message provided by the
+ user, and PRIORITY, LOGGER, THREAD_NAME,
+ CODE_{FILE,LINE,FUNC} fields are appended
+ automatically. In addition, record.MESSAGE_ID will be
+ used if present.
+ """
+ try:
+ msg = self.format(record)
+ pri = self.mapPriority(record.levelno)
+ mid = getattr(record, 'MESSAGE_ID', None)
+ send(msg,
+ MESSAGE_ID=mid,
+ PRIORITY=format(pri),
+ LOGGER=record.name,
+ THREAD_NAME=record.threadName,
+ CODE_FILE=record.pathname,
+ CODE_LINE=record.lineno,
+ CODE_FUNC=record.funcName)
+ except Exception:
+ self.handleError(record)
+
+ @staticmethod
+ def mapPriority(levelno):
+ """Map logging levels to journald priorities.
+
+ Since Python log level numbers are "sparse", we have
+ to map numbers in between the standard levels too.
+ """
+ if levelno <= _logging.DEBUG:
+ return LOG_DEBUG
+ elif levelno <= _logging.INFO:
+ return LOG_INFO
+ elif levelno <= _logging.WARNING:
+ return LOG_WARNING
+ elif levelno <= _logging.ERROR:
+ return LOG_ERR
+ elif levelno <= _logging.CRITICAL:
+ return LOG_CRIT
+ else:
+ return LOG_ALERT
commit 568c7e02372ff7b8eb41172ad7c3a426723512f8
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Thu Nov 1 08:58:13 2012 +0100
systemd-python: use python${PYTHON_VERSION}-config as python-config
This is the usual setup, where pythonX.Y and pythonX.Y-config go
together. Using python-config with python3 will only lead to
confusion.
--libs is changed to --ldflags, since the latter also includes other
required flags like -L<dir>.
The tests for HAVE_PYTHON and HAVE_PYTHON_DEVEL are separated. It is
possible to have python development libraries installed without the
binary (or to want to build python modules without using python during
the build).
A line is added to the output, to show what flags will be used for
python.
diff --git a/configure.ac b/configure.ac
index d0ce504..c4638d1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -100,21 +100,20 @@ AC_ARG_WITH([python],
AS_IF([test "x$with_python" != "xno"], [
AM_PATH_PYTHON(,, [:])
- if test "$PYTHON" != : ; then
- have_python=yes
- AC_PATH_PROG([PYTHON_CONFIG], python-config)
-
- if test -n "$PYTHON_CONFIG" ; then
- have_python_devel=yes
- PYTHON_CFLAGS="`$PYTHON_CONFIG --cflags`"
- PYTHON_LIBS="`$PYTHON_CONFIG --libs`"
- AC_SUBST(PYTHON_CFLAGS)
- AC_SUBST(PYTHON_LIBS)
- fi
- fi
+ AS_IF([test "$PYTHON" != :], [have_python=yes])
])
-
AM_CONDITIONAL([HAVE_PYTHON], [test "$have_python" = "yes"])
+
+AS_IF([test "x$with_python" != "xno"], [
+ AC_PATH_PROG(PYTHON_CONFIG, python${PYTHON_VERSION}-config)
+ AS_IF([test -n "$PYTHON_CONFIG"], [
+ have_python_devel=yes
+ PYTHON_CFLAGS="`$PYTHON_CONFIG --cflags`"
+ PYTHON_LIBS="`$PYTHON_CONFIG --ldflags`"
+ AC_SUBST(PYTHON_CFLAGS)
+ AC_SUBST(PYTHON_LIBS)
+ ])
+])
AM_CONDITIONAL([HAVE_PYTHON_DEVEL], [test "$have_python_devel" = "yes"])
CC_CHECK_FLAGS_APPEND([with_cflags], [CFLAGS], [\
@@ -877,4 +876,6 @@ AC_MSG_RESULT([
CFLAGS: ${OUR_CFLAGS} ${CFLAGS}
CPPLAGS: ${OUR_CPPFLAGS} ${CPPFLAGS}
LDFLAGS: ${OUR_LDFLAGS} ${LDFLAGS}
+ PYTHON_CFLAGS: ${PYTHON_CFLAGS}
+ PYTHON_LIBS: ${PYTHON_LIBS}
])
commit 0c0271841ab45595f71528c50bcf1904d4b841d5
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Sat Nov 3 18:26:28 2012 +0100
systemd-analyze: use argparse instead of getopt
Makes the output way nicer with shorter code. Also brings
systemd-analyze behaviour more in line with other systemd-programs.
Argparse is in Python since 2.6, and is available as a package for
previous versions, if someone is stuck with very old Python.
diff --git a/src/analyze/systemd-analyze b/src/analyze/systemd-analyze
index 87a83dd..88699d6 100755
--- a/src/analyze/systemd-analyze
+++ b/src/analyze/systemd-analyze
@@ -1,6 +1,7 @@
#!/usr/bin/python
-import getopt, sys, os
+import sys, os
+import argparse
from gi.repository import Gio
try:
import cairo
@@ -75,20 +76,6 @@ def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5
context.restore()
-def usage():
- sys.stdout.write("""systemd-analyze [--user] time
-systemd-analyze [--user] blame
-systemd-analyze [--user] plot
-
-Process systemd profiling information
-
- -h --help Show this help
-""")
-
-def help():
- usage()
- sys.exit()
-
def time():
initrd_time, start_time, finish_time = acquire_start_time()
@@ -279,34 +266,29 @@ def plot():
surface.finish()
-def unknown_verb():
- sys.stderr.write("Unknown verb '%s'.\n" % args[0])
- usage()
- sys.exit(1)
+parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
+ description='Process systemd profiling information',
+ epilog='''\
+time - print time spent in the kernel before reaching userspace
+blame - print list of running units ordered by time to init
+plot - output SVG graphic showing service initialization
+''')
-bus = Gio.BusType.SYSTEM
+parser.add_argument('action', choices=('time', 'blame', 'plot'),
+ default='time', nargs='?',
+ help='action to perform (default: time)')
+parser.add_argument('--user', action='store_true',
+ help='use the session bus')
-try:
- opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help", "user"])
-except getopt.GetoptError as err:
- sys.stdout.write(str(err) + "\n")
- usage()
- sys.exit(2)
-for o, a in opts:
- if o in ("-h", "--help"):
- help()
- elif o == '--user':
- bus = Gio.BusType.SESSION
- else:
- assert False, "unhandled option"
+args = parser.parse_args()
+
+if args.user:
+ bus = Gio.BusType.SESSION
+else:
+ bus = Gio.BusType.SYSTEM
verb = {'time' : time,
'blame': blame,
'plot' : plot,
- 'help' : help,
}
-
-if len(args) == 0:
- time()
-else:
- verb.get(args[0], unknown_verb)()
+verb.get(args.action)()
commit c94f4b8b53aad25ba364bb79fc53c5b79de28088
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Thu Nov 1 00:10:47 2012 +0100
systemd-python: fix nesting of #ifs and #pragmas
diff --git a/src/python-systemd/_journal.c b/src/python-systemd/_journal.c
index d27178d..0bdf709 100644
--- a/src/python-systemd/_journal.c
+++ b/src/python-systemd/_journal.c
@@ -136,6 +136,6 @@ PyMODINIT_FUNC PyInit__journal(void) {
return PyModule_Create(&module);
}
-#pragma GCC diagnostic pop
-
#endif
+
+#pragma GCC diagnostic pop
commit 6b6ed3e3eb629a822447e1266a137b8ff852aebf
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Wed Oct 31 21:44:54 2012 +0100
shared/socket-util: kill gcc warning about uninitialized variable
The warning was invalid, but distracting.
diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c
index 8bc3729..49ea758 100644
--- a/src/shared/socket-util.c
+++ b/src/shared/socket-util.c
@@ -363,13 +363,12 @@ int socket_address_print(const SocketAddress *a, char **p) {
}
case AF_NETLINK: {
- char *sfamily;
+ char _cleanup_free_ *sfamily = NULL;
r = netlink_family_to_string_alloc(a->protocol, &sfamily);
if (r < 0)
return r;
r = asprintf(p, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
- free(sfamily);
return 0;
}
More information about the systemd-commits
mailing list