summaryrefslogtreecommitdiffstats
path: root/src/Core
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-09-30 22:28:33 +0200
committerMatthias Schiffer <matthias@gamezock.de>2009-09-30 22:28:33 +0200
commit1a43fad56dc945c72a4ab711d2c9ff6d1a5a1502 (patch)
treee2b6847ce5e6a6d41269f1d2fa30011060abcd08 /src/Core
parent241f1947580df7f905ed3d6969cc3a6a4bb99f5a (diff)
downloadmad-1a43fad56dc945c72a4ab711d2c9ff6d1a5a1502.tar
mad-1a43fad56dc945c72a4ab711d2c9ff6d1a5a1502.zip
Added Format class for improved logging experience :)
Diffstat (limited to 'src/Core')
-rw-r--r--src/Core/Application.cpp65
-rw-r--r--src/Core/Application.h29
-rw-r--r--src/Core/CMakeLists.txt1
-rw-r--r--src/Core/Exception.h6
-rw-r--r--src/Core/Format.h72
-rw-r--r--src/Core/LogManager.cpp2
6 files changed, 97 insertions, 78 deletions
diff --git a/src/Core/Application.cpp b/src/Core/Application.cpp
index 86b5b7b..83ef3a7 100644
--- a/src/Core/Application.cpp
+++ b/src/Core/Application.cpp
@@ -23,11 +23,7 @@
#include "ThreadManager.h"
#include <clocale>
-#include <cstdlib>
-
-#ifndef va_copy
-# define va_copy(d, s) (d) = (s)
-#endif
+#include <boost/date_time/microsec_time_clock.hpp>
namespace Mad {
namespace Core {
@@ -46,66 +42,9 @@ Application::~Application() {
delete configManager;
}
-
-void Application::logfv(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &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 = vsnprintf(buf, size, format.toLocale().c_str(), ap2);
- va_end(ap2);
-
- if(n > -1 && n < size) {
- logManager->log(category, level, boost::posix_time::microsec_clock::universal_time(), Core::String::fromLocale(buf));
- std::free(buf);
- return;
- }
-
- if(n > -1)
- size = n+1;
- else
- size *= 2;
-
- buf = (char*)std::realloc(buf, size);
- }
-
-}
-
-void Application::log(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &message) {
+void Application::doLog(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &message) {
logManager->log(category, level, boost::posix_time::microsec_clock::universal_time(), message);
}
-void Application::logf(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &format, ...) {
- va_list ap;
- va_start(ap, format);
- logfv(category, level, format, ap);
- va_end(ap);
-}
-
-void Application::logf(Logger::MessageCategory category, const Core::String &format, ...) {
- va_list ap;
- va_start(ap, format);
- logfv(category, Logger::LOG_DEFAULT, format, ap);
- va_end(ap);
-}
-
-void Application::logf(Logger::MessageLevel level, const Core::String &format, ...) {
- va_list ap;
- va_start(ap, format);
- logfv(Logger::LOG_GENERAL, level, format, ap);
- va_end(ap);
-}
-
-void Application::logf(const Core::String &format, ...) {
- va_list ap;
- va_start(ap, format);
- logfv(Logger::LOG_GENERAL, Logger::LOG_DEFAULT, format, ap);
- va_end(ap);
-}
-
}
}
diff --git a/src/Core/Application.h b/src/Core/Application.h
index ffab639..7f7ec76 100644
--- a/src/Core/Application.h
+++ b/src/Core/Application.h
@@ -22,11 +22,10 @@
#include "export.h"
+#include "Format.h"
#include "Logger.h"
-#include <cstdarg>
-#include <string>
-
+#include <sstream>
#include <boost/asio/io_service.hpp>
#include <boost/noncopyable.hpp>
@@ -45,7 +44,7 @@ class MAD_CORE_EXPORT Application : private boost::noncopyable {
LogManager *logManager;
ThreadManager *threadManager;
- void logfv(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &format, va_list ap);
+ void doLog(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &message);
protected:
Application();
@@ -69,25 +68,27 @@ class MAD_CORE_EXPORT Application : private boost::noncopyable {
}
- void log(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &message);
+ template<typename T>
+ void log(Logger::MessageCategory category, Logger::MessageLevel level, const T &message) {
+ std::ostringstream stream;
+ stream << message;
+ doLog(category, level, String::fromLocale(stream.str()));
+ }
- void log(Logger::MessageCategory category, const Core::String &message) {
+ template<typename T>
+ void log(Logger::MessageCategory category, const T &message) {
log(category, Logger::LOG_DEFAULT, message);
}
- void log(Logger::MessageLevel level, const Core::String &message) {
+ template<typename T>
+ void log(Logger::MessageLevel level, const T &message) {
log(Logger::LOG_GENERAL, level, message);
}
- void log(const Core::String &message) {
+ template<typename T>
+ void log(const T &message) {
log(Logger::LOG_GENERAL, Logger::LOG_DEFAULT, message);
}
-
-
- void logf(Logger::MessageCategory category, Logger::MessageLevel level, const Core::String &format, ...);
- void logf(Logger::MessageCategory category, const Core::String &format, ...);
- void logf(Logger::MessageLevel level, const Core::String &format, ...);
- void logf(const Core::String &format, ...);
};
}
diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt
index 60a5a25..9b458c5 100644
--- a/src/Core/CMakeLists.txt
+++ b/src/Core/CMakeLists.txt
@@ -16,6 +16,7 @@ mad_library(Core
ConfigManager.cpp ConfigManager.h
Configurable.h
Exception.cpp Exception.h
+ Format.h
Logger.h
LogManager.cpp LogManager.h
Signals.h
diff --git a/src/Core/Exception.h b/src/Core/Exception.h
index 48688a4..1d8eb40 100644
--- a/src/Core/Exception.h
+++ b/src/Core/Exception.h
@@ -82,6 +82,12 @@ class MAD_CORE_EXPORT Exception : public std::exception {
}
};
+template<typename ElemType, typename Traits>
+std::basic_ostream<ElemType, Traits>& operator<<(std::basic_ostream<ElemType, Traits> &stream, const Exception &exception) {
+ stream << exception.toString();
+ return stream;
+}
+
}
}
diff --git a/src/Core/Format.h b/src/Core/Format.h
new file mode 100644
index 0000000..bd57af5
--- /dev/null
+++ b/src/Core/Format.h
@@ -0,0 +1,72 @@
+/*
+ * Format.h
+ *
+ * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAD_CORE_FORMAT_H_
+#define MAD_CORE_FORMAT_H_
+
+#include "String.h"
+
+#include <boost/format.hpp>
+
+namespace Mad {
+namespace Core {
+
+class Format {
+ private:
+ boost::format format;
+
+ public:
+ Format(const String &formatString) : format(formatString.toLocale()) {}
+
+ void clear() {
+ format.clear();
+ }
+
+ Format& parse(const String &formatString) {
+ format.parse(formatString.toLocale());
+ return *this;
+ }
+
+ String str() const {
+ return String::fromLocale(format.str());
+ }
+
+ template<class T>
+ Format& operator%(T &x) {
+ format % x;
+ return *this;
+ }
+
+ template<class T>
+ Format& operator%(const T &x) {
+ format % x;
+ return *this;
+ }
+};
+
+template<typename ElemType, typename Traits>
+std::basic_ostream<ElemType, Traits>& operator<<(std::basic_ostream<ElemType, Traits> &stream, const Format &format) {
+ stream << format.str();
+ return stream;
+}
+
+}
+}
+
+#endif /* MAD_CORE_FORMAT_H_ */
diff --git a/src/Core/LogManager.cpp b/src/Core/LogManager.cpp
index 955ac76..7aac571 100644
--- a/src/Core/LogManager.cpp
+++ b/src/Core/LogManager.cpp
@@ -105,7 +105,7 @@ void LogManager::configure() {
consoleLogger->setLevel(parseLevel(level));
}
catch(Core::Exception e) {
- application->logf(Logger::LOG_WARNING, "Unknown log level '%s'.", level.toLocale().c_str());
+ application->log(Logger::LOG_WARNING, Format("Unknown log level '%1%'.") % level);
}
}
}