aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
authorMarcus Fritzsch <marcus_fritzsch@mentor.com>2017-06-07 14:28:57 +0200
committerMarcus Fritzsch <marcus_fritzsch@mentor.com>2017-08-08 17:23:31 +0200
commit5194d08da23a315c1405b108889f4c6ae9cb0426 (patch)
tree11bc1667c7f6956bc7b304f6c9e38c39b2ee60d7 /src/util.c
initial commit
Signed-off-by: Marcus Fritzsch <marcus_fritzsch@mentor.com>
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..ee4b7b6
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,55 @@
+#include "util.h"
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+struct strftime_cache {
+ time_t time;
+ char buf[128];
+};
+
+void log_(char const *log_type, FILE *stream, char const *fmt, va_list args) {
+ static struct strftime_cache strft;
+
+ time_t t = time(NULL);
+ if (t != strft.time) {
+ strft.time = t;
+ struct tm tm;
+ struct tm *tmp = localtime_r(&t, &tm);
+ strftime(strft.buf, sizeof(strft.buf), "%Y-%m-%dT%H:%M:%S", tmp);
+ }
+
+ fputs(program_invocation_short_name, stream);
+ fputs(" ", stream);
+ fputs(strft.buf, stream);
+ fputs(" ", stream);
+ fputs(log_type, stream);
+ fputs(" ", stream);
+ vfprintf(stream, fmt, args);
+ fputs("\n", stream);
+}
+
+void lognotice(char const *fmt, ...) {
+ va_list a;
+ va_start(a, fmt);
+ log_("notice", stdout, fmt, a);
+ va_end(a);
+}
+
+void logerror(char const *fmt, ...) {
+ va_list a;
+ va_start(a, fmt);
+ log_("error", stderr, fmt, a);
+ va_end(a);
+}
+
+void fatal(char const *fmt, ...) {
+ va_list a;
+ va_start(a, fmt);
+ log_("fatal", stderr, fmt, a);
+ va_end(a);
+ abort();
+}