From afc137c1462c1cef56349256469e24debc747a13 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Mon, 8 Apr 2019 01:41:48 +0000 Subject: [PATCH] Make logfile optional. --- main.c | 11 +++++----- util.c | 65 ++++++++++++++++++++++++++++++++++++++++++---------------- util.h | 1 + 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/main.c b/main.c index 7eb48d5..1f34046 100644 --- a/main.c +++ b/main.c @@ -29,6 +29,7 @@ /* Config parser */ static void on_config(const char *group, const char *name, const char *key, const char *value) { + util_config(group, name, key, value); view_config(group, name, key, value); chat_config(group, name, key, value); } @@ -47,17 +48,17 @@ int main(int argc, char **argv) /* Misc setup */ signal(SIGINT, on_sigint); - /* Initialize */ - util_init(); - net_init(); - + /* Early setup */ args_setup(argc, argv); conf_setup(".lamechatrc", on_config); + /* Initialize */ + util_init(); + net_init(); chat_init(); view_init(); - /* Mode main */ + /* Main loop */ while (poll_run(1000)) { view_sync(); conf_sync(); diff --git a/util.c b/util.c index 3040ba2..1ea505f 100644 --- a/util.c +++ b/util.c @@ -30,11 +30,13 @@ #include #include #include +#include #include #include #include +#include "conf.h" #include "view.h" #include "util.h" @@ -43,7 +45,8 @@ /* Static data */ static int epoll = 0; -static FILE *debug_fd = NULL; +static char *log_file = NULL; +static FILE *log_fd = NULL; static int running = 0; static AspellSpeller *speller = NULL; @@ -52,7 +55,7 @@ static AspellSpeller *speller = NULL; extern void view_message(const char *prefix, const char *msg); /* Helper functions */ -static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap) +static void message(FILE *out_fd, const char *prefix, const char *fmt, va_list ap) { va_list tmp; struct timespec ts; @@ -62,23 +65,23 @@ static void message(FILE *output_fd, const char *prefix, const char *fmt, va_lis unsigned int msec = ts.tv_nsec / 1000000; /* Log to standard out */ - if (output_fd) { + if (out_fd) { va_copy(tmp, ap); - fprintf(output_fd, "%u.%03u: ", sec, msec); - fprintf(output_fd, "%s: ", prefix); - vfprintf(output_fd, fmt, tmp); - fprintf(output_fd, "\n"); - fflush(output_fd); + fprintf(out_fd, "%u.%03u: ", sec, msec); + fprintf(out_fd, "%s: ", prefix); + vfprintf(out_fd, fmt, tmp); + fprintf(out_fd, "\n"); + fflush(out_fd); } /* Log to debug file */ - if (debug_fd) { + if (log_fd) { va_copy(tmp, ap); - fprintf(debug_fd, "%u.%03u: ", sec, msec); - fprintf(debug_fd, "%s: ", prefix); - vfprintf(debug_fd, fmt, tmp); - fprintf(debug_fd, "\n"); - fflush(debug_fd); + fprintf(log_fd, "%u.%03u: ", sec, msec); + fprintf(log_fd, "%s: ", prefix); + vfprintf(log_fd, fmt, tmp); + fprintf(log_fd, "\n"); + fflush(log_fd); } /* Log to status bar */ @@ -102,26 +105,52 @@ static void on_idle(void *_idle) /* Initialize */ void util_init(void) { - epoll = epoll_create(1); - debug_fd = fopen("/tmp/lamechat.log", "w+"); - running = 1; + /* Init epol */ + epoll = epoll_create(1); + + /* Init log file */ + if (log_file) { + wordexp_t wexp; + wordexp(log_file, &wexp, WRDE_NOCMD); + if (wexp.we_wordc > 1) + error("Found multiple log files: %s\n %s\n %s\n ...", + log_file, wexp.we_wordv[0], wexp.we_wordv[1]); + if (!(log_fd = fopen(wexp.we_wordv[0], "w+"))) + error("Failed to create log file: %s", log_file); + wordfree(&wexp); + } + /* Init spelling */ AspellConfig *config = new_aspell_config(); AspellCanHaveError *status = new_aspell_speller(config); if (aspell_error_number(status)) error("ASpell error: %s\n", aspell_error_message(status)); speller = to_aspell_speller(status); + + /* Start running */ + running = 1; } /* Cleanup */ void util_exit(void) { close(epoll); - fclose(debug_fd); + if (log_fd) + fclose(log_fd); + free0(&log_file); aspell_speller_save_all_word_lists(speller); delete_aspell_speller(speller); } +/* Config */ +void util_config(const char *group, const char *name, const char *key, const char *value) +{ + if (match(group, "general")) { + if (match(key, "logfile")) + strset(&log_file, get_string(value)); + } +} + /* String functions */ void strsub(char *str, char find, char repl) { diff --git a/util.h b/util.h index 2c77901..9ce3594 100644 --- a/util.h +++ b/util.h @@ -66,6 +66,7 @@ typedef struct { /* Debug functions */ void util_init(void); void util_exit(void); +void util_config(const char *group, const char *name, const char *key, const char *value); /* String functions */ void strsub(char *str, char find, char repl); -- 2.43.2