mirror of
https://github.com/FairRootGroup/FairLogger.git
synced 2025-10-13 16:46:46 +00:00
135 lines
6.4 KiB
C++
135 lines
6.4 KiB
C++
/********************************************************************************
|
|
* Copyright (C) 2014-2020 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
|
* *
|
|
* This software is distributed under the terms of the *
|
|
* GNU Lesser General Public Licence (LGPL) version 3, *
|
|
* copied verbatim in the file "LICENSE" *
|
|
********************************************************************************/
|
|
|
|
#include "Common.h"
|
|
#include <Logger.h>
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <random>
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
using namespace fair;
|
|
using namespace fair::logger::test;
|
|
|
|
int main()
|
|
{
|
|
Logger::SetConsoleColor(false);
|
|
Logger::SetVerbosity(Verbosity::low);
|
|
|
|
Logger::SetConsoleSeverity(Severity::nolog);
|
|
|
|
try {
|
|
#ifdef FAIR_MIN_SEVERITY
|
|
if (static_cast<int>(Severity::FAIR_MIN_SEVERITY) > static_cast<int>(Severity::warn)) {
|
|
cout << "test requires at least FAIR_MIN_SEVERITY == warn to run, skipping" << endl;
|
|
return 0;
|
|
}
|
|
#endif
|
|
if (Logger::Logging(Severity::warn)) { cout << "Logger expected to NOT log warn, but it reports to do so" << endl; return 1; }
|
|
if (Logger::Logging(Severity::error)) { cout << "Logger expected to NOT log error, but it reports to do so" << endl; return 1; }
|
|
if (!Logger::Logging(Severity::fatal)) { cout << "Logger expected to log fatal, but it reports not to" << endl; return 1; }
|
|
|
|
cout << "##### adding file sink with warn severity" << endl;
|
|
random_device rd;
|
|
mt19937 gen(rd());
|
|
uniform_int_distribution<> distrib(1, 65536);
|
|
string name = Logger::InitFileSink(Severity::warn, string("test_log_" + to_string(distrib(gen))), true);
|
|
|
|
CheckOutput("^\\[FATAL] fatal\n$", [](){
|
|
LOG(state) << "state";
|
|
LOG(warn) << "warning";
|
|
LOG(error) << "error";
|
|
LOG(fatal) << "fatal";
|
|
});
|
|
|
|
if (Logger::Logging(Severity::state)) { cout << "Logger expected to NOT log warn, but it reports to do so" << endl; return 1; }
|
|
if (!Logger::Logging(Severity::warn)) { cout << "Logger expected to log warn, but it reports not to" << endl; return 1; }
|
|
if (!Logger::Logging(Severity::error)) { cout << "Logger expected to log error, but it reports not to" << endl; return 1; }
|
|
if (!Logger::Logging(Severity::fatal)) { cout << "Logger expected to log fatal, but it reports not to" << endl; return 1; }
|
|
|
|
ifstream t(name);
|
|
stringstream buffer;
|
|
buffer << t.rdbuf();
|
|
string fileContent = buffer.str();
|
|
|
|
if (fileContent != "[WARN] warning\n[ERROR] error\n[FATAL] fatal\n") {
|
|
throw runtime_error(ToStr("unexpected file sink output. expected:\n[WARN] warning\n[ERROR] error\n[FATAL] fatal\nfound:\n", fileContent));
|
|
}
|
|
|
|
cout << "##### removing file sink with warn severity" << endl;
|
|
Logger::RemoveFileSink();
|
|
|
|
if (Logger::Logging(Severity::warn)) { cout << "Logger expected to NOT log warn, but it reports to do so" << endl; return 1; }
|
|
if (Logger::Logging(Severity::error)) { cout << "Logger expected to NOT log error, but it reports to do so" << endl; return 1; }
|
|
if (!Logger::Logging(Severity::fatal)) { cout << "Logger expected to log fatal, but it reports not to" << endl; return 1; }
|
|
|
|
cout << "##### adding custom sink with warn severity" << endl;
|
|
|
|
Logger::AddCustomSink("CustomSink", "warn", [](const string& content, const LogMetaData& metadata)
|
|
{
|
|
cout << "CustomSink " << content << endl;
|
|
|
|
if (metadata.severity != Severity::warn && metadata.severity != Severity::error && metadata.severity != Severity::fatal) {
|
|
throw runtime_error(ToStr("unexpected severity message arrived at custom sink that accepts only warn,error,fatal: ", Logger::fSeverityNames.at(static_cast<int>(metadata.severity))));
|
|
}
|
|
|
|
if (metadata.severity_name != "WARN" && metadata.severity_name != "ERROR" && metadata.severity_name != "FATAL") {
|
|
throw runtime_error(ToStr("unexpected severity name arrived at custom sink that accepts only warn,error,fatal: ", metadata.severity_name));
|
|
}
|
|
});
|
|
|
|
CheckOutput("^CustomSink warning\nCustomSink error\nCustomSink fatal\n\\[FATAL] fatal\n$", [](){
|
|
LOG(state) << "state";
|
|
LOG(warn) << "warning";
|
|
LOG(error) << "error";
|
|
LOG(fatal) << "fatal";
|
|
});
|
|
|
|
if (Logger::Logging(Severity::state)) { cout << "Logger expected to NOT log warn, but it reports to do so" << endl; return 1; }
|
|
if (!Logger::Logging(Severity::warn)) { cout << "Logger expected to log warn, but it reports not to" << endl; return 1; }
|
|
if (!Logger::Logging(Severity::error)) { cout << "Logger expected to log error, but it reports not to" << endl; return 1; }
|
|
if (!Logger::Logging(Severity::fatal)) { cout << "Logger expected to log fatal, but it reports not to" << endl; return 1; }
|
|
|
|
|
|
cout << "##### removing custom sink with error severity" << endl;
|
|
|
|
bool caught = false;
|
|
try {
|
|
Logger::AddCustomSink("CustomSink", Severity::error, [](const string& /*content*/, const LogMetaData& /*metadata*/){});
|
|
} catch (runtime_error& rte) {
|
|
caught = true;
|
|
}
|
|
if (!caught) {
|
|
throw runtime_error("expected to throw a runtime_error upon adding sink with same key, but none was thrown");
|
|
}
|
|
|
|
Logger::RemoveCustomSink("CustomSink");
|
|
|
|
if (Logger::Logging(Severity::warn)) { cout << "Logger expected to NOT log warn, but it reports to do so" << endl; return 1; }
|
|
if (Logger::Logging(Severity::error)) { cout << "Logger expected to NOT log error, but it reports to do so" << endl; return 1; }
|
|
if (!Logger::Logging(Severity::fatal)) { cout << "Logger expected to log fatal, but it reports not to" << endl; return 1; }
|
|
|
|
caught = false;
|
|
try {
|
|
Logger::RemoveCustomSink("bla");
|
|
} catch (runtime_error& rte) {
|
|
caught = true;
|
|
}
|
|
if (!caught) {
|
|
throw runtime_error("expected to throw a runtime_error upon removing non-existent sink, but none was thrown");
|
|
}
|
|
} catch (runtime_error& rte) {
|
|
cout << rte.what() << endl;
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|