From 05c414f8dd4ef338e7360c0bb0e8973f20da277e Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Sun, 13 Nov 2011 12:52:08 +0000 Subject: [PATCH] logging --- po/Makefile.in | 2 +- src/lib/log.c | 31 ++++++++++++++++++++++++------- src/lib/log.h | 1 + src/main.c | 14 ++++++-------- src/rsensor.c | 2 +- src/ui.c | 21 ++++++++------------- src/ui_appindicator.c | 4 ++-- src/ui_notify.c | 8 ++------ src/ui_status.c | 10 +++++----- 9 files changed, 50 insertions(+), 43 deletions(-) diff --git a/po/Makefile.in b/po/Makefile.in index 8b758af..f901ffc 100644 --- a/po/Makefile.in +++ b/po/Makefile.in @@ -21,7 +21,7 @@ srcdir = . top_srcdir = .. -prefix = /usr/local +prefix = /tmp/r exec_prefix = ${prefix} datarootdir = ${prefix}/share datadir = ${datarootdir} diff --git a/src/lib/log.c b/src/lib/log.c index 2de7037..d43619d 100644 --- a/src/lib/log.c +++ b/src/lib/log.c @@ -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); +} diff --git a/src/lib/log.h b/src/lib/log.h index cf4fcd3..38efb3e 100644 --- a/src/lib/log.h +++ b/src/lib/log.h @@ -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(); diff --git a/src/main.c b/src/main.c index f5ff0b3..2b2b5ca 100644 --- a/src/main.c +++ b/src/main.c @@ -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())) diff --git a/src/rsensor.c b/src/rsensor.c index 32e4664..e328da9 100644 --- a/src/rsensor.c +++ b/src/rsensor.c @@ -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 diff --git a/src/ui.c b/src/ui.c index 9a78a86..4c73ccd 100644 --- 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)); } diff --git a/src/ui_appindicator.c b/src/ui_appindicator.c index 08ef670..720ab49 100644 --- a/src/ui_appindicator.c +++ b/src/ui_appindicator.c @@ -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; } diff --git a/src/ui_notify.c b/src/ui_notify.c index cf0da1a..dcc3264 100644 --- a/src/ui_notify.c +++ b/src/ui_notify.c @@ -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); diff --git a/src/ui_status.c b/src/ui_status.c index 1b27b6e..df79172 100644 --- a/src/ui_status.c +++ b/src/ui_status.c @@ -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"); -- 2.7.4