diff options
author | Gergely Nagy <algernon@balabit.hu> | 2012-03-19 17:41:10 +0100 |
---|---|---|
committer | Gergely Nagy <algernon@balabit.hu> | 2012-03-19 17:41:10 +0100 |
commit | d665a62248df7e14f3787138e7089caaad648ded (patch) | |
tree | 8c819c02bc382b89b92855a60753173e9d3c2fb1 /lib | |
parent | dac475d2a5f568a7ac648d1c8bdbb75510586297 (diff) | |
download | libumberlog-d665a62248df7e14f3787138e7089caaad648ded.tar.gz libumberlog-d665a62248df7e14f3787138e7089caaad648ded.tar.xz libumberlog-d665a62248df7e14f3787138e7089caaad648ded.zip |
Lift out the formatting into a separate function.
The formatting itself is now lifted out to _cee_vformat(), which
returns a const char *, and puts the JSON object into its first
(output) parameter.
We then export cee_format() and cee_vformat(), that use this, strdup
and return the result, and free the JSON object too.
cee_vsyslog() was changed to use _cee_vformat(), because it does not
need to strdup() the result.
Signed-off-by: Gergely Nagy <algernon@balabit.hu>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cee-syslog.c | 60 | ||||
-rw-r--r-- | lib/cee-syslog.h | 3 |
2 files changed, 50 insertions, 13 deletions
diff --git a/lib/cee-syslog.c b/lib/cee-syslog.c index 7e9cdec..a459ff8 100644 --- a/lib/cee-syslog.c +++ b/lib/cee-syslog.c @@ -36,6 +36,7 @@ #include <stdio.h> #include <dlfcn.h> #include <stdlib.h> +#include <string.h> static void (*old_syslog) (); @@ -47,18 +48,8 @@ cee_init (void) old_syslog = dlsym (RTLD_NEXT, "syslog"); } -void -cee_syslog (int priority, const char *msg_format, ...) -{ - va_list ap; - - va_start (ap, msg_format); - vsyslog (priority, msg_format, ap); - va_end (ap); -} - -void -cee_vsyslog (int priority, const char *msg_format, va_list ap) +static const char * +_cee_vformat (struct json_object **json, const char *msg_format, va_list ap) { struct json_object *jo; char *key, *fmt; @@ -80,7 +71,50 @@ cee_vsyslog (int priority, const char *msg_format, va_list ap) free (value); } - old_syslog (priority, "@cee:%s", json_object_to_json_string (jo)); + *json = jo; + return json_object_to_json_string (jo); +} + +char * +cee_format (const char *msg_format, ...) +{ + char *result; + va_list ap; + + va_start (ap, msg_format); + result = cee_vformat (msg_format, ap); + va_end (ap); + + return result; +} + +char * +cee_vformat (const char *msg_format, va_list ap) +{ + struct json_object *jo; + char *result; + + result = strdup (_cee_vformat (&jo, msg_format, ap)); + json_object_put (jo); + return result; +} + +void +cee_syslog (int priority, const char *msg_format, ...) +{ + va_list ap; + + va_start (ap, msg_format); + vsyslog (priority, msg_format, ap); + va_end (ap); +} + +void +cee_vsyslog (int priority, const char *msg_format, va_list ap) +{ + struct json_object *jo; + + old_syslog (priority, "@cee:%s", _cee_vformat (&jo, msg_format, ap)); json_object_put (jo); } diff --git a/lib/cee-syslog.h b/lib/cee-syslog.h index 4c35cea..07756d5 100644 --- a/lib/cee-syslog.h +++ b/lib/cee-syslog.h @@ -31,6 +31,9 @@ #include <syslog.h> #include <stdarg.h> +char *cee_format (const char *msg_format, ...); +char *cee_vformat (const char *msg_format, va_list ap); + void cee_syslog (int priority, const char *msg_format, ...); void cee_vsyslog (int priority, const char *msg_format, va_list ap); |