logging
authorJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 13 Nov 2011 12:52:08 +0000 (12:52 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 13 Nov 2011 12:52:08 +0000 (12:52 +0000)
po/Makefile.in
src/lib/log.c
src/lib/log.h
src/main.c
src/rsensor.c
src/ui.c
src/ui_appindicator.c
src/ui_notify.c
src/ui_status.c

index 8b758af..f901ffc 100644 (file)
@@ -21,7 +21,7 @@ srcdir = .
 top_srcdir = ..
 
 
-prefix = /usr/local
+prefix = /tmp/r
 exec_prefix = ${prefix}
 datarootdir = ${prefix}/share
 datadir = ${datarootdir}
index 2de7037..d43619d 100644 (file)
@@ -34,9 +34,7 @@ void log_open(const char *path)
 {
        file = fopen(path, "a");
 
-       if (file)
-               log_printf(LOG_INFO, "Start logging");
-       else
+       if (!file)
                log_printf(LOG_ERR, _("Cannot open log file: %s"), path);
 }
 
@@ -50,22 +48,20 @@ void log_close()
        file = NULL;
 }
 
+
 #define LOG_BUFFER 4096
-void log_printf(int lvl, const char *fmt, ...)
+static void vlogf(int lvl, const char *fmt, va_list ap)
 {
        struct timeval tv;
        char buffer[1 + LOG_BUFFER];
-       va_list ap;
        char *lvl_str;
        FILE *stdf;
 
        if (lvl > LOG_INFO && (!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);
@@ -101,3 +97,24 @@ void log_printf(int lvl, const char *fmt, ...)
                fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
        }
 }
