]> git.sesse.net Git - cubemap/blobdiff - log.cpp
Add suppor for raw (non-Metacube) inputs over HTTP. Only really useful for TS.
[cubemap] / log.cpp
diff --git a/log.cpp b/log.cpp
index a4f923644a5b9597febecb038fa8c54c8526a207..409639fed7fa02e913b7024db82c6a92a8ca9f3a 100644 (file)
--- a/log.cpp
+++ b/log.cpp
@@ -1,23 +1,25 @@
-#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 <time.h>
 #include <string>
 #include <vector>
 
+#include "log.h"
+
 using namespace std;
 
 // Yes, it's a bit ugly.
 #define SYSLOG_FAKE_FILE (static_cast<FILE *>(NULL))
 
 bool logging_started = false;
-std::vector<FILE *> log_destinations;
+vector<FILE *> log_destinations;
 
-void add_log_destination_file(const std::string &filename)
+void add_log_destination_file(const string &filename)
 {
        FILE *fp = fopen(filename.c_str(), "a");
        if (fp == NULL) {
@@ -67,16 +69,22 @@ 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;
 
        switch (log_level) {
-       case NO_LEVEL:
-               log_level_str = "";
-               syslog_level = LOG_INFO;
-               break;
        case INFO:
-               log_level_str = "INFO: ";
+               log_level_str = "INFO:    ";
                syslog_level = LOG_INFO;
                break;
        case WARNING:
@@ -84,7 +92,7 @@ void log(LogLevel log_level, const char *fmt, ...)
                syslog_level = LOG_WARNING;
                break;
        case ERROR:
-               log_level_str = "ERROR: ";
+               log_level_str = "ERROR:   ";
                syslog_level = LOG_ERR;
                break;
        default:
@@ -94,7 +102,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;
        }
 
@@ -102,7 +110,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");
                        }
@@ -116,6 +124,5 @@ void log(LogLevel log_level, const char *fmt, ...)
 void log_perror(const char *msg)
 {
        char errbuf[4096];
-       strerror_r(errno, errbuf, sizeof(errbuf));
-       log(ERROR, "%s: %s", msg, errbuf);
+       log(ERROR, "%s: %s", msg, strerror_r(errno, errbuf, sizeof(errbuf)));
 }