1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Refactored log

Added shortcut functions for prof logging levels
This commit is contained in:
James Booth 2012-08-26 00:54:18 +01:00
parent 0fe70ce7d3
commit 2bbac1c811
7 changed files with 92 additions and 54 deletions

View File

@ -337,7 +337,7 @@ static PAutocomplete commands_ac;
void void
cmd_init(void) cmd_init(void)
{ {
log_msg(PROF_LEVEL_INFO, "prof", "Initialising commands"); log_info("Initialising commands");
commands_ac = p_autocomplete_new(); commands_ac = p_autocomplete_new();
unsigned int i; unsigned int i;
@ -471,16 +471,16 @@ _cmd_connect(const char * const inp, struct cmd_help_t help)
inp_get_password(passwd); inp_get_password(passwd);
inp_non_block(); 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); conn_status = jabber_connect(lower, passwd);
if (conn_status == JABBER_CONNECTING) { if (conn_status == JABBER_CONNECTING) {
cons_show("Connecting..."); cons_show("Connecting...");
log_msg(PROF_LEVEL_DEBUG, PROF, "Connecting..."); log_debug("Connecting...");
} }
if (conn_status == JABBER_DISCONNECTED) { if (conn_status == JABBER_DISCONNECTED) {
cons_bad_show("Connection to server failed."); 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; result = TRUE;

View File

@ -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() 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) { if (prof_level == PROF_LEVEL_DEBUG) {
return XMPP_LEVEL_DEBUG; return XMPP_LEVEL_DEBUG;
@ -111,7 +111,7 @@ static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata);
void void
jabber_init(const int disable_tls) 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.conn_status = JABBER_STARTED;
jabber_conn.presence = PRESENCE_OFFLINE; jabber_conn.presence = PRESENCE_OFFLINE;
jabber_conn.tls_disabled = disable_tls; jabber_conn.tls_disabled = disable_tls;
@ -127,7 +127,7 @@ jabber_conn_status_t
jabber_connect(const char * const user, jabber_connect(const char * const user,
const char * const passwd) const char * const passwd)
{ {
log_msg(PROF_LEVEL_INFO, "prof", "Connecting as %s", user); log_info("Connecting as %s", user);
xmpp_initialize(); xmpp_initialize();
jabber_conn.log = xmpp_get_file_logger(); jabber_conn.log = xmpp_get_file_logger();
@ -161,7 +161,7 @@ void
jabber_disconnect(void) jabber_disconnect(void)
{ {
if (jabber_conn.conn_status == JABBER_CONNECTED) { 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_conn_release(jabber_conn.conn);
xmpp_ctx_free(jabber_conn.ctx); xmpp_ctx_free(jabber_conn.ctx);
xmpp_shutdown(); xmpp_shutdown();
@ -346,7 +346,7 @@ _jabber_conn_handler(xmpp_conn_t * const conn,
title_bar_set_status(PRESENCE_ONLINE); title_bar_set_status(PRESENCE_ONLINE);
cons_show(line); cons_show(line);
log_msg(PROF_LEVEL_INFO, "prof", line); log_info(line);
win_page_off(); win_page_off();
status_bar_print_message(jid); status_bar_print_message(jid);
status_bar_refresh(); status_bar_refresh();
@ -370,14 +370,14 @@ _jabber_conn_handler(xmpp_conn_t * const conn,
else { else {
if (jabber_conn.conn_status == JABBER_CONNECTED) { if (jabber_conn.conn_status == JABBER_CONNECTED) {
cons_bad_show("Lost connection."); cons_bad_show("Lost connection.");
log_msg(PROF_LEVEL_INFO, "prof", "Lost connection"); log_info("Lost connection");
win_disconnected(); win_disconnected();
} else { } else {
cons_bad_show("Login failed."); cons_bad_show("Login failed.");
log_msg(PROF_LEVEL_INFO, "prof", "Login failed"); log_info("Login failed");
} }
win_page_off(); win_page_off();
log_msg(PROF_LEVEL_INFO, CONN, "disconnected"); log_info("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;
@ -394,7 +394,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(PROF_LEVEL_ERROR, CONN, "ERROR: query failed"); log_error("Roster 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

@ -28,38 +28,54 @@
#include "log.h" #include "log.h"
#include "common.h" #include "common.h"
#define PROF "prof"
static FILE *logp; static FILE *logp;
static GTimeZone *tz; static GTimeZone *tz;
static GDateTime *dt; static GDateTime *dt;
static log_level_t prof_log_level; static log_level_t level_filter;
void 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) { va_list arg;
dt = g_date_time_new_now(tz); va_start(arg, msg);
log_msg(PROF_LEVEL_DEBUG, PROF, msg, arg);
va_list arg; va_end(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);
}
} }
void 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(); 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");
@ -70,9 +86,9 @@ log_init(log_level_t log_level)
} }
log_level_t log_level_t
log_get_level(void) log_get_filter(void)
{ {
return prof_log_level; return level_filter;
} }
void void
@ -81,3 +97,25 @@ log_close(void)
g_time_zone_unref(tz); g_time_zone_unref(tz);
fclose(logp); 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);
}
}

View File

@ -25,10 +25,6 @@
#include <stdio.h> #include <stdio.h>
// log areas
#define PROF "prof"
#define CONN "conn"
// log levels // log levels
typedef enum { typedef enum {
PROF_LEVEL_DEBUG, PROF_LEVEL_DEBUG,
@ -37,10 +33,14 @@ typedef enum {
PROF_LEVEL_ERROR PROF_LEVEL_ERROR
} log_level_t; } 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, void log_msg(log_level_t level, const char * const area,
const char * const msg, ...); const char * const msg, ...);
log_level_t log_get_level(void);
void log_close(void);
#endif #endif

View File

@ -80,7 +80,7 @@ static void _save_prefs(void);
void void
prefs_load(void) prefs_load(void)
{ {
log_msg(PROF_LEVEL_INFO, "prof", "Loading preferences"); log_info("Loading preferences");
ac = p_autocomplete_new(); ac = p_autocomplete_new();
prefs_loc = g_string_new(getenv("HOME")); prefs_loc = g_string_new(getenv("HOME"));
g_string_append(prefs_loc, "/.profanity/config"); g_string_append(prefs_loc, "/.profanity/config");
@ -110,7 +110,7 @@ prefs_load(void)
void void
prefs_close(void) prefs_close(void)
{ {
log_msg(PROF_LEVEL_INFO, "prof", "Closing down preferences"); log_info("Closing down preferences");
g_key_file_free(prefs); g_key_file_free(prefs);
} }

View File

@ -47,7 +47,7 @@ profanity_run(void)
{ {
gboolean cmd_result = TRUE; gboolean cmd_result = TRUE;
log_msg(PROF_LEVEL_INFO, "prof", "Starting main event loop"); log_info("Starting main event loop");
inp_non_block(); inp_non_block();
while(cmd_result == TRUE) { while(cmd_result == TRUE) {
@ -80,13 +80,13 @@ profanity_init(const int disable_tls, char *log_level)
create_config_directory(); create_config_directory();
log_level_t prof_log_level = _get_log_level(log_level); log_level_t prof_log_level = _get_log_level(log_level);
log_init(prof_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(); chat_log_init();
prefs_load(); prefs_load();
gui_init(); gui_init();
jabber_init(disable_tls); jabber_init(disable_tls);
cmd_init(); cmd_init();
log_msg(PROF_LEVEL_INFO, "prof", "Initialising contact list"); log_info("Initialising contact list");
contact_list_init(); contact_list_init();
atexit(_profanity_shutdown); atexit(_profanity_shutdown);
} }
@ -94,12 +94,12 @@ profanity_init(const int disable_tls, char *log_level)
void void
_profanity_shutdown(void) _profanity_shutdown(void)
{ {
log_msg(PROF_LEVEL_INFO, PROF, "Profanity is shutting down."); log_info("Profanity is shutting down.");
jabber_disconnect(); jabber_disconnect();
gui_close(); gui_close();
chat_log_close(); chat_log_close();
prefs_close(); prefs_close();
log_msg(PROF_LEVEL_INFO, "prof", "Shutdown complete"); log_info("Shutdown complete");
log_close(); log_close();
} }
@ -124,7 +124,7 @@ _get_log_level(char *log_level)
gboolean gboolean
_process_input(char *inp) _process_input(char *inp)
{ {
log_msg(PROF_LEVEL_DEBUG, PROF, "Input recieved: %s", inp); log_debug("Input recieved: %s", inp);
gboolean result = FALSE; gboolean result = FALSE;
g_strstrip(inp); g_strstrip(inp);

View File

@ -85,7 +85,7 @@ static void _win_notify_typing(char * short_from);
void void
gui_init(void) gui_init(void)
{ {
log_msg(PROF_LEVEL_INFO, "prof", "Initialising UI"); log_info("Initialising UI");
initscr(); initscr();
cbreak(); cbreak();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
@ -137,14 +137,14 @@ gui_refresh(void)
void void
gui_close(void) gui_close(void)
{ {
log_msg(PROF_LEVEL_INFO, "prof", "Closing UI"); log_info("Closing UI");
endwin(); endwin();
} }
void void
gui_resize(const int ch, const char * const input, const int size) 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(); title_bar_resize();
status_bar_resize(); status_bar_resize();
_win_resize_all(); _win_resize_all();