diff --git a/src/jabber.c b/src/jabber.c index 1b10ad8c..42dd959b 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -44,19 +44,52 @@ static struct _jabber_conn_t { int tls_disabled; } jabber_conn; +static log_level_t get_log_level(xmpp_log_level_t xmpp_level) +{ + if (xmpp_level == XMPP_LEVEL_DEBUG) { + return PROF_LEVEL_DEBUG; + } else if (xmpp_level == XMPP_LEVEL_INFO) { + return PROF_LEVEL_INFO; + } else if (xmpp_level == XMPP_LEVEL_WARN) { + return PROF_LEVEL_WARN; + } else { + return PROF_LEVEL_ERROR; + } +} + +static xmpp_log_level_t get_xmpp_log_level() +{ + log_level_t prof_level = log_get_level(); + + if (prof_level == PROF_LEVEL_DEBUG) { + return XMPP_LEVEL_DEBUG; + } else if (prof_level == PROF_LEVEL_INFO) { + return XMPP_LEVEL_INFO; + } else if (prof_level == PROF_LEVEL_WARN) { + return XMPP_LEVEL_WARN; + } else { + return XMPP_LEVEL_ERROR; + } +} + void xmpp_file_logger(void * const userdata, const xmpp_log_level_t level, const char * const area, const char * const msg) { - log_msg(area, msg); + log_level_t prof_level = get_log_level(level); + log_msg(prof_level, area, msg); } -static const xmpp_log_t file_log = { &xmpp_file_logger, XMPP_LEVEL_DEBUG }; - xmpp_log_t * xmpp_get_file_logger() { - return (xmpp_log_t*) &file_log; + xmpp_log_level_t level = get_xmpp_log_level(); + xmpp_log_t *file_log = malloc(sizeof(xmpp_log_t)); + + file_log->handler = xmpp_file_logger; + file_log->userdata = &level; + + return file_log; } // private XMPP handlers @@ -338,7 +371,7 @@ _jabber_conn_handler(xmpp_conn_t * const conn, cons_bad_show("Login failed."); } win_page_off(); - log_msg(CONN, "disconnected"); + log_msg(PROF_LEVEL_INFO, CONN, "disconnected"); xmpp_stop(ctx); jabber_conn.conn_status = JABBER_DISCONNECTED; jabber_conn.presence = PRESENCE_OFFLINE; @@ -355,7 +388,7 @@ _roster_handler(xmpp_conn_t * const conn, type = xmpp_stanza_get_type(stanza); if (strcmp(type, "error") == 0) - log_msg(CONN, "ERROR: query failed"); + log_msg(PROF_LEVEL_ERROR, CONN, "ERROR: 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 2952fcaa..cbaf7eb7 100644 --- a/src/log.c +++ b/src/log.c @@ -32,19 +32,23 @@ static FILE *logp; static GTimeZone *tz; static GDateTime *dt; +static log_level_t prof_log_level; void -log_msg(const char * const area, const char * const msg) +log_msg(log_level_t level, const char * const area, const char * const msg) { - dt = g_date_time_new_now(tz); - gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S"); - fprintf(logp, "%s: %s DEBUG: %s\n", date_fmt, area, msg); - g_date_time_unref(dt); + if (level >= prof_log_level) { + dt = g_date_time_new_now(tz); + gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S"); + fprintf(logp, "%s: %s: %s\n", date_fmt, area, msg); + g_date_time_unref(dt); + } } void -log_init(void) +log_init(log_level_t log_level) { + prof_log_level = log_level; tz = g_time_zone_new_local(); GString *log_file = g_string_new(getenv("HOME")); g_string_append(log_file, "/.profanity/log"); @@ -52,7 +56,13 @@ log_init(void) g_string_append(log_file, "/profanity.log"); logp = fopen(log_file->str, "a"); g_string_free(log_file, TRUE); - log_msg(PROF, "Starting Profanity..."); + log_msg(PROF_LEVEL_INFO, PROF, "Starting Profanity..."); +} + +log_level_t +log_get_level(void) +{ + return prof_log_level; } void diff --git a/src/log.h b/src/log.h index 6cf175df..00dbc45c 100644 --- a/src/log.h +++ b/src/log.h @@ -29,8 +29,17 @@ #define PROF "prof" #define CONN "conn" -void log_init(void); -void log_msg(const char * const area, const char * const msg); +// log levels +typedef enum { + PROF_LEVEL_DEBUG, + PROF_LEVEL_INFO, + PROF_LEVEL_WARN, + PROF_LEVEL_ERROR +} log_level_t; + +void log_init(log_level_t log_level); +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/profanity.c b/src/profanity.c index 6f13ba4f..3292951b 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -72,7 +72,7 @@ void profanity_init(const int disable_tls) { create_config_directory(); - log_init(); + log_init(PROF_LEVEL_DEBUG); chat_log_init(); prefs_load(); gui_init();