summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2018-03-20 13:12:10 (GMT)
committerTobias Markmann <tm@ayena.de>2018-03-20 14:48:04 (GMT)
commitf2c2c7d035029fb9615b42c18ccea83e8e705b10 (patch)
tree2f56fb77de0f366528395f21732d418f016f63b5 /Swiften/Base
parent44581c5285d13c0ec715b35ddc79177e5ebeef39 (diff)
parent5ba3f18ad8efa040d49f36d83ec2e7891a9add9f (diff)
downloadswift-f2c2c7d035029fb9615b42c18ccea83e8e705b10.zip
swift-f2c2c7d035029fb9615b42c18ccea83e8e705b10.tar.bz2
Merge branch 'swift-4.x'swift-5.0alpha2
* swift-4.x: (44 commits) Test-Information: Builds on macOS 10.13.3 with clang trunk. Change-Id: If50381f103b0ad18d038b920d3d43537642141cb
Diffstat (limited to 'Swiften/Base')
-rw-r--r--Swiften/Base/Log.cpp17
-rw-r--r--Swiften/Base/Log.h11
2 files changed, 26 insertions, 2 deletions
diff --git a/Swiften/Base/Log.cpp b/Swiften/Base/Log.cpp
index 0efac7e..9b16531 100644
--- a/Swiften/Base/Log.cpp
+++ b/Swiften/Base/Log.cpp
@@ -16,6 +16,7 @@
namespace Swift {
static Log::Severity logLevel = Log::warning;
+std::unique_ptr<FILE, Log::LogFileClose> Log::logfile;
Log::Log() {
}
@@ -25,8 +26,14 @@ Log::~Log() {
__android_log_print(ANDROID_LOG_VERBOSE, "Swift", stream.str().c_str(), 1);
#else
// Using stdio for thread safety (POSIX file i/o calls are guaranteed to be atomic)
- fprintf(stderr, "%s", stream.str().c_str());
- fflush(stderr);
+ if (logfile) {
+ fwrite(stream.str().c_str(), sizeof(char), stream.str().size(), logfile.get());
+ fflush(logfile.get());
+ }
+ else {
+ fwrite(stream.str().c_str(), sizeof(char), stream.str().size(), stderr);
+ fflush(stderr);
+ }
#endif
}
@@ -48,4 +55,10 @@ void Log::setLogLevel(Severity level) {
logLevel = level;
}
+void Log::setLogFile(const std::string& fileName) {
+ if (!fileName.empty()) {
+ logfile = std::unique_ptr<FILE, Log::LogFileClose>(fopen(fileName.c_str(), "a"));
+ }
+}
+
}
diff --git a/Swiften/Base/Log.h b/Swiften/Base/Log.h
index 33c969d..e3e04a5 100644
--- a/Swiften/Base/Log.h
+++ b/Swiften/Base/Log.h
@@ -6,6 +6,8 @@
#pragma once
+#include <cstdio>
+#include <memory>
#include <sstream>
#include <Swiften/Base/API.h>
@@ -29,9 +31,18 @@ namespace Swift {
static Severity getLogLevel();
static void setLogLevel(Severity level);
+ static void setLogFile(const std::string& fileName);
private:
+ struct LogFileClose {
+ void operator()(FILE* p) {
+ if (p) {
+ fclose(p);
+ }
+ }
+ };
std::ostringstream stream;
+ static std::unique_ptr<FILE, LogFileClose> logfile;
};
}