Improve coverage

Signed-off-by: JCW <a1q123456@users.noreply.github.com>
This commit is contained in:
JCW
2025-08-22 13:33:09 +01:00
parent 57d57705a3
commit bb7e7ad430
4 changed files with 70 additions and 12 deletions

View File

@@ -256,19 +256,19 @@ public:
static LogSeverity
fromString(std::string const& s);
private:
enum {
// Maximum line length for log messages.
// If the message exceeds this length it will be truncated with elipses.
maximumMessageCharacters = 12 * 1024
};
static void
format(
std::string& output,
std::string const& message,
beast::severities::Severity severity,
std::string const& partition);
private:
enum {
// Maximum line length for log messages.
// If the message exceeds this length it will be truncated with elipses.
maximumMessageCharacters = 12 * 1024
};
};
// Wraps a Journal::Stream to skip evaluation of

View File

@@ -90,6 +90,12 @@ public:
m_structuredJournalImpl = impl;
}
static void
disableStructuredJournal()
{
m_structuredJournalImpl = nullptr;
}
static bool
isStructuredJournalEnabled()
{

View File

@@ -81,7 +81,46 @@ public:
}
};
TEST_CASE("Enable Json Logs")
TEST_CASE("Text logs")
{
std::stringstream logStream;
MockLogs logs{logStream, beast::severities::kAll};
logs.journal("Test").debug() << "Test";
CHECK(logStream.str().find("Test") != std::string::npos);
logStream.str("");
logs.journal("Test").debug() << "\n";
CHECK(logStream.str().find("\n") == std::string::npos);
}
TEST_CASE("Test format output")
{
std::string output;
Logs::format(output, "Message", beast::severities::kDebug, "Test");
CHECK(output.find("Message") != std::string::npos);
CHECK(output != "Message");
}
TEST_CASE("Test format output when structured logs are enabled")
{
static log::JsonStructuredJournal structuredJournal;
beast::Journal::enableStructuredJournal(&structuredJournal);
std::string output;
Logs::format(output, "Message", beast::severities::kDebug, "Test");
CHECK(output == "Message");
beast::Journal::disableStructuredJournal();
}
TEST_CASE("Enable json logs")
{
static log::JsonStructuredJournal structuredJournal;
@@ -97,7 +136,7 @@ TEST_CASE("Enable Json Logs")
beast::Journal::enableStructuredJournal(&structuredJournal);
logs.journal("Test").debug() << "Test";
logs.journal("Test").debug() << "\n";
Json::Reader reader;
Json::Value jsonLog;
@@ -108,7 +147,8 @@ TEST_CASE("Enable Json Logs")
CHECK(jsonLog.isObject());
CHECK(jsonLog.isMember("Message"));
CHECK(jsonLog["Message"].isString());
CHECK(jsonLog["Message"].asString() == "Test");
CHECK(jsonLog["Message"].asString() == "");
beast::Journal::disableStructuredJournal();
}
TEST_CASE("Global attributes")
@@ -134,6 +174,7 @@ TEST_CASE("Global attributes")
CHECK(jsonLog.isMember("Field1"));
CHECK(jsonLog["Field1"].isString());
CHECK(jsonLog["Field1"].asString() == "Value1");
beast::Journal::disableStructuredJournal();
}
TEST_CASE("Global attributes inheritable")
@@ -166,4 +207,5 @@ TEST_CASE("Global attributes inheritable")
CHECK(jsonLog["Field1"].asString() == "Value3");
CHECK(jsonLog["Field2"].isString());
CHECK(jsonLog["Field2"].asString() == "Value2");
beast::Journal::disableStructuredJournal();
}

View File

@@ -62,6 +62,11 @@ public:
beast::Journal::enableStructuredJournal(&structuredJournal);
}
~JsonLogStreamFixture()
{
beast::Journal::disableStructuredJournal();
}
std::stringstream&
stream()
{
@@ -82,7 +87,12 @@ private:
TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogFields")
{
journal().debug() << "Test";
journal().debug() << std::boolalpha
<< true
<< std::noboolalpha
<< " Test "
<< std::boolalpha
<< false;
Json::Value logValue;
Json::Reader reader;
@@ -103,7 +113,7 @@ TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogFields")
CHECK(logValue["Line"].isNumeric());
CHECK(logValue["Params"].isNull());
CHECK(logValue["Message"].isString());
CHECK(logValue["Message"].asString() == "Test");
CHECK(logValue["Message"].asString() == "true Test false");
}
TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogLevels")