diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2014-01-12 21:45:54 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2014-01-12 21:45:54 +0100 |
commit | e9c4e1762c3b7531ffe82dc26c67919ee38bfc3a (patch) | |
tree | ab1d49f6bd9538a2dba69dda98a00fcac5ab8953 | |
parent | 1ee5bb5d436067d979fd95f98ed36addcd2efaed (diff) | |
download | dabmux-e9c4e1762c3b7531ffe82dc26c67919ee38bfc3a.tar.gz dabmux-e9c4e1762c3b7531ffe82dc26c67919ee38bfc3a.tar.bz2 dabmux-e9c4e1762c3b7531ffe82dc26c67919ee38bfc3a.zip |
update Log.{h,cpp}
-rw-r--r-- | src/Log.cpp | 28 | ||||
-rw-r--r-- | src/Log.h | 17 |
2 files changed, 36 insertions, 9 deletions
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 <list> +#include <stdarg.h> #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<LogBackend*>::iterator it = backends.begin(); it != backends.end(); it++) { @@ -29,6 +29,7 @@ # include "config.h" #endif +#include <stdarg.h> #include <stdio.h> #include <syslog.h> #include <fstream> @@ -38,13 +39,9 @@ #include <stdexcept> #include <string> -#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<LogBackend*> 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 + |