diff options
author | Remko Tronçon <git@el-tramo.be> | 2013-02-03 18:21:12 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2013-02-05 20:26:22 (GMT) |
commit | b3a803ed132b5f1b7a21130c5466a8deb106d193 (patch) | |
tree | bd50171aa0384f9308efd920b720b83226c6d5f9 /Swiften/Base | |
parent | 28c99a37d09a68ef993330b9ece2732ec03eba8f (diff) | |
download | swift-b3a803ed132b5f1b7a21130c5466a8deb106d193.zip swift-b3a803ed132b5f1b7a21130c5466a8deb106d193.tar.bz2 |
Make logging thread-safe.
Change-Id: Ifab368474bd9e42e10f2cb0c29ff696c0aeaf3ea
Diffstat (limited to 'Swiften/Base')
-rw-r--r-- | Swiften/Base/Log.cpp | 34 | ||||
-rw-r--r-- | Swiften/Base/Log.h | 36 |
2 files changed, 57 insertions, 13 deletions
diff --git a/Swiften/Base/Log.cpp b/Swiften/Base/Log.cpp index 4132353..317798c 100644 --- a/Swiften/Base/Log.cpp +++ b/Swiften/Base/Log.cpp @@ -1,13 +1,43 @@ /* - * Copyright (c) 2010 Remko Tronçon + * Copyright (c) 2010-2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include <Swiften/Base/Log.h> +#include <cstdio> + + namespace Swift { -bool logging = false; +static Log::Severity logLevel = Log::warning; + +Log::Log() { +} + +Log::~Log() { + // Using stdio for thread safety (POSIX file i/o calls are guaranteed to be atomic) + fprintf(stderr, "%s", stream.str().c_str()); + fflush(stderr); +} + +std::ostringstream& Log::getStream( + Severity /*severity*/, + const std::string& severityString, + const std::string& file, + int line, + const std::string& function) { + stream << "[" << severityString << "] " << file << ":" << line << " " << function << ": "; + return stream; +} + +Log::Severity Log::getLogLevel() { + return logLevel; +} + +void Log::setLogLevel(Severity level) { + logLevel = level; +} } diff --git a/Swiften/Base/Log.h b/Swiften/Base/Log.h index 6d76dc6..f8d524b 100644 --- a/Swiften/Base/Log.h +++ b/Swiften/Base/Log.h @@ -1,26 +1,40 @@ /* - * Copyright (c) 2010-2012 Remko Tronçon + * Copyright (c) 2010-2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #pragma once -#include <iostream> +#include <sstream> #include <Swiften/Base/API.h> namespace Swift { - extern SWIFTEN_API bool logging; - namespace LogDetail { - // Only here to be able to statically check the correctness of the severity levers - namespace Severity { - enum { - debug, info, warning, error + class SWIFTEN_API Log { + public: + enum Severity { + error, warning, info, debug }; - } - } + + Log(); + ~Log(); + + std::ostringstream& getStream( + Severity severity, + const std::string& severityString, + const std::string& file, + int line, + const std::string& function); + + static Severity getLogLevel(); + static void setLogLevel(Severity level); + + private: + std::ostringstream stream; + }; } #define SWIFT_LOG(severity) \ - if (!Swift::logging) {} else (void) LogDetail::Severity::severity, std::cerr << "[" << #severity << "] " << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << ": " + if (Log::severity > Log::getLogLevel()) ; \ + else Log().getStream(Log::severity, #severity, __FILE__, __LINE__, __FUNCTION__) |