Skip to content

Commit 78383c7

Browse files
committed
Improve coverage
Signed-off-by: JCW <[email protected]>
1 parent 42b743a commit 78383c7

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

include/xrpl/basics/Log.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,19 @@ class Logs
256256
static LogSeverity
257257
fromString(std::string const& s);
258258

259-
private:
260-
enum {
261-
// Maximum line length for log messages.
262-
// If the message exceeds this length it will be truncated with elipses.
263-
maximumMessageCharacters = 12 * 1024
264-
};
265-
266259
static void
267260
format(
268261
std::string& output,
269262
std::string const& message,
270263
beast::severities::Severity severity,
271264
std::string const& partition);
265+
266+
private:
267+
enum {
268+
// Maximum line length for log messages.
269+
// If the message exceeds this length it will be truncated with elipses.
270+
maximumMessageCharacters = 12 * 1024
271+
};
272272
};
273273

274274
// Wraps a Journal::Stream to skip evaluation of

include/xrpl/beast/utility/Journal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ class Journal
9090
m_structuredJournalImpl = impl;
9191
}
9292

93+
static void
94+
disableStructuredJournal()
95+
{
96+
m_structuredJournalImpl = nullptr;
97+
}
98+
9399
static bool
94100
isStructuredJournalEnabled()
95101
{

src/tests/libxrpl/basics/log.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,46 @@ class MockLogs : public Logs
8181
}
8282
};
8383

84-
TEST_CASE("Enable Json Logs")
84+
TEST_CASE("Text logs")
85+
{
86+
std::stringstream logStream;
87+
88+
MockLogs logs{logStream, beast::severities::kAll};
89+
90+
logs.journal("Test").debug() << "Test";
91+
92+
CHECK(logStream.str().find("Test") != std::string::npos);
93+
94+
logStream.str("");
95+
96+
logs.journal("Test").debug() << "\n";
97+
98+
CHECK(logStream.str().find("\n") == std::string::npos);
99+
}
100+
101+
102+
TEST_CASE("Test format output")
103+
{
104+
std::string output;
105+
Logs::format(output, "Message", beast::severities::kDebug, "Test");
106+
CHECK(output.find("Message") != std::string::npos);
107+
CHECK(output != "Message");
108+
}
109+
110+
TEST_CASE("Test format output when structured logs are enabled")
111+
{
112+
static log::JsonStructuredJournal structuredJournal;
113+
beast::Journal::enableStructuredJournal(&structuredJournal);
114+
115+
std::string output;
116+
Logs::format(output, "Message", beast::severities::kDebug, "Test");
117+
118+
CHECK(output == "Message");
119+
120+
beast::Journal::disableStructuredJournal();
121+
}
122+
123+
TEST_CASE("Enable json logs")
85124
{
86125
static log::JsonStructuredJournal structuredJournal;
87126

@@ -97,7 +136,7 @@ TEST_CASE("Enable Json Logs")
97136

98137
beast::Journal::enableStructuredJournal(&structuredJournal);
99138

100-
logs.journal("Test").debug() << "Test";
139+
logs.journal("Test").debug() << "\n";
101140

102141
Json::Reader reader;
103142
Json::Value jsonLog;
@@ -108,7 +147,8 @@ TEST_CASE("Enable Json Logs")
108147
CHECK(jsonLog.isObject());
109148
CHECK(jsonLog.isMember("Message"));
110149
CHECK(jsonLog["Message"].isString());
111-
CHECK(jsonLog["Message"].asString() == "Test");
150+
CHECK(jsonLog["Message"].asString() == "");
151+
beast::Journal::disableStructuredJournal();
112152
}
113153

114154
TEST_CASE("Global attributes")
@@ -134,6 +174,7 @@ TEST_CASE("Global attributes")
134174
CHECK(jsonLog.isMember("Field1"));
135175
CHECK(jsonLog["Field1"].isString());
136176
CHECK(jsonLog["Field1"].asString() == "Value1");
177+
beast::Journal::disableStructuredJournal();
137178
}
138179

139180
TEST_CASE("Global attributes inheritable")
@@ -166,4 +207,5 @@ TEST_CASE("Global attributes inheritable")
166207
CHECK(jsonLog["Field1"].asString() == "Value3");
167208
CHECK(jsonLog["Field2"].isString());
168209
CHECK(jsonLog["Field2"].asString() == "Value2");
210+
beast::Journal::disableStructuredJournal();
169211
}

src/tests/libxrpl/telemetry/json_logs.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class JsonLogStreamFixture
6262
beast::Journal::enableStructuredJournal(&structuredJournal);
6363
}
6464

65+
~JsonLogStreamFixture()
66+
{
67+
beast::Journal::disableStructuredJournal();
68+
}
69+
6570
std::stringstream&
6671
stream()
6772
{
@@ -82,7 +87,12 @@ class JsonLogStreamFixture
8287

8388
TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogFields")
8489
{
85-
journal().debug() << "Test";
90+
journal().debug() << std::boolalpha
91+
<< true
92+
<< std::noboolalpha
93+
<< " Test "
94+
<< std::boolalpha
95+
<< false;
8696

8797
Json::Value logValue;
8898
Json::Reader reader;
@@ -103,7 +113,7 @@ TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogFields")
103113
CHECK(logValue["Line"].isNumeric());
104114
CHECK(logValue["Params"].isNull());
105115
CHECK(logValue["Message"].isString());
106-
CHECK(logValue["Message"].asString() == "Test");
116+
CHECK(logValue["Message"].asString() == "true Test false");
107117
}
108118

109119
TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogLevels")

0 commit comments

Comments
 (0)