96 lines
2.4 KiB
C++
96 lines
2.4 KiB
C++
#include <Void/Logging/FileLogger.hpp>
|
|
#include <Void/Logging/Level.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Fixture.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
class FileLoggerTests : public Void::Testing::Fixture
|
|
{
|
|
public:
|
|
|
|
std::filesystem::path path;
|
|
|
|
void setup() override
|
|
{
|
|
path = std::filesystem::temp_directory_path() /
|
|
"void_filelogger_test.log";
|
|
std::filesystem::remove(path);
|
|
}
|
|
|
|
void teardown() override { std::filesystem::remove(path); }
|
|
|
|
Void::Testing::Test creates_file()
|
|
{
|
|
{
|
|
Void::Logging::FileLogger log(path);
|
|
log.info("hello");
|
|
}
|
|
Void::Testing::expect(std::filesystem::exists(path));
|
|
}
|
|
|
|
Void::Testing::Test writes_messages()
|
|
{
|
|
{
|
|
Void::Logging::FileLogger log(
|
|
path, Void::Logging::Mode::Buffered, 1);
|
|
log.info("test message");
|
|
}
|
|
std::ifstream f(path);
|
|
std::string content((std::istreambuf_iterator<char>(f)), {});
|
|
Void::Testing::expect(content.find("test message") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test level_filtering()
|
|
{
|
|
{
|
|
Void::Logging::FileLogger log(
|
|
path, Void::Logging::Mode::Buffered, 1);
|
|
log.set_level(Void::Logging::Level::Warning);
|
|
log.debug("should be dropped");
|
|
log.warning("should appear");
|
|
}
|
|
std::ifstream f(path);
|
|
std::string content((std::istreambuf_iterator<char>(f)), {});
|
|
Void::Testing::expect(content.find("should be dropped") ==
|
|
std::string::npos);
|
|
Void::Testing::expect(content.find("should appear") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test buffered_flush()
|
|
{
|
|
{
|
|
Void::Logging::FileLogger log(
|
|
path, Void::Logging::Mode::Buffered, 100);
|
|
for (int i = 0; i < 5; ++i) {
|
|
log.info(std::format("entry {}", i));
|
|
}
|
|
}
|
|
std::ifstream f(path);
|
|
std::string content((std::istreambuf_iterator<char>(f)), {});
|
|
Void::Testing::expect(content.find("entry 4") != std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test open_factory_returns_ok_on_valid_path()
|
|
{
|
|
auto r = Void::Logging::FileLogger::open(path);
|
|
Void::Testing::expect(r.has_value());
|
|
(*r)->info("via factory");
|
|
}
|
|
|
|
Void::Testing::Test open_factory_returns_error_on_bad_path()
|
|
{
|
|
auto r = Void::Logging::FileLogger::open(
|
|
"/this/path/does/not/exist/file.log");
|
|
Void::Testing::expect(!r.has_value());
|
|
Void::Testing::expect(r.error().message().find("FileLogger") !=
|
|
std::string::npos);
|
|
}
|
|
};
|