Rework conditional feature usage
Currently, the infrastructure that we have to enable a flexible
compilation environment has a few issues:
- the allocator configuration is performed at run-time, while the log
configuration is performed at compile-time
- for a standard compile (ie, standard userspace, using
autoconf+automake to build), we need a few pre-defined configuration
options.
This change adds a bit of runtime selection to the logging
infrastructure, to match the allocator setup. We also unify the
compile-time defines into config.h, using MCTP_-prefixed macro names,
allowing integration into other build systems.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/log.c b/log.c
new file mode 100644
index 0000000..12662a3
--- /dev/null
+++ b/log.c
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "libmctp.h"
+#include "libmctp-log.h"
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef MCTP_HAVE_SYSLOG
+#include <syslog.h>
+#endif
+
+enum {
+ MCTP_LOG_NONE,
+ MCTP_LOG_STDIO,
+ MCTP_LOG_SYSLOG,
+ MCTP_LOG_CUSTOM,
+} log_type = MCTP_LOG_NONE;
+
+static int log_stdio_level;
+static void (*log_custom_fn)(int, const char *, va_list);
+
+void mctp_prlog(int level, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+
+ switch (log_type) {
+ case MCTP_LOG_NONE:
+ break;
+ case MCTP_LOG_STDIO:
+ if (level <= log_stdio_level) {
+ vfprintf(stderr, fmt, ap);
+ fputs("\n", stderr);
+ }
+ break;
+ case MCTP_LOG_SYSLOG:
+#ifdef MCTP_HAVE_SYSLOG
+ vsyslog(level, fmt, ap);
+#endif
+ break;
+ case MCTP_LOG_CUSTOM:
+ log_custom_fn(level, fmt, ap);
+ break;
+ }
+
+ va_end(ap);
+}
+
+void mctp_set_log_stdio(int level)
+{
+ log_type = MCTP_LOG_STDIO;
+ log_stdio_level = level;
+}
+
+void mctp_set_log_syslog(void)
+{
+ log_type = MCTP_LOG_SYSLOG;
+}
+
+void mctp_set_log_custom(void (*fn)(int, const char *, va_list))
+{
+ log_type = MCTP_LOG_CUSTOM;
+ log_custom_fn = fn;
+}