]> git.sesse.net Git - cubemap/blobdiff - log.cpp
Support UDP packets larger than 4 kB.
[cubemap] / log.cpp
diff --git a/log.cpp b/log.cpp
index 0b3db4fdf2c0d7c363e204951a9539a73e0fec9b..d481cc43496a3ab962b6f7c19239bcaf471c21b7 100644 (file)
--- a/log.cpp
+++ b/log.cpp
@@ -1,14 +1,15 @@
-#include "log.h"
-
+#include <assert.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stddef.h>
 #include <stdio.h>
 #include <string.h>
-#include <stdarg.h>
 #include <syslog.h>
-#include <assert.h>
-#include <errno.h>
 #include <string>
 #include <vector>
 
+#include "log.h"
+
 using namespace std;
 
 // Yes, it's a bit ugly.
@@ -67,6 +68,16 @@ void log(LogLevel log_level, const char *fmt, ...)
        vsnprintf(formatted_msg, sizeof(formatted_msg), fmt, ap);
        va_end(ap);
 
+       time_t now = time(NULL);
+       struct tm lt;
+       struct tm *ltime = localtime_r(&now, &lt);
+       char timestamp[1024];
+       if (ltime == NULL) {
+               strcpy(timestamp, "???");
+       } else {
+               strftime(timestamp, sizeof(timestamp), "%a, %d %b %Y %T %z", ltime);
+       }
+
        const char *log_level_str;
        int syslog_level;
 
@@ -90,7 +101,7 @@ void log(LogLevel log_level, const char *fmt, ...)
        // Log to stderr if logging hasn't been set up yet. Note that this means
        // that such messages will come even if there are no “error_log” lines.
        if (!logging_started) {
-               fprintf(stderr, "%s%s\n", log_level_str, formatted_msg);
+               fprintf(stderr, "[%s] %s%s\n", timestamp, log_level_str, formatted_msg);
                return;
        }
 
@@ -98,7 +109,7 @@ void log(LogLevel log_level, const char *fmt, ...)
                 if (log_destinations[i] == SYSLOG_FAKE_FILE) {
                        syslog(syslog_level, "%s", formatted_msg);
                } else {
-                       int err = fprintf(log_destinations[i], "%s%s\n", log_level_str, formatted_msg);
+                       int err = fprintf(log_destinations[i], "[%s] %s%s\n", timestamp, log_level_str, formatted_msg);
                        if (err < 0) {
                                perror("fprintf");
                        }