Debian Codegraph @

tests/unit_tests/logging.cpp

raw symbols SCIP
1 // Copyright (c) 2016-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <condition_variable>
32 #include <mutex>
33 #include <thread>
34
35 #include <boost/filesystem.hpp>
36 #include "gtest/gtest.h"
37 #include "file_io_utils.h"
38 #include "misc_log_ex.h"
39
40 static std::string log_filename;
41
42 static void init()
43 {
44 boost::filesystem::path p = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
45 log_filename = p.string();
46 mlog_configure(log_filename, false, 0);
47 }
48
49 static void cleanup()
50 {
51 // windows does not let files be deleted if still in use, so leave droppings there
52 #ifndef _WIN32
53 boost::filesystem::remove(log_filename);
54 #endif
55 }
56
57 static size_t nlines(const std::string &str)
58 {
59 size_t n = 0;
60 for (const char *ptr = str.c_str(); *ptr; ++ptr)
61 if (*ptr == '\n')
62 ++n;
63 return n;
64 }
65
66 static bool load_log_to_string(const std::string &filename, std::string &str)
67 {
68 if (!epee::file_io_utils::load_file_to_string(filename, str))
69 return false;
70 for (const char *ptr = str.c_str(); *ptr; ++ptr)
71 {
72 if (*ptr == '\n')
73 {
74 std::string prefix = std::string(str.c_str(), ptr - str.c_str());
75 if (prefix.find("New log categories:") != std::string::npos)
76 {
77 str = std::string(ptr + 1, strlen(ptr + 1));
78 break;
79 }
80 }
81 }
82 return true;
83 }
84
85 static void log()
86 {
87 MFATAL("fatal");
88 MERROR("error");
89 MWARNING("warning");
90 MINFO("info");
91 MDEBUG("debug");
92 MTRACE("trace");
93
94 MCINFO("a.b.c.d", "a.b.c.d");
95 MCINFO("a.b.c.e", "a.b.c.e");
96 MCINFO("global", "global");
97 MCINFO("x.y.z", "x.y.z");
98 MCINFO("y.y.z", "y.y.z");
99 MCINFO("x.y.x", "x.y.x");
100 }
101
102 TEST(logging, no_logs)
103 {
104 init();
105 mlog_set_categories("");
106 log();
107 std::string str;
108 ASSERT_TRUE(load_log_to_string(log_filename, str));
109 ASSERT_TRUE(str == "");
110 cleanup();
111 }
112
113 TEST(logging, default)
114 {
115 init();
116 log();
117 std::string str;
118 ASSERT_TRUE(load_log_to_string(log_filename, str));
119 ASSERT_TRUE(str.find("global") != std::string::npos);
120 ASSERT_TRUE(str.find("fatal") != std::string::npos);
121 ASSERT_TRUE(str.find("error") != std::string::npos);
122 ASSERT_TRUE(str.find("debug") == std::string::npos);
123 ASSERT_TRUE(str.find("trace") == std::string::npos);
124 cleanup();
125 }
126
127 TEST(logging, all)
128 {
129 init();
130 mlog_set_categories("*:TRACE");
131 log();
132 std::string str;
133 ASSERT_TRUE(load_log_to_string(log_filename, str));
134 ASSERT_TRUE(str.find("global") != std::string::npos);
135 ASSERT_TRUE(str.find("fatal") != std::string::npos);
136 ASSERT_TRUE(str.find("error") != std::string::npos);
137 ASSERT_TRUE(str.find("debug") != std::string::npos);
138 ASSERT_TRUE(str.find("trace") != std::string::npos);
139 cleanup();
140 }
141
142 TEST(logging, glob_suffix)
143 {
144 init();
145 mlog_set_categories("x.y*:TRACE");
146 log();
147 std::string str;
148 ASSERT_TRUE(load_log_to_string(log_filename, str));
149 ASSERT_TRUE(str.find("global") == std::string::npos);
150 ASSERT_TRUE(str.find("x.y.z") != std::string::npos);
151 ASSERT_TRUE(str.find("x.y.x") != std::string::npos);
152 ASSERT_TRUE(str.find("y.y.z") == std::string::npos);
153 cleanup();
154 }
155
156 TEST(logging, glob_prefix)
157 {
158 init();
159 mlog_set_categories("*y.z:TRACE");
160 log();
161 std::string str;
162 ASSERT_TRUE(load_log_to_string(log_filename, str));
163 ASSERT_TRUE(str.find("global") == std::string::npos);
164 ASSERT_TRUE(str.find("x.y.z") != std::string::npos);
165 ASSERT_TRUE(str.find("x.y.x") == std::string::npos);
166 ASSERT_TRUE(str.find("y.y.z") != std::string::npos);
167 cleanup();
168 }
169
170 TEST(logging, last_precedence)
171 {
172 init();
173 mlog_set_categories("gobal:FATAL,glo*:DEBUG");
174 log();
175 std::string str;
176 ASSERT_TRUE(load_log_to_string(log_filename, str));
177 ASSERT_TRUE(nlines(str) == 1);
178 ASSERT_TRUE(str.find("global") != std::string::npos);
179 ASSERT_TRUE(str.find("x.y.z") == std::string::npos);
180 ASSERT_TRUE(str.find("x.y.x") == std::string::npos);
181 ASSERT_TRUE(str.find("y.y.z") == std::string::npos);
182 cleanup();
183 }
184
185 TEST(logging, multiline)
186 {
187 init();
188 mlog_set_categories("global:INFO");
189 MGINFO("first\nsecond\nthird");
190 std::string str;
191 ASSERT_TRUE(load_log_to_string(log_filename, str));
192 ASSERT_TRUE(nlines(str) == 3);
193 ASSERT_TRUE(str.find("global") != std::string::npos);
194 ASSERT_TRUE(str.find("first") != std::string::npos);
195 ASSERT_TRUE(str.find("second") != std::string::npos);
196 ASSERT_TRUE(str.find("third") != std::string::npos);
197 ASSERT_TRUE(str.find("first\nsecond") == std::string::npos);
198 ASSERT_TRUE(str.find("second\nthird") == std::string::npos);
199 cleanup();
200 }
201
202 // These operations might segfault
203 TEST(logging, copy_ctor_segfault)
204 {
205 const el::Logger log1("id1", nullptr);
206 const el::Logger log2(log1);
207 }
208
209 TEST(logging, operator_equals_segfault)
210 {
211 const el::Logger log1("id1", nullptr);
212 el::Logger log2("id2", nullptr);
213 log2 = log1;
214 }
215
216 TEST(logging, empty_configurations_throws)
217 {
218 el::Logger log1("id1", nullptr);
219 const el::Configurations cfg;
220 EXPECT_ANY_THROW(log1.configure(cfg));
221 }
222
223 TEST(logging, deadlock)
224 {
225 std::mutex inner_mutex;
226
227 // 1. Thread 1 starts logger
228 // 2. Thread 2 grabs inner mutex shared across threads
229 // 3. Thread 2 logs
230 // 4. Thread 1 grabs inner mutex shared across threads
231 // 5. Thread 1 finishes logging
232 std::condition_variable cv1, cv2;
233 std::mutex mutex1, mutex2;
234 std::unique_lock<std::mutex> lock_until_t1_starts_logger(mutex1);
235 std::unique_lock<std::mutex> lock_until_t2_finishes_logging(mutex2);
236 bool t1_started_logger = false;
237 bool t2_finished_logging = false;
238
239 const auto thread1_func = [&]
240 {
241 const auto thread1_inner_func = [&]() -> std::string
242 {
243 t1_started_logger = true;
244 lock_until_t1_starts_logger.unlock();
245 cv1.notify_one();
246 cv2.wait(lock_until_t2_finishes_logging, [&]{return t2_finished_logging;});
247
248 std::lock_guard<std::mutex> guard(inner_mutex);
249 return "world!";
250 };
251 MGINFO("Hello, " << thread1_inner_func() << " - Sincerely, thread 1");
252 };
253
254 const auto thread2_func = [&]
255 {
256 cv1.wait(lock_until_t1_starts_logger, [&]{return t1_started_logger;});
257
258 {
259 std::lock_guard<std::mutex> guard(inner_mutex);
260 MGINFO("Hello, world! - Sincerely, thread 2");
261 }
262
263 t2_finished_logging = true;
264 lock_until_t2_finishes_logging.unlock();
265 cv2.notify_one();
266 };
267
268 std::thread t1(thread1_func);
269 std::thread t2(thread2_func);
270
271 t1.join();
272 t2.join();
273 }