summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2013-02-03 18:21:12 (GMT)
committerRemko Tronçon <git@el-tramo.be>2013-02-05 20:26:22 (GMT)
commitb3a803ed132b5f1b7a21130c5466a8deb106d193 (patch)
treebd50171aa0384f9308efd920b720b83226c6d5f9 /Swiften
parent28c99a37d09a68ef993330b9ece2732ec03eba8f (diff)
downloadswift-b3a803ed132b5f1b7a21130c5466a8deb106d193.zip
swift-b3a803ed132b5f1b7a21130c5466a8deb106d193.tar.bz2
Make logging thread-safe.
Change-Id: Ifab368474bd9e42e10f2cb0c29ff696c0aeaf3ea
Diffstat (limited to 'Swiften')
-rw-r--r--Swiften/Base/Log.cpp34
-rw-r--r--Swiften/Base/Log.h36
-rw-r--r--Swiften/Examples/SendFile/ReceiveFile.cpp2
-rw-r--r--Swiften/Examples/SendFile/SendFile.cpp2
4 files changed, 59 insertions, 15 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__)
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();
{