summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Swiften/Base/Log.cpp28
-rw-r--r--Swiften/Base/Log.h14
-rw-r--r--Swiften/Base/UnitTest/LogTest.cpp49
-rw-r--r--Swiften/SConscript1
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 {
static Log::Severity logLevel = Log::warning;
std::unique_ptr<FILE, Log::LogFileClose> Log::logfile;
+Log::Callback Log::logCallback;
Log::Log() {
}
@@ -26,7 +27,10 @@ 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)
- if (logfile) {
+ if (logCallback) {
+ logCallback(severity_, std::move(file_), line_, std::move(function_), stream.str());
+ }
+ else if (logfile) {
fwrite(stream.str().c_str(), sizeof(char), stream.str().size(), logfile.get());
fflush(logfile.get());
}
@@ -38,12 +42,20 @@ Log::~Log() {
}
std::ostringstream& Log::getStream(
- Severity /*severity*/,
- const std::string& severityString,
- const std::string& file,
+ Severity severity,
+ std::string severityString,
+ std::string file,
int line,
- const std::string& function) {
- stream << "[" << severityString << "] " << file << ":" << line << " " << function << ": ";
+ std::string function) {
+ if (logCallback) {
+ severity_ = severity;
+ file_ = std::move(file);
+ line_ = line;
+ function_ = std::move(function);
+ }
+ else {
+ stream << "[" << severityString << "] " << file << ":" << line << " " << function << ": ";
+ }
return stream;
}
@@ -61,4 +73,8 @@ void Log::setLogFile(const std::string& fileName) {
}
}
+void Log::setLogCallback(Callback callback) {
+ Log::logCallback = callback;
+}
+
}
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 @@
#pragma once
#include <cstdio>
+#include <functional>
#include <memory>
#include <sstream>
@@ -18,20 +19,22 @@ namespace Swift {
enum Severity {
error, warning, info, debug
};
+ using Callback = std::function<void(Severity severity, std::string file, int line, std::string function, std::string message)>;
Log();
~Log();
std::ostringstream& getStream(
Severity severity,
- const std::string& severityString,
- const std::string& file,
+ std::string severityString,
+ std::string file,
int line,
- const std::string& function);
+ std::string function);
static Severity getLogLevel();
static void setLogLevel(Severity level);
static void setLogFile(const std::string& fileName);
+ static void setLogCallback(Callback callback);
private:
struct LogFileClose {
@@ -43,6 +46,11 @@ namespace Swift {
};
std::ostringstream stream;
static std::unique_ptr<FILE, LogFileClose> logfile;
+ static Callback logCallback;
+ Severity severity_;
+ std::string file_;
+ int line_;
+ std::string function_;
};
}
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 @@
+/*
+ * Copyright (c) 2019 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <vector>
+
+#include <boost/algorithm/string/predicate.hpp>
+
+#include <gtest/gtest.h>
+
+#include <Swiften/Base/Log.h>
+
+using namespace Swift;
+
+struct LogEntry {
+ 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)) {}
+
+ Log::Severity severity;
+ std::string file;
+ int line;
+ std::string function;
+ std::string message;
+};
+
+// Helper class to set the logging callback. Using this class to set it will ensure the
+// logCallback is reset to empty (its default state) after each test.
+class LogCallbackSetter {
+public:
+ LogCallbackSetter(Log::Callback callback) {
+ Log::setLogCallback(callback);
+ }
+ ~LogCallbackSetter() {
+ Log::setLogCallback({});
+ }
+};
+
+TEST(LogTest, testCallback) {
+ std::vector<LogEntry> logEntries;
+ LogCallbackSetter callbackSetter = {[&](Log::Severity severity, const std::string& file, int line, const std::string& function, const std::string& message) {
+ logEntries.emplace_back(severity, file, line, function, message);
+ }};
+
+ SWIFT_LOG(error) << "An error";
+ ASSERT_EQ(1, logEntries.size());
+ ASSERT_EQ(Log::error, logEntries[0].severity);
+ ASSERT_EQ("An error", logEntries[0].message);
+}
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" :
File("Avatars/UnitTest/CombinedAvatarProviderTest.cpp"),
File("Avatars/UnitTest/AvatarManagerImplTest.cpp"),
File("Base/UnitTest/IDGeneratorTest.cpp"),
+ File("Base/UnitTest/LogTest.cpp"),
File("Base/UnitTest/LRUCacheTest.cpp"),
File("Base/UnitTest/SimpleIDGeneratorTest.cpp"),
File("Base/UnitTest/StringTest.cpp"),