summaryrefslogtreecommitdiff
path: root/Logger.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Logger.cpp')
-rw-r--r--Logger.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/Logger.cpp b/Logger.cpp
new file mode 100644
index 0000000..b0850a6
--- /dev/null
+++ b/Logger.cpp
@@ -0,0 +1,58 @@
+/**
+ * General purpose logger class.
+ *
+ * Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>
+ *
+ * This file is subject to the terms and conditions of the GNU General
+ * Public License, version 2.
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <time.h>
+
+#include "Logger.h"
+
+const char *Logger::_LOGGER_DATE_FMT = "%b %d %Y %H:%M:%S";
+
+int Logger::_log_vfprintf(FILE *f, const char *prefix, const char *fmt, va_list ap)
+{
+ struct timeval now;
+ char buf[64];
+ int ret;
+
+ if (gettimeofday(&now, NULL))
+ return -EINVAL;
+
+ strftime(buf, sizeof(buf), _LOGGER_DATE_FMT, localtime(&now.tv_sec));
+ ret = fprintf(f, "[%s.%03lu] %s%s", buf, now.tv_usec / 1000,
+ prefix ? prefix : "", prefix ? ": " : "");
+
+ return vfprintf(f, fmt, ap);
+}
+
+int Logger::log(const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, fmt);
+ ret = _log_vfprintf(_f_out, NULL, fmt, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+int Logger::err(const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, fmt);
+ ret = _log_vfprintf(_f_err, "Error", fmt, ap);
+ va_end(ap);
+
+ return ret;
+}