diff options
author | Lennart Poettering <lennart@poettering.net> | 2008-08-29 19:33:06 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2008-08-29 19:33:06 +0200 |
commit | bdcb3a493cc5bdba254d365b7e567876a72c789b (patch) | |
tree | 16d0c238903d606cb64df9325e955f302a426337 /src/pulsecore | |
parent | 54da71ef5de500016c549e20a26131365668b74c (diff) |
optionally add timestampts to every line logged
Diffstat (limited to 'src/pulsecore')
-rw-r--r-- | src/pulsecore/log.c | 37 | ||||
-rw-r--r-- | src/pulsecore/log.h | 4 |
2 files changed, 32 insertions, 9 deletions
diff --git a/src/pulsecore/log.c b/src/pulsecore/log.c index 5eda4f65..9507a854 100644 --- a/src/pulsecore/log.c +++ b/src/pulsecore/log.c @@ -37,18 +37,22 @@ #include <pulse/utf8.h> #include <pulse/xmalloc.h> #include <pulse/util.h> +#include <pulse/timeval.h> #include <pulsecore/macro.h> #include <pulsecore/core-util.h> +#include <pulsecore/rtclock.h> +#include <pulsecore/once.h> #include "log.h" #define ENV_LOGLEVEL "PULSE_LOG" #define ENV_LOGMETA "PULSE_LOG_META" +#define ENV_LOGTIME "PULSE_LOG_TIME" static char *log_ident = NULL, *log_ident_local = NULL; static pa_log_target_t log_target = PA_LOG_STDERR; -static void (*user_log_func)(pa_log_level_t l, const char *s) = NULL; +static pa_log_func_t user_log_func = NULL; static pa_log_level_t maximal_level = PA_LOG_NOTICE; #ifdef HAVE_SYSLOG_H @@ -91,7 +95,7 @@ void pa_log_set_maximal_level(pa_log_level_t l) { maximal_level = l; } -void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t l, const char*s)) { +void pa_log_set_target(pa_log_target_t t, pa_log_func_t func) { pa_assert(t == PA_LOG_USER || !func); log_target = t; @@ -112,7 +116,7 @@ void pa_log_levelv_meta( /* We don't use dynamic memory allocation here to minimize the hit * in RT threads */ - char text[1024], location[128]; + char text[1024], location[128], timestamp[32]; pa_assert(level < PA_LOG_LEVEL_MAX); pa_assert(format); @@ -134,6 +138,23 @@ void pa_log_levelv_meta( else location[0] = 0; + if (getenv(ENV_LOGTIME)) { + static pa_usec_t start; + pa_usec_t u; + + u = pa_rtclock_usec(); + + PA_ONCE_BEGIN { + start = u; + } PA_ONCE_END; + + u -= start; + + pa_snprintf(timestamp, sizeof(timestamp), "(%4llu.%03llu) ", (unsigned long long) (u / PA_USEC_PER_SEC), (unsigned long long) (((u / PA_USEC_PER_MSEC)) % 1000)); + + } else + timestamp[0] = 0; + if (!pa_utf8_valid(text)) pa_log_level(level, __FILE__": invalid UTF-8 string following below:"); @@ -168,9 +189,9 @@ void pa_log_levelv_meta( * minimize the hit in RT threads */ local_t = pa_utf8_to_locale(t); if (!local_t) - fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, t, suffix); + fprintf(stderr, "%s%c: %s%s%s%s\n", timestamp, level_to_char[level], location, prefix, t, suffix); else { - fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, local_t, suffix); + fprintf(stderr, "%s%c: %s%s%s%s\n", timestamp, level_to_char[level], location, prefix, local_t, suffix); pa_xfree(local_t); } @@ -185,9 +206,9 @@ void pa_log_levelv_meta( local_t = pa_utf8_to_locale(t); if (!local_t) - syslog(level_to_syslog[level], "%s%s", location, t); + syslog(level_to_syslog[level], "%s%s%s", timestamp, location, t); else { - syslog(level_to_syslog[level], "%s%s", location, local_t); + syslog(level_to_syslog[level], "%s%s%s", timestamp, location, local_t); pa_xfree(local_t); } @@ -199,7 +220,7 @@ void pa_log_levelv_meta( case PA_LOG_USER: { char x[1024]; - pa_snprintf(x, sizeof(x), "%s%s", location, t); + pa_snprintf(x, sizeof(x), "%s%s%s", timestamp, location, t); user_log_func(level, x); break; diff --git a/src/pulsecore/log.h b/src/pulsecore/log.h index 2047696e..633227f3 100644 --- a/src/pulsecore/log.h +++ b/src/pulsecore/log.h @@ -49,8 +49,10 @@ typedef enum pa_log_level { /* Set an identification for the current daemon. Used when logging to syslog. */ void pa_log_set_ident(const char *p); +typedef void (*pa_log_func_t)(pa_log_level_t t, const char*s); + /* Set another log target. If t is PA_LOG_USER you may specify a function that is called every log string */ -void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t t, const char*s)); +void pa_log_set_target(pa_log_target_t t, pa_log_func_t func); /* Minimal log level */ void pa_log_set_maximal_level(pa_log_level_t l); |