summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/log/log.c48
-rw-r--r--lib/log/log.h15
2 files changed, 44 insertions, 19 deletions
diff --git a/lib/log/log.c b/lib/log/log.c
index ecbd714..b250a9e 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -1,38 +1,54 @@
+#include <assert.h>
#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;
- FILE *stream;
-
- stream = logf ? logf : stderr;
-
va_start(ap, fmt);
- vfprintf(stream, fmt, ap);
+ __log(fmt, 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;
}
diff --git a/lib/log/log.h b/lib/log/log.h
index 6f44bea..e34de33 100644
--- a/lib/log/log.h
+++ b/lib/log/log.h
@@ -1,11 +1,20 @@
#ifndef _LOG_H
#define _LOG_H
+#include <stdbool.h>
#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 */
OpenPOWER on IntegriCloud