From bab9d654f028a214e6f3abe8ade1c1da002d25ff Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 17 Jun 2016 11:19:29 +0200 Subject: Add TRACE support for timing analysis --- src/Log.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'src/Log.cpp') 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 #include +#include #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 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(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(now).count(); + + fprintf(m_trace_file, "%ld,%s\n", + micros - m_trace_micros_startup, + message.c_str()); + } +} -- cgit v1.2.3