[systemd-commits] 9 commits - src/core src/journal src/libsystemd-bus src/python-systemd src/readahead src/shared src/systemd src/test src/udev

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Thu Apr 25 18:52:30 PDT 2013


 src/core/manager.h                |    2 -
 src/core/unit.c                   |    5 +++-
 src/core/unit.h                   |    2 -
 src/journal/journald-server.h     |    2 -
 src/journal/microhttpd-util.h     |    4 ++-
 src/libsystemd-bus/bus-kernel.c   |    2 -
 src/python-systemd/journal.py     |   42 +++++++++++++++++++++++++++++---------
 src/readahead/readahead-analyze.c |   12 +++++-----
 src/shared/cgroup-util.c          |    1 
 src/shared/log.h                  |    6 ++---
 src/shared/macro.h                |    1 
 src/shared/strv.h                 |   14 ++++++------
 src/shared/util.h                 |   12 +++++-----
 src/systemd/sd-bus.h              |   10 ++++++++-
 src/systemd/sd-daemon.h           |   10 ++++-----
 src/systemd/sd-journal.h          |   24 ++++++++++++++++-----
 src/test/test-util.c              |   11 +++++++++
 src/udev/udev.h                   |    7 ++----
 18 files changed, 113 insertions(+), 54 deletions(-)

New commits:
commit fcf8c44041a5138a170f9a442809c032259a26b1
Author: Mirco Tischler <mt-ml at gmx.de>
Date:   Thu Apr 25 15:14:57 2013 +0200

    core: remove duplicate MESSAGE= from log message
    
    This was needed with log_struct_unit() but log_notice_unit() adds it
    anyway.

diff --git a/src/core/unit.c b/src/core/unit.c
index 7029b64..282852f 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -1468,7 +1468,7 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su
 
                 if (ns != os && ns == UNIT_FAILED) {
                         log_notice_unit(u->id,
-                                        "MESSAGE=Unit %s entered failed state.", u->id);
+                                        "Unit %s entered failed state.", u->id);
                         unit_start_on_failure(u);
                 }
         }

commit 2a371001f8d23533a339a150eeffa3215773058d
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Thu Apr 25 20:53:29 2013 -0400

    Use attribute(unused) in PROTECT_ERRNO
    
    clang emits warnings about unused attribute _saved_errno_, which drown
    out other—potentially useful—warnings. gcc documentation is not exactly
    verbose about the effects of __attribute__((unused)) on variables, but
    let's assume that it works if the unit test passes.

diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
index b79a24a..46a8128 100644
--- a/src/shared/cgroup-util.c
+++ b/src/shared/cgroup-util.c
@@ -1208,7 +1208,6 @@ int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup) {
 }
 
 int cg_path_decode_unit(const char *cgroup, char **unit){
-        _cleanup_free_ char *unescaped = NULL;
         char *p, *e, *c, *s, *k;
 
         assert(cgroup);
diff --git a/src/shared/util.h b/src/shared/util.h
index 68e87da..e3fc876 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -644,7 +644,7 @@ static inline void _reset_errno_(int *saved_errno) {
         errno = *saved_errno;
 }
 
-#define PROTECT_ERRNO _cleanup_(_reset_errno_) int _saved_errno_ = errno
+#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
 
 struct _umask_struct_ {
         mode_t mask;
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 66a10ea..4c3a8a6 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -24,6 +24,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <locale.h>
+#include <errno.h>
 
 #include "util.h"
 
@@ -429,6 +430,15 @@ static void test_get_process_comm(void) {
         log_info("pid1 $PATH: '%s'", strna(i));
 }
 
+static void test_protect_errno(void) {
+        errno = 12;
+        {
+                PROTECT_ERRNO;
+                errno = 11;
+        }
+        assert(errno == 12);
+}
+
 int main(int argc, char *argv[]) {
         test_streq_ptr();
         test_first_word();
@@ -456,6 +466,7 @@ int main(int argc, char *argv[]) {
         test_hostname_is_valid();
         test_u64log2();
         test_get_process_comm();
+        test_protect_errno();
 
         return 0;
 }

commit e0a974b471cced122a91f03b3f635a6dd89e1499
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Thu Apr 25 20:31:49 2013 -0400

    bus: avoid gcc warning about casting a pointer to int of different size

diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index 9e057fb..61013ff 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -66,7 +66,7 @@ static void append_payload_vec(struct kdbus_msg_item **d, const void *p, size_t
 
         (*d)->size = offsetof(struct kdbus_msg_item, vec) + sizeof(struct kdbus_vec);
         (*d)->type = KDBUS_MSG_PAYLOAD_VEC;
-        (*d)->vec.address = (uint64_t) p;
+        (*d)->vec.address = (intptr_t) p;
         (*d)->vec.size = sz;
 
         *d = (struct kdbus_msg_item *) ((uint8_t*) *d + (*d)->size);

commit 406e86fdd59946641737ef9d4df3bfd46c6e23c6
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Thu Apr 25 20:10:57 2013 -0400

    readahead: fix format string issue
    
    (struct stat).st is off_t, which usually is a long, or a long long.
    There's no good format string modifier for it, so use a cast.

diff --git a/src/readahead/readahead-analyze.c b/src/readahead/readahead-analyze.c
index f409b2f..76db3cb 100644
--- a/src/readahead/readahead-analyze.c
+++ b/src/readahead/readahead-analyze.c
@@ -105,7 +105,7 @@ int main_analyze(const char *pack_path) {
                 }
 
                 if (stat(path, &st) == 0) {
-                        size_t size;
+                        off_t size;
 
                         if (sections == 0)
                                 size = st.st_size;
@@ -114,11 +114,11 @@ int main_analyze(const char *pack_path) {
 
                         tsize += size;
 
-                        printf("  %4zd%% (%2d) %12zd: %s\n",
-                                sections && st.st_size ? size * 100 / st.st_size : 100,
-                                sections ? sections : 1,
-                                size,
-                                path);
+                        printf("  %4jd%% (%2d) %12jd: %s\n",
+                               (intmax_t) (sections && st.st_size ? size * 100 / st.st_size : 100),
+                               sections ? sections : 1,
+                               (intmax_t) size,
+                               path);
                 } else {
                         printf("  %4dp (%2d) %12s: %s (MISSING)\n",
                                 sections ? pages : -1,

commit e4d2bfdd73648f4b2bce123b6425d3e63bb99f33
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Thu Apr 25 21:31:05 2013 -0400

    Add printf attributes in exported headers
    
    gcc (and other compilers) sometimes generate spurious warnings, and
    thus users of public headers must be able to disable warnings.
    
    Printf format attributes can be disabled by setting
        #define _sd_printf_attr_
    before including the header file.
    
    Also, add similar logic for sentinel attribute:
        #define _sd_sentinel_attr_
    before including the header file disables the attribute.

diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index 36fab9f..55648e0 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -32,6 +32,14 @@
 extern "C" {
 #endif
 
+#ifndef _sd_printf_attr_
+#  if __GNUC__ >= 4
+#    define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
+#  else
+#    define _sd_printf_attr_(a,b)
+#  endif
+#endif
+
 /* TODO:
  * - add page donation logic
  * - api for appending/reading fixed arrays
@@ -187,7 +195,7 @@ int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machi
 #define SD_BUS_ERROR_MAKE(name, message) ((sd_bus_error) {(name), (message), 0})
 
 void sd_bus_error_free(sd_bus_error *e);
-int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...);
+int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...)  _sd_printf_attr_(3, 0);
 void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message);
 int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e);
 int sd_bus_error_is_set(const sd_bus_error *e);
diff --git a/src/systemd/sd-daemon.h b/src/systemd/sd-daemon.h
index fb7456d..daa3f4c 100644
--- a/src/systemd/sd-daemon.h
+++ b/src/systemd/sd-daemon.h
@@ -68,11 +68,11 @@ extern "C" {
 */
 
 #ifndef _sd_printf_attr_
-#if __GNUC__ >= 4
-#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
-#else
-#define _sd_printf_attr_(a,b)
-#endif
+#  if __GNUC__ >= 4
+#    define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
+#  else
+#    define _sd_printf_attr_(a,b)
+#  endif
 #endif
 
 /*
diff --git a/src/systemd/sd-journal.h b/src/systemd/sd-journal.h
index 51653ba..5375d49 100644
--- a/src/systemd/sd-journal.h
+++ b/src/systemd/sd-journal.h
@@ -34,19 +34,31 @@
 extern "C" {
 #endif
 
+#ifndef _sd_printf_attr_
+#  if __GNUC__ >= 4
+#    define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
+#  else
+#    define _sd_printf_attr_(a,b)
+#  endif
+#endif
+
+#ifndef _sd_sentinel_attr_
+#  define _sd_sentinel_attr_ __attribute__((sentinel))
+#endif
+
 /* Journal APIs. See sd-journal(3) for more information. */
 
 /* Write to daemon */
-int sd_journal_print(int priority, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
-int sd_journal_printv(int priority, const char *format, va_list ap);
-int sd_journal_send(const char *format, ...) __attribute__((sentinel));
+int sd_journal_print(int priority, const char *format, ...) _sd_printf_attr_(2, 3);
+int sd_journal_printv(int priority, const char *format, va_list ap) _sd_printf_attr_(2, 0);
+int sd_journal_send(const char *format, ...) _sd_printf_attr_(1, 0) _sd_sentinel_attr_;
 int sd_journal_sendv(const struct iovec *iov, int n);
 int sd_journal_perror(const char *message);
 
 /* Used by the macros below. Don't call this directly. */
-int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
-int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap);
-int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) __attribute__((sentinel));
+int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) _sd_printf_attr_(5, 6);
+int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) _sd_printf_attr_(5, 0);
+int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) _sd_printf_attr_(4, 0) _sd_sentinel_attr_;
 int sd_journal_sendv_with_location(const char *file, const char *line, const char *func, const struct iovec *iov, int n);
 int sd_journal_perror_with_location(const char *file, const char *line, const char *func, const char *message);
 
diff --git a/src/udev/udev.h b/src/udev/udev.h
index 906dfba..caec5f0 100644
--- a/src/udev/udev.h
+++ b/src/udev/udev.h
@@ -16,13 +16,13 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef _UDEV_H_
-#define _UDEV_H_
+#pragma once
 
 #include <sys/types.h>
 #include <sys/param.h>
 #include <signal.h>
 
+#include "macro.h"
 #include "libudev.h"
 #include "libudev-private.h"
 #include "util.h"
@@ -195,7 +195,7 @@ int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *modalias, bool
 /* udev logging */
 void udev_main_log(struct udev *udev, int priority,
                    const char *file, int line, const char *fn,
-                   const char *format, va_list args);
+                   const char *format, va_list args) _printf_attr_(6, 0);
 
 /* udevadm commands */
 struct udevadm_cmd {
@@ -212,4 +212,3 @@ extern const struct udevadm_cmd udevadm_monitor;
 extern const struct udevadm_cmd udevadm_hwdb;
 extern const struct udevadm_cmd udevadm_test;
 extern const struct udevadm_cmd udevadm_test_builtin;
-#endif

commit b1e2b33c5258f1cefcee55116ac5d049478c804d
Author: Cristian Rodríguez <crrodriguez at opensuse.org>
Date:   Tue Apr 2 04:02:58 2013 -0300

    Add some extra __attribute__ ((format)) s

diff --git a/src/core/manager.h b/src/core/manager.h
index a0be292..b9bd209 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -301,6 +301,6 @@ void manager_undo_generators(Manager *m);
 void manager_recheck_journal(Manager *m);
 
 void manager_set_show_status(Manager *m, bool b);
-void manager_status_printf(Manager *m, bool ephemeral, const char *status, const char *format, ...);
+void manager_status_printf(Manager *m, bool ephemeral, const char *status, const char *format, ...) _printf_attr_(4,5);
 
 void watch_init(Watch *w);
diff --git a/src/core/unit.c b/src/core/unit.c
index b4a4f8c..7029b64 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -2576,9 +2576,12 @@ int unit_coldplug(Unit *u) {
         return 0;
 }
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
 void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) {
         manager_status_printf(u->manager, false, status, unit_status_msg_format, unit_description(u));
 }
+#pragma GCC diagnostic pop
 
 bool unit_need_daemon_reload(Unit *u) {
         _cleanup_strv_free_ char **t = NULL;
diff --git a/src/core/unit.h b/src/core/unit.h
index a086538..0f121e2 100644
--- a/src/core/unit.h
+++ b/src/core/unit.h
@@ -528,7 +528,7 @@ int unit_add_node_link(Unit *u, const char *what, bool wants);
 
 int unit_coldplug(Unit *u);
 
-void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format);
+void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) _printf_attr_(3, 0);
 
 bool unit_need_daemon_reload(Unit *u);
 
diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h
index fb4e8e4..86f7145 100644
--- a/src/journal/journald-server.h
+++ b/src/journal/journald-server.h
@@ -130,7 +130,7 @@ typedef struct Server {
 #define N_IOVEC_UDEV_FIELDS 32
 
 void server_dispatch_message(Server *s, struct iovec *iovec, unsigned n, unsigned m, struct ucred *ucred, struct timeval *tv, const char *label, size_t label_len, const char *unit_id, int priority);
-void server_driver_message(Server *s, sd_id128_t message_id, const char *format, ...);
+void server_driver_message(Server *s, sd_id128_t message_id, const char *format, ...) _printf_attr_(3,4);
 
 /* gperf lookup function */
 const struct ConfigPerfItem* journald_gperf_lookup(const char *key, unsigned length);
diff --git a/src/journal/microhttpd-util.h b/src/journal/microhttpd-util.h
index d4fefa7..20ad769 100644
--- a/src/journal/microhttpd-util.h
+++ b/src/journal/microhttpd-util.h
@@ -23,4 +23,6 @@
 
 #include <stdarg.h>
 
-void microhttpd_logger(void *arg, const char *fmt, va_list ap);
+#include "macro.h"
+
+void microhttpd_logger(void *arg, const char *fmt, va_list ap) _printf_attr_(2, 0);
diff --git a/src/shared/log.h b/src/shared/log.h
index 9aafcb4..5fc8988 100644
--- a/src/shared/log.h
+++ b/src/shared/log.h
@@ -83,7 +83,7 @@ int log_metav(
                 int line,
                 const char *func,
                 const char *format,
-                va_list ap);
+                va_list ap) _printf_attr_(5,0);
 
 int log_meta_object(
                 int level,
@@ -102,14 +102,14 @@ int log_metav_object(
                 const char *object_name,
                 const char *object,
                 const char *format,
-                va_list ap);
+                va_list ap) _printf_attr_(7,0);
 
 int log_struct_internal(
                 int level,
                 const char *file,
                 int line,
                 const char *func,
-                const char *format, ...) _sentinel_;
+                const char *format, ...) _printf_attr_(5,0) _sentinel_;
 
 int log_oom_internal(
                 const char *file,
diff --git a/src/shared/util.h b/src/shared/util.h
index 69b717e..68e87da 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -361,8 +361,8 @@ int pipe_eof(int fd);
 
 cpu_set_t* cpu_set_malloc(unsigned *ncpus);
 
-int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap);
-int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...);
+int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_attr_(4,0);
+int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_attr_(4,5);
 int status_welcome(void);
 
 int fd_columns(int fd);

