use llarp::LogSilencer to shut up loging in unit tests

pull/1301/head
Jeff Becker 4 years ago
parent a73335579a
commit 3d2990f90d
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -174,14 +174,17 @@ namespace llarp
}
}
void
SilenceLog(std::function<void(void)> func)
LogSilencer::LogSilencer() : LogSilencer(LogContext::Instance())
{
}
LogSilencer::LogSilencer(LogContext& ctx) : stream(std::move(ctx.logStream))
{
}
LogSilencer::~LogSilencer()
{
auto& log = LogContext::Instance();
ILogStream_ptr oldStream = std::move(log.logStream);
log.logStream = nullptr;
func();
log.logStream = std::move(oldStream);
LogContext::Instance().logStream = std::move(stream);
}
} // namespace llarp

@ -60,9 +60,17 @@ namespace llarp
std::shared_ptr<thread::ThreadPool> threadpool);
};
// call a function where the logging is slienced
void
SilenceLog(std::function<void(void)> func);
/// RAII type to turn logging off
/// logging is suppressed as long as the silencer is in scope
struct LogSilencer
{
LogSilencer();
~LogSilencer();
explicit LogSilencer(LogContext& ctx);
private:
ILogStream_ptr stream;
};
void
SetLogLevel(LogLevel lvl);

@ -1,5 +1,7 @@
#include <gtest/gtest.h>
#include <util/logging/logger.hpp>
#ifdef _WIN32
#include <winsock2.h>
int
@ -20,6 +22,7 @@ startWinsock()
int
main(int argc, char** argv)
{
llarp::LogSilencer shutup;
#ifdef _WIN32
if (startWinsock())
return -1;

@ -19,7 +19,7 @@ make_context(fs::path keyfile)
TEST_CASE("key backup bug regression test", "[regress]")
{
llarp::SilenceLog([]() {
llarp::LogSilencer shutup;
for (const fs::path& path : {"regress-1.private", "regress-2.private", ""})
{
llarp::service::Address endpointAddress{};
@ -59,5 +59,4 @@ TEST_CASE("key backup bug regression test", "[regress]")
}
fs::remove(path);
}
});
}

Loading…
Cancel
Save