diff options
-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 @@ -103,2 +103,3 @@ po::options_description QtSwift::getOptionsDescription() { #endif + ("logfile", po::value<std::string>()->implicit_value(""), "Save all logging information to a file") ; @@ -191,2 +192,12 @@ QtSwift::QtSwift(const po::variables_map& options) : networkFactories_(&clientMa + 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 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 @@ -18,2 +18,3 @@ namespace Swift { static Log::Severity logLevel = Log::warning; +std::unique_ptr<FILE, Log::LogFileClose> Log::logfile; @@ -27,4 +28,10 @@ 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); + 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 @@ -50,2 +57,8 @@ void Log::setLogLevel(Severity 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 @@ -8,2 +8,4 @@ +#include <cstdio> +#include <memory> #include <sstream> @@ -31,5 +33,14 @@ namespace Swift { 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; }; |