added -d option to enable debug log (written to $HOME/.psensor/log)
authorJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 3 Oct 2011 07:25:57 +0000 (07:25 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 3 Oct 2011 07:25:57 +0000 (07:25 +0000)
log sensor values in debug mode

NEWS
src/lib/log.c
src/lib/log.h
src/main.c

diff --git a/NEWS b/NEWS
index 0436cba..25a14d2 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+* v0.6.2.12
+** psensor: added -d option to enable debug log (written to $HOME/.psensor/log).
+** psensor: log sensor values in debug mode.
+
 * v0.6.2.11
 ** psensor-server/web interface: use jqplot default css.
 ** psensor-server/web interface: added excanvas for ie < 9 support.
index fd7cf7a..73079f3 100644 (file)
 #include <libintl.h>
 #define _(str) gettext(str)
 
+#include <stdarg.h>
 #include <stdio.h>
+#include <sys/time.h>
 
 #include "log.h"
 
 static FILE *file;
-static int log_level =  LOG_DEBUG;
+int log_level =  LOG_INFO;
 
 void log_open(const char *path, int lvl)
 {
@@ -40,39 +42,56 @@ void log_open(const char *path, int lvl)
 
 void log_puts(int lvl, const char *msg)
 {
+       log_printf(lvl, msg);
+}
+
+void log_close()
+{
+       if (!file)
+               return ;
+
+       fclose(file);
+
+       file = NULL;
+}
+
+#define LOG_BUFFER 4096
+void log_printf(int lvl, const char *fmt, ...)
+{
+       struct timeval tv;
+       static char buffer[1 + LOG_BUFFER];
+       va_list ap;
+       char *lvl_str;
+
        if (!file || lvl > log_level)
                return ;
 
+       va_start(ap, fmt);
+       vsnprintf(buffer, LOG_BUFFER, fmt, ap);
+       buffer[LOG_BUFFER] = '\0';
+       va_end(ap);
+
+       if (gettimeofday(&tv, NULL) != 0)
+               timerclear(&tv);
+
        switch (lvl) {
        case LOG_WARN:
-               fputs("[WARN] ", file);
+               lvl_str = "[WARN]";
                break;
        case LOG_ERR:
-               fputs("[ERR] ", file);
+               lvl_str = "[ERR]";
                break;
        case LOG_DEBUG:
-               fputs("[DEBUG] ", file);
+               lvl_str = "[DEBUG]";
                break;
        case LOG_INFO:
-               fputs("[INFO] ", file);
+               lvl_str = "[INFO]";
                break;
        default:
-               fputs("[??] ", file);
+               lvl_str = "[??]";
        }
 
-       fputs(msg, file);
-       fputc('\n', file);
-
+       fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
        fflush(file);
 }
 
-void log_close()
-{
-       if (!file)
-               return ;
-
-       fclose(file);
-
-       file = NULL;
-}
-
index 20c6229..a8a0245 100644 (file)
@@ -29,8 +29,11 @@ enum log_level {
 
 void log_open(const char *path, int lvl);
 
+void log_printf(int lvl, const char *fmt, ...);
 void log_puts(int lvl, const char *msg);
 
 void log_close();
 
+extern int log_level;
+
 #endif
index d8e7234..6e0ded5 100644 (file)
@@ -131,6 +131,18 @@ update_psensor_values_size(struct psensor **sensors, struct config *cfg)
        }
 }
 
+static void log_measures(struct psensor **sensors)
+{
+       if (log_level == LOG_DEBUG)
+               while (*sensors) {
+                       log_printf(LOG_DEBUG, "%s %.2f",
+                                  (*sensors)->name,
+                                  psensor_get_current_value(*sensors));
+
+                       sensors++;
+               }
+}
+
 void update_psensor_measures(struct ui_psensor *ui)
 {
        struct psensor **sensors = ui->sensors;
@@ -154,6 +166,9 @@ void update_psensor_measures(struct ui_psensor *ui)
 #ifdef HAVE_LIBATIADL
                amd_psensor_list_update(sensors);
 #endif
+
+               log_measures(sensors);
+
                g_mutex_unlock(ui->sensors_mutex);
 
                sleep(cfg->sensor_update_interval);
@@ -309,6 +324,7 @@ static struct option long_options[] = {
        {"version", no_argument, 0, 'v'},
        {"help", no_argument, 0, 'h'},
        {"url", required_argument, 0, 'u'},
+       {"debug", no_argument, 0, 'd'},
        {0, 0, 0, 0}
 };
 
@@ -330,7 +346,7 @@ int main(int argc, char **argv)
        textdomain(PACKAGE);
 #endif
 
-       while ((optc = getopt_long(argc, argv, "vhu:", long_options,
+       while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
                                   NULL)) != -1) {
                switch (optc) {
                case 'u':
@@ -343,6 +359,10 @@ int main(int argc, char **argv)
                case 'v':
                        print_version();
                        exit(EXIT_SUCCESS);
+               case 'd':
+                       printf(_("Enables debug mode.\n"));
+                       log_level = LOG_DEBUG;
+                       break;
                default:
                        cmdok = 0;
                        break;