]> git.ozlabs.org Git - petitboot/commitdiff
lib/log: Cleanup log API
authorJeremy Kerr <jk@ozlabs.org>
Thu, 19 Sep 2013 04:18:56 +0000 (12:18 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 19 Sep 2013 13:36:32 +0000 (21:36 +0800)
Rather than exposing log internals (through always_flush and
set_stream), do all logging init through pb_log_init(). If pb_log_init()
hasn't been called, pb_log will drop messages.

Also, add a pb_debug() function, specifically for debugging information.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/pb-discover.c
lib/log/log.c
lib/log/log.h
test/urls/parse-url.c
ui/ncurses/generic-main.c
ui/twin/main-generic.c

index 6d62e14db2b931e20afdeb55f82cebb0a8b9e31b..c16d6903ed5c1da180dfd615beed1950c824b20b 100644 (file)
@@ -123,6 +123,7 @@ int main(int argc, char *argv[])
        struct opts opts;
        struct pb_udev *udev;
        struct user_event *uev;
        struct opts opts;
        struct pb_udev *udev;
        struct user_event *uev;
+       FILE *log;
 
        if (opts_parse(&opts, argc, argv)) {
                print_usage();
 
        if (opts_parse(&opts, argc, argv)) {
                print_usage();
@@ -139,17 +140,17 @@ int main(int argc, char *argv[])
                return EXIT_SUCCESS;
        }
 
                return EXIT_SUCCESS;
        }
 
+       log = stderr;
        if (strcmp(opts.log_file, "-")) {
        if (strcmp(opts.log_file, "-")) {
-               FILE *log = fopen(opts.log_file, "a");
-
-               assert(log);
-               pb_log_set_stream(log);
-       } else
-               pb_log_set_stream(stderr);
+               log = fopen(opts.log_file, "a");
+               if (!log) {
+                       fprintf(stderr, "can't open log file %s, logging to "
+                                       "stderr\n", opts.log_file);
+                       log = stderr;
+               }
+       }
+       pb_log_init(log);
 
 
-#if defined(DEBUG)
-       pb_log_always_flush(1);
-#endif
        pb_log("--- pb-discover ---\n");
 
        /* we look for closed sockets when we write, so ignore SIGPIPE */
        pb_log("--- pb-discover ---\n");
 
        /* we look for closed sockets when we write, so ignore SIGPIPE */
index ecbd7142d2e5c14ce1f5847d2bb93a6e467790df..b250a9ec26e71beea0761a44ce0efac729b50b2b 100644 (file)
@@ -1,38 +1,54 @@
 
 
+#include <assert.h>
 #include <stdarg.h>
 
 #include "log.h"
 
 static FILE *logf;
 #include <stdarg.h>
 
 #include "log.h"
 
 static FILE *logf;
-static int always_flush;
+static bool debug;
+
+static void __log(const char *fmt, va_list ap)
+{
+       if (!logf)
+               return;
+       vfprintf(logf, fmt, ap);
+       if (debug)
+               fflush(logf);
+}
 
 void pb_log(const char *fmt, ...)
 {
        va_list ap;
 
 void pb_log(const char *fmt, ...)
 {
        va_list ap;
-       FILE *stream;
-
-       stream = logf ? logf : stderr;
-
        va_start(ap, fmt);
        va_start(ap, fmt);
-       vfprintf(stream, fmt, ap);
+       __log(fmt, ap);
        va_end(ap);
        va_end(ap);
-
-       if (always_flush)
-               fflush(stream);
 }
 
 }
 
-void pb_log_set_stream(FILE *stream)
+void pb_debug(const char *fmt, ...)
 {
 {
-       fflush(logf ? logf : stderr);
-       logf = stream;
+       va_list ap;
+       if (!debug)
+               return;
+       va_start(ap, fmt);
+       __log(fmt, ap);
+       va_end(ap);
 }
 
 }
 
-FILE * pb_log_get_stream(void)
+void __pb_log_init(FILE *fp, bool _debug)
 {
 {
-       return logf ? logf : stderr;
+       if (logf)
+               fflush(logf);
+       logf = fp;
+       debug = _debug;
 }
 
 }
 
-void pb_log_always_flush(int state)
+FILE *pb_log_get_stream(void)
 {
 {
-       always_flush = state;
+       static FILE *null_stream;
+       if (!logf) {
+               if (!null_stream)
+                       null_stream = fopen("/dev/null", "a");
+               return null_stream;
+       }
+       return logf;
 }
 }
index 6f44bea0d38ac1fd9bd543676757ce47af788a64..e34de33bbe6134ce23a8bcf0c009258e86084b6b 100644 (file)
@@ -1,11 +1,20 @@
 #ifndef _LOG_H
 #define _LOG_H
 
 #ifndef _LOG_H
 #define _LOG_H
 
+#include <stdbool.h>
 #include <stdio.h>
 
 void __attribute__ ((format (printf, 1, 2))) pb_log(const char *fmt, ...);
 #include <stdio.h>
 
 void __attribute__ ((format (printf, 1, 2))) pb_log(const char *fmt, ...);
-void pb_log_set_stream(FILE *stream);
-FILE * pb_log_get_stream(void);
-void pb_log_always_flush(int state);
+void __attribute__ ((format (printf, 1, 2))) pb_debug(const char *fmt, ...);
+
+void __pb_log_init(FILE *stream, bool debug);
+
+#ifdef DEBUG
+#define pb_log_init(s) __pb_log_init(s, true)
+#else
+#define pb_log_init(s) __pb_log_init(s, false)
+#endif
+
+FILE *pb_log_get_stream(void);
 
 #endif /* _LOG_H */
 
 #endif /* _LOG_H */
index cfa67628a96b63ccc564c85adc81d1b9283240bd..1688eab9540a5f1331bbed76c47cdbab554b11bb 100644 (file)
@@ -8,17 +8,12 @@
 int main(int argc, char **argv)
 {
        struct pb_url *url;
 int main(int argc, char **argv)
 {
        struct pb_url *url;
-       FILE *null;
 
        if (argc != 2 && argc != 3) {
                fprintf(stderr, "Usage: %s <URL> [update]\n", argv[0]);
                return EXIT_FAILURE;
        }
 
 
        if (argc != 2 && argc != 3) {
                fprintf(stderr, "Usage: %s <URL> [update]\n", argv[0]);
                return EXIT_FAILURE;
        }
 
-       /* discard log output */
-       null = fopen("/dev/null", "w");
-       pb_log_set_stream(null);
-
        url = pb_url_parse(NULL, argv[1]);
        if (!url)
                return EXIT_FAILURE;
        url = pb_url_parse(NULL, argv[1]);
        if (!url)
                return EXIT_FAILURE;
index cafd23a5778258db070b8b612889e2db4681c1a0..9236a800beac18a11d1041037955687f109364c6 100644 (file)
@@ -206,6 +206,7 @@ int main(int argc, char *argv[])
        int result;
        int cui_result;
        struct opts opts;
        int result;
        int cui_result;
        struct opts opts;
+       FILE *log;
 
        result = opts_parse(&opts, argc, argv);
 
 
        result = opts_parse(&opts, argc, argv);
 
@@ -224,20 +225,15 @@ int main(int argc, char *argv[])
                return EXIT_SUCCESS;
        }
 
                return EXIT_SUCCESS;
        }
 
+       log = stderr;
        if (strcmp(opts.log_file, "-")) {
        if (strcmp(opts.log_file, "-")) {
-               FILE *log = fopen(opts.log_file, "a");
+               log = fopen(opts.log_file, "a");
 
                if (!log)
                        log = fopen("/dev/null", "a");
 
                if (!log)
                        log = fopen("/dev/null", "a");
+       }
 
 
-               assert(log);
-               pb_log_set_stream(log);
-       } else
-               pb_log_set_stream(stderr);
-
-#if defined(DEBUG)
-       pb_log_always_flush(1);
-#endif
+       pb_log_init(log);
 
        pb_log("--- petitboot-nc ---\n");
 
 
        pb_log("--- petitboot-nc ---\n");
 
index 99520d0adde32b040884ab5511c97e8b117eb828..649ad00121c800cb57a98d9c58ba71bbe760712f 100644 (file)
@@ -268,6 +268,7 @@ int main(int argc, char *argv[])
        int result;
        int ui_result;
        struct pbt_client *client;
        int result;
        int ui_result;
        struct pbt_client *client;
+       FILE *log;
 
        result = pbt_opts_parse(&opts, argc, argv);
 
 
        result = pbt_opts_parse(&opts, argc, argv);
 
@@ -286,17 +287,13 @@ int main(int argc, char *argv[])
                return EXIT_SUCCESS;
        }
 
                return EXIT_SUCCESS;
        }
 
+       log = stderr;
        if (strcmp(opts.log_file, "-")) {
                FILE *log = fopen(opts.log_file, "a");
        if (strcmp(opts.log_file, "-")) {
                FILE *log = fopen(opts.log_file, "a");
-
-               assert(log);
-               pb_log_set_stream(log);
-       } else
-               pb_log_set_stream(stderr);
-
-#if defined(DEBUG)
-       pb_log_always_flush(1);
-#endif
+               if (!log)
+                       log = stderr;
+       }
+       pb_log_init(log);
 
        pb_log("--- petitboot-twin ---\n");
 
 
        pb_log("--- petitboot-twin ---\n");