commit 750ef27274cdc274f8c6b9245d6ba0179c68fa50
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Thu Apr 25 19:59:35 2013 -0400

    Make up for attribute malloc with alloc_size
    
    It is imperative that open source code be well attributed.
    Sprinkle attribute((alloc_size)) here and there, telling gcc
    how much memory we are actually allocating.

diff --git a/src/shared/macro.h b/src/shared/macro.h
index ac61b49..0874102 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -28,6 +28,7 @@
 #include <inttypes.h>
 
 #define _printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
+#define _alloc_(...) __attribute__ ((alloc_size(__VA_ARGS__)))
 #define _sentinel_ __attribute__ ((sentinel))
 #define _noreturn_ __attribute__((noreturn))
 #define _unused_ __attribute__ ((unused))
diff --git a/src/shared/util.h b/src/shared/util.h
index c0398f1..69b717e 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -500,7 +500,7 @@ char *format_bytes(char *buf, size_t l, off_t t);
 
 int fd_wait_for_event(int fd, int event, usec_t timeout);
 
-void* memdup(const void *p, size_t l);
+void* memdup(const void *p, size_t l) _alloc_(2);
 
 int is_kernel_thread(pid_t pid);
 
@@ -560,14 +560,14 @@ static inline void umaskp(mode_t *u) {
 #define _cleanup_umask_ _cleanup_(umaskp)
 #define _cleanup_globfree_ _cleanup_(globfree)
 
-_malloc_  static inline void *malloc_multiply(size_t a, size_t b) {
+_malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
                 return NULL;
 
         return malloc(a * b);
 }
 
- static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
+_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
                 return NULL;
 

commit b231b54780dfa687fc116ed436790c87d5557188
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Thu Apr 25 19:47:43 2013 -0400

    Remove erroneous attribute((malloc)) annotations
    
    According to gcc documentation, returned pointer "cannot alias any
    other pointer valid when the function returns" and "the memory has
    undefined content". This second part is (hopefully) untrue for all
    those functions.

diff --git a/src/shared/strv.h b/src/shared/strv.h
index 92696b0..73ee416 100644
--- a/src/shared/strv.h
+++ b/src/shared/strv.h
@@ -36,7 +36,7 @@ static inline void strv_freep(char ***l) {
 
 #define _cleanup_strv_free_ _cleanup_(strv_freep)
 
-char **strv_copy(char * const *l) _malloc_;
+char **strv_copy(char * const *l);
 unsigned strv_length(char * const *l);
 
 char **strv_merge(char **a, char **b);
@@ -51,8 +51,8 @@ char **strv_uniq(char **l);
 
 #define strv_contains(l, s) (!!strv_find((l), (s)))
 
-char **strv_new(const char *x, ...) _sentinel_ _malloc_;
-char **strv_new_ap(const char *x, va_list ap) _malloc_;
+char **strv_new(const char *x, ...) _sentinel_;
+char **strv_new_ap(const char *x, va_list ap);
 
 static inline const char* STRV_IFNOTNULL(const char *x) {
         return x ? x : (const char *) -1;
@@ -62,11 +62,11 @@ static inline bool strv_isempty(char * const *l) {
         return !l || !*l;
 }
 
-char **strv_split(const char *s, const char *separator) _malloc_;
-char **strv_split_quoted(const char *s) _malloc_;
-char **strv_split_newlines(const char *s) _malloc_;
+char **strv_split(const char *s, const char *separator);
+char **strv_split_quoted(const char *s);
+char **strv_split_newlines(const char *s);
 
-char *strv_join(char **l, const char *separator) _malloc_;
+char *strv_join(char **l, const char *separator);
 
 char **strv_parse_nulstr(const char *s, size_t l);
 char **strv_split_nulstr(const char *s);
diff --git a/src/shared/util.h b/src/shared/util.h
index d76f41e..c0398f1 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -500,7 +500,7 @@ char *format_bytes(char *buf, size_t l, off_t t);
 
 int fd_wait_for_event(int fd, int event, usec_t timeout);
 
-void* memdup(const void *p, size_t l) _malloc_;
+void* memdup(const void *p, size_t l);
 
 int is_kernel_thread(pid_t pid);
 
@@ -567,7 +567,7 @@ _malloc_  static inline void *malloc_multiply(size_t a, size_t b) {
         return malloc(a * b);
 }
 
-_malloc_ static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
+ static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
                 return NULL;
 

commit b5edbff0356c0b161c46992d9e298866e54c448c
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Mon Apr 22 22:07:18 2013 -0400

    systemd-python: attach fields to JournalHandler, add SYSLOG_IDENTIFIER
    
    Arbitrary fields can be attached at the level of the handler,
    and they'll be sent with all messages from this handler.
    
    This facility is used to attach SYSLOG_IDENTIFIER to all messages,
    since otherwise journald attaches SYSLOG_IDENTIFIER=python or
    something similar, which is completely useless.

diff --git a/src/python-systemd/journal.py b/src/python-systemd/journal.py
index 6c740b0..7d42525 100644
--- a/src/python-systemd/journal.py
+++ b/src/python-systemd/journal.py
@@ -27,7 +27,7 @@ import uuid as _uuid
 import traceback as _traceback
 import os as _os
 import logging as _logging
-if _sys.version_info >= (3,):
+if _sys.version_info >= (3,3):
     from collections import ChainMap as _ChainMap
 from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
                     LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
@@ -96,6 +96,11 @@ DEFAULT_CONVERTERS = {
     'COREDUMP_TIMESTAMP': _convert_timestamp,
 }
 
+_IDENT_LETTER = set('ABCDEFGHIJKLMNOPQRTSUVWXYZ_')
+
+def _valid_field_name(s):
+    return not (set(s) - _IDENT_LETTER)
+
 class Reader(_Reader):
     """Reader allows the access and filtering of systemd journal
     entries. Note that in order to access the system journal, a
@@ -450,12 +455,6 @@ class JournalHandler(_logging.Handler):
 
         >>> log.setLevel(logging.DEBUG)
 
-        To attach journal MESSAGE_ID, an extra field is supported:
-
-        >>> import uuid
-        >>> mid = uuid.UUID('0123456789ABCDEF0123456789ABCDEF')
-        >>> log.warn("Message with ID", extra={'MESSAGE_ID': mid})
-
         To redirect all logging messages to journal regardless of where
         they come from, attach it to the root logger:
 
@@ -466,12 +465,36 @@ class JournalHandler(_logging.Handler):
         handler class.  Only standard handler configuration options
         are supported: `level`, `formatter`, `filters`.
 
+        To attach journal MESSAGE_ID, an extra field is supported:
+
+        >>> import uuid
+        >>> mid = uuid.UUID('0123456789ABCDEF0123456789ABCDEF')
+        >>> log.warn("Message with ID", extra={'MESSAGE_ID': mid})
+
+        Fields to be attached to all messages sent through this
+        handler can be specified as keyword arguments. This probably
+        makes sense only for SYSLOG_IDENTIFIER and similar fields
+        which are constant for the whole program:
+
+        >>> journal.JournalHandler(SYSLOG_IDENTIFIER='my-cool-app')
+
         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).
+        `MESSAGE_ID` (optional, see above), `SYSLOG_IDENTIFIER` (defaults
+        to sys.argv[0]).
         """
 
+        def __init__(self, level=_logging.NOTSET, **kwargs):
+                super(JournalHandler, self).__init__(level)
+
+                for name in kwargs:
+                        if not _valid_field_name(name):
+                                raise ValueError('Invalid field name: ' + name)
+                if 'SYSLOG_IDENTIFIER' not in kwargs:
+                        kwargs['SYSLOG_IDENTIFIER'] = _sys.argv[0]
+                self._extra = kwargs
+
         def emit(self, record):
                 """Write record as journal event.
 
@@ -492,7 +515,8 @@ class JournalHandler(_logging.Handler):
                              THREAD_NAME=record.threadName,
                              CODE_FILE=record.pathname,
                              CODE_LINE=record.lineno,
-                             CODE_FUNC=record.funcName)
+                             CODE_FUNC=record.funcName,
+                             **self._extra)
                 except Exception:
                         self.handleError(record)
 



More information about the systemd-commits mailing list