1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-01 19:24:15 -04:00

Basic log levels settings

This commit is contained in:
James Booth 2012-08-19 02:44:46 +01:00
parent 108194c944
commit 74a88ad566
4 changed files with 68 additions and 16 deletions

View File

@ -44,19 +44,52 @@ static struct _jabber_conn_t {
int tls_disabled; int tls_disabled;
} jabber_conn; } 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 void
xmpp_file_logger(void * const userdata, const xmpp_log_level_t level, xmpp_file_logger(void * const userdata, const xmpp_log_level_t level,
const char * const area, const char * const msg) 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_log_t *
xmpp_get_file_logger() 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 // private XMPP handlers
@ -338,7 +371,7 @@ _jabber_conn_handler(xmpp_conn_t * const conn,
cons_bad_show("Login failed."); cons_bad_show("Login failed.");
} }
win_page_off(); win_page_off();
log_msg(CONN, "disconnected"); log_msg(PROF_LEVEL_INFO, CONN, "disconnected");
xmpp_stop(ctx); xmpp_stop(ctx);
jabber_conn.conn_status = JABBER_DISCONNECTED; jabber_conn.conn_status = JABBER_DISCONNECTED;
jabber_conn.presence = PRESENCE_OFFLINE; jabber_conn.presence = PRESENCE_OFFLINE;
@ -355,7 +388,7 @@ _roster_handler(xmpp_conn_t * const conn,
type = xmpp_stanza_get_type(stanza); type = xmpp_stanza_get_type(stanza);
if (strcmp(type, "error") == 0) if (strcmp(type, "error") == 0)
log_msg(CONN, "ERROR: query failed"); log_msg(PROF_LEVEL_ERROR, CONN, "ERROR: query failed");
else { else {
query = xmpp_stanza_get_child_by_name(stanza, "query"); query = xmpp_stanza_get_child_by_name(stanza, "query");
cons_show("Roster:"); cons_show("Roster:");

View File

@ -32,19 +32,23 @@ static FILE *logp;
static GTimeZone *tz; static GTimeZone *tz;
static GDateTime *dt; static GDateTime *dt;
static log_level_t prof_log_level;
void 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); if (level >= prof_log_level) {
gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S"); dt = g_date_time_new_now(tz);
fprintf(logp, "%s: %s DEBUG: %s\n", date_fmt, area, msg); gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S");
g_date_time_unref(dt); fprintf(logp, "%s: %s: %s\n", date_fmt, area, msg);
g_date_time_unref(dt);
}
} }
void void
log_init(void) log_init(log_level_t log_level)
{ {
prof_log_level = log_level;
tz = g_time_zone_new_local(); tz = g_time_zone_new_local();
GString *log_file = g_string_new(getenv("HOME")); GString *log_file = g_string_new(getenv("HOME"));
g_string_append(log_file, "/.profanity/log"); g_string_append(log_file, "/.profanity/log");
@ -52,7 +56,13 @@ log_init(void)
g_string_append(log_file, "/profanity.log"); g_string_append(log_file, "/profanity.log");
logp = fopen(log_file->str, "a"); logp = fopen(log_file->str, "a");
g_string_free(log_file, TRUE); 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 void

View File

@ -29,8 +29,17 @@
#define PROF "prof" #define PROF "prof"
#define CONN "conn" #define CONN "conn"
void log_init(void); // log levels
void log_msg(const char * const area, const char * const msg); 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); void log_close(void);
#endif #endif

View File

@ -72,7 +72,7 @@ void
profanity_init(const int disable_tls) profanity_init(const int disable_tls)
{ {
create_config_directory(); create_config_directory();
log_init(); log_init(PROF_LEVEL_DEBUG);
chat_log_init(); chat_log_init();
prefs_load(); prefs_load();
gui_init(); gui_init();