Add log API
authorDavid Herrmann <dh.herrmann@googlemail.com>
Fri, 2 Dec 2011 14:15:05 +0000 (15:15 +0100)
committerDavid Herrmann <dh.herrmann@googlemail.com>
Fri, 2 Dec 2011 14:15:05 +0000 (15:15 +0100)
We simply forward all log messages to stderr. We use syslog/prink-like severity
prefixes. Use systemd or alike to forward stderr to syslog, kernel-log or
similar.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Makefile.am
src/log.c [new file with mode: 0644]
src/log.h [new file with mode: 0644]

index 07fab13..b071429 100644 (file)
@@ -17,7 +17,8 @@ endif
 libkmscon_core_la_SOURCES = \
        src/console.c src/console.h \
        src/output.c src/output.h \
-       src/console_char.c
+       src/console_char.c \
+       src/log.c src/log.h
 
 libkmscon_core_la_CFLAGS = \
        $(AM_CFLAGS) \
diff --git a/src/log.c b/src/log.c
new file mode 100644 (file)
index 0000000..40aa1d2
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,53 @@
+/*
+ * kmscon - Log Control
+ *
+ * Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
+ * Copyright (c) 2011 University of Tuebingen
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/*
+ * Log Control
+ * Forward log messages to stderr. We use syslog and printk like severity
+ * prefixes.
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "log.h"
+
+__attribute__ ((format (printf, 1, 2)))
+void log_printf(const char *format, ...)
+{
+       va_list list;
+
+       va_start(list, format);
+       log_vprintf(format, list);
+       va_end(list);
+}
+
+__attribute__ ((format (printf, 1, 0)))
+void log_vprintf(const char *format, va_list list)
+{
+       vfprintf(stderr, format, list);
+}
diff --git a/src/log.h b/src/log.h
new file mode 100644 (file)
index 0000000..4ea1471
--- /dev/null
+++ b/src/log.h
@@ -0,0 +1,73 @@
+/*
+ * kmscon - Log Control
+ *
+ * Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
+ * Copyright (c) 2011 University of Tuebingen
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/*
+ * Log Control
+ * This is a fairly simply logging API. It forwards all messages to stderr.
+ * They may be prefixed with a priority level like kernel messages.
+ * To forward the messages to syslog simply connect stderr to the syslog daemon
+ * via your init-manager.
+ */
+
+#ifndef KMSCON_LOG_H
+#define KMSCON_LOG_H
+
+#include <stdarg.h>
+#include <stdlib.h>
+
+/* LOG_EMERG and LOG_ALERT do not make sense for this application */
+#define LOG_CRIT       "<2>" /* error that cannot be handled correctly */
+#define LOG_ERR                "<3>" /* error detected */
+#define LOG_WARNING    "<4>" /* warn about unexpected conditions */
+#define LOG_NOTICE     "<5>" /* notify about unusual conditions */
+#define LOG_INFO       "<6>" /* basic inforomational messages */
+#define LOG_DEBUG      "<7>" /* debug messages */
+
+/* dummy logger which allows gcc to check for printk-format */
+static inline __attribute__ ((format (printf, 1, 2)))
+void log_dummy(const char *format, ...)
+{
+}
+
+__attribute__ ((format (printf, 1, 2)))
+void log_printf(const char *format, ...);
+__attribute__ ((format (printf, 1, 0)))
+void log_vprintf(const char *format, va_list list);
+
+#define log_crit(format, ...) log_printf(LOG_CRIT format, ##__VA_ARGS__)
+#define log_err(format, ...) log_printf(LOG_ERR format, ##__VA_ARGS__)
+#define log_warning(format, ...) log_printf(LOG_WARNING format, ##__VA_ARGS__)
+#define log_notice(format, ...) log_printf(LOG_NOTICE format, ##__VA_ARGS__)
+#define log_info(format, ...) log_printf(LOG_INFO format, ##__VA_ARGS__)
+
+/* log_debug() should produce zero code if DEBUG is disabled */
+#ifndef NDEBUG
+#define log_debug(format, ...) log_printf(LOG_DEBUG format, ##__VA_ARGS__)
+#else
+#define log_debug(format, ...) log_dummy(LOG_DEBUG format, ##__VA_ARGS__)
+#endif
+
+#endif /* KMSCON_LOG_H */