+
+void log_printf(int lvl, const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       vlogf(lvl, fmt, ap);
+       va_end(ap);
+}
+
+void log_debug(const char *fmt, ...)
+{
+       va_list ap;
+
+       if (log_level < LOG_DEBUG)
+               return ;
+
+       va_start(ap, fmt);
+       vlogf(LOG_DEBUG, fmt, ap);
+       va_end(ap);
+}
index cf4fcd3..38efb3e 100644 (file)
@@ -30,6 +30,7 @@ enum log_level {
 void log_open(const char *path);
 
 void log_printf(int lvl, const char *fmt, ...);
+void log_debug(const char *fmt, ...);
 
 void log_close();
 
index f5ff0b3..2b2b5ca 100644 (file)
@@ -134,7 +134,7 @@ static void log_measures(struct psensor **sensors)
 {
        if (log_level == LOG_DEBUG)
                while (*sensors) {
-                       log_printf(LOG_DEBUG, "Measure: %s %.2f",
+                       log_debug("Measure: %s %.2f",
                                   (*sensors)->name,
                                   psensor_get_current_value(*sensors));
 
@@ -355,16 +355,14 @@ static gboolean initial_window_show(gpointer data)
 {
        struct ui_psensor *ui;
 
-       log_printf(LOG_DEBUG, "initial_window_show()");
+       log_debug("initial_window_show()");
 
        ui = (struct ui_psensor *)data;
 
-       log_printf(LOG_DEBUG,
-                  "is_status_supported: %d", is_status_supported());
-       log_printf(LOG_DEBUG,
-                  "is_appindicator_supported: %d", is_appindicator_supported());
-       log_printf(LOG_DEBUG,
-                  "hide_on_startup: %d", ui->config->hide_on_startup);
+       log_debug("is_status_supported: %d", is_status_supported());
+       log_debug("is_appindicator_supported: %d",
+                  is_appindicator_supported());
+       log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
 
        if (!ui->config->hide_on_startup
            || (!is_appindicator_supported() && !is_status_supported()))
index 32e4664..e328da9 100644 (file)
@@ -100,7 +100,7 @@ static json_object *get_json_object(const char *url)
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
 
-       log_printf(LOG_DEBUG, "HTTP request %s", url);
+       log_debug("HTTP request %s", url);
        if (curl_easy_perform(curl) == CURLE_OK)
                obj = json_tokener_parse(chunk.data);
        else
index 9a78a86..4c73ccd 100644 (file)
--- a/src/ui.c
+++ b/src/ui.c
@@ -33,7 +33,7 @@ static void save_window_pos(struct ui_psensor *ui)
        struct config *cfg;
 
        visible = gtk_widget_get_visible(ui->main_window);
-       log_printf(LOG_DEBUG, "Window visible: %d", visible);
+       log_debug("Window visible: %d", visible);
 
        if (visible == TRUE) {
                cfg = ui->config;
@@ -41,18 +41,14 @@ static void save_window_pos(struct ui_psensor *ui)
                win = GTK_WINDOW(ui->main_window);
 
                gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
-               log_printf(LOG_DEBUG,
-                          "Window position: %d %d",
-                          cfg->window_x,
-                          cfg->window_y);
+               log_debug("Window position: %d %d",
+                         cfg->window_x,
+                         cfg->window_y);
 
                gtk_window_get_size(win,
                                    &cfg->window_w,
                                    &cfg->window_h);
-               log_printf(LOG_DEBUG,
-                          "Window size: %d %d",
-                          cfg->window_w,
-                          cfg->window_h);
+               log_debug("Window size: %d %d", cfg->window_w, cfg->window_h);
 
                cfg->window_divider_pos
                        = gtk_paned_get_position(GTK_PANED(ui->sensor_box));
@@ -68,8 +64,7 @@ on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
 
        save_window_pos(ui);
 
-       log_printf(LOG_DEBUG,
-                  "is_status_supported: %d\n", is_status_supported());
+       log_debug("is_status_supported: %d\n", is_status_supported());
 
        if (is_appindicator_supported() || is_status_supported())
                gtk_widget_hide(ui->main_window);
@@ -123,7 +118,7 @@ void ui_psensor_quit(struct ui_psensor *ui)
 {
        save_window_pos(ui);
 
-       log_printf(LOG_DEBUG, "Destroy main window");
+       log_debug("Destroy main window");
        gtk_widget_destroy(ui->main_window);
        gtk_main_quit();
 }
@@ -344,6 +339,6 @@ void ui_window_update(struct ui_psensor *ui)
 
 void ui_window_show(struct ui_psensor *ui)
 {
-       log_printf(LOG_DEBUG, "ui_window_show()");
+       log_debug("ui_window_show()");
        gtk_window_present(GTK_WINDOW(ui->main_window));
 }
index 08ef670..720ab49 100644 (file)
@@ -215,7 +215,7 @@ void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention)
 
 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
 {
-       log_printf(LOG_DEBUG, "ui_appindicator#unity_fallback");
+       log_debug("ui_appindicator#unity_fallback");
 
        appindicator_supported = 0;
 
@@ -225,7 +225,7 @@ static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
 static void
 unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
 {
-       log_printf(LOG_DEBUG, "ui_appindicator#unity_unfallback");
+       log_debug("ui_appindicator#unity_unfallback");
 
        appindicator_supported = 1;
 }
index cf0da1a..dcc3264 100644 (file)
@@ -39,9 +39,7 @@ void ui_notify(struct psensor *sensor, struct ui_psensor *ui)
        char *name;
        NotifyNotification *notif;
 
-       log_printf(LOG_DEBUG,
-                  "ui_notify() last_notification %d",
-                  last_notification_tv.tv_sec);
+       log_debug("last_notification %d", last_notification_tv.tv_sec);
 
        if (gettimeofday(&t, NULL) != 0) {
                log_printf(LOG_ERR,  _("gettimeofday failed"));
@@ -73,9 +71,7 @@ void ui_notify(struct psensor *sensor, struct ui_psensor *ui)
                                                NULL,
                                                GTK_WIDGET(ui->main_window));
 #endif
-               log_printf(LOG_DEBUG,
-                          "ui_notify() notif_notification_new %s",
-                          sensor->name);
+               log_debug("notif_notification_new %s", sensor->name);
 
                notify_notification_show(notif, NULL);
 
index 1b27b6e..df79172 100644 (file)
@@ -26,7 +26,7 @@ static unsigned status_attention;
 static void cb_activate(GtkStatusIcon *icon,
                        gpointer data)
 {
-       log_printf(LOG_DEBUG, "cb_activate()");
+       log_debug("cb_activate()");
 
        ui_window_show((struct ui_psensor *)data);
 }
@@ -36,7 +36,7 @@ static void cb_popup_menu(GtkStatusIcon *icon,
                          guint activate_time,
                          gpointer data)
 {
-       log_printf(LOG_DEBUG, "cb_popup_menu()");
+       log_debug("cb_popup_menu()");
 }
 
 void ui_status_init(struct ui_psensor *ui)
@@ -44,7 +44,7 @@ void ui_status_init(struct ui_psensor *ui)
        if (status)
                return ;
 
-       log_printf(LOG_DEBUG, "ui_status_create()");
+       log_debug("ui_status_create()");
 
        status = gtk_status_icon_new();
        gtk_status_icon_set_from_icon_name(status, "psensor_normal");
@@ -68,14 +68,14 @@ int is_status_supported()
 
 void ui_status_cleanup()
 {
-       log_printf(LOG_DEBUG, "ui_status_cleanup()");
+       log_debug("ui_status_cleanup()");
 
        g_object_unref(G_OBJECT(status));
 }
 
 void ui_status_update(struct ui_psensor *ui, unsigned int attention)
 {
-       log_printf(LOG_DEBUG, "ui_status_update()");
+       log_debug("ui_status_update()");
 
        if (status_attention && !attention)
                gtk_status_icon_set_from_icon_name(status, "psensor_normal");