diff options
| author | Edwin Mons <edwin.mons@isode.com> | 2019-11-13 12:03:22 (GMT) |
|---|---|---|
| committer | Edwin Mons <edwin.mons@isode.com> | 2019-11-13 12:46:21 (GMT) |
| commit | d640ec248ca8bf86a03007a0f8df352df696cf92 (patch) | |
| tree | 049bda0ad8e3fe50eda047b5e8086fd903cb5afe | |
| parent | 959a42d21fd70ea002da9afa7482194e8b6097e1 (diff) | |
| download | swift-d640ec248ca8bf86a03007a0f8df352df696cf92.zip swift-d640ec248ca8bf86a03007a0f8df352df696cf92.tar.bz2 | |
Support application-supplied logging
This adds a method to set a logging callback. If such a callback is
set, all SWIFT_LOG calls will invoke this callback instead of writing to
either stderr or the swift logging file.
Test-Information:
Updated unit tests pass.
Checked that logs generated by Swift and Sluift (which do not set the
callback) resulted in logging in the expected location.
Change-Id: I0eb2a1057aa77839e1b8d5f320205eb9d5fdc253
| -rw-r--r-- | Swiften/Base/Log.cpp | 28 | ||||
| -rw-r--r-- | Swiften/Base/Log.h | 14 | ||||
| -rw-r--r-- | Swiften/Base/UnitTest/LogTest.cpp | 49 | ||||
| -rw-r--r-- | Swiften/SConscript | 1 |
4 files changed, 83 insertions, 9 deletions
diff --git a/Swiften/Base/Log.cpp b/Swiften/Base/Log.cpp index 9b16531..abfd2bc 100644 --- a/Swiften/Base/Log.cpp +++ b/Swiften/Base/Log.cpp | |||
| @@ -17,6 +17,7 @@ namespace Swift { | |||
| 17 | 17 | ||
| 18 | static Log::Severity logLevel = Log::warning; | 18 | static Log::Severity logLevel = Log::warning; |
| 19 | std::unique_ptr<FILE, Log::LogFileClose> Log::logfile; | 19 | std::unique_ptr<FILE, Log::LogFileClose> Log::logfile; |
| 20 | Log::Callback Log::logCallback; | ||
| 20 | 21 | ||
| 21 | Log::Log() { | 22 | Log::Log() { |
| 22 | } | 23 | } |
| @@ -26,7 +27,10 @@ Log::~Log() { | |||
| 26 | __android_log_print(ANDROID_LOG_VERBOSE, "Swift", stream.str().c_str(), 1); | 27 | __android_log_print(ANDROID_LOG_VERBOSE, "Swift", stream.str().c_str(), 1); |
| 27 | #else | 28 | #else |
| 28 | // Using stdio for thread safety (POSIX file i/o calls are guaranteed to be atomic) | 29 | // Using stdio for thread safety (POSIX file i/o calls are guaranteed to be atomic) |
| 29 | if (logfile) { | 30 | if (logCallback) { |
| 31 | logCallback(severity_, std::move(file_), line_, std::move(function_), stream.str()); | ||
| 32 | } | ||
| 33 | else if (logfile) { | ||
| 30 | fwrite(stream.str().c_str(), sizeof(char), stream.str().size(), logfile.get()); | 34 | fwrite(stream.str().c_str(), sizeof(char), stream.str().size(), logfile.get()); |
| 31 | fflush(logfile.get()); | 35 | fflush(logfile.get()); |
| 32 | } | 36 | } |
| @@ -38,12 +42,20 @@ Log::~Log() { | |||
| 38 | } | 42 | } |
| 39 | 43 | ||
| 40 | std::ostringstream& Log::getStream( | 44 | std::ostringstream& Log::getStream( |
| 41 | Severity /*severity*/, | 45 | Severity severity, |
| 42 | const std::string& severityString, | 46 | std::string severityString, |
| 43 | const std::string& file, | 47 | std::string file, |
| 44 | int line, | 48 | int line, |
| 45 | const std::string& function) { | 49 | std::string function) { |
| 46 | stream << "[" << severityString << "] " << file << ":" << line << " " << function << ": "; | 50 | if (logCallback) { |
| 51 | severity_ = severity; | ||
| 52 | file_ = std::move(file); | ||
| 53 | line_ = line; | ||
| 54 | function_ = std::move(function); | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | stream << "[" << severityString << "] " << file << ":" << line << " " << function << ": "; | ||
| 58 | } | ||
| 47 | return stream; | 59 | return stream; |
| 48 | } | 60 | } |
| 49 | 61 | ||
| @@ -61,4 +73,8 @@ void Log::setLogFile(const std::string& fileName) { | |||
| 61 | } | 73 | } |
| 62 | } | 74 | } |
| 63 | 75 | ||
| 76 | void Log::setLogCallback(Callback callback) { | ||
| 77 | Log::logCallback = callback; | ||
| 78 | } | ||
| 79 | |||
| 64 | } | 80 | } |
diff --git a/Swiften/Base/Log.h b/Swiften/Base/Log.h index e3e04a5..255e478 100644 --- a/Swiften/Base/Log.h +++ b/Swiften/Base/Log.h | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | #pragma once | 7 | #pragma once |
| 8 | 8 | ||
| 9 | #include <cstdio> | 9 | #include <cstdio> |
| 10 | #include <functional> | ||
| 10 | #include <memory> | 11 | #include <memory> |
| 11 | #include <sstream> | 12 | #include <sstream> |
| 12 | 13 | ||
| @@ -18,20 +19,22 @@ namespace Swift { | |||
| 18 | enum Severity { | 19 | enum Severity { |
| 19 | error, warning, info, debug | 20 | error, warning, info, debug |
| 20 | }; | 21 | }; |
| 22 | using Callback = std::function<void(Severity severity, std::string file, int line, std::string function, std::string message)>; | ||
| 21 | 23 | ||
| 22 | Log(); | 24 | Log(); |
| 23 | ~Log(); | 25 | ~Log(); |
| 24 | 26 | ||
| 25 | std::ostringstream& getStream( | 27 | std::ostringstream& getStream( |
| 26 | Severity severity, | 28 | Severity severity, |
| 27 | const std::string& severityString, | 29 | std::string severityString, |
| 28 | const std::string& file, | 30 | std::string file, |
| 29 | int line, | 31 | int line, |
| 30 | const std::string& function); | 32 | std::string function); |
| 31 | 33 | ||
| 32 | static Severity getLogLevel(); | 34 | static Severity getLogLevel(); |
| 33 | static void setLogLevel(Severity level); | 35 | static void setLogLevel(Severity level); |
| 34 | static void setLogFile(const std::string& fileName); | 36 | static void setLogFile(const std::string& fileName); |
| 37 | static void setLogCallback(Callback callback); | ||
| 35 | 38 | ||
| 36 | private: | 39 | private: |
| 37 | struct LogFileClose { | 40 | struct LogFileClose { |
| @@ -43,6 +46,11 @@ namespace Swift { | |||
| 43 | }; | 46 | }; |
| 44 | std::ostringstream stream; | 47 | std::ostringstream stream; |
| 45 | static std::unique_ptr<FILE, LogFileClose> logfile; | 48 | static std::unique_ptr<FILE, LogFileClose> logfile; |
| 49 | static Callback logCallback; | ||
| 50 | Severity severity_; | ||
| 51 | std::string file_; | ||
| 52 | int line_; | ||
| 53 | std::string function_; | ||
| 46 | }; | 54 | }; |
| 47 | } | 55 | } |
| 48 | 56 | ||
diff --git a/Swiften/Base/UnitTest/LogTest.cpp b/Swiften/Base/UnitTest/LogTest.cpp new file mode 100644 index 0000000..5d710db --- /dev/null +++ b/Swiften/Base/UnitTest/LogTest.cpp | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2019 Isode Limited. | ||
| 3 | * All rights reserved. | ||
| 4 | * See the COPYING file for more information. | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include <boost/algorithm/string/predicate.hpp> | ||
| 10 | |||
| 11 | #include <gtest/gtest.h> | ||
| 12 | |||
| 13 | #include <Swiften/Base/Log.h> | ||
| 14 | |||
| 15 | using namespace Swift; | ||
| 16 | |||
| 17 | struct LogEntry { | ||
| 18 | LogEntry(Log::Severity severity, std::string file, int line, std::string function, std::string message) : severity(severity), file(std::move(file)), line(line), function(std::move(function)), message(std::move(message)) {} | ||
| 19 | |||
| 20 | Log::Severity severity; | ||
| 21 | std::string file; | ||
| 22 | int line; | ||
| 23 | std::string function; | ||
| 24 | std::string message; | ||
| 25 | }; | ||
| 26 | |||
| 27 | // Helper class to set the logging callback. Using this class to set it will ensure the | ||
| 28 | // logCallback is reset to empty (its default state) after each test. | ||
| 29 | class LogCallbackSetter { | ||
| 30 | public: | ||
| 31 | LogCallbackSetter(Log::Callback callback) { | ||
| 32 | Log::setLogCallback(callback); | ||
| 33 | } | ||
| 34 | ~LogCallbackSetter() { | ||
| 35 | Log::setLogCallback({}); | ||
| 36 | } | ||
| 37 | }; | ||
| 38 | |||
| 39 | TEST(LogTest, testCallback) { | ||
| 40 | std::vector<LogEntry> logEntries; | ||
| 41 | LogCallbackSetter callbackSetter = {[&](Log::Severity severity, const std::string& file, int line, const std::string& function, const std::string& message) { | ||
| 42 | logEntries.emplace_back(severity, file, line, function, message); | ||
| 43 | }}; | ||
| 44 | |||
| 45 | SWIFT_LOG(error) << "An error"; | ||
| 46 | ASSERT_EQ(1, logEntries.size()); | ||
| 47 | ASSERT_EQ(Log::error, logEntries[0].severity); | ||
| 48 | ASSERT_EQ("An error", logEntries[0].message); | ||
| 49 | } | ||
diff --git a/Swiften/SConscript b/Swiften/SConscript index 5705113..7ea7355 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript | |||
| @@ -383,6 +383,7 @@ if env["SCONS_STAGE"] == "build" : | |||
| 383 | File("Avatars/UnitTest/CombinedAvatarProviderTest.cpp"), | 383 | File("Avatars/UnitTest/CombinedAvatarProviderTest.cpp"), |
| 384 | File("Avatars/UnitTest/AvatarManagerImplTest.cpp"), | 384 | File("Avatars/UnitTest/AvatarManagerImplTest.cpp"), |
| 385 | File("Base/UnitTest/IDGeneratorTest.cpp"), | 385 | File("Base/UnitTest/IDGeneratorTest.cpp"), |
| 386 | File("Base/UnitTest/LogTest.cpp"), | ||
| 386 | File("Base/UnitTest/LRUCacheTest.cpp"), | 387 | File("Base/UnitTest/LRUCacheTest.cpp"), |
| 387 | File("Base/UnitTest/SimpleIDGeneratorTest.cpp"), | 388 | File("Base/UnitTest/SimpleIDGeneratorTest.cpp"), |
| 388 | File("Base/UnitTest/StringTest.cpp"), | 389 | File("Base/UnitTest/StringTest.cpp"), |
Swift