summaryrefslogtreecommitdiffstats
path: root/src/Log.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Log.cpp')
-rw-r--r--src/Log.cpp48
1 files changed, 46 insertions, 2 deletions
diff --git a/src/Log.cpp b/src/Log.cpp
index 6bbc7a9..7d6d6f5 100644
--- a/src/Log.cpp
+++ b/src/Log.cpp
@@ -24,9 +24,12 @@
#include <list>
#include <stdarg.h>
+#include <chrono>
#include "Log.h"
+using namespace std;
+
Logger etiLog;
@@ -72,15 +75,56 @@ void Logger::logstr(log_level_t level, std::string message)
backend->log(level, message);
}
- {
+ if (level != log_level_t::trace) {
std::lock_guard<std::mutex> guard(m_cerr_mutex);
std::cerr << levels_as_str[level] << " " << message << std::endl;
}
}
-
LogLine Logger::level(log_level_t level)
{
return LogLine(this, level);
}
+void LogToFile::log(log_level_t level, std::string message)
+{
+ if (level != log_level_t::trace) {
+ const char* log_level_text[] = {
+ "DEBUG", "INFO", "WARN", "ERROR", "ALERT", "EMERG"};
+
+ // fprintf is thread-safe
+ fprintf(log_file, SYSLOG_IDENT ": %s: %s\n",
+ log_level_text[(size_t)level], message.c_str());
+ fflush(log_file);
+ }
+}
+
+LogTracer::LogTracer(const string& trace_filename)
+{
+ name = "TRACE";
+ etiLog.level(info) << "Setting up TRACE to " << trace_filename;
+
+ m_trace_file = fopen(trace_filename.c_str(), "a");
+ if (m_trace_file == NULL) {
+ fprintf(stderr, "Cannot open trace file !");
+ throw std::runtime_error("Cannot open trace file !");
+ }
+
+ auto now = chrono::steady_clock::now().time_since_epoch();
+ m_trace_micros_startup =
+ chrono::duration_cast<chrono::microseconds>(now).count();
+
+ fprintf(m_trace_file, "0,TRACER,startup at %ld\n", m_trace_micros_startup);
+}
+
+void LogTracer::log(log_level_t level, std::string message)
+{
+ if (level == log_level_t::trace) {
+ const auto now = chrono::steady_clock::now().time_since_epoch();
+ const auto micros = chrono::duration_cast<chrono::microseconds>(now).count();
+
+ fprintf(m_trace_file, "%ld,%s\n",
+ micros - m_trace_micros_startup,
+ message.c_str());
+ }
+}