From e9c4e1762c3b7531ffe82dc26c67919ee38bfc3a Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 12 Jan 2014 21:45:54 +0100 Subject: update Log.{h,cpp} --- src/Log.cpp | 28 ++++++++++++++++++++++++++-- src/Log.h | 17 ++++++++++------- 2 files changed, 36 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/Log.cpp b/src/Log.cpp index 7dd9d18..64e7ceb 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -23,9 +23,9 @@ */ #include +#include #include "Log.h" -#include "porting.h" Logger etiLog; @@ -36,7 +36,31 @@ void Logger::register_backend(LogBackend* backend) { } -void Logger::log(log_level_t level, std::string message) { +void Logger::log(log_level_t level, const char* fmt, ...) +{ + int size = 100; + std::string str; + va_list ap; + while (1) { + str.resize(size); + va_start(ap, fmt); + int n = vsnprintf((char *)str.c_str(), size, fmt, ap); + va_end(ap); + if (n > -1 && n < size) { + str.resize(n); + break; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + + logstr(level, str); +} + +void Logger::logstr(log_level_t level, const std::string message) +{ for (std::list::iterator it = backends.begin(); it != backends.end(); it++) { diff --git a/src/Log.h b/src/Log.h index 607735c..e07e825 100644 --- a/src/Log.h +++ b/src/Log.h @@ -29,6 +29,7 @@ # include "config.h" #endif +#include #include #include #include @@ -38,13 +39,9 @@ #include #include -#include "porting.h" - #define SYSLOG_IDENT "CRC-DABMUX" #define SYSLOG_FACILITY LOG_LOCAL0 -extern Logger etiLog; - enum log_level_t {debug = 0, info, warn, error, alert, emerg}; /** Abstract class all backends must inherit from */ @@ -132,15 +129,20 @@ class Logger { void register_backend(LogBackend* backend); /* Log the message to all backends */ - void log(log_level_t level, std::string message); + void log(log_level_t level, const char* fmt, ...); + + void logstr(log_level_t level, const std::string message); - /* Return a LogLine for the given level */ + /* Return a LogLine for the given level + * so that you can write etiLog.level(info) << "stuff = " << 21 */ LogLine level(log_level_t level); private: std::list backends; }; +extern Logger etiLog; + // Accumulate a line of logs, using same syntax as stringstream // The line is logged when the LogLine gets destroyed class LogLine { @@ -161,7 +163,7 @@ class LogLine { ~LogLine() { - logger_->log(level_, os.str()); + logger_->logstr(level_, os.str()); } private: @@ -172,3 +174,4 @@ class LogLine { #endif + -- cgit v1.2.3