|
1 | 1 | // Copyright - 2016-2020 - Jan Christoph Uhde <[email protected]> |
2 | 2 | // Please see LICENSE.md for license or visit https://github.com/extcpp/basics |
3 | 3 | #include <cstring> |
4 | | -#include <ext/logging.hpp> |
| 4 | +#include <fstream> |
| 5 | +#include <filesystem> |
| 6 | + |
| 7 | +#include <gtest/gtest.h> |
| 8 | + |
5 | 9 | #include <ext/macros/platform.hpp> |
6 | 10 | #include <ext/util/except.hpp> |
7 | | -#include <gtest/gtest.h> |
| 11 | +#include <ext/util/files.hpp> |
| 12 | + |
| 13 | +#define EXT_LOGGING_DEFAULT_LEVEL warn |
| 14 | +#include <ext/logging.hpp> |
| 15 | + |
8 | 16 |
|
9 | 17 | using namespace std::literals; |
10 | 18 |
|
11 | | -class LoggingTest : public ::testing::Test { |
| 19 | +struct LoggingTest : public ::testing::Test { |
| 20 | + LoggingTest() : _log{"test.log", std::ios::trunc} { |
| 21 | + ext::logging::configuration::stream = &_log; |
| 22 | + }; |
| 23 | + |
| 24 | + |
| 25 | + void compare(std::string const& expected = ""){ |
| 26 | + _log.close(); |
| 27 | + std::ifstream file{"test.log"}; |
| 28 | + std::istreambuf_iterator<char> eos; |
| 29 | + std::string logged{std::istreambuf_iterator<char>(file), eos}; |
| 30 | + ASSERT_EQ(expected, logged); |
| 31 | + } |
| 32 | + |
| 33 | + std::string path() { |
| 34 | + auto path = std::filesystem::current_path().parent_path() |
| 35 | + .parent_path() |
| 36 | + .parent_path() |
| 37 | + / "logging" / "tests" / "logging.cpp"; |
| 38 | + return path.string(); |
| 39 | + }; |
| 40 | + |
| 41 | + std::string line() { |
| 42 | + return std::to_string(_line); |
| 43 | + } |
| 44 | + |
| 45 | + std::ofstream _log; |
| 46 | + std::size_t _line; |
12 | 47 |
|
13 | 48 | }; |
14 | 49 | using LoggingDeathTest = LoggingTest; |
15 | 50 |
|
16 | | -TEST_F(LoggingTest, logging_no_crash) { |
| 51 | +TEST_F(LoggingTest, logging_no_crash_gdb_vim) { |
17 | 52 | using namespace ext::logging; |
18 | | - |
19 | 53 | configuration::gdb = true; |
20 | 54 | configuration::vim = true; |
21 | 55 | configuration::prefix_newline = false; |
22 | 56 | configuration::append_newline = true; |
23 | 57 |
|
| 58 | + _line = __LINE__ + 1; |
24 | 59 | ASSERT_NO_THROW(EXT_LOG("babe") << "cafe?"); |
25 | 60 |
|
| 61 | + compare( |
| 62 | + "# vim "s + path() + " +" + line() +"\n" |
| 63 | + "# break logging.cpp:" + line() + "\n" |
| 64 | + "[babe] warning in TestBody(): 'cafe?'\n" |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +TEST_F(LoggingTest, logging_no_crash) { |
| 69 | + using namespace ext::logging; |
26 | 70 | configuration::gdb = false; |
27 | 71 | configuration::vim = false; |
28 | 72 | configuration::prefix_newline = true; |
29 | 73 | configuration::append_newline = false; |
30 | 74 |
|
| 75 | + _line = __LINE__ + 1; |
31 | 76 | ASSERT_NO_THROW(EXT_LOG("babe") << "2cafe?"); |
32 | | - ASSERT_NO_THROW(EXT_LOG("music", network, warn) << "Green Day"); |
| 77 | + compare("\n[babe] warning logging.cpp:" + line() + " in TestBody(): '2cafe?'"); |
| 78 | +} |
| 79 | + |
| 80 | +TEST_F(LoggingTest, logging_level_too_low) { |
| 81 | + using namespace ext::logging; |
| 82 | + configuration::gdb = false; |
| 83 | + configuration::vim = false; |
| 84 | + configuration::prefix_newline = true; |
| 85 | + configuration::append_newline = false; |
| 86 | + topic::network.activation_level = level::error; |
| 87 | + |
| 88 | + _line = __LINE__ + 1; |
| 89 | + ASSERT_NO_THROW(EXT_LOG("music", network, warn) << "Green" << "Day"); |
| 90 | + compare(""); |
| 91 | +} |
| 92 | + |
| 93 | +TEST_F(LoggingTest, logging_level_ok) { |
| 94 | + using namespace ext::logging; |
| 95 | + configuration::gdb = false; |
| 96 | + configuration::vim = false; |
| 97 | + configuration::prefix_newline = true; |
| 98 | + configuration::append_newline = false; |
| 99 | + |
| 100 | + _line = __LINE__ + 1; |
| 101 | + ASSERT_NO_THROW(EXT_LOG("music", network, error) << "Green" << " Day"); |
| 102 | + compare("\n[music] error (network) logging.cpp:" + line() +" in TestBody(): 'Green Day'"); |
33 | 103 | } |
34 | 104 |
|
35 | 105 | // does not die |
36 | 106 | #ifndef EXT_COMPILER_VC |
37 | 107 | TEST_F(LoggingDeathTest, fatal) { |
38 | 108 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
39 | 109 | using namespace ext::logging; |
40 | | - |
41 | 110 | ASSERT_DEATH_IF_SUPPORTED(EXT_LOG("work", network, fatal) << "What?!?! No Cafe!?!?!? :(", ""); |
42 | 111 | } |
43 | 112 | #endif // EXT_COMPILER_VC |
44 | 113 |
|
45 | | -TEST_F(LoggingTest, levels) { |
| 114 | +TEST_F(LoggingTest, change_all_levels) { |
46 | 115 | using namespace ext::logging; |
47 | 116 |
|
48 | 117 | EXPECT_TRUE(_detail::level_is_active(level::error)); |
49 | | - EXPECT_TRUE(_detail::level_is_active(level::info)); |
| 118 | + EXPECT_TRUE(_detail::level_is_active(level::warn)); |
| 119 | + EXPECT_FALSE(_detail::level_is_active(level::info)); |
50 | 120 | EXPECT_FALSE(_detail::level_is_active(level::trace)); |
51 | 121 |
|
| 122 | + // fix compiled in |
52 | 123 | EXPECT_TRUE(_detail::default_level_is_active(level::error)); |
53 | | - EXPECT_TRUE(_detail::default_level_is_active(level::info)); |
| 124 | + EXPECT_TRUE(_detail::default_level_is_active(level::warn)); |
| 125 | + EXPECT_FALSE(_detail::default_level_is_active(level::info)); |
54 | 126 | EXPECT_FALSE(_detail::default_level_is_active(level::trace)); |
55 | 127 |
|
56 | | - set_level_all(level::warn); |
| 128 | + set_level_all(level::error); |
57 | 129 |
|
58 | 130 | EXPECT_TRUE(_detail::level_is_active(level::error)); |
| 131 | + EXPECT_FALSE(_detail::level_is_active(level::warn)); |
59 | 132 | EXPECT_FALSE(_detail::level_is_active(level::info)); |
60 | 133 | EXPECT_FALSE(_detail::level_is_active(level::trace)); |
61 | 134 |
|
| 135 | + // fix compiled in |
62 | 136 | EXPECT_TRUE(_detail::default_level_is_active(level::error)); |
63 | | - EXPECT_TRUE(_detail::default_level_is_active(level::info)); |
| 137 | + EXPECT_TRUE(_detail::default_level_is_active(level::warn)); |
| 138 | + EXPECT_FALSE(_detail::default_level_is_active(level::info)); |
64 | 139 | EXPECT_FALSE(_detail::default_level_is_active(level::trace)); |
65 | 140 | } |
66 | 141 |
|
67 | | -TEST_F(LoggingTest, levels_to_string) { |
| 142 | +TEST_F(LoggingTest, change_single_level) { |
68 | 143 | using namespace ext::logging; |
| 144 | + EXPECT_FALSE(_detail::level_is_active(level::info, topic::network)); |
| 145 | + topic::network.activation_level = level::info; |
| 146 | + EXPECT_TRUE(_detail::level_is_active(level::info, topic::network)); |
| 147 | +} |
69 | 148 |
|
| 149 | +TEST_F(LoggingTest, levels_to_string) { |
| 150 | + using namespace ext::logging; |
70 | 151 | EXPECT_STREQ(_detail::level_to_str(level::fatal).c_str(), "fatal"); |
71 | 152 | EXPECT_STREQ(_detail::level_to_str(level::error).c_str(), "error"); |
72 | 153 | EXPECT_STREQ(_detail::level_to_str(level::warn).c_str(), "warning"); |
|
0 commit comments