summaryrefslogtreecommitdiffstats
path: root/src/reporting.c
diff options
context:
space:
mode:
authorAlex Dowad <alexinbeijing@gmail.com>2014-04-04 16:31:24 +0200
committerTimo Teräs <timo.teras@iki.fi>2014-04-25 10:16:30 +0300
commiteb5b69d278b1430750a5e1d74bd2804cbc4b14e8 (patch)
tree7a99d0443e7ac76478f47f777ef96e231983d88e /src/reporting.c
parent0ad83e2f69e69aeea1ac0baee108c4232449e36d (diff)
downloadsquark-eb5b69d278b1430750a5e1d74bd2804cbc4b14e8.tar.bz2
squark-eb5b69d278b1430750a5e1d74bd2804cbc4b14e8.tar.xz
all: unified framework for reporting errors/warnings/info messages to stderr/syslog
Diffstat (limited to 'src/reporting.c')
-rw-r--r--src/reporting.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/reporting.c b/src/reporting.c
new file mode 100644
index 0000000..329d850
--- /dev/null
+++ b/src/reporting.c
@@ -0,0 +1,69 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <syslog.h>
+
+#include "reporting.h"
+
+int verbosity_level = REPORT_WARNING;
+static int syslog_flag = 0;
+/* for converting our own levels to syslog's levels: */
+static int log_levels[5] = { LOG_ALERT, LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG };
+
+void reporting_init(const char *program_name)
+{
+ openlog(program_name, LOG_PID, LOG_USER);
+ /* reporting to syslog is on by default if stderr is not printed at a terminal */
+ if (!isatty(fileno(stderr)))
+ reporting_use_syslog(1);
+}
+
+void reporting_use_syslog(int flag)
+{
+ syslog_flag = flag;
+}
+
+void reporting_verbosity(int level)
+{
+ verbosity_level = level;
+}
+
+void report_message(int level, const char *format, ...)
+{
+ va_list args;
+ va_start(args, format);
+
+ if(syslog_flag || level == REPORT_ALERT) {
+ syslog(LOG_USER | log_levels[level], format, args);
+ /* va_list can only be used once */
+ va_end(args);
+ va_start(args, format);
+ }
+
+ vfprintf(stderr, format, args);
+ va_end(args);
+}
+
+/* For messages which contain sensitive information, and should not be shown to
+ * all users: */
+void report_private_message(int level, const char *format, ...)
+{
+ if(level <= verbosity_level)
+ {
+ va_list args;
+ va_start(args, format);
+
+ if(syslog_flag || level == REPORT_ALERT) {
+ syslog(LOG_AUTHPRIV | log_levels[level], format, args);
+ /* va_list can only be used once */
+ va_end(args);
+ va_start(args, format);
+ }
+
+ if (getuid() == 0) /* being run by root */
+ vfprintf(stderr, format, args);
+
+ va_end(args);
+ }
+} \ No newline at end of file