summaryrefslogtreecommitdiffstats
path: root/src/Common
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common')
-rw-r--r--src/Common/Exception.cpp2
-rw-r--r--src/Common/Logger.cpp46
-rw-r--r--src/Common/Logger.h6
3 files changed, 53 insertions, 1 deletions
diff --git a/src/Common/Exception.cpp b/src/Common/Exception.cpp
index dc04f7d..9366fe7 100644
--- a/src/Common/Exception.cpp
+++ b/src/Common/Exception.cpp
@@ -43,7 +43,7 @@ std::string Exception::strerror() const {
case NOT_FINISHED:
return ret + "Not finished";
case INTERNAL_ERRNO:
- return ret + "Internal error: " + std::strerror(subCode);
+ return ret + std::strerror(subCode);
case INTERNAL_GNUTLS:
return ret + "GnuTLS error: " + gnutls_strerror(subCode);
case INVALID_ADDRESS:
diff --git a/src/Common/Logger.cpp b/src/Common/Logger.cpp
index e479979..22748d1 100644
--- a/src/Common/Logger.cpp
+++ b/src/Common/Logger.cpp
@@ -18,12 +18,42 @@
*/
#include "Logger.h"
+#include <cstdlib>
namespace Mad {
namespace Common {
std::list<Logger*> Logger::loggers;
+
+void Logger::logfv(MessageLevel level, const char *format, va_list ap) {
+ int size = 100;
+ char *buf = (char*)std::malloc(size);
+
+ // If buffer is too small, try again with bigger buffer
+ while(true) {
+ va_list ap2;
+
+ va_copy(ap2, ap);
+ int n = std::vsnprintf(buf, size, format, ap2);
+ va_end(ap2);
+
+ if(n > -1 && n < size) {
+ log(level, buf);
+ std::free(buf);
+ return;
+ }
+
+ if(n > -1)
+ size = n+1;
+ else
+ size *= 2;
+
+ buf = (char*)std::realloc(buf, size);
+ }
+
+}
+
void Logger::log(MessageLevel level, const std::string &message) {
for(std::list<Logger*>::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) {
if((*logger)->getLevel() >= level)
@@ -31,5 +61,21 @@ void Logger::log(MessageLevel level, const std::string &message) {
}
}
+void Logger::logf(MessageLevel level, const char *format, ...) {
+ va_list ap;
+
+ va_start(ap, format);
+ logfv(level, format, ap);
+ va_end(ap);
+}
+
+void Logger::logf(const char *format, ...) {
+ va_list ap;
+
+ va_start(ap, format);
+ logfv(DEFAULT, format, ap);
+ va_end(ap);
+}
+
}
}
diff --git a/src/Common/Logger.h b/src/Common/Logger.h
index 39ca5dd..f1cd33d 100644
--- a/src/Common/Logger.h
+++ b/src/Common/Logger.h
@@ -21,6 +21,7 @@
#define MAD_COMMON_LOGGER_H_
#include <algorithm>
+#include <cstdarg>
#include <list>
#include <string>
@@ -37,6 +38,8 @@ class Logger {
static std::list<Logger*> loggers;
MessageLevel level;
+ static void logfv(MessageLevel level, const char *format, va_list ap);
+
protected:
Logger() : level(DEFAULT) {}
@@ -48,6 +51,9 @@ class Logger {
log(DEFAULT, message);
}
+ static void logf(MessageLevel level, const char *format, ...);
+ static void logf(const char *format, ...);
+
static void registerLogger(Logger *logger) {
loggers.push_back(logger);
}