]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/log.c
don't allow --start in system mode
[pulseaudio] / src / pulsecore / log.c
index 8bc673e598f4da5bf34a87d7b033d6b939cb2643..5eda4f655d437beb88b89ddb717027f23747bccf 100644 (file)
@@ -1,18 +1,19 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
+
+  Copyright 2004-2006 Lennart Poettering
+  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
   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
   License, or (at your option) any later version.
+
   PulseAudio is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
+
   You should have received a copy of the GNU Lesser General Public
   License along with PulseAudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 #include <config.h>
 #endif
 
-#include <assert.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
+#include <errno.h>
 
 #ifdef HAVE_SYSLOG_H
 #include <syslog.h>
@@ -37,6 +38,7 @@
 #include <pulse/xmalloc.h>
 #include <pulse/util.h>
 
+#include <pulsecore/macro.h>
 #include <pulsecore/core-util.h>
 
 #include "log.h"
@@ -59,25 +61,39 @@ static const int level_to_syslog[] = {
 };
 #endif
 
+static const char level_to_char[] = {
+    [PA_LOG_ERROR] = 'E',
+    [PA_LOG_WARN] = 'W',
+    [PA_LOG_NOTICE] = 'N',
+    [PA_LOG_INFO] = 'I',
+    [PA_LOG_DEBUG] = 'D'
+};
+
 void pa_log_set_ident(const char *p) {
-    if (log_ident)
-        pa_xfree(log_ident);
-    if (log_ident_local)
-        pa_xfree(log_ident_local);
+    pa_xfree(log_ident);
+    pa_xfree(log_ident_local);
 
     log_ident = pa_xstrdup(p);
-    log_ident_local = pa_utf8_to_locale(log_ident);
-    if (!log_ident_local)
+    if (!(log_ident_local = pa_utf8_to_locale(log_ident)))
         log_ident_local = pa_xstrdup(log_ident);
 }
 
+/* To make valgrind shut up. */
+static void ident_destructor(void) PA_GCC_DESTRUCTOR;
+static void ident_destructor(void) {
+    pa_xfree(log_ident);
+    pa_xfree(log_ident_local);
+}
+
 void pa_log_set_maximal_level(pa_log_level_t l) {
-    assert(l < PA_LOG_LEVEL_MAX);
+    pa_assert(l < PA_LOG_LEVEL_MAX);
+
     maximal_level = l;
 }
 
 void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t l, const char*s)) {
-    assert(t == PA_LOG_USER || !func);
+    pa_assert(t == PA_LOG_USER || !func);
+
     log_target = t;
     user_log_func = func;
 }
@@ -89,25 +105,34 @@ void pa_log_levelv_meta(
         const char *func,
         const char *format,
         va_list ap) {
-    
+
     const char *e;
-    char *text, *t, *n, *location = pa_xstrdup("");
-    
-    assert(level < PA_LOG_LEVEL_MAX);
-    assert(format);
+    char *t, *n;
+    int saved_errno = errno;
+
+    /* We don't use dynamic memory allocation here to minimize the hit
+     * in RT threads */
+    char text[1024], location[128];
+
+    pa_assert(level < PA_LOG_LEVEL_MAX);
+    pa_assert(format);
 
     if ((e = getenv(ENV_LOGLEVEL)))
         maximal_level = atoi(e);
-    
-    if (level > maximal_level)
+
+    if (level > maximal_level) {
+        errno = saved_errno;
         return;
+    }
 
-    text = pa_vsprintf_malloc(format, ap);
+    pa_vsnprintf(text, sizeof(text), format, ap);
 
     if (getenv(ENV_LOGMETA) && file && line > 0 && func)
-        location = pa_sprintf_malloc("[%s:%i %s()] ", file, line, func);
+        pa_snprintf(location, sizeof(location), "[%s:%i %s()] ", file, line, func);
     else if (file)
-        location = pa_sprintf_malloc("%s: ", pa_path_get_filename(file));
+        pa_snprintf(location, sizeof(location), "%s: ", pa_path_get_filename(file));
+    else
+        location[0] = 0;
 
     if (!pa_utf8_valid(text))
         pa_log_level(level, __FILE__": invalid UTF-8 string following below:");
@@ -120,13 +145,13 @@ void pa_log_levelv_meta(
 
         if (!*t)
             continue;
-    
+
         switch (log_target) {
             case PA_LOG_STDERR: {
                 const char *prefix = "", *suffix = "";
                 char *local_t;
 
-#ifndef OS_IS_WIN32                
+#ifndef OS_IS_WIN32
                 /* Yes indeed. Useless, but fun! */
                 if (isatty(STDERR_FILENO)) {
                     if (level <= PA_LOG_ERROR) {
@@ -139,18 +164,20 @@ void pa_log_levelv_meta(
                 }
 #endif
 
+                /* We shouldn't be using dynamic allocation here to
+                 * minimize the hit in RT threads */
                 local_t = pa_utf8_to_locale(t);
                 if (!local_t)
-                    fprintf(stderr, "%s%s%s%s\n", location, prefix, t, suffix);
+                    fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, t, suffix);
                 else {
-                    fprintf(stderr, "%s%s%s%s\n", location, prefix, local_t, suffix);
+                    fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, local_t, suffix);
                     pa_xfree(local_t);
                 }
 
                 break;
             }
-                
-#ifdef HAVE_SYSLOG_H            
+
+#ifdef HAVE_SYSLOG_H
             case PA_LOG_SYSLOG: {
                 char *local_t;
 
@@ -165,28 +192,26 @@ void pa_log_levelv_meta(
                 }
 
                 closelog();
-                break;            
+                break;
             }
 #endif
-                
+
             case PA_LOG_USER: {
-                char *x;
+                char x[1024];
 
-                x = pa_sprintf_malloc("%s%s", location, t);
+                pa_snprintf(x, sizeof(x), "%s%s", location, t);
                 user_log_func(level, x);
-                pa_xfree(x);
-                
+
                 break;
             }
-                
+
             case PA_LOG_NULL:
             default:
                 break;
         }
     }
 
-    pa_xfree(text);
-    pa_xfree(location);
+    errno = saved_errno;
 }
 
 void pa_log_level_meta(
@@ -195,7 +220,7 @@ void pa_log_level_meta(
         int line,
         const char *func,
         const char *format, ...) {
-    
+
     va_list ap;
     va_start(ap, format);
     pa_log_levelv_meta(level, file, line, func, format, ap);
@@ -208,6 +233,7 @@ void pa_log_levelv(pa_log_level_t level, const char *format, va_list ap) {
 
 void pa_log_level(pa_log_level_t level, const char *format, ...) {
     va_list ap;
+
     va_start(ap, format);
     pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
     va_end(ap);