[Open Screen] Adds a unit test for logging macros.
- Adds unit tests for the macros defined in util/osp_logging.h
- Adds testing hooks to logging_posix.h
Tests both behavior in _DEBUG and !_DEBUG builds.
Change-Id: I5fa3382d5455fbdf5e5b75d0d519cd43c5960d12
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/2289036
Commit-Queue: mark a. foltz <mfoltz@chromium.org>
Reviewed-by: Brandon Tolsch <btolsch@chromium.org>
diff --git a/platform/impl/logging_posix.cc b/platform/impl/logging_posix.cc
index 2041077..6e9564c 100644
--- a/platform/impl/logging_posix.cc
+++ b/platform/impl/logging_posix.cc
@@ -13,6 +13,7 @@
#include <sstream>
#include "platform/impl/logging.h"
+#include "platform/impl/logging_test.h"
#include "util/trace_logging.h"
namespace openscreen {
@@ -20,6 +21,8 @@
int g_log_fd = STDERR_FILENO;
LogLevel g_log_level = LogLevel::kWarning;
+std::vector<std::string>* g_log_messages_for_test = nullptr;
+bool* g_break_was_called_for_test = nullptr;
std::ostream& operator<<(std::ostream& os, const LogLevel& level) {
const char* level_string = "";
@@ -79,6 +82,10 @@
g_log_level = level;
}
+LogLevel GetLogLevel() {
+ return g_log_level;
+}
+
bool IsLoggingOn(LogLevel level, const char* file) {
// Possible future enhancement: Use glob patterns passed on the command-line
// to use a different logging level for certain files, like in Chromium.
@@ -98,9 +105,16 @@
const auto ss_str = ss.str();
const auto bytes_written = write(g_log_fd, ss_str.c_str(), ss_str.size());
OSP_DCHECK(bytes_written);
+ if (g_log_messages_for_test) {
+ g_log_messages_for_test->push_back(ss_str);
+ }
}
void Break() {
+ if (g_break_was_called_for_test) {
+ *g_break_was_called_for_test = true;
+ return;
+ }
#if defined(_DEBUG)
__builtin_trap();
#else
@@ -108,4 +122,12 @@
#endif
}
+void SetLogBufferForTest(std::vector<std::string>* messages) {
+ g_log_messages_for_test = messages;
+}
+
+void DisableBreakForTest(bool* break_was_called) {
+ g_break_was_called_for_test = break_was_called;
+}
+
} // namespace openscreen