Electroneum
logging.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2019, 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();
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 
#define MERROR(x)
Definition: misc_log_ex.h:73
#define MTRACE(x)
Definition: misc_log_ex.h:77
#define MINFO(x)
Definition: misc_log_ex.h:75
#define MFATAL(x)
Definition: misc_log_ex.h:72
::std::string string
Definition: gtest-port.h:1097
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size=MAX_LOG_FILE_SIZE, const std::size_t max_log_files=MAX_LOG_FILES)
Definition: mlog.cpp:148
bool load_file_to_string(const std::string &path_to_file, std::string &target_str, size_t max_size=1000000000)
#define MDEBUG(x)
Definition: misc_log_ex.h:76
void mlog_set_categories(const char *categories)
Definition: mlog.cpp:238
TEST(logging, no_logs)
Definition: logging.cpp:98
#define MWARNING(x)
Definition: misc_log_ex.h:74
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
#define MCINFO(cat, x)
Definition: misc_log_ex.h:53