Debian Codegraph @

monero/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 <boost/filesystem.hpp>
32 #include "gtest/gtest.h"
33 #include "file_io_utils.h"
34 #include "misc_log_ex.h"
35
36 static std::string log_filename;
37
38 static void init()
39 {
40 boost::filesystem::path p = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
41 log_filename = p.string();
42 mlog_configure(log_filename, false, 0);
43 }
44
45 static void cleanup()
46 {
47 // windows does not let files be deleted if still in use, so leave droppings there
48 #ifndef _WIN32
49 boost::filesystem::remove(log_filename);
50 #endif
51 }
52
53 static size_t nlines(const std::string &str)
54 {
55 size_t n = 0;
56 for (const char *ptr = str.c_str(); *ptr; ++ptr)
57 if (*ptr == '\n')
58 ++n;
59 return n;
60 }
61
62 static bool load_log_to_string(const std::string &filename, std::string &str)
63 {
64 if (!epee::file_io_utils::load_file_to_string(filename, str))
65 return false;
66 for (const char *ptr = str.c_str(); *ptr; ++ptr)
67 {
68 if (*ptr == '\n')
69 {
70 std::string prefix = std::string(str.c_str(), ptr - str.c_str());
71 if (prefix.find("New log categories:") != std::string::npos)
72 {
73 str = std::string(ptr + 1, strlen(ptr + 1));
74 break;
75 }
76 }
77 }
78 return true;
79 }
80
81 static void log()
82 {
83 MFATAL("fatal");
84 MERROR("error");
85 MWARNING("warning");
86 MINFO("info");
87 MDEBUG("debug");
88 MTRACE("trace");
89
90 MCINFO("a.b.c.d", "a.b.c.d");
91 MCINFO("a.b.c.e", "a.b.c.e");
92 MCINFO("global", "global");
93 MCINFO("x.y.z", "x.y.z");
94 MCINFO("y.y.z", "y.y.z");
95 MCINFO("x.y.x", "x.y.x");
96 }
97
98 TEST(logging, no_logs)
99 {
100 init();
101 mlog_set_categories("");
102 log();
103 std::string str;
104 ASSERT_TRUE(load_log_to_string(log_filename, str));
105 ASSERT_TRUE(str == "");
106 cleanup();
107 }
108
109 TEST(logging, default)
110 {
111 init();
112 log();
113 std::string str;
114 ASSERT_TRUE(load_log_to_string(log_filename, str));
115 ASSERT_TRUE(str.find("global") != std::string::npos);
116 ASSERT_TRUE(str.find("fatal") != std::string::npos);
117 ASSERT_TRUE(str.find("error") != std::string::npos);
118 ASSERT_TRUE(str.find("debug") == std::string::npos);
119 ASSERT_TRUE(str.find("trace") == std::string::npos);
120 cleanup();
121 }
122
123 TEST(logging, all)
124 {
125 init();
126 mlog_set_categories("*:TRACE");
127 log();
128 std::string str;
129 ASSERT_TRUE(load_log_to_string(log_filename, str));
130 ASSERT_TRUE(str.find("global") != std::string::npos);
131 ASSERT_TRUE(str.find("fatal") != std::string::npos);
132 ASSERT_TRUE(str.find("error") != std::string::npos);
133 ASSERT_TRUE(str.find("debug") != std::string::npos);
134 ASSERT_TRUE(str.find("trace") != std::string::npos);
135 cleanup();
136 }
137
138 TEST(logging, glob_suffix)
139 {
140 init();
141 mlog_set_categories("x.y*:TRACE");
142 log();
143 std::string str;
144 ASSERT_TRUE(load_log_to_string(log_filename, str));
145 ASSERT_TRUE(str.find("global") == std::string::npos);
146 ASSERT_TRUE(str.find("x.y.z") != std::string::npos);
147 ASSERT_TRUE(str.find("x.y.x") != std::string::npos);
148 ASSERT_TRUE(str.find("y.y.z") == std::string::npos);
149 cleanup();
150 }
151
152 TEST(logging, glob_prefix)
153 {
154 init();
155 mlog_set_categories("*y.z:TRACE");
156 log();
157 std::string str;
158 ASSERT_TRUE(load_log_to_string(log_filename, str));
159 ASSERT_TRUE(str.find("global") == std::string::npos);
160 ASSERT_TRUE(str.find("x.y.z") != std::string::npos);
161 ASSERT_TRUE(str.find("x.y.x") == std::string::npos);
162 ASSERT_TRUE(str.find("y.y.z") != std::string::npos);
163 cleanup();
164 }
165
166 TEST(logging, last_precedence)
167 {
168 init();
169 mlog_set_categories("gobal:FATAL,glo*:DEBUG");
170 log();
171 std::string str;
172 ASSERT_TRUE(load_log_to_string(log_filename, str));
173 ASSERT_TRUE(nlines(str) == 1);
174 ASSERT_TRUE(str.find("global") != std::string::npos);
175 ASSERT_TRUE(str.find("x.y.z") == std::string::npos);
176 ASSERT_TRUE(str.find("x.y.x") == std::string::npos);
177 ASSERT_TRUE(str.find("y.y.z") == std::string::npos);
178 cleanup();
179 }
180
181 TEST(logging, multiline)
182 {
183 init();
184 mlog_set_categories("global:INFO");
185 MGINFO("first\nsecond\nthird");
186 std::string str;
187 ASSERT_TRUE(load_log_to_string(log_filename, str));
188 ASSERT_TRUE(nlines(str) == 3);
189 ASSERT_TRUE(str.find("global") != std::string::npos);
190 ASSERT_TRUE(str.find("first") != std::string::npos);
191 ASSERT_TRUE(str.find("second") != std::string::npos);
192 ASSERT_TRUE(str.find("third") != std::string::npos);
193 ASSERT_TRUE(str.find("first\nsecond") == std::string::npos);
194 ASSERT_TRUE(str.find("second\nthird") == std::string::npos);
195 cleanup();
196 }
197
198 // These operations might segfault
199 TEST(logging, copy_ctor_segfault)
200 {
201 const el::Logger log1("id1", nullptr);
202 const el::Logger log2(log1);
203 }
204
205 TEST(logging, operator_equals_segfault)
206 {
207 const el::Logger log1("id1", nullptr);
208 el::Logger log2("id2", nullptr);
209 log2 = log1;
210 }
211
212 TEST(logging, empty_configurations_throws)
213 {
214 el::Logger log1("id1", nullptr);
215 const el::Configurations cfg;
216 EXPECT_ANY_THROW(log1.configure(cfg));
217 }