aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2014-07-15 22:23:53 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2014-07-15 22:23:53 +0200
commita2a979ea2e7ded82cbe4d4cab5da6e613d74a9d0 (patch)
tree330144048aeb84716e6eb3b6e74761e06706750f /src
parent18cb2941f340550c1425dfa1f1376fdf3e69d42e (diff)
downloadxK-a2a979ea2e7ded82cbe4d4cab5da6e613d74a9d0.tar.gz
xK-a2a979ea2e7ded82cbe4d4cab5da6e613d74a9d0.tar.xz
xK-a2a979ea2e7ded82cbe4d4cab5da6e613d74a9d0.zip
Make it possible to route messages to syslog
Diffstat (limited to 'src')
-rw-r--r--src/common.c51
-rw-r--r--src/kike.c6
2 files changed, 44 insertions, 13 deletions
diff --git a/src/common.c b/src/common.c
index 8f7ffd1..800aea5 100644
--- a/src/common.c
+++ b/src/common.c
@@ -42,6 +42,7 @@
#include <strings.h>
#include <regex.h>
#include <libgen.h>
+#include <syslog.h>
#include <sys/socket.h>
#include <sys/types.h>
@@ -86,28 +87,54 @@ extern char **environ;
#define BLOCK_START do {
#define BLOCK_END } while (0)
-// --- Utilities ---------------------------------------------------------------
+// --- Logging -----------------------------------------------------------------
static void
-print_message (FILE *stream, const char *type, const char *fmt, ...)
- ATTRIBUTE_PRINTF (3, 4);
+log_message_syslog (int prio, const char *quote, const char *fmt, va_list ap)
+{
+ va_list va;
+ va_copy (va, ap);
+ int size = vsnprintf (NULL, 0, fmt, va);
+ va_end (va);
+ if (size < 0)
+ return;
+
+ char buf[size + 1];
+ if (vsnprintf (buf, sizeof buf, fmt, ap) >= 0)
+ syslog (prio, "%s%s", quote, buf);
+}
static void
-print_message (FILE *stream, const char *type, const char *fmt, ...)
+log_message_stdio (int prio, const char *quote, const char *fmt, va_list ap)
{
- va_list ap;
+ (void) prio;
+ FILE *stream = stderr;
- va_start (ap, fmt);
- fprintf (stream, "%s ", type);
+ fputs (quote, stream);
vfprintf (stream, fmt, ap);
fputs ("\n", stream);
+}
+
+static void (*g_log_message_real) (int, const char *, const char *, va_list)
+ = log_message_stdio;
+
+static void
+log_message (int priority, const char *quote, const char *fmt, ...)
+ ATTRIBUTE_PRINTF (3, 4);
+
+static void
+log_message (int priority, const char *quote, const char *fmt, ...)
+{
+ va_list ap;
+ va_start (ap, fmt);
+ g_log_message_real (priority, quote, fmt, ap);
va_end (ap);
}
-#define print_fatal(...) print_message (stderr, "fatal:", __VA_ARGS__)
-#define print_error(...) print_message (stderr, "error:", __VA_ARGS__)
-#define print_warning(...) print_message (stderr, "warning:", __VA_ARGS__)
-#define print_status(...) print_message (stdout, "--", __VA_ARGS__)
+#define print_fatal(...) log_message (LOG_ERR, "fatal: ", __VA_ARGS__)
+#define print_error(...) log_message (LOG_ERR, "error: ", __VA_ARGS__)
+#define print_warning(...) log_message (LOG_WARNING, "warning: ", __VA_ARGS__)
+#define print_status(...) log_message (LOG_INFO, "-- ", __VA_ARGS__)
// --- Debugging and assertions ------------------------------------------------
@@ -122,7 +149,7 @@ static bool g_soft_asserts_are_deadly; ///< soft_assert() aborts as well
#define print_debug(...) \
BLOCK_START \
if (g_debug_mode) \
- print_message (stderr, "debug:", __VA_ARGS__); \
+ log_message (LOG_DEBUG, "debug: ", __VA_ARGS__); \
BLOCK_END
static void
diff --git a/src/kike.c b/src/kike.c
index 04dd4fc..07ec21f 100644
--- a/src/kike.c
+++ b/src/kike.c
@@ -1414,7 +1414,11 @@ main (int argc, char *argv[])
}
// TODO: daemonize
- // TODO: syslog (if not running in debug mode)
+ if (!g_debug_mode)
+ {
+ openlog (PROGRAM_NAME, LOG_NDELAY | LOG_NOWAIT | LOG_PID, 0);
+ g_log_message_real = log_message_syslog;
+ }
ctx.polling = true;
while (ctx.polling)