]> xenbits.xensource.com Git - unikraft/libs/newlib.git/commitdiff
syslog.c: Import from osv
authorCostin Lupu <costin.lupu@cs.pub.ro>
Wed, 14 Aug 2019 18:57:02 +0000 (21:57 +0300)
committerFelipe Huici <felipe.huici@neclab.eu>
Thu, 15 Aug 2019 08:33:39 +0000 (10:33 +0200)
Copied as is from the official osv git mirror
https://github.com/cloudius-systems/osv.git, commit ee7a2cd4.

Original file locations:
* libc/syslog.c

We had to decide between importing from musl or osv. We decided to pick the osv
variant because it's simpler: it outputs to stdio. The musl implementation uses
UNIX sockets, which we currently don't have in Unikraft. Moreover, the osv
implementation was originally imported from musl and adapted. This is also the
reason why we put the file under the musl-imported/ subdirectory.

In the future, a better syslog implementation may replace this one and it might
even become an internal library with fully fledged functionality, just like
classic OSes.

Signed-off-by: Costin Lupu <costin.lupu@cs.pub.ro>
Reviewed-by: Felipe Huici <felipe.huici@neclab.eu>
musl-imported/src/syslog.c [new file with mode: 0644]

diff --git a/musl-imported/src/syslog.c b/musl-imported/src/syslog.c
new file mode 100644 (file)
index 0000000..be7cfb6
--- /dev/null
@@ -0,0 +1,73 @@
+
+// adapted from musl's version, just writes to stdio
+
+#include <syslog.h>
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <libc.h>
+
+static mutex_t lock;
+static char log_ident[32];
+static int log_opt;
+static int log_facility = LOG_USER;
+
+void openlog(const char *ident, int opt, int facility)
+{
+    LOCK(lock);
+
+    if (ident) {
+        size_t n = strnlen(ident, sizeof log_ident - 1);
+        memcpy(log_ident, ident, n);
+        log_ident[n] = 0;
+    } else {
+        log_ident[0] = 0;
+    }
+    log_opt = opt;
+    log_facility = facility;
+
+    UNLOCK(lock);
+}
+
+void closelog(void)
+{
+}
+
+void __syslog_chk(int priority, int flag, const char *message, ...)
+{
+    LOCK(lock);
+
+    va_list ap;
+    va_start(ap, message);
+
+    char timebuf[16];
+    time_t now;
+    struct tm tm;
+    char buf[256];
+    int pid;
+    int l, l2;
+
+    if (!(priority & LOG_FACMASK)) priority |= log_facility;
+
+    now = time(NULL);
+    gmtime_r(&now, &tm);
+    strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
+
+    pid = (log_opt & LOG_PID) ? getpid() : 0;
+    l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
+        priority, timebuf, log_ident, "["+!pid, pid, "]"+!pid);
+    l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
+    if (l2 >= 0) {
+        if (l2 >= sizeof buf - l) l = sizeof buf - 1;
+        else l += l2;
+        if (buf[l-1] != '\n') buf[l++] = '\n';
+        fwrite(buf, 1, l, LOG_PRI(priority) >= LOG_ERR ? stderr : stdout);            
+    }
+
+    va_end(ap);
+
+    UNLOCK(lock);
+}
+