diff options
Diffstat (limited to 'src')
-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 + |