Every log line showed LoggingAdapter.hpp as its source because make_sink
called Void::Logging::Logger::log() with a defaulted source_location captured
inside the adapter, discarding the real call site.
Capture std::source_location at each Void::Log::{info,warn,...} call via an
FmtLoc wrapper (the same trick Logger uses internally), thread it through a
4-argument Sink, and forward it from make_sink to logger.log(). Sinks now
receive the originating file/line/function.
Formatted emission is routed through a single non-overloaded Detail::emit():
forwarding a (level, module, std::string, source_location) call to the public
log() would otherwise also match the variadic log() overload, deducing
source_location as a format argument and failing in FmtLoc's consteval ctor.
Sink is now a 4-arg callable; updated the bundled tests accordingly.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
232 lines
6.6 KiB
C++
232 lines
6.6 KiB
C++
#include <Void/Log/Log.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
// Reset the global facade state between tests via a scoped RAII guard.
|
|
struct SinkGuard {
|
|
~SinkGuard()
|
|
{
|
|
Void::Log::set_sink({});
|
|
Void::Log::set_min_level(Void::Log::Level::Trace);
|
|
Void::Log::clear_module_level("Filtered");
|
|
Void::Log::clear_module_level("Verbose");
|
|
}
|
|
};
|
|
|
|
Void::Testing::Test no_sink_is_silent()
|
|
{
|
|
SinkGuard guard;
|
|
bool called = false;
|
|
// No sink installed — this must not crash or invoke anything.
|
|
Void::Log::log(Void::Log::Level::Info, "test", "silent message");
|
|
Void::Testing::expect(!called);
|
|
}
|
|
|
|
Void::Testing::Test sink_receives_all_fields()
|
|
{
|
|
SinkGuard guard;
|
|
Void::Log::Level got_level{};
|
|
std::string got_module;
|
|
std::string got_message;
|
|
|
|
Void::Log::set_sink(
|
|
[&](Void::Log::Level l, std::string_view mod, std::string_view msg,
|
|
const std::source_location&) {
|
|
got_level = l;
|
|
got_module = mod;
|
|
got_message = msg;
|
|
});
|
|
|
|
Void::Log::log(Void::Log::Level::Warning, "MyModule", "something happened");
|
|
|
|
Void::Testing::expect(got_level == Void::Log::Level::Warning);
|
|
Void::Testing::expect(got_module == "MyModule");
|
|
Void::Testing::expect(got_message == "something happened");
|
|
}
|
|
|
|
Void::Testing::Test set_sink_replaces_previous()
|
|
{
|
|
SinkGuard guard;
|
|
int first_calls = 0;
|
|
int second_calls = 0;
|
|
|
|
Void::Log::set_sink([&](auto, auto, auto, auto) { ++first_calls; });
|
|
Void::Log::log(Void::Log::Level::Info, "m", "a");
|
|
|
|
Void::Log::set_sink([&](auto, auto, auto, auto) { ++second_calls; });
|
|
Void::Log::log(Void::Log::Level::Info, "m", "b");
|
|
|
|
Void::Testing::expect_eq(first_calls, 1);
|
|
Void::Testing::expect_eq(second_calls, 1);
|
|
}
|
|
|
|
Void::Testing::Test clear_sink_silences_output()
|
|
{
|
|
SinkGuard guard;
|
|
int calls = 0;
|
|
Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; });
|
|
Void::Log::log(Void::Log::Level::Info, "m", "before clear");
|
|
Void::Log::set_sink({});
|
|
Void::Log::log(Void::Log::Level::Info, "m", "after clear");
|
|
Void::Testing::expect_eq(calls, 1);
|
|
}
|
|
|
|
Void::Testing::Test concurrent_log_calls_are_safe()
|
|
{
|
|
SinkGuard guard;
|
|
std::atomic<int> count{0};
|
|
Void::Log::set_sink([&](auto, auto, auto, auto) { ++count; });
|
|
|
|
constexpr int N = 64;
|
|
std::vector<std::thread> threads;
|
|
threads.reserve(N);
|
|
for (int i = 0; i < N; ++i) {
|
|
threads.emplace_back([] {
|
|
Void::Log::log(Void::Log::Level::Debug, "test", "concurrent");
|
|
});
|
|
}
|
|
for (auto& t : threads) {
|
|
t.join();
|
|
}
|
|
|
|
Void::Testing::expect_eq(count.load(), N);
|
|
}
|
|
|
|
Void::Testing::Test all_levels_are_forwarded()
|
|
{
|
|
SinkGuard guard;
|
|
int calls = 0;
|
|
Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; });
|
|
|
|
Void::Log::log(Void::Log::Level::Trace, "m", "t");
|
|
Void::Log::log(Void::Log::Level::Debug, "m", "d");
|
|
Void::Log::log(Void::Log::Level::Info, "m", "i");
|
|
Void::Log::log(Void::Log::Level::Warning, "m", "w");
|
|
Void::Log::log(Void::Log::Level::Error, "m", "e");
|
|
Void::Log::log(Void::Log::Level::Fatal, "m", "f");
|
|
|
|
Void::Testing::expect_eq(calls, 6);
|
|
}
|
|
|
|
// ── leveled helpers & lazy formatting ─────────────────────────────────────────
|
|
|
|
Void::Testing::Test leveled_helpers_format_arguments()
|
|
{
|
|
SinkGuard guard;
|
|
std::string got;
|
|
Void::Log::Level got_level{};
|
|
Void::Log::set_sink(
|
|
[&](Void::Log::Level l, auto, std::string_view msg, auto) {
|
|
got_level = l;
|
|
got = msg;
|
|
});
|
|
|
|
Void::Log::warn("m", "answer = {}", 42);
|
|
Void::Testing::expect(got_level == Void::Log::Level::Warning);
|
|
Void::Testing::expect_eq(got, std::string{"answer = 42"});
|
|
}
|
|
|
|
namespace
|
|
{
|
|
// A format argument whose formatting is observable — proves laziness.
|
|
struct FormatProbe {
|
|
int* format_count;
|
|
};
|
|
} // namespace
|
|
|
|
template <>
|
|
struct std::formatter<FormatProbe> : std::formatter<int> {
|
|
template <class Ctx>
|
|
auto format(const FormatProbe& p, Ctx& ctx) const
|
|
{
|
|
++*p.format_count;
|
|
return std::formatter<int>::format(0, ctx);
|
|
}
|
|
};
|
|
|
|
Void::Testing::Test disabled_calls_do_not_format()
|
|
{
|
|
SinkGuard guard;
|
|
int formatted = 0;
|
|
|
|
// No sink: nothing may be formatted.
|
|
Void::Log::debug("m", "{}", FormatProbe{&formatted});
|
|
Void::Testing::expect_eq(formatted, 0);
|
|
|
|
// Sink present but level below the global minimum: still nothing.
|
|
int calls = 0;
|
|
Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; });
|
|
Void::Log::set_min_level(Void::Log::Level::Error);
|
|
Void::Log::debug("m", "{}", FormatProbe{&formatted});
|
|
Void::Testing::expect_eq(formatted, 0);
|
|
Void::Testing::expect_eq(calls, 0);
|
|
|
|
// Passing the gate formats exactly once.
|
|
Void::Log::error("m", "{}", FormatProbe{&formatted});
|
|
Void::Testing::expect_eq(formatted, 1);
|
|
Void::Testing::expect_eq(calls, 1);
|
|
}
|
|
|
|
// ── level filtering ───────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test global_min_level_filters()
|
|
{
|
|
SinkGuard guard;
|
|
int calls = 0;
|
|
Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; });
|
|
Void::Log::set_min_level(Void::Log::Level::Warning);
|
|
|
|
Void::Log::info("m", "dropped");
|
|
Void::Log::warn("m", "kept");
|
|
Void::Log::error("m", "kept");
|
|
Void::Testing::expect_eq(calls, 2);
|
|
}
|
|
|
|
Void::Testing::Test disabled_level_silences_everything()
|
|
{
|
|
SinkGuard guard;
|
|
int calls = 0;
|
|
Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; });
|
|
Void::Log::set_min_level(Void::Log::Level::Disabled);
|
|
|
|
Void::Log::fatal("m", "still dropped");
|
|
Void::Testing::expect_eq(calls, 0);
|
|
}
|
|
|
|
Void::Testing::Test module_override_lowers_and_raises()
|
|
{
|
|
SinkGuard guard;
|
|
int calls = 0;
|
|
Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; });
|
|
Void::Log::set_min_level(Void::Log::Level::Warning);
|
|
|
|
// Lower the threshold for one module only.
|
|
Void::Log::set_module_level("Verbose", Void::Log::Level::Trace);
|
|
Void::Log::debug("Verbose", "kept by override");
|
|
Void::Log::debug("Other", "dropped by global");
|
|
Void::Testing::expect_eq(calls, 1);
|
|
|
|
// Raise the threshold for one module above the global.
|
|
Void::Log::set_module_level("Filtered", Void::Log::Level::Fatal);
|
|
Void::Log::error("Filtered", "dropped by override");
|
|
Void::Log::error("Other", "kept by global");
|
|
Void::Testing::expect_eq(calls, 2);
|
|
}
|
|
|
|
Void::Testing::Test enabled_reflects_configuration()
|
|
{
|
|
SinkGuard guard;
|
|
Void::Testing::expect(
|
|
!Void::Log::enabled(Void::Log::Level::Fatal, "m")); // no sink
|
|
|
|
Void::Log::set_sink([](auto, auto, auto, auto) {});
|
|
Void::Log::set_min_level(Void::Log::Level::Info);
|
|
Void::Testing::expect(Void::Log::enabled(Void::Log::Level::Info, "m"));
|
|
Void::Testing::expect(!Void::Log::enabled(Void::Log::Level::Debug, "m"));
|
|
}
|