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 /Swiften/Base/Log.cpp | |
| 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
Diffstat (limited to 'Swiften/Base/Log.cpp')
| -rw-r--r-- | Swiften/Base/Log.cpp | 28 |
1 files changed, 22 insertions, 6 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 | } |
Swift