From 1a43fad56dc945c72a4ab711d2c9ff6d1a5a1502 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 30 Sep 2009 22:28:33 +0200 Subject: Added Format class for improved logging experience :) --- src/Core/Application.cpp | 65 ++----------------------------------------- src/Core/Application.h | 29 +++++++++---------- src/Core/CMakeLists.txt | 1 + src/Core/Exception.h | 6 ++++ src/Core/Format.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Core/LogManager.cpp | 2 +- 6 files changed, 97 insertions(+), 78 deletions(-) create mode 100644 src/Core/Format.h (limited to 'src/Core') 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 -#include - -#ifndef va_copy -# define va_copy(d, s) (d) = (s) -#endif +#include 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 -#include - +#include #include #include @@ -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 + 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 + void log(Logger::MessageCategory category, const T &message) { log(category, Logger::LOG_DEFAULT, message); } - void log(Logger::MessageLevel level, const Core::String &message) { + template + void log(Logger::MessageLevel level, const T &message) { log(Logger::LOG_GENERAL, level, message); } - void log(const Core::String &message) { + template + 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 +std::basic_ostream& operator<<(std::basic_ostream &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 + * + * 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 . + */ + +#ifndef MAD_CORE_FORMAT_H_ +#define MAD_CORE_FORMAT_H_ + +#include "String.h" + +#include + +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 + Format& operator%(T &x) { + format % x; + return *this; + } + + template + Format& operator%(const T &x) { + format % x; + return *this; + } +}; + +template +std::basic_ostream& operator<<(std::basic_ostream &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); } } } -- cgit v1.2.3