fixed logging
[psensor.git] / src / lib / log.c
index ea2e0ff..2de7037 100644 (file)
 static FILE *file;
 int log_level =  LOG_WARN;
 
-void log_open(const char *path, int lvl)
+void log_open(const char *path)
 {
        file = fopen(path, "a");
 
        if (file)
                log_printf(LOG_INFO, "Start logging");
        else
-               fprintf(stderr, _("Cannot open log file: %s\n"), path);
+               log_printf(LOG_ERR, _("Cannot open log file: %s"), path);
 }
 
 void log_close()
@@ -54,11 +54,12 @@ void log_close()
 void log_printf(int lvl, const char *fmt, ...)
 {
        struct timeval tv;
-       static char buffer[1 + LOG_BUFFER];
+       char buffer[1 + LOG_BUFFER];
        va_list ap;
        char *lvl_str;
+       FILE *stdf;
 
-       if (!file || lvl > log_level)
+       if (lvl > LOG_INFO && (!file || lvl > log_level))
                return ;
 
        va_start(ap, fmt);
@@ -86,7 +87,17 @@ void log_printf(int lvl, const char *fmt, ...)
                lvl_str = "[??]";
        }
 
-       fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
-       fflush(file);
-}
+       if (file && lvl <= log_level) {
+               fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
+               fflush(file);
+       }
+
+       if (lvl <= LOG_INFO) {
+               if (lvl == LOG_WARN || lvl == LOG_ERR)
+                       stdf = stderr;
+               else
+                       stdf = stdout;
 
+               fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
+       }
+}