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
cmd_init(void)
{
log_msg(PROF_LEVEL_INFO, "prof", "Initialising commands");
log_info("Initialising commands");
commands_ac = p_autocomplete_new();
unsigned int i;
@ -471,16 +471,16 @@ _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);
log_debug("Connecting as %s", lower);
conn_status = jabber_connect(lower, passwd);
if (conn_status == JABBER_CONNECTING) {
cons_show("Connecting...");
log_msg(PROF_LEVEL_DEBUG, PROF, "Connecting...");
log_debug("Connecting...");
}
if (conn_status == JABBER_DISCONNECTED) {
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;

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

View File

@ -28,16 +28,80 @@
#include "log.h"
#include "common.h"
#define PROF "prof"
static FILE *logp;
static GTimeZone *tz;
static GDateTime *dt;
static log_level_t prof_log_level;
static log_level_t level_filter;
void
log_debug(const char * const msg, ...)
{
va_list arg;
va_start(arg, msg);
log_msg(PROF_LEVEL_DEBUG, PROF, msg, arg);
va_end(arg);
}
void
log_info(const char * const msg, ...)
{
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();
GString *log_file = g_string_new(getenv("HOME"));
g_string_append(log_file, "/.profanity/log");
create_dir(log_file->str);
g_string_append(log_file, "/profanity.log");
logp = fopen(log_file->str, "a");
g_string_free(log_file, TRUE);
}
log_level_t
log_get_filter(void)
{
return level_filter;
}
void
log_close(void)
{
g_time_zone_unref(tz);
fclose(logp);
}
void
log_msg(log_level_t level, const char * const area, const char * const msg, ...)
{
if (level >= prof_log_level) {
if (level >= level_filter) {
dt = g_date_time_new_now(tz);
va_list arg;
@ -52,32 +116,6 @@ log_msg(log_level_t level, const char * const area, const char * const msg, ...)
g_string_free(msg_formatted, TRUE);
fflush(logp);
}
}
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");
create_dir(log_file->str);
g_string_append(log_file, "/profanity.log");
logp = fopen(log_file->str, "a");
g_string_free(log_file, TRUE);
}
log_level_t
log_get_level(void)
{
return prof_log_level;
}
void
log_close(void)
{
g_time_zone_unref(tz);
fclose(logp);
}

View File

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

View File

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

View File

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

View File

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