1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Customisable logging levels

This commit is contained in:
James Booth 2012-08-19 03:43:18 +01:00
parent 74a88ad566
commit e698738745
6 changed files with 50 additions and 12 deletions

View File

@ -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);
@ -496,11 +499,17 @@ _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);
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;
}

View File

@ -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);
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

View File

@ -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);

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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);