Core: add configurable startup log rotation
This commit is contained in:
1 parent
a277f2102b
commit
9637f1eef7
9 files changed
+361
-27
No files matched your search
@@ -41,6 +41,33 @@ int main()
|
||||
const auto defaults = ConfigLoader::createDefaultConfig();
|
||||
expect(!defaults.urlShortening.enabled,
|
||||
"generated defaults keep URL shortening disabled");
|
||||
expect(defaults.logging.fileName == "echoion.log" &&
|
||||
defaults.logging.maxFiles == 10,
|
||||
"generated defaults configure bounded rolling logs");
|
||||
|
||||
const auto logging = ConfigLoader::loadFromJson(R"({
|
||||
"logging": {
|
||||
"level": "verbose",
|
||||
"fileName": "../outside.log",
|
||||
"maxFileSizeBytes": 1,
|
||||
"maxFiles": 0
|
||||
}
|
||||
})");
|
||||
expect(logging.has_value(), "logging configuration parses");
|
||||
if (logging) {
|
||||
expect(logging->logging.level == "debug",
|
||||
"invalid log level is normalized");
|
||||
expect(logging->logging.fileName == "echoion.log",
|
||||
"log file cannot escape the state log directory");
|
||||
expect(logging->logging.maxFileSizeBytes == 64 * 1024,
|
||||
"log size is clamped to a safe minimum");
|
||||
expect(logging->logging.maxFiles == 1,
|
||||
"at least one rolled log is retained");
|
||||
}
|
||||
const auto parentLog =
|
||||
ConfigLoader::loadFromJson(R"({"logging":{"fileName":".."}})");
|
||||
expect(parentLog && parentLog->logging.fileName == "echoion.log",
|
||||
"parent-directory logging filename is normalized");
|
||||
|
||||
expectShortening("legacy disabled",
|
||||
R"({
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "echoion/core/LogRotation.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
|
||||
namespace
|
||||
{
|
||||
int failures = 0;
|
||||
|
||||
void expect(bool condition, std::string_view message)
|
||||
{
|
||||
if (!condition) {
|
||||
std::cerr << "FAILED: " << message << '\n';
|
||||
++failures;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t archiveCount(const std::filesystem::path& history)
|
||||
{
|
||||
std::size_t count = 0;
|
||||
std::error_code error;
|
||||
for (std::filesystem::directory_iterator iterator(history, error), end;
|
||||
!error && iterator != end; iterator.increment(error)) {
|
||||
if (iterator->is_regular_file()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root =
|
||||
std::filesystem::temp_directory_path() / "echoion-log-rotation-test";
|
||||
std::error_code error;
|
||||
std::filesystem::remove_all(root, error);
|
||||
std::filesystem::create_directories(root, error);
|
||||
const auto active = root / "echoion.log";
|
||||
|
||||
{
|
||||
std::ofstream file(active);
|
||||
file << "first run\n";
|
||||
}
|
||||
error = Echoion::Core::rotateLogOnStartup(active, 2);
|
||||
expect(!error, "first startup rotation succeeds");
|
||||
expect(!std::filesystem::exists(active),
|
||||
"startup rotation leaves no old active file");
|
||||
expect(archiveCount(root / "history") == 1, "first run is archived");
|
||||
|
||||
for (int run = 0; run < 3; ++run) {
|
||||
{
|
||||
std::ofstream file(active);
|
||||
file << "run " << run << '\n';
|
||||
}
|
||||
error = Echoion::Core::rotateLogOnStartup(active, 2);
|
||||
expect(!error, "subsequent startup rotation succeeds");
|
||||
}
|
||||
expect(archiveCount(root / "history") == 2,
|
||||
"startup rotation prunes to configured history count");
|
||||
|
||||
{
|
||||
std::ofstream file(active);
|
||||
}
|
||||
error = Echoion::Core::rotateLogOnStartup(active, 2);
|
||||
expect(!error && !std::filesystem::exists(active),
|
||||
"empty active file is removed instead of archived");
|
||||
|
||||
std::filesystem::remove_all(root / "history", error);
|
||||
{
|
||||
std::ofstream invalidHistory(root / "history");
|
||||
invalidHistory << "not a directory\n";
|
||||
}
|
||||
error = Echoion::Core::rotateLogOnStartup(active, 2);
|
||||
expect(error == std::errc::invalid_argument,
|
||||
"non-directory history path was accepted");
|
||||
|
||||
std::filesystem::remove_all(root, error);
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user