diff options
author | Thanos Doukoudakis <thanos.doukoudakis@isode.com> | 2017-08-02 14:30:41 (GMT) |
---|---|---|
committer | Thanos Doukoudakis <thanos.doukoudakis@isode.com> | 2017-08-08 07:56:55 (GMT) |
commit | c5aa27ee869cc4859e494bde8ddf7da60e177f40 (patch) | |
tree | 2305ca850082c4d0565f293afced4bc47be40b64 | |
parent | bc58d59636f28909d90464cea7ebf5eeb8f230be (diff) | |
download | swift-c5aa27ee869cc4859e494bde8ddf7da60e177f40.zip swift-c5aa27ee869cc4859e494bde8ddf7da60e177f40.tar.bz2 |
Enable log information export to a file
This patch introduces the “logfile” argument to the client, which can be
used to specify a file path for a log file, where all logging information
will be stored.
Test-Information:
Tested on windows 10 and Ubuntu 17.04
Change-Id: I6a2f14585a72f25e7e78d79cb633e1ddc4d43c3b
-rw-r--r-- | Swift/QtUI/QtSwift.cpp | 11 | ||||
-rw-r--r-- | Swiften/Base/Log.cpp | 17 | ||||
-rw-r--r-- | Swiften/Base/Log.h | 11 |
3 files changed, 37 insertions, 2 deletions
diff --git a/Swift/QtUI/QtSwift.cpp b/Swift/QtUI/QtSwift.cpp index 67fc659..7daa5ce 100644 --- a/Swift/QtUI/QtSwift.cpp +++ b/Swift/QtUI/QtSwift.cpp @@ -101,6 +101,7 @@ po::options_description QtSwift::getOptionsDescription() { #if QT_VERSION >= 0x040800 ("language", po::value<std::string>(), "Use a specific language, instead of the system-wide one") #endif + ("logfile", po::value<std::string>()->implicit_value(""), "Save all logging information to a file") ; return result; } @@ -189,6 +190,16 @@ QtSwift::QtSwift(const po::variables_map& options) : networkFactories_(&clientMa Log::setLogLevel(Swift::Log::debug); } + if (options.count("logfile")) { + try { + std::string fileName = options["logfile"].as<std::string>(); + Log::setLogFile(fileName); + } + catch (...) { + SWIFT_LOG(error) << "Error while retrieving the specified log file name from the command line" << std::endl; + } + } + // Load fonts std::vector<std::string> fontNames = { "themes/Default/Lato2OFL/Lato-Black.ttf", 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; }; } |