5
5
#include < logging.h>
6
6
#include < logging/timer.h>
7
7
#include < test/util/setup_common.h>
8
+ #include < util/string.h>
8
9
9
10
#include < chrono>
11
+ #include < fstream>
12
+ #include < iostream>
13
+ #include < utility>
14
+ #include < vector>
10
15
11
16
#include < boost/test/unit_test.hpp>
12
17
13
18
BOOST_FIXTURE_TEST_SUITE (logging_tests, BasicTestingSetup)
14
19
20
+ struct LogSetup : public BasicTestingSetup {
21
+ fs::path prev_log_path;
22
+ fs::path tmp_log_path;
23
+ bool prev_reopen_file;
24
+ bool prev_print_to_file;
25
+ bool prev_log_timestamps;
26
+ bool prev_log_threadnames;
27
+ bool prev_log_sourcelocations;
28
+
29
+ LogSetup () : prev_log_path{LogInstance ().m_file_path },
30
+ tmp_log_path{m_args.GetDataDirBase () / " tmp_debug.log" },
31
+ prev_reopen_file{LogInstance ().m_reopen_file },
32
+ prev_print_to_file{LogInstance ().m_print_to_file },
33
+ prev_log_timestamps{LogInstance ().m_log_timestamps },
34
+ prev_log_threadnames{LogInstance ().m_log_threadnames },
35
+ prev_log_sourcelocations{LogInstance ().m_log_sourcelocations }
36
+ {
37
+ LogInstance ().m_file_path = tmp_log_path;
38
+ LogInstance ().m_reopen_file = true ;
39
+ LogInstance ().m_print_to_file = true ;
40
+ LogInstance ().m_log_timestamps = false ;
41
+ LogInstance ().m_log_threadnames = false ;
42
+ LogInstance ().m_log_sourcelocations = true ;
43
+ }
44
+
45
+ ~LogSetup ()
46
+ {
47
+ LogInstance ().m_file_path = prev_log_path;
48
+ LogPrintf (" Sentinel log to reopen log file\n " );
49
+ LogInstance ().m_print_to_file = prev_print_to_file;
50
+ LogInstance ().m_reopen_file = prev_reopen_file;
51
+ LogInstance ().m_log_timestamps = prev_log_timestamps;
52
+ LogInstance ().m_log_threadnames = prev_log_threadnames;
53
+ LogInstance ().m_log_sourcelocations = prev_log_sourcelocations;
54
+ }
55
+ };
56
+
15
57
BOOST_AUTO_TEST_CASE (logging_timer)
16
58
{
17
59
SetMockTime (1 );
@@ -30,4 +72,82 @@ BOOST_AUTO_TEST_CASE(logging_timer)
30
72
BOOST_CHECK_EQUAL (sec_timer.LogMsg (" test secs" ), " tests: test secs (1.00s)" );
31
73
}
32
74
75
+ BOOST_FIXTURE_TEST_CASE (logging_LogPrintf_, LogSetup)
76
+ {
77
+ LogPrintf_ (" fn1" , " src1" , 1 , BCLog::LogFlags::NET, BCLog::Level::Debug, " foo1: %s" , " bar1\n " );
78
+ LogPrintf_ (" fn2" , " src2" , 2 , BCLog::LogFlags::NET, BCLog::Level::None, " foo2: %s" , " bar2\n " );
79
+ LogPrintf_ (" fn3" , " src3" , 3 , BCLog::LogFlags::NONE, BCLog::Level::Debug, " foo3: %s" , " bar3\n " );
80
+ LogPrintf_ (" fn4" , " src4" , 4 , BCLog::LogFlags::NONE, BCLog::Level::None, " foo4: %s" , " bar4\n " );
81
+ std::ifstream file{tmp_log_path};
82
+ std::vector<std::string> log_lines;
83
+ for (std::string log; std::getline (file, log);) {
84
+ log_lines.push_back (log);
85
+ }
86
+ std::vector<std::string> expected = {
87
+ " [src1:1] [fn1] [net:debug] foo1: bar1" ,
88
+ " [src2:2] [fn2] [net] foo2: bar2" ,
89
+ " [src3:3] [fn3] [debug] foo3: bar3" ,
90
+ " [src4:4] [fn4] foo4: bar4" ,
91
+ };
92
+ BOOST_CHECK_EQUAL_COLLECTIONS (log_lines.begin (), log_lines.end (), expected.begin (), expected.end ());
93
+ }
94
+
95
+ BOOST_FIXTURE_TEST_CASE (logging_LogPrintMacros, LogSetup)
96
+ {
97
+ // Prevent tests from failing when the line number of the following log calls changes.
98
+ LogInstance ().m_log_sourcelocations = false ;
99
+
100
+ LogPrintf (" foo5: %s\n " , " bar5" );
101
+ LogPrint (BCLog::NET, " foo6: %s\n " , " bar6" );
102
+ LogPrintLevel (BCLog::Level::Debug, BCLog::NET, " foo7: %s\n " , " bar7" );
103
+ LogPrintLevel (BCLog::Level::Info, BCLog::NET, " foo8: %s\n " , " bar8" );
104
+ LogPrintLevel (BCLog::Level::Warning, BCLog::NET, " foo9: %s\n " , " bar9" );
105
+ LogPrintLevel (BCLog::Level::Error, BCLog::NET, " foo10: %s\n " , " bar10" );
106
+ std::ifstream file{tmp_log_path};
107
+ std::vector<std::string> log_lines;
108
+ for (std::string log; std::getline (file, log);) {
109
+ log_lines.push_back (log);
110
+ }
111
+ std::vector<std::string> expected = {
112
+ " foo5: bar5" ,
113
+ " [net] foo6: bar6" ,
114
+ " [net:debug] foo7: bar7" ,
115
+ " [net:info] foo8: bar8" ,
116
+ " [net:warning] foo9: bar9" ,
117
+ " [net:error] foo10: bar10" };
118
+ BOOST_CHECK_EQUAL_COLLECTIONS (log_lines.begin (), log_lines.end (), expected.begin (), expected.end ());
119
+ }
120
+
121
+ BOOST_FIXTURE_TEST_CASE (logging_LogPrintMacros_CategoryName, LogSetup)
122
+ {
123
+ // Prevent tests from failing when the line number of the following log calls changes.
124
+ LogInstance ().m_log_sourcelocations = false ;
125
+ LogInstance ().EnableCategory (BCLog::LogFlags::ALL);
126
+ const auto concated_categery_names = LogInstance ().LogCategoriesString ();
127
+ std::vector<std::pair<BCLog::LogFlags, std::string>> expected_category_names;
128
+ const auto category_names = SplitString (concated_categery_names, ' ,' );
129
+ for (const auto & category_name : category_names) {
130
+ BCLog::LogFlags category = BCLog::NONE;
131
+ const auto trimmed_category_name = TrimString (category_name);
132
+ BOOST_TEST (GetLogCategory (category, trimmed_category_name));
133
+ expected_category_names.emplace_back (category, trimmed_category_name);
134
+ }
135
+
136
+ std::vector<std::string> expected;
137
+ for (const auto & [category, name] : expected_category_names) {
138
+ LogPrint (category, " foo: %s\n " , " bar" );
139
+ std::string expected_log = " [" ;
140
+ expected_log += name;
141
+ expected_log += " ] foo: bar" ;
142
+ expected.push_back (expected_log);
143
+ }
144
+
145
+ std::ifstream file{tmp_log_path};
146
+ std::vector<std::string> log_lines;
147
+ for (std::string log; std::getline (file, log);) {
148
+ log_lines.push_back (log);
149
+ }
150
+ BOOST_CHECK_EQUAL_COLLECTIONS (log_lines.begin (), log_lines.end (), expected.begin (), expected.end ());
151
+ }
152
+
33
153
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments