]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/log.c
Remove unnecessary #includes
[pulseaudio] / src / pulsecore / log.c
index 9931586d507abdf87aa2bc4bbd2e2d4ae9d675f0..13038148caff7db2dcc4eaa9ec5c44b7cfc7fcd9 100644 (file)
@@ -6,7 +6,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
-  published by the Free Software Foundation; either version 2 of the
+  published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
@@ -38,6 +38,8 @@
 #include <syslog.h>
 #endif
 
+#include <pulse/gccmacro.h>
+#include <pulse/rtclock.h>
 #include <pulse/utf8.h>
 #include <pulse/xmalloc.h>
 #include <pulse/util.h>
@@ -45,7 +47,6 @@
 
 #include <pulsecore/macro.h>
 #include <pulsecore/core-util.h>
-#include <pulsecore/rtclock.h>
 #include <pulsecore/once.h>
 #include <pulsecore/ratelimit.h>
 
 #define ENV_LOG_PRINT_META "PULSE_LOG_META"
 #define ENV_LOG_PRINT_LEVEL "PULSE_LOG_LEVEL"
 #define ENV_LOG_BACKTRACE "PULSE_LOG_BACKTRACE"
+#define ENV_LOG_BACKTRACE_SKIP "PULSE_LOG_BACKTRACE_SKIP"
+#define ENV_LOG_NO_RATELIMIT "PULSE_LOG_NO_RATE_LIMIT"
 
 static char *ident = NULL; /* in local charset format */
 static pa_log_target_t target = PA_LOG_STDERR, target_override;
 static pa_bool_t target_override_set = FALSE;
 static pa_log_level_t maximum_level = PA_LOG_ERROR, maximum_level_override = PA_LOG_ERROR;
-static unsigned show_backtrace = 0, show_backtrace_override = 0;
+static unsigned show_backtrace = 0, show_backtrace_override = 0, skip_backtrace = 0;
 static pa_log_flags_t flags = 0, flags_override = 0;
+static pa_bool_t no_rate_limit = FALSE;
+static int log_fd = -1;
 
 #ifdef HAVE_SYSLOG_H
 static const int level_to_syslog[] = {
@@ -124,17 +129,30 @@ void pa_log_set_flags(pa_log_flags_t _flags, pa_log_merge_t merge) {
         flags = _flags;
 }
 
+void pa_log_set_fd(int fd) {
+    if (fd >= 0)
+        log_fd = fd;
+    else if (log_fd >= 0) {
+        pa_close(log_fd);
+        log_fd = -1;
+    }
+}
+
 void pa_log_set_show_backtrace(unsigned nlevels) {
     show_backtrace = nlevels;
 }
 
+void pa_log_set_skip_backtrace(unsigned nlevels) {
+    skip_backtrace = nlevels;
+}
+
 #ifdef HAVE_EXECINFO_H
 
 static char* get_backtrace(unsigned show_nframes) {
     void* trace[32];
     int n_frames;
     char **symbols, *e, *r;
-    unsigned j, n;
+    unsigned j, n, s;
     size_t a;
 
     pa_assert(show_nframes > 0);
@@ -149,14 +167,15 @@ static char* get_backtrace(unsigned show_nframes) {
     if (!symbols)
         return NULL;
 
-    n = PA_MIN((unsigned) n_frames, show_nframes);
+    s = skip_backtrace;
+    n = PA_MIN((unsigned) n_frames, s + show_nframes);
 
     a = 4;
 
-    for (j = 0; j < n; j++) {
-        if (j > 0)
+    for (j = s; j < n; j++) {
+        if (j > s)
             a += 2;
-        a += strlen(symbols[j]);
+        a += strlen(pa_path_get_filename(symbols[j]));
     }
 
     r = pa_xnew(char, a);
@@ -164,14 +183,18 @@ static char* get_backtrace(unsigned show_nframes) {
     strcpy(r, " (");
     e = r + 2;
 
-    for (j = 0; j < n; j++) {
-        if (j > 0) {
+    for (j = s; j < n; j++) {
+        const char *sym;
+
+        if (j > s) {
             strcpy(e, "<<");
             e += 2;
         }
 
-        strcpy(e, symbols[j]);
-        e += strlen(symbols[j]);
+        sym = pa_path_get_filename(symbols[j]);
+
+        strcpy(e, sym);
+        e += strlen(sym);
     }
 
     strcpy(e, ")");
@@ -184,47 +207,61 @@ static char* get_backtrace(unsigned show_nframes) {
 #endif
 
 static void init_defaults(void) {
-    const char *e;
+    PA_ONCE_BEGIN {
 
-    if (!ident) {
-        char binary[256];
-        if (pa_get_binary_name(binary, sizeof(binary)))
-            pa_log_set_ident(binary);
-    }
+        const char *e;
 
-    if (getenv(ENV_LOG_SYSLOG)) {
-        target_override = PA_LOG_SYSLOG;
-        target_override_set = TRUE;
-    }
+        if (!ident) {
+            char binary[256];
+            if (pa_get_binary_name(binary, sizeof(binary)))
+                pa_log_set_ident(binary);
+        }
 
-    if ((e = getenv(ENV_LOG_LEVEL))) {
-        maximum_level_override = (pa_log_level_t) atoi(e);
+        if (getenv(ENV_LOG_SYSLOG)) {
+            target_override = PA_LOG_SYSLOG;
+            target_override_set = TRUE;
+        }
 
-        if (maximum_level_override >= PA_LOG_LEVEL_MAX)
-            maximum_level_override = PA_LOG_LEVEL_MAX-1;
-    }
+        if ((e = getenv(ENV_LOG_LEVEL))) {
+            maximum_level_override = (pa_log_level_t) atoi(e);
 
-    if (getenv(ENV_LOG_COLORS))
-        flags_override |= PA_LOG_COLORS;
+            if (maximum_level_override >= PA_LOG_LEVEL_MAX)
+                maximum_level_override = PA_LOG_LEVEL_MAX-1;
+        }
 
-    if (getenv(ENV_LOG_PRINT_TIME))
-        flags_override |= PA_LOG_PRINT_TIME;
+        if (getenv(ENV_LOG_COLORS))
+            flags_override |= PA_LOG_COLORS;
 
-    if (getenv(ENV_LOG_PRINT_FILE))
-        flags_override |= PA_LOG_PRINT_FILE;
+        if (getenv(ENV_LOG_PRINT_TIME))
+            flags_override |= PA_LOG_PRINT_TIME;
 
-    if (getenv(ENV_LOG_PRINT_META))
-        flags_override |= PA_LOG_PRINT_META;
+        if (getenv(ENV_LOG_PRINT_FILE))
+            flags_override |= PA_LOG_PRINT_FILE;
 
-    if (getenv(ENV_LOG_PRINT_LEVEL))
-        flags_override |= PA_LOG_PRINT_LEVEL;
+        if (getenv(ENV_LOG_PRINT_META))
+            flags_override |= PA_LOG_PRINT_META;
 
-    if ((e = getenv(ENV_LOG_BACKTRACE))) {
-        show_backtrace_override = (unsigned) atoi(e);
+        if (getenv(ENV_LOG_PRINT_LEVEL))
+            flags_override |= PA_LOG_PRINT_LEVEL;
 
-        if (show_backtrace_override <= 0)
-            show_backtrace_override = 0;
-    }
+        if ((e = getenv(ENV_LOG_BACKTRACE))) {
+            show_backtrace_override = (unsigned) atoi(e);
+
+            if (show_backtrace_override <= 0)
+                show_backtrace_override = 0;
+        }
+
+        if ((e = getenv(ENV_LOG_BACKTRACE_SKIP))) {
+            skip_backtrace = (unsigned) atoi(e);
+
+            if (skip_backtrace <= 0)
+                skip_backtrace = 0;
+        }
+
+        if (getenv(ENV_LOG_NO_RATELIMIT))
+            no_rate_limit = TRUE;
+
+    } PA_ONCE_END;
 }
 
 void pa_log_levelv_meta(
@@ -245,14 +282,12 @@ void pa_log_levelv_meta(
 
     /* We don't use dynamic memory allocation here to minimize the hit
      * in RT threads */
-    char text[4096], location[128], timestamp[32];
+    char text[16*1024], location[128], timestamp[32];
 
     pa_assert(level < PA_LOG_LEVEL_MAX);
     pa_assert(format);
 
-    PA_ONCE_BEGIN {
-        init_defaults();
-    } PA_ONCE_END;
+    init_defaults();
 
     _target = target_override_set ? target_override : target;
     _maximum_level = PA_MAX(maximum_level, maximum_level_override);
@@ -268,7 +303,7 @@ void pa_log_levelv_meta(
 
     if ((_flags & PA_LOG_PRINT_META) && file && line > 0 && func)
         pa_snprintf(location, sizeof(location), "[%s:%i %s()] ", file, line, func);
-    else if (_flags & (PA_LOG_PRINT_META|PA_LOG_PRINT_FILE))
+    else if ((_flags & (PA_LOG_PRINT_META|PA_LOG_PRINT_FILE)) && file)
         pa_snprintf(location, sizeof(location), "%s: ", pa_path_get_filename(file));
     else
         location[0] = 0;
@@ -277,7 +312,7 @@ void pa_log_levelv_meta(
         static pa_usec_t start, last;
         pa_usec_t u, a, r;
 
-        u = pa_rtclock_usec();
+        u = pa_rtclock_now();
 
         PA_ONCE_BEGIN {
             start = u;
@@ -306,7 +341,7 @@ void pa_log_levelv_meta(
 #endif
 
     if (!pa_utf8_valid(text))
-        pa_log_level(level, __FILE__": invalid UTF-8 string following below:");
+        pa_logl(level, "Invalid UTF-8 string following below:");
 
     for (t = text; t; t = n) {
         if ((n = strchr(t, '\n'))) {
@@ -349,6 +384,9 @@ void pa_log_levelv_meta(
                     fprintf(stderr, "%s%c: %s%s%s%s%s%s\n", timestamp, level_to_char[level], location, prefix, t, grey, pa_strempty(bt), suffix);
                 else
                     fprintf(stderr, "%s%s%s%s%s%s%s\n", timestamp, location, prefix, t, grey, pa_strempty(bt), suffix);
+#ifdef OS_IS_WIN32
+                fflush(stderr);
+#endif
 
                 pa_xfree(local_t);
 
@@ -371,6 +409,23 @@ void pa_log_levelv_meta(
             }
 #endif
 
+            case PA_LOG_FD: {
+                if (log_fd >= 0) {
+                    char metadata[256];
+
+                    pa_snprintf(metadata, sizeof(metadata), "\n%c %s %s", level_to_char[level], timestamp, location);
+
+                    if ((write(log_fd, metadata, strlen(metadata)) < 0) || (write(log_fd, t, strlen(t)) < 0)) {
+                        saved_errno = errno;
+                        pa_log_set_fd(-1);
+                        fprintf(stderr, "%s\n", "Error writing logs to a file descriptor. Redirect log messages to console.");
+                        fprintf(stderr, "%s %s\n", metadata, t);
+                        pa_log_set_target(PA_LOG_STDERR);
+                    }
+                }
+
+                break;
+            }
             case PA_LOG_NULL:
             default:
                 break;
@@ -406,9 +461,14 @@ void pa_log_level(pa_log_level_t level, const char *format, ...) {
     va_end(ap);
 }
 
-pa_bool_t pa_log_ratelimit(void) {
+pa_bool_t pa_log_ratelimit(pa_log_level_t level) {
     /* Not more than 10 messages every 5s */
     static PA_DEFINE_RATELIMIT(ratelimit, 5 * PA_USEC_PER_SEC, 10);
 
-    return pa_ratelimit_test(&ratelimit);
+    init_defaults();
+
+    if (no_rate_limit)
+        return TRUE;
+
+    return pa_ratelimit_test(&ratelimit, level);
 }