1 parent
fa30098129
commit
8583a479dd
11 files changed
+1763
-129
No files matched your search
+144
-125
@@ -1,7 +1,7 @@
|
||||
#include <Void/Log/Sinks.hpp>
|
||||
#include <Void/Log/ConsoleLogger.hpp>
|
||||
#include <Void/Log/Formatter.hpp>
|
||||
#include <Void/Log/RotatingFileLogger.hpp>
|
||||
#include <Void/Log/Sinks.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Fixture.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
@@ -14,170 +14,189 @@
|
||||
|
||||
using namespace Void::Logging;
|
||||
|
||||
// ── NullLogger / CallbackLogger ───────────────────────────────────────────────
|
||||
namespace
|
||||
{
|
||||
std::size_t archive_count(const std::filesystem::path& path)
|
||||
{
|
||||
const auto directory = path.parent_path() / "history";
|
||||
if (!std::filesystem::exists(directory)) {
|
||||
return 0;
|
||||
}
|
||||
const auto prefix = path.stem().string() + "-";
|
||||
std::size_t count = 0;
|
||||
for (const auto& entry :
|
||||
std::filesystem::directory_iterator(directory)) {
|
||||
const auto filename = entry.path().filename().string();
|
||||
if (entry.is_regular_file() && filename.starts_with(prefix) &&
|
||||
entry.path().extension() == path.extension()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// ── NullLogger / CallbackLogger
|
||||
// ───────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test null_logger_swallows_everything()
|
||||
{
|
||||
NullLogger log;
|
||||
log.fatal("into the void");
|
||||
log.flush();
|
||||
Void::Testing::expect(true); // reached without crashing
|
||||
NullLogger log;
|
||||
log.fatal("into the void");
|
||||
log.flush();
|
||||
Void::Testing::expect(true); // reached without crashing
|
||||
}
|
||||
|
||||
Void::Testing::Test callback_logger_receives_entry_fields()
|
||||
{
|
||||
Entry got;
|
||||
CallbackLogger log{[&](const Entry& e) { got = e; },
|
||||
Mode::Immediate};
|
||||
log.warning("payload");
|
||||
Void::Testing::expect(got.level == Level::Warning);
|
||||
Void::Testing::expect_eq(got.message, std::string{"payload"});
|
||||
Entry got;
|
||||
CallbackLogger log{[&](const Entry& e) { got = e; }, Mode::Immediate};
|
||||
log.warning("payload");
|
||||
Void::Testing::expect(got.level == Level::Warning);
|
||||
Void::Testing::expect_eq(got.message, std::string{"payload"});
|
||||
}
|
||||
|
||||
// ── MultiLogger ───────────────────────────────────────────────────────────────
|
||||
// ── MultiLogger
|
||||
// ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test multi_logger_fans_out_to_all_children()
|
||||
{
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
MultiLogger multi;
|
||||
multi.add(std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++a; }, Mode::Immediate));
|
||||
multi.add(std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++b; }, Mode::Immediate));
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
MultiLogger multi;
|
||||
multi.add(std::make_shared<CallbackLogger>([&](const Entry&) { ++a; },
|
||||
Mode::Immediate));
|
||||
multi.add(std::make_shared<CallbackLogger>([&](const Entry&) { ++b; },
|
||||
Mode::Immediate));
|
||||
|
||||
multi.info("to both");
|
||||
Void::Testing::expect_eq(a, 1);
|
||||
Void::Testing::expect_eq(b, 1);
|
||||
Void::Testing::expect_eq(multi.child_count(), std::size_t{2});
|
||||
multi.info("to both");
|
||||
Void::Testing::expect_eq(a, 1);
|
||||
Void::Testing::expect_eq(b, 1);
|
||||
Void::Testing::expect_eq(multi.child_count(), std::size_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test multi_logger_children_keep_own_levels()
|
||||
{
|
||||
int verbose_count = 0;
|
||||
int errors_only = 0;
|
||||
auto verbose = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++verbose_count; }, Mode::Immediate);
|
||||
auto strict = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++errors_only; }, Mode::Immediate);
|
||||
strict->set_level(Level::Error);
|
||||
int verbose_count = 0;
|
||||
int errors_only = 0;
|
||||
auto verbose = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++verbose_count; }, Mode::Immediate);
|
||||
auto strict = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++errors_only; }, Mode::Immediate);
|
||||
strict->set_level(Level::Error);
|
||||
|
||||
MultiLogger multi;
|
||||
multi.add(verbose);
|
||||
multi.add(strict);
|
||||
MultiLogger multi;
|
||||
multi.add(verbose);
|
||||
multi.add(strict);
|
||||
|
||||
multi.info("info");
|
||||
multi.error("error");
|
||||
Void::Testing::expect_eq(verbose_count, 2);
|
||||
Void::Testing::expect_eq(errors_only, 1);
|
||||
multi.info("info");
|
||||
multi.error("error");
|
||||
Void::Testing::expect_eq(verbose_count, 2);
|
||||
Void::Testing::expect_eq(errors_only, 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test multi_logger_relays_async_children_with_flush()
|
||||
{
|
||||
int delivered = 0;
|
||||
auto async_child = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++delivered; }); // Async by default
|
||||
MultiLogger multi;
|
||||
multi.add(async_child);
|
||||
int delivered = 0;
|
||||
auto async_child = std::make_shared<CallbackLogger>(
|
||||
[&](const Entry&) { ++delivered; }); // Async by default
|
||||
MultiLogger multi;
|
||||
multi.add(async_child);
|
||||
|
||||
multi.info("queued in child");
|
||||
multi.flush(); // flushes children too
|
||||
Void::Testing::expect_eq(delivered, 1);
|
||||
multi.info("queued in child");
|
||||
multi.flush(); // flushes children too
|
||||
Void::Testing::expect_eq(delivered, 1);
|
||||
}
|
||||
|
||||
// ── ConsoleLogger + Formatter ─────────────────────────────────────────────────
|
||||
// ── ConsoleLogger + Formatter
|
||||
// ─────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test console_logger_uses_installed_formatter()
|
||||
{
|
||||
std::ostringstream out;
|
||||
{
|
||||
ConsoleLogger log{out, Mode::Immediate};
|
||||
log.set_formatter(
|
||||
std::make_shared<PatternFormatter>("{level}>{message}"));
|
||||
log.info("custom format");
|
||||
}
|
||||
Void::Testing::expect_eq(out.str(),
|
||||
std::string{"Info>custom format\n"});
|
||||
std::ostringstream out;
|
||||
{
|
||||
ConsoleLogger log{out, Mode::Immediate};
|
||||
log.set_formatter(
|
||||
std::make_shared<PatternFormatter>("{level}>{message}"));
|
||||
log.info("custom format");
|
||||
}
|
||||
Void::Testing::expect_eq(out.str(), std::string{"Info>custom format\n"});
|
||||
}
|
||||
|
||||
// ── RotatingFileLogger ────────────────────────────────────────────────────────
|
||||
// ── RotatingFileLogger
|
||||
// ────────────────────────────────────────────────────────
|
||||
|
||||
class RotatingFileLoggerTests : public Void::Testing::Fixture
|
||||
{
|
||||
public:
|
||||
public:
|
||||
|
||||
std::filesystem::path dir;
|
||||
std::filesystem::path file;
|
||||
std::filesystem::path dir;
|
||||
std::filesystem::path file;
|
||||
|
||||
void setup() override
|
||||
{
|
||||
dir = std::filesystem::temp_directory_path() /
|
||||
"void_rotating_test";
|
||||
std::filesystem::remove_all(dir);
|
||||
std::filesystem::create_directories(dir);
|
||||
file = dir / "app.log";
|
||||
}
|
||||
void setup() override
|
||||
{
|
||||
dir = std::filesystem::temp_directory_path() / "void_rotating_test";
|
||||
std::filesystem::remove_all(dir);
|
||||
std::filesystem::create_directories(dir);
|
||||
file = dir / "app.log";
|
||||
}
|
||||
|
||||
void teardown() override { std::filesystem::remove_all(dir); }
|
||||
void teardown() override { std::filesystem::remove_all(dir); }
|
||||
|
||||
static std::string slurp(const std::filesystem::path& p)
|
||||
{
|
||||
std::ifstream f(p);
|
||||
return {std::istreambuf_iterator<char>(f), {}};
|
||||
}
|
||||
static std::string slurp(const std::filesystem::path& p)
|
||||
{
|
||||
std::ifstream f(p);
|
||||
return {std::istreambuf_iterator<char>(f), {}};
|
||||
}
|
||||
|
||||
Void::Testing::Test writes_and_flushes()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger log{file};
|
||||
log.info("rotating hello");
|
||||
}
|
||||
Void::Testing::expect(slurp(file).find("rotating hello") !=
|
||||
std::string::npos);
|
||||
}
|
||||
Void::Testing::Test writes_and_flushes()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger log{file};
|
||||
log.info("rotating hello");
|
||||
}
|
||||
Void::Testing::expect(slurp(file).find("rotating hello") !=
|
||||
std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test rotates_when_size_exceeded()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger::Options opt;
|
||||
opt.max_bytes = 256;
|
||||
opt.max_files = 3;
|
||||
opt.mode = Mode::Immediate;
|
||||
RotatingFileLogger log{file, opt};
|
||||
for (int i = 0; i < 40; ++i) {
|
||||
log.info("a reasonably long line to fill the file " +
|
||||
std::to_string(i));
|
||||
}
|
||||
}
|
||||
Void::Testing::expect(std::filesystem::exists(file));
|
||||
auto rotated = file;
|
||||
rotated += ".1";
|
||||
Void::Testing::expect(std::filesystem::exists(rotated));
|
||||
// The bound holds: no file beyond .3
|
||||
auto beyond = file;
|
||||
beyond += ".4";
|
||||
Void::Testing::expect(!std::filesystem::exists(beyond));
|
||||
}
|
||||
Void::Testing::Test rotates_when_size_exceeded()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger::Options opt;
|
||||
opt.max_bytes = 256;
|
||||
opt.max_files = 3;
|
||||
opt.mode = Mode::Immediate;
|
||||
RotatingFileLogger log{file, opt};
|
||||
for (int i = 0; i < 40; ++i) {
|
||||
log.info("a reasonably long line to fill the file " +
|
||||
std::to_string(i));
|
||||
}
|
||||
}
|
||||
Void::Testing::expect(std::filesystem::exists(file));
|
||||
const auto archives = archive_count(file);
|
||||
Void::Testing::expect(archives >= 1);
|
||||
Void::Testing::expect(archives <= 3);
|
||||
}
|
||||
|
||||
Void::Testing::Test open_factory_rejects_bad_path()
|
||||
{
|
||||
auto r = RotatingFileLogger::open(
|
||||
"/this/path/does/not/exist/app.log");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
Void::Testing::Test open_factory_rejects_bad_path()
|
||||
{
|
||||
auto r =
|
||||
RotatingFileLogger::open("/this/path/does/not/exist/app.log");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test async_rotation_under_load()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger::Options opt;
|
||||
opt.max_bytes = 512;
|
||||
opt.max_files = 2;
|
||||
RotatingFileLogger log{file, opt}; // Async default
|
||||
for (int i = 0; i < 200; ++i) {
|
||||
log.info("async rotation line " + std::to_string(i));
|
||||
}
|
||||
log.flush();
|
||||
}
|
||||
Void::Testing::expect(std::filesystem::exists(file));
|
||||
}
|
||||
Void::Testing::Test async_rotation_under_load()
|
||||
{
|
||||
{
|
||||
RotatingFileLogger::Options opt;
|
||||
opt.max_bytes = 512;
|
||||
opt.max_files = 2;
|
||||
RotatingFileLogger log{file, opt}; // Async default
|
||||
for (int i = 0; i < 200; ++i) {
|
||||
log.info("async rotation line " + std::to_string(i));
|
||||
}
|
||||
log.flush();
|
||||
}
|
||||
Void::Testing::expect(std::filesystem::exists(file));
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user