summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-08-03 15:42:29 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-08-03 15:42:29 -0300
commit3b7d4c04106b01bbbedad81e62011eba74ab3c6c (patch)
tree29f28df2812c1fcc1ed6c038a25f0efc7e557c04
parent1c59139687546db411fe6957cbdb2122666a0515 (diff)
log: Create `log.{c,h}`, and add `vprintlog()`
`vprintlog()` prints out the provided string to either syslog, stderr, or stdout depending on the output type.
-rw-r--r--log.c28
-rw-r--r--log.h18
2 files changed, 46 insertions, 0 deletions
diff --git a/log.c b/log.c
new file mode 100644
index 0000000..12898f4
--- /dev/null
+++ b/log.c
@@ -0,0 +1,28 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include "log.h"
+#include "misc.h"
+
+void vprintlog(log_output output_type, int priority, const char *fmt, va_list args) {
+ char *buf = calloc(vformat_len_copy(fmt, args)+1, sizeof(char));
+ vsprintf(buf, fmt, args);
+
+ switch (output_type) {
+ case LOG_OUT_SYSLOG: {
+ syslog(priority, "%s", buf);
+ break;
+ }
+ case LOG_OUT_STDERR:
+ case LOG_OUT_STDOUT: {
+ FILE *output = (output_type == LOG_OUT_STDOUT) ? stdout : stderr;
+ fprintf(output, "%s\n", buf);
+ break;
+ }
+ default:
+ break;
+ }
+ free(buf);
+}
diff --git a/log.h b/log.h
new file mode 100644
index 0000000..08477e9
--- /dev/null
+++ b/log.h
@@ -0,0 +1,18 @@
+#ifndef LOG_H
+#define LOG_H
+
+#include <stdarg.h>
+
+typedef enum log_output log_output;
+
+enum log_output {
+ LOG_OUT_SYSLOG,
+ LOG_OUT_STDERR,
+ LOG_OUT_STDOUT,
+ LOG_OUT_NONE,
+};
+
+extern void vprintlog(log_output output_type, int priority, const char *fmt, va_list args);
+
+#endif
+