summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGergely Nagy <algernon@balabit.hu>2012-08-10 15:58:30 +0200
committerGergely Nagy <algernon@balabit.hu>2012-08-10 16:53:06 +0200
commit8f7cfb8658fc3b2714a5b1c15dd350d5466ef947 (patch)
tree79add3202492d506b964f693558b8958aad4cbad
parent8af1d67bd3e8a1766cb9984b3771162cc4c19669 (diff)
downloadlibumberlog-8f7cfb8658fc3b2714a5b1c15dd350d5466ef947.tar.gz
libumberlog-8f7cfb8658fc3b2714a5b1c15dd350d5466ef947.tar.xz
libumberlog-8f7cfb8658fc3b2714a5b1c15dd350d5466ef947.zip
Introduce ul_set_log_flags()
Instead of piggy-backing on ul_openlog(), and accepting new flags at openlog()-time, use a separate function, ul_set_log_flags() to achieve the same. This way, anyone who wants to flip any of the new flags, will have to use ul_set_log_flags(), as ul_openlog() ignores them from now on. This is based on work done by Miloslav Trmač <mitr@redhat.com>, and is both an API and an ABI breakage. Signed-off-by: Gergely Nagy <algernon@balabit.hu>
-rw-r--r--lib/libumberlog.ld5
-rw-r--r--lib/umberlog.c34
-rw-r--r--lib/umberlog.h1
-rw-r--r--lib/umberlog.rst11
-rw-r--r--t/test_umberlog.c142
-rw-r--r--t/test_umberlog_preload.c97
6 files changed, 180 insertions, 110 deletions
diff --git a/lib/libumberlog.ld b/lib/libumberlog.ld
index 225d51c..5b59d9d 100644
--- a/lib/libumberlog.ld
+++ b/lib/libumberlog.ld
@@ -20,3 +20,8 @@ LIBUMBERLOG_0.2.1 {
global:
ul_closelog;
} LIBUMBERLOG_0.1.0;
+
+LIBUMBERLOG_0.2.2 {
+ global:
+ ul_set_log_flags;
+} LIBUMBERLOG_0.2.1;
diff --git a/lib/umberlog.c b/lib/umberlog.c
index 3bb834a..be0b9d6 100644
--- a/lib/umberlog.c
+++ b/lib/umberlog.c
@@ -99,16 +99,10 @@ ul_finish (void)
free (ul_buffer.msg);
}
-void
-ul_openlog (const char *ident, int option, int facility)
+/* Must be called with ul_process_data.lock held. */
+static void
+_ul_reset_caches_locked (void)
{
- old_openlog (ident, option, facility);
-
- pthread_mutex_lock (&ul_process_data.lock);
- ul_process_data.flags = option;
- ul_process_data.facility = facility;
- ul_process_data.ident = ident;
-
/* If either NODISCOVER or NOCACHE is set, don't cache stuff we
won't use. */
if ((ul_process_data.flags & (LOG_UL_NODISCOVER | LOG_UL_NOCACHE)) != 0)
@@ -137,6 +131,28 @@ ul_openlog (const char *ident, int option, int facility)
}
gethostname (ul_process_data.hostname, _POSIX_HOST_NAME_MAX);
+}
+
+void
+ul_set_log_flags (int flags)
+{
+ pthread_mutex_lock (&ul_process_data.lock);
+ ul_process_data.flags = flags;
+ _ul_reset_caches_locked ();
+ pthread_mutex_unlock (&ul_process_data.lock);
+}
+
+void
+ul_openlog (const char *ident, int option, int facility)
+{
+ old_openlog (ident, option, facility);
+
+ pthread_mutex_lock (&ul_process_data.lock);
+ ul_process_data.facility = facility;
+ ul_process_data.ident = ident;
+
+ _ul_reset_caches_locked ();
+
pthread_mutex_unlock (&ul_process_data.lock);
}
diff --git a/lib/umberlog.h b/lib/umberlog.h
index 1bb5a59..eca6bcd 100644
--- a/lib/umberlog.h
+++ b/lib/umberlog.h
@@ -42,6 +42,7 @@ char *ul_vformat (int priority, const char *msg_format, va_list ap)
__attribute__((warn_unused_result));
void ul_openlog (const char *ident, int option, int facility);
+void ul_set_log_flags (int flags);
void ul_closelog (void);
int ul_setlogmask (int mask);
diff --git a/lib/umberlog.rst b/lib/umberlog.rst
index 1c7b909..c3cdcb8 100644
--- a/lib/umberlog.rst
+++ b/lib/umberlog.rst
@@ -19,6 +19,7 @@ SYNOPSIS
#include <umberlog.h>
void ul_openlog (const char *ident, int option, int facility);
+ void ul_set_log_flags (int flags);
void ul_closelog (void);
int ul_syslog (int priority, const char *format, ....);
@@ -35,8 +36,10 @@ DESCRIPTION
**ul_openlog()** is a wrapper around the original **openlog()**
function, which opens a connection to the system logger for a
-program. The updated version adds support for a number of new option
-flags, described below.
+program.
+
+**ul_set_log_flags** is to be used to set any combination of the new
+log flags described below.
**ul_closelog()** is similar to **ul_openlog()** in that it is a
wrapper around the original **closelog()**.
@@ -117,8 +120,8 @@ into the payload:
EXTRA OPTION FLAGS
==================
-The *option* argument to **ul_openlog()** is an OR of any of the
-original **openlog()** flags, and these:
+The *flag* argument to **ul_set_log_flags()** is an OR of any of these
+flags:
LOG_UL_NODISCOVER
Disable all automatic discovery, and only include the *message*,
diff --git a/t/test_umberlog.c b/t/test_umberlog.c
index 21ac50d..07b2d17 100644
--- a/t/test_umberlog.c
+++ b/t/test_umberlog.c
@@ -80,6 +80,144 @@ START_TEST (test_ul_openlog)
}
END_TEST
+START_TEST (test_ul_openlog_flag_ignore)
+{
+ char *msg;
+ struct json_object *jo;
+
+ ul_openlog ("umberlog/test_ul_openlog_flag_ignore", LOG_UL_NODISCOVER,
+ LOG_LOCAL0);
+
+ msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value_exists (jo, "pid");
+ verify_value_exists (jo, "uid");
+ verify_value_exists (jo, "gid");
+ verify_value_exists (jo, "host");
+
+ json_object_put (jo);
+
+ ul_closelog ();
+}
+END_TEST
+
+START_TEST (test_ul_set_log_flags)
+{
+ char *msg;
+ struct json_object *jo;
+
+ ul_openlog ("umberlog/test_ul_set_log_flags", 0, LOG_LOCAL0);
+ ul_set_log_flags (LOG_UL_NODISCOVER);
+
+ msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value_missing (jo, "pid");
+ verify_value_missing (jo, "uid");
+ verify_value_missing (jo, "gid");
+ verify_value_missing (jo, "host");
+
+ json_object_put (jo);
+
+ ul_closelog ();
+}
+END_TEST
+
+START_TEST (test_no_discover)
+{
+ char *msg;
+ struct json_object *jo;
+
+ ul_openlog ("umberlog/test_no_discover", 0, LOG_LOCAL0);
+ ul_set_log_flags (LOG_UL_NODISCOVER);
+
+ msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value (jo, "msg", "hello, I'm test_no_discover!");
+ verify_value_missing (jo, "facility");
+ verify_value_missing (jo, "priority");
+ verify_value_missing (jo, "program");
+ verify_value_missing (jo, "pid");
+ verify_value_missing (jo, "uid");
+ verify_value_missing (jo, "gid");
+ verify_value_missing (jo, "host");
+ verify_value_missing (jo, "timestamp");
+
+ json_object_put (jo);
+
+ ul_closelog ();
+}
+END_TEST
+
+START_TEST (test_no_timestamp)
+{
+ char *msg;
+ struct json_object *jo;
+
+ ul_openlog ("umberlog/test_no_timestamp", 0, LOG_LOCAL0);
+ ul_set_log_flags (LOG_UL_NOTIME);
+
+ msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value (jo, "msg", "hello, I'm test_no_timestamp!");
+ verify_value (jo, "facility", "local0");
+ verify_value (jo, "priority", "debug");
+ verify_value (jo, "program", "umberlog/test_no_timestamp");
+ verify_value_exists (jo, "pid");
+ verify_value_exists (jo, "uid");
+ verify_value_exists (jo, "gid");
+ verify_value_missing (jo, "timestamp");
+ verify_value_exists (jo, "host");
+
+ json_object_put (jo);
+
+ ul_closelog ();
+}
+END_TEST
+
+START_TEST (test_closelog)
+{
+ char *msg;
+ struct json_object *jo;
+
+ ul_openlog ("umberlog/test_closelog", 0, LOG_LOCAL0);
+ ul_set_log_flags (LOG_UL_NODISCOVER);
+ ul_closelog ();
+
+ msg = ul_format (LOG_DEBUG, "%s", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value_missing (jo, "facility");
+
+ json_object_put (jo);
+
+ ul_openlog ("umberlog/test_closelog", 0, LOG_LOCAL0);
+ ul_closelog ();
+
+ msg = ul_format (LOG_DEBUG, "%s", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value_missing (jo, "facility");
+ verify_value_missing (jo, "program");
+
+ verify_value_missing (jo, "pid");
+ verify_value_missing (jo, "uid");
+ verify_value_missing (jo, "gid");
+ verify_value_missing (jo, "host");
+
+ json_object_put (jo);
+}
+END_TEST
+
int
main (void)
{
@@ -93,6 +231,10 @@ main (void)
ft = tcase_create ("Basic tests");
tcase_add_test (ft, test_overrides);
tcase_add_test (ft, test_ul_openlog);
+ tcase_add_test (ft, test_ul_openlog_flag_ignore);
+ tcase_add_test (ft, test_ul_set_log_flags);
+ tcase_add_test (ft, test_closelog);
+ tcase_add_test (ft, test_no_discover);
suite_add_tcase (s, ft);
sr = srunner_create (s);
diff --git a/t/test_umberlog_preload.c b/t/test_umberlog_preload.c
index e931210..09d3210 100644
--- a/t/test_umberlog_preload.c
+++ b/t/test_umberlog_preload.c
@@ -44,33 +44,6 @@ START_TEST (test_simple)
}
END_TEST
-START_TEST (test_no_discover)
-{
- char *msg;
- struct json_object *jo;
-
- openlog ("umberlog/test_no_discover", LOG_UL_NODISCOVER, LOG_LOCAL0);
-
- msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
- jo = parse_msg (msg);
- free (msg);
-
- verify_value (jo, "msg", "hello, I'm test_no_discover!");
- verify_value_missing (jo, "facility");
- verify_value_missing (jo, "priority");
- verify_value_missing (jo, "program");
- verify_value_missing (jo, "pid");
- verify_value_missing (jo, "uid");
- verify_value_missing (jo, "gid");
- verify_value_missing (jo, "host");
- verify_value_missing (jo, "timestamp");
-
- json_object_put (jo);
-
- closelog ();
-}
-END_TEST
-
START_TEST (test_additional_fields)
{
char *msg;
@@ -121,33 +94,6 @@ START_TEST (test_discover_priority)
}
END_TEST
-START_TEST (test_no_timestamp)
-{
- char *msg;
- struct json_object *jo;
-
- openlog ("umberlog/test_no_timestamp", LOG_UL_NOTIME, LOG_LOCAL0);
-
- msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
- jo = parse_msg (msg);
- free (msg);
-
- verify_value (jo, "msg", "hello, I'm test_no_timestamp!");
- verify_value (jo, "facility", "local0");
- verify_value (jo, "priority", "debug");
- verify_value (jo, "program", "umberlog/test_no_timestamp");
- verify_value_exists (jo, "pid");
- verify_value_exists (jo, "uid");
- verify_value_exists (jo, "gid");
- verify_value_missing (jo, "timestamp");
- verify_value_exists (jo, "host");
-
- json_object_put (jo);
-
- closelog ();
-}
-END_TEST
-
START_TEST (test_json_escape)
{
static const char control_chars[] =
@@ -201,46 +147,6 @@ START_TEST (test_facprio)
}
END_TEST
-START_TEST (test_closelog)
-{
- char *msg;
- struct json_object *jo;
-
- openlog ("umberlog/test_closelog", LOG_UL_NODISCOVER, LOG_LOCAL0);
- closelog ();
-
- msg = ul_format (LOG_DEBUG, "%s", __FUNCTION__, NULL);
- jo = parse_msg (msg);
- free (msg);
-
- verify_value_missing (jo, "facility");
-
- json_object_put (jo);
-
- openlog ("umberlog/test_closelog", 0, LOG_LOCAL0);
- closelog ();
-
- msg = ul_format (LOG_DEBUG, "%s", __FUNCTION__, NULL);
- jo = parse_msg (msg);
- free (msg);
-
- verify_value (jo, "facility", "local0");
-#ifdef HAVE_PROGRAM_INVOCATION_SHORT_NAME
- verify_value (jo, "program", "test_umberlog_preload");
-#else
- verify_value_missing (jo, "program");
-#endif
- verify_value_differs (jo, "pid", "0");
- if (getuid () != 0)
- verify_value_differs (jo, "uid", "0");
- if (getgid () != 0)
- verify_value_differs (jo, "gid", "0");
- verify_value_differs (jo, "host", "");
-
- json_object_put (jo);
-}
-END_TEST
-
#ifdef HAVE_PARSE_PRINTF_FORMAT
START_TEST (test_positional_params)
{
@@ -315,10 +221,8 @@ main (void)
ft = tcase_create ("Basic tests");
tcase_add_test (ft, test_openlog_defaults);
tcase_add_test (ft, test_simple);
- tcase_add_test (ft, test_no_discover);
tcase_add_test (ft, test_additional_fields);
tcase_add_test (ft, test_discover_priority);
- tcase_add_test (ft, test_no_timestamp);
#ifdef HAVE_PARSE_PRINTF_FORMAT
tcase_add_test (ft, test_positional_params);
#endif
@@ -327,7 +231,6 @@ main (void)
bt = tcase_create ("Bug tests");
tcase_add_test (bt, test_json_escape);
tcase_add_test (bt, test_facprio);
- tcase_add_test (bt, test_closelog);
suite_add_tcase (s, bt);
sr = srunner_create (s);