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.h | |
| 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.h')
| -rw-r--r-- | Swiften/Base/Log.h | 14 |
1 files changed, 11 insertions, 3 deletions
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 | ||
Swift