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 | |
parent | 28c99a37d09a68ef993330b9ece2732ec03eba8f (diff) | |
download | swift-b3a803ed132b5f1b7a21130c5466a8deb106d193.zip swift-b3a803ed132b5f1b7a21130c5466a8deb106d193.tar.bz2 |
Make logging thread-safe.
Change-Id: Ifab368474bd9e42e10f2cb0c29ff696c0aeaf3ea
-rw-r--r-- | QA/Checker/checker.cpp | 4 | ||||
-rw-r--r-- | Swift/Controllers/Chat/ChatController.cpp | 2 | ||||
-rw-r--r-- | Swift/QtUI/QtSwift.cpp | 2 | ||||
-rw-r--r-- | Swiften/Base/Log.cpp | 34 | ||||
-rw-r--r-- | Swiften/Base/Log.h | 36 | ||||
-rw-r--r-- | Swiften/Examples/SendFile/ReceiveFile.cpp | 2 | ||||
-rw-r--r-- | Swiften/Examples/SendFile/SendFile.cpp | 2 |
7 files changed, 64 insertions, 18 deletions
diff --git a/QA/Checker/checker.cpp b/QA/Checker/checker.cpp index 12237dc..1ec8892 100644 --- a/QA/Checker/checker.cpp +++ b/QA/Checker/checker.cpp @@ -19,6 +19,8 @@ int main(int argc, char* argv[]) { bool verbose = false; bool outputXML = false; + Swift::Log::setLogLevel(Swift::Log::error); + // Parse parameters std::vector<std::string> testsToRun; for (int i = 1; i < argc; ++i) { @@ -30,7 +32,7 @@ int main(int argc, char* argv[]) { outputXML = true; } else if (param == "--debug") { - Swift::logging = true; + Swift::Log::setLogLevel(Swift::Log::debug); } else { testsToRun.push_back(param); diff --git a/Swift/Controllers/Chat/ChatController.cpp b/Swift/Controllers/Chat/ChatController.cpp index 0ffef0c..edd2e3b 100644 --- a/Swift/Controllers/Chat/ChatController.cpp +++ b/Swift/Controllers/Chat/ChatController.cpp @@ -291,7 +291,7 @@ void ChatController::handleFileTransferStart(std::string id, std::string descrip } void ChatController::handleFileTransferAccept(std::string id, std::string filename) { - SWIFT_LOG(debug) "handleFileTransferAccept(" << id << ", " << filename << ")" << std::endl; + SWIFT_LOG(debug) << "handleFileTransferAccept(" << id << ", " << filename << ")" << std::endl; if (ftControllers.find(id) != ftControllers.end()) { ftControllers[id]->accept(filename); } else { diff --git a/Swift/QtUI/QtSwift.cpp b/Swift/QtUI/QtSwift.cpp index 4d10956..515c83f 100644 --- a/Swift/QtUI/QtSwift.cpp +++ b/Swift/QtUI/QtSwift.cpp @@ -153,7 +153,7 @@ QtSwift::QtSwift(const po::variables_map& options) : networkFactories_(&clientMa } if (options.count("debug")) { - Swift::logging = true; + Log::setLogLevel(Swift::Log::debug); } tabs_ = options.count("no-tabs") && !splitter_ ? NULL : new QtChatTabs(); 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__) diff --git a/Swiften/Examples/SendFile/ReceiveFile.cpp b/Swiften/Examples/SendFile/ReceiveFile.cpp index 8b0af37..7b361ef 100644 --- a/Swiften/Examples/SendFile/ReceiveFile.cpp +++ b/Swiften/Examples/SendFile/ReceiveFile.cpp @@ -65,7 +65,7 @@ class FileReceiver { private: void handleConnected() { - Swift::logging = true; + Log::setLogLevel(Log::debug); client->getFileTransferManager()->startListeningOnPort(9999); client->getFileTransferManager()->onIncomingFileTransfer.connect(boost::bind(&FileReceiver::handleIncomingFileTransfer, this, _1)); diff --git a/Swiften/Examples/SendFile/SendFile.cpp b/Swiften/Examples/SendFile/SendFile.cpp index 657949b..39a8603 100644 --- a/Swiften/Examples/SendFile/SendFile.cpp +++ b/Swiften/Examples/SendFile/SendFile.cpp @@ -144,7 +144,7 @@ int main(int argc, char* argv[]) { JID sender(argv[1]); JID recipient(argv[3]); - Swift::logging = true; + Log::setLogLevel(Log::debug); FileSender fileSender(sender, std::string(argv[2]), recipient, boost::filesystem::path(argv[4])); fileSender.start(); { |