Allow redirecting logging to an FD
This change allows redirection of logging facilities, from syslog to a
file.
Bug: None
Test: make tests // see logging in stderr
Change-Id: Ia45ccb87908f1d4a2f7964a01d11a74da6e9fdb7
diff --git a/util.c b/util.c
index 2605a3f..f228ff7 100644
--- a/util.c
+++ b/util.c
@@ -8,6 +8,7 @@
#include <ctype.h>
#include <errno.h>
#include <limits.h>
+#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -64,6 +65,41 @@
const size_t log_syscalls_len = ARRAY_SIZE(log_syscalls);
+/* clang-format off */
+static struct logging_config_t {
+ /* The logging system to use. The default is syslog. */
+ enum logging_system_t logger;
+
+ /* File descriptor to log to. Only used when logger is LOG_TO_FD. */
+ int fd;
+
+ /* Minimum priority to log. Only used when logger is LOG_TO_FD. */
+ int min_priority;
+} logging_config = {
+ .logger = LOG_TO_SYSLOG,
+};
+/* clang-format on */
+
+void do_log(int priority, const char *format, ...)
+{
+ if (logging_config.logger == LOG_TO_SYSLOG) {
+ va_list args;
+ va_start(args, format);
+ vsyslog(priority, format, args);
+ va_end(args);
+ return;
+ }
+
+ if (logging_config.min_priority < priority)
+ return;
+
+ va_list args;
+ va_start(args, format);
+ vdprintf(logging_config.fd, format, args);
+ va_end(args);
+ dprintf(logging_config.fd, "\n");
+}
+
int lookup_syscall(const char *name)
{
const struct syscall_entry *entry = syscall_table;
@@ -298,3 +334,10 @@
return NULL;
return consumebytes(len + 1, buf, buflength);
}
+
+void init_logging(enum logging_system_t logger, int fd, int min_priority)
+{
+ logging_config.logger = logger;
+ logging_config.fd = fd;
+ logging_config.min_priority = min_priority;
+}