]> Pileus Git - ~andy/lamechat/commitdiff
Make logfile optional. master
authorAndy Spencer <andy753421@gmail.com>
Mon, 8 Apr 2019 01:41:48 +0000 (01:41 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 8 Apr 2019 01:41:48 +0000 (01:41 +0000)
main.c
util.c
util.h

diff --git a/main.c b/main.c
index 7eb48d574092a4b785ff5a0ada0ba6ac307cdabb..1f3404604b7cc27685ae3be990abb60efb68225b 100644 (file)
--- 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 3040ba2f2732d5a79e48ee0059eaccc30759e9e5..1ea505fff6f24719bd1d6fe399253495c7c7fc6e 100644 (file)
--- a/util.c
+++ b/util.c
 #include <time.h>
 #include <unistd.h>
 #include <regex.h>
+#include <wordexp.h>
 #include <aspell.h>
 
 #include <sys/epoll.h>
 #include <sys/timerfd.h>
 
+#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 2c77901718e9f175c7efebfed237150e336e4c0a..9ce35943d0e8f7d2b942c269a66e6633b5bbe8e4 100644 (file)
--- 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);