From 2bbac1c811ba017512349ff8170aa877b853b2a4 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 26 Aug 2012 00:54:18 +0100 Subject: [PATCH] Refactored log Added shortcut functions for prof logging levels --- src/command.c | 8 ++--- src/jabber.c | 18 +++++----- src/log.c | 84 ++++++++++++++++++++++++++++++++++------------- src/log.h | 14 ++++---- src/preferences.c | 4 +-- src/profanity.c | 12 +++---- src/windows.c | 6 ++-- 7 files changed, 92 insertions(+), 54 deletions(-) diff --git a/src/command.c b/src/command.c index 0b1e10cf..0b021853 100644 --- a/src/command.c +++ b/src/command.c @@ -337,7 +337,7 @@ static PAutocomplete commands_ac; void cmd_init(void) { - log_msg(PROF_LEVEL_INFO, "prof", "Initialising commands"); + log_info("Initialising commands"); commands_ac = p_autocomplete_new(); unsigned int i; @@ -471,16 +471,16 @@ _cmd_connect(const char * const inp, struct cmd_help_t help) inp_get_password(passwd); inp_non_block(); - log_msg(PROF_LEVEL_DEBUG, PROF, "Connecting as %s", lower); + log_debug("Connecting as %s", lower); conn_status = jabber_connect(lower, passwd); if (conn_status == JABBER_CONNECTING) { cons_show("Connecting..."); - log_msg(PROF_LEVEL_DEBUG, PROF, "Connecting..."); + log_debug("Connecting..."); } if (conn_status == JABBER_DISCONNECTED) { cons_bad_show("Connection to server failed."); - log_msg(PROF_LEVEL_DEBUG, PROF, "Connection using %s failed", lower); + log_debug("Connection using %s failed", lower); } result = TRUE; diff --git a/src/jabber.c b/src/jabber.c index 1901b6b2..6d5a50b4 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -59,7 +59,7 @@ static log_level_t get_log_level(xmpp_log_level_t xmpp_level) static xmpp_log_level_t get_xmpp_log_level() { - log_level_t prof_level = log_get_level(); + log_level_t prof_level = log_get_filter(); if (prof_level == PROF_LEVEL_DEBUG) { return XMPP_LEVEL_DEBUG; @@ -111,7 +111,7 @@ static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata); void jabber_init(const int disable_tls) { - log_msg(PROF_LEVEL_INFO, "prof", "Initialising XMPP"); + log_info("Initialising XMPP"); jabber_conn.conn_status = JABBER_STARTED; jabber_conn.presence = PRESENCE_OFFLINE; jabber_conn.tls_disabled = disable_tls; @@ -127,7 +127,7 @@ jabber_conn_status_t jabber_connect(const char * const user, const char * const passwd) { - log_msg(PROF_LEVEL_INFO, "prof", "Connecting as %s", user); + log_info("Connecting as %s", user); xmpp_initialize(); jabber_conn.log = xmpp_get_file_logger(); @@ -161,7 +161,7 @@ void jabber_disconnect(void) { if (jabber_conn.conn_status == JABBER_CONNECTED) { - log_msg(PROF_LEVEL_INFO, "prof", "Closing connection"); + log_info("Closing connection"); xmpp_conn_release(jabber_conn.conn); xmpp_ctx_free(jabber_conn.ctx); xmpp_shutdown(); @@ -346,7 +346,7 @@ _jabber_conn_handler(xmpp_conn_t * const conn, title_bar_set_status(PRESENCE_ONLINE); cons_show(line); - log_msg(PROF_LEVEL_INFO, "prof", line); + log_info(line); win_page_off(); status_bar_print_message(jid); status_bar_refresh(); @@ -370,14 +370,14 @@ _jabber_conn_handler(xmpp_conn_t * const conn, else { if (jabber_conn.conn_status == JABBER_CONNECTED) { cons_bad_show("Lost connection."); - log_msg(PROF_LEVEL_INFO, "prof", "Lost connection"); + log_info("Lost connection"); win_disconnected(); } else { cons_bad_show("Login failed."); - log_msg(PROF_LEVEL_INFO, "prof", "Login failed"); + log_info("Login failed"); } win_page_off(); - log_msg(PROF_LEVEL_INFO, CONN, "disconnected"); + log_info("disconnected"); xmpp_stop(ctx); jabber_conn.conn_status = JABBER_DISCONNECTED; jabber_conn.presence = PRESENCE_OFFLINE; @@ -394,7 +394,7 @@ _roster_handler(xmpp_conn_t * const conn, type = xmpp_stanza_get_type(stanza); if (strcmp(type, "error") == 0) - log_msg(PROF_LEVEL_ERROR, CONN, "ERROR: query failed"); + log_error("Roster query failed"); else { query = xmpp_stanza_get_child_by_name(stanza, "query"); cons_show("Roster:"); diff --git a/src/log.c b/src/log.c index b063f2b4..8b9fb5cf 100644 --- a/src/log.c +++ b/src/log.c @@ -28,38 +28,54 @@ #include "log.h" #include "common.h" +#define PROF "prof" + static FILE *logp; static GTimeZone *tz; static GDateTime *dt; -static log_level_t prof_log_level; +static log_level_t level_filter; void -log_msg(log_level_t level, const char * const area, const char * const msg, ...) +log_debug(const char * const msg, ...) { - if (level >= prof_log_level) { - dt = g_date_time_new_now(tz); - - va_list arg; - va_start(arg, msg); - GString *msg_formatted = g_string_new(NULL); - g_string_vprintf(msg_formatted, msg, arg); - va_end(arg); - - gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S"); - fprintf(logp, "%s: %s: %s\n", date_fmt, area, msg_formatted->str); - g_date_time_unref(dt); - g_string_free(msg_formatted, TRUE); - - fflush(logp); - - } + va_list arg; + va_start(arg, msg); + log_msg(PROF_LEVEL_DEBUG, PROF, msg, arg); + va_end(arg); } void -log_init(log_level_t log_level) +log_info(const char * const msg, ...) { - prof_log_level = log_level; + va_list arg; + va_start(arg, msg); + log_msg(PROF_LEVEL_INFO, PROF, msg, arg); + va_end(arg); +} + +void +log_warning(const char * const msg, ...) +{ + va_list arg; + va_start(arg, msg); + log_msg(PROF_LEVEL_WARN, PROF, msg, arg); + va_end(arg); +} + +void +log_error(const char * const msg, ...) +{ + va_list arg; + va_start(arg, msg); + log_msg(PROF_LEVEL_ERROR, PROF, msg, arg); + va_end(arg); +} + +void +log_init(log_level_t filter) +{ + level_filter = filter; tz = g_time_zone_new_local(); GString *log_file = g_string_new(getenv("HOME")); g_string_append(log_file, "/.profanity/log"); @@ -70,9 +86,9 @@ log_init(log_level_t log_level) } log_level_t -log_get_level(void) +log_get_filter(void) { - return prof_log_level; + return level_filter; } void @@ -81,3 +97,25 @@ log_close(void) g_time_zone_unref(tz); fclose(logp); } + +void +log_msg(log_level_t level, const char * const area, const char * const msg, ...) +{ + if (level >= level_filter) { + dt = g_date_time_new_now(tz); + + va_list arg; + va_start(arg, msg); + GString *msg_formatted = g_string_new(NULL); + g_string_vprintf(msg_formatted, msg, arg); + va_end(arg); + + gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S"); + fprintf(logp, "%s: %s: %s\n", date_fmt, area, msg_formatted->str); + g_date_time_unref(dt); + g_string_free(msg_formatted, TRUE); + + fflush(logp); + } +} + diff --git a/src/log.h b/src/log.h index a1fb681e..d8c09e14 100644 --- a/src/log.h +++ b/src/log.h @@ -25,10 +25,6 @@ #include -// log areas -#define PROF "prof" -#define CONN "conn" - // log levels typedef enum { PROF_LEVEL_DEBUG, @@ -37,10 +33,14 @@ typedef enum { PROF_LEVEL_ERROR } log_level_t; -void log_init(log_level_t log_level); +void log_init(log_level_t filter); +log_level_t log_get_filter(void); +void log_close(void); +void log_debug(const char * const msg, ...); +void log_info(const char * const msg, ...); +void log_warning(const char * const msg, ...); +void log_error(const char * const msg, ...); void log_msg(log_level_t level, const char * const area, const char * const msg, ...); -log_level_t log_get_level(void); -void log_close(void); #endif diff --git a/src/preferences.c b/src/preferences.c index 7aaed850..02b97a59 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -80,7 +80,7 @@ static void _save_prefs(void); void prefs_load(void) { - log_msg(PROF_LEVEL_INFO, "prof", "Loading preferences"); + log_info("Loading preferences"); ac = p_autocomplete_new(); prefs_loc = g_string_new(getenv("HOME")); g_string_append(prefs_loc, "/.profanity/config"); @@ -110,7 +110,7 @@ prefs_load(void) void prefs_close(void) { - log_msg(PROF_LEVEL_INFO, "prof", "Closing down preferences"); + log_info("Closing down preferences"); g_key_file_free(prefs); } diff --git a/src/profanity.c b/src/profanity.c index 253c7f32..66593b97 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -47,7 +47,7 @@ profanity_run(void) { gboolean cmd_result = TRUE; - log_msg(PROF_LEVEL_INFO, "prof", "Starting main event loop"); + log_info("Starting main event loop"); inp_non_block(); while(cmd_result == TRUE) { @@ -80,13 +80,13 @@ profanity_init(const int disable_tls, char *log_level) create_config_directory(); log_level_t prof_log_level = _get_log_level(log_level); log_init(prof_log_level); - log_msg(PROF_LEVEL_INFO, PROF, "Starting Profanity (%s)...", PACKAGE_VERSION); + log_info("Starting Profanity (%s)...", PACKAGE_VERSION); chat_log_init(); prefs_load(); gui_init(); jabber_init(disable_tls); cmd_init(); - log_msg(PROF_LEVEL_INFO, "prof", "Initialising contact list"); + log_info("Initialising contact list"); contact_list_init(); atexit(_profanity_shutdown); } @@ -94,12 +94,12 @@ profanity_init(const int disable_tls, char *log_level) void _profanity_shutdown(void) { - log_msg(PROF_LEVEL_INFO, PROF, "Profanity is shutting down."); + log_info("Profanity is shutting down."); jabber_disconnect(); gui_close(); chat_log_close(); prefs_close(); - log_msg(PROF_LEVEL_INFO, "prof", "Shutdown complete"); + log_info("Shutdown complete"); log_close(); } @@ -124,7 +124,7 @@ _get_log_level(char *log_level) gboolean _process_input(char *inp) { - log_msg(PROF_LEVEL_DEBUG, PROF, "Input recieved: %s", inp); + log_debug("Input recieved: %s", inp); gboolean result = FALSE; g_strstrip(inp); diff --git a/src/windows.c b/src/windows.c index 3a6a5c7e..afd88056 100644 --- a/src/windows.c +++ b/src/windows.c @@ -85,7 +85,7 @@ static void _win_notify_typing(char * short_from); void gui_init(void) { - log_msg(PROF_LEVEL_INFO, "prof", "Initialising UI"); + log_info("Initialising UI"); initscr(); cbreak(); keypad(stdscr, TRUE); @@ -137,14 +137,14 @@ gui_refresh(void) void gui_close(void) { - log_msg(PROF_LEVEL_INFO, "prof", "Closing UI"); + log_info("Closing UI"); endwin(); } void gui_resize(const int ch, const char * const input, const int size) { - log_msg(PROF_LEVEL_INFO, "prof", "Resizing UI"); + log_info("Resizing UI"); title_bar_resize(); status_bar_resize(); _win_resize_all();