diff --git a/src/command.c b/src/command.c index 66c00805..3fffef02 100644 --- a/src/command.c +++ b/src/command.c @@ -35,6 +35,7 @@ #include "preferences.h" #include "prof_autocomplete.h" #include "tinyurl.h" +#include "log.h" /* command structure * cmd - The actual string of the command @@ -329,6 +330,8 @@ static PAutocomplete commands_ac; gboolean process_input(char *inp) { + log_msg(PROF_LEVEL_DEBUG, PROF, "Input recieved: %s", inp); + gboolean result = FALSE; g_strstrip(inp); @@ -495,12 +498,18 @@ _cmd_connect(const char * const inp, struct cmd_help_t help) inp_block(); inp_get_password(passwd); inp_non_block(); + + log_msg(PROF_LEVEL_DEBUG, PROF, "Connecting as %s", lower); conn_status = jabber_connect(lower, passwd); - if (conn_status == JABBER_CONNECTING) + if (conn_status == JABBER_CONNECTING) { cons_show("Connecting..."); - if (conn_status == JABBER_DISCONNECTED) + log_msg(PROF_LEVEL_DEBUG, PROF, "Connecting..."); + } + if (conn_status == JABBER_DISCONNECTED) { cons_bad_show("Connection to server failed."); + log_msg(PROF_LEVEL_DEBUG, PROF, "Connection using %s failed", lower); + } result = TRUE; } diff --git a/src/log.c b/src/log.c index cbaf7eb7..845d6a4d 100644 --- a/src/log.c +++ b/src/log.c @@ -35,13 +35,24 @@ static GDateTime *dt; static log_level_t prof_log_level; void -log_msg(log_level_t level, const char * const area, const char * const msg) +log_msg(log_level_t level, const char * const area, const char * const msg, ...) { if (level >= prof_log_level) { - dt = g_date_time_new_now(tz); + 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); + 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); + } } @@ -56,7 +67,6 @@ log_init(log_level_t log_level) g_string_append(log_file, "/profanity.log"); logp = fopen(log_file->str, "a"); g_string_free(log_file, TRUE); - log_msg(PROF_LEVEL_INFO, PROF, "Starting Profanity..."); } log_level_t diff --git a/src/log.h b/src/log.h index 00dbc45c..a1fb681e 100644 --- a/src/log.h +++ b/src/log.h @@ -38,7 +38,8 @@ typedef enum { } 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); +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); diff --git a/src/main.c b/src/main.c index 5e1ef9c5..8c696fdc 100644 --- a/src/main.c +++ b/src/main.c @@ -30,6 +30,7 @@ static gboolean disable_tls = FALSE; static gboolean version = FALSE; +static char *log = "INFO"; int main(int argc, char **argv) @@ -38,6 +39,7 @@ main(int argc, char **argv) { { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Show version information", NULL }, { "disable-tls", 'd', 0, G_OPTION_ARG_NONE, &disable_tls, "Disable TLS", NULL }, + { "log",'l', 0, G_OPTION_ARG_STRING, &log, "Set logging levels, DEBUG, INFO (default), WARN, ERROR", "LEVEL" }, { NULL } }; @@ -46,7 +48,6 @@ main(int argc, char **argv) context = g_option_context_new(NULL); g_option_context_add_main_entries(context, entries, NULL); - //g_option_context_add_group(context, gtk_get_option_group (TRUE)); if (!g_option_context_parse(context, &argc, &argv, &error)) { g_print("%s\n", error->message); return 1; @@ -64,7 +65,7 @@ main(int argc, char **argv) return 0; } - profanity_init(disable_tls); + profanity_init(disable_tls, log); profanity_run(); return 0; diff --git a/src/profanity.c b/src/profanity.c index 3292951b..b805e5b3 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -36,6 +36,7 @@ #include "contact_list.h" #include "tinyurl.h" +static log_level_t get_log_level(char *log_level); static void _profanity_shutdown(void); void @@ -69,10 +70,12 @@ profanity_run(void) } void -profanity_init(const int disable_tls) +profanity_init(const int disable_tls, char *log_level) { create_config_directory(); - log_init(PROF_LEVEL_DEBUG); + log_level_t prof_log_level = get_log_level(log_level); + log_init(prof_log_level); + log_msg(PROF_LEVEL_INFO, PROF, "Starting Profanity..."); chat_log_init(); prefs_load(); gui_init(); @@ -85,9 +88,23 @@ profanity_init(const int disable_tls) void _profanity_shutdown(void) { + log_msg(PROF_LEVEL_INFO, PROF, "Profanity is shutting down."); jabber_disconnect(); gui_close(); log_close(); chat_log_close(); prefs_close(); } + +static log_level_t get_log_level(char *log_level) +{ + if (strcmp(log_level, "DEBUG") == 0) { + return PROF_LEVEL_DEBUG; + } else if (strcmp(log_level, "INFO") == 0) { + return PROF_LEVEL_INFO; + } else if (strcmp(log_level, "WARN") == 0) { + return PROF_LEVEL_WARN; + } else { + return PROF_LEVEL_ERROR; + } +} diff --git a/src/profanity.h b/src/profanity.h index 92316ff6..e0d01a23 100644 --- a/src/profanity.h +++ b/src/profanity.h @@ -23,7 +23,7 @@ #ifndef PROFANITY_H #define PROFANITY_H -void profanity_init(const int disable_tls); +void profanity_init(const int disable_tls, char *log_level); void profanity_run(void); void profanity_shutdown(void);