Skip to content

Commit e1ab7e6

Browse files
committed
Improve tests and examples.
1 parent 5a435c3 commit e1ab7e6

File tree

6 files changed

+153
-45
lines changed

6 files changed

+153
-45
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ target_compile_options(ext-logging PRIVATE ${ext_stone-warnings})
6969
target_compile_definitions(ext-logging PUBLIC ${ext_common_compile_definitions})
7070
target_compile_definitions(ext-logging PUBLIC EXT_SHARED_LIB)
7171
target_compile_definitions(ext-logging PRIVATE EXT_IN_LIB=1)
72-
target_link_libraries(ext-logging PUBLIC ext::basics Threads::Threads)
72+
target_link_libraries(ext-logging PUBLIC
73+
ext::basics
74+
Threads::Threads
75+
$<$<BOOL:UNIX>:stdc++fs>
76+
)
7377

7478
# set up folder structure for XCode and VisualStudio
7579
set_target_properties (ext-logging PROPERTIES FOLDER logging)

examples/logging.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,44 @@
55
#include <vector>
66

77
#ifndef _WIN32
8-
#include <ext/logging.hpp>
8+
#include <ext/logging.hpp>
9+
10+
struct test {
11+
test() {
12+
EXT_LOG("test class", error) << "hello form ctor";
13+
}
14+
};
15+
16+
static test someclass{};
917

1018
int main(/*int argc, const char *argv[]*/) {
11-
{
12-
using namespace ext::logging;
13-
configuration::function = false;
14-
configuration::append_newline = true;
15-
}
19+
namespace el = ext::logging;
1620

17-
EXT_DEV << "geil";
18-
EXT_DEVV << "ballern";
21+
using namespace ext::logging;
22+
el::configuration::function = false;
23+
el::configuration::append_newline = true;
24+
el::set_level_all(ext::logging::level::info);
1925

20-
EXT_LOG("mic", info) << "1"
21-
<< "2"
22-
<< "3";
26+
EXT_DEV << "foo";
27+
EXT_DEVV << "bar";
28+
29+
// clang-format off
30+
EXT_LOG("hi mic", warn) << "1" << "2" << "3";
31+
// clang-format on
2332

2433
EXT_LOG("cafe") << "default level and topic";
2534
EXT_LOG("0000", trace) << "where is the byte gone";
2635
EXT_LOG("1111", network, debug) << "ohlala";
2736
EXT_LOG("2222", info) << "Hi there!";
37+
38+
el::set_level_all(ext::logging::level::error);
39+
EXT_LOG("2222", info) << "Hi there!";
40+
EXT_LOG_STATIC("3333", warn) << "something is wrong";
2841
EXT_LOG_STATIC("3333", warn) << "something is wrong";
42+
2943
EXT_LOG_STATIC("4444", network, error, false) << "your network is broken";
3044
EXT_LOG_STATIC("5555", network, error, true) << "your network is broken";
45+
EXT_LOG("music", network, warn) << "Green Day";
3146
EXT_LOG("6666", fatal) << "your app will terminate";
3247

3348
#else

include/ext/logging.hpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333
// inactivity completely away. If you want the level to be configurable at
3434
// runtime you need to pay for it by having the instructions in your code.
3535

36-
// TODO -- static_assert parameters
36+
// non static
3737
#define _EXT_LOG_INTERNAL(id_, topic_, macro_level_, cond_) \
3838
if (ext::logging::_detail::level_is_active((macro_level_), (topic_)) && cond_) \
3939
ext::logging::_detail::logger(id_, (topic_), (macro_level_), __FILE__, __LINE__, __FUNCTION__)
4040

41+
// static
4142
#define _EXT_LOG_INTERNAL_STATIC(id_, topic_, macro_level_, cond_) \
4243
if constexpr (ext::logging::_detail::default_level_is_active((macro_level_)) \
4344
ext::logging::_detail::logger(id_, (topic_), (macro_level_), __FILE__, __LINE__, __FUNCTION__)
@@ -51,13 +52,13 @@
5152
#define _EXT_LOG_SELECT5TH_PARAMETER(_1, _2, _3, _4, NAME, ...) NAME
5253

5354
// constexpr macros
54-
#define EXT_LOGC4(id, topic_, macro_level_, cond_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, no_topic, macro_level_, cond_)
55+
#define EXT_LOGVARIABLE4(id, topic_, macro_level_, cond_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, no_topic, macro_level_, cond_)
5556

56-
#define EXT_LOGC3(id, topic_, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, topic_, macro_level_, true)
57+
#define EXT_LOGVARIABLE3(id, topic_, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, topic_, macro_level_, true)
5758

58-
#define EXT_LOGC2(id, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, no_topic, macro_level_, true)
59+
#define EXT_LOGVARIABLE2(id, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, no_topic, macro_level_, true)
5960

60-
#define EXT_LOGC1(id) _EXT_LOG_INTERNAL_ADD_PREFIX(id, no_topic, EXT_LOGGING_DEFAULT_LEVEL, true)
61+
#define EXT_LOGVARIABLE1(id) _EXT_LOG_INTERNAL_ADD_PREFIX(id, no_topic, EXT_LOGGING_DEFAULT_LEVEL, true)
6162

6263
#define EXT_DEVC _EXT_LOG_INTERNAL_ADD_PREFIX("@@@@", dev, EXT_LOGGING_DEFAULT_LEVEL, true)
6364

@@ -66,34 +67,34 @@
6667
#ifdef EXT_COMPILER_VC
6768
// __VA_ARGS__ does not expand on VisualStudio compiler :(
6869
#define _EXT_LOG_EXPAND(x) x
69-
#define EXT_LOGC(...) \
70-
_EXT_LOG_SELECT5TH_PARAMETER(_EXT_LOG_EXPAND(__VA_ARGS__), EXT_LOGC4, EXT_LOGC3, EXT_LOGC2, EXT_LOGC1, ) \
70+
#define EXT_LOGVARIABLE(...) \
71+
_EXT_LOG_SELECT5TH_PARAMETER(_EXT_LOG_EXPAND(__VA_ARGS__), EXT_LOGVARIABLE4, EXT_LOGVARIABLE3, EXT_LOGVARIABLE2, EXT_LOGVARIABLE1, ) \
7172
(_EXT_LOG_EXPAND(__VA_ARGS__))
7273
#else
73-
#define EXT_LOGC(...) \
74-
_EXT_LOG_SELECT5TH_PARAMETER(__VA_ARGS__, EXT_LOGC4, EXT_LOGC3, EXT_LOGC2, EXT_LOGC1, ) \
74+
#define EXT_LOGVARIABLE(...) \
75+
_EXT_LOG_SELECT5TH_PARAMETER(__VA_ARGS__, EXT_LOGVARIABLE4, EXT_LOGVARIABLE3, EXT_LOGVARIABLE2, EXT_LOGVARIABLE1, ) \
7576
(__VA_ARGS__)
7677
#endif // EXT_COMPILER_VC
7778

7879
// runtime configurable macros
79-
#define EXT_LOGV4(id, topic_, macro_level_, cond_) \
80+
#define EXT_LOGSTATIC4(id, topic_, macro_level_, cond_) \
8081
_EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, no_topic, macro_level_, cond_)
8182

82-
#define EXT_LOGV3(id, topic_, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, topic_, macro_level_, true)
83+
#define EXT_LOGSTATIC3(id, topic_, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, topic_, macro_level_, true)
8384

84-
#define EXT_LOGV2(id, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, no_topic, macro_level_, true)
85+
#define EXT_LOGSTATIC2(id, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, no_topic, macro_level_, true)
8586

86-
#define EXT_LOGV1(id) _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, no_topic, EXT_LOGGING_DEFAULT_LEVEL, true)
87+
#define EXT_LOGSTATIC1(id) _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, no_topic, EXT_LOGGING_DEFAULT_LEVEL, true)
8788

8889
#define EXT_DEVV _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC("$$$$", dev, EXT_LOGGING_DEFAULT_LEVEL, true)
8990

90-
#define EXT_LOGV(...) \
91-
_EXT_LOG_SELECT5TH_PARAMETER(__VA_ARGS__, EXT_LOGV4, EXT_LOGV3, EXT_LOGV2, EXT_LOGV1, ) \
91+
#define EXT_LOGSTATIC(...) \
92+
_EXT_LOG_SELECT5TH_PARAMETER(__VA_ARGS__, EXT_LOGSTATIC4, EXT_LOGSTATIC3, EXT_LOGSTATIC2, EXT_LOGSTATIC1, ) \
9293
(__VA_ARGS__)
9394

9495
// set default macros
95-
#define EXT_LOG EXT_LOGC
96-
#define EXT_LOG_STATIC EXT_LOGV
96+
#define EXT_LOG EXT_LOGVARIABLE
97+
#define EXT_LOG_STATIC EXT_LOGSTATIC
9798
#define EXT_DEV EXT_DEVC
9899

99100
#endif // EXT_LOGGING_HEADER

include/ext/logging/functionality.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ inline bool level_is_active(level macro_level, logtopic topic = topic::no_topic)
1717
// activation_level 60(info) && macro_level 20 (error) -> log
1818
// activation_level 60(info) && macro_level 100(trace) -> no log
1919
// activation level must be greater than macro level
20+
#ifdef NOT_DEFINED
21+
std::cerr << "####################" << std::endl;
22+
std::cerr << "activation_level: " << level_to_str(topic.activation_level) << std::endl;
23+
std::cerr << "macro_level: " << level_to_str(macro_level) << std::endl;
24+
std::cerr << "activates: " << std::boolalpha << (topic.activation_level >= macro_level) << std::endl;
25+
std::cerr << "####################" << std::endl;
26+
#endif
2027
return topic.activation_level >= macro_level;
2128
}
2229

src/logging.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ EXT_INIT_PRIORITY_GNU(102)
1717
std::map<int, _detail::logtopic*> _detail::topics_map{};
1818

1919
EXT_INIT_PRIORITY_GNU(103)
20-
_detail::logtopic topic::no_topic{1, "default"s, level::info};
20+
_detail::logtopic topic::no_topic{1, "default"s, level::warn};
2121
EXT_INIT_PRIORITY_GNU(103)
2222
_detail::logtopic topic::dev{2, "development"s, level::debug};
2323
EXT_INIT_PRIORITY_GNU(103)
24-
_detail::logtopic topic::network{3, "network"s, level::error};
24+
_detail::logtopic topic::network{3, "network"s, level::warn};
2525
EXT_INIT_PRIORITY_GNU(103)
26-
_detail::logtopic topic::engine{4, "engine"s, level::error};
26+
_detail::logtopic topic::engine{4, "engine"s, level::warn};
2727

2828
bool configuration::prefix_newline{false};
2929
bool configuration::append_newline{true};

tests/logging.cpp

Lines changed: 94 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,153 @@
11
// Copyright - 2016-2020 - Jan Christoph Uhde <[email protected]>
22
// Please see LICENSE.md for license or visit https://github.com/extcpp/basics
33
#include <cstring>
4-
#include <ext/logging.hpp>
4+
#include <fstream>
5+
#include <filesystem>
6+
7+
#include <gtest/gtest.h>
8+
59
#include <ext/macros/platform.hpp>
610
#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+
816

917
using namespace std::literals;
1018

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;
1247

1348
};
1449
using LoggingDeathTest = LoggingTest;
1550

16-
TEST_F(LoggingTest, logging_no_crash) {
51+
TEST_F(LoggingTest, logging_no_crash_gdb_vim) {
1752
using namespace ext::logging;
18-
1953
configuration::gdb = true;
2054
configuration::vim = true;
2155
configuration::prefix_newline = false;
2256
configuration::append_newline = true;
2357

58+
_line = __LINE__ + 1;
2459
ASSERT_NO_THROW(EXT_LOG("babe") << "cafe?");
2560

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;
2670
configuration::gdb = false;
2771
configuration::vim = false;
2872
configuration::prefix_newline = true;
2973
configuration::append_newline = false;
3074

75+
_line = __LINE__ + 1;
3176
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'");
33103
}
34104

35105
// does not die
36106
#ifndef EXT_COMPILER_VC
37107
TEST_F(LoggingDeathTest, fatal) {
38108
::testing::FLAGS_gtest_death_test_style = "threadsafe";
39109
using namespace ext::logging;
40-
41110
ASSERT_DEATH_IF_SUPPORTED(EXT_LOG("work", network, fatal) << "What?!?! No Cafe!?!?!? :(", "");
42111
}
43112
#endif // EXT_COMPILER_VC
44113

45-
TEST_F(LoggingTest, levels) {
114+
TEST_F(LoggingTest, change_all_levels) {
46115
using namespace ext::logging;
47116

48117
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));
50120
EXPECT_FALSE(_detail::level_is_active(level::trace));
51121

122+
// fix compiled in
52123
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));
54126
EXPECT_FALSE(_detail::default_level_is_active(level::trace));
55127

56-
set_level_all(level::warn);
128+
set_level_all(level::error);
57129

58130
EXPECT_TRUE(_detail::level_is_active(level::error));
131+
EXPECT_FALSE(_detail::level_is_active(level::warn));
59132
EXPECT_FALSE(_detail::level_is_active(level::info));
60133
EXPECT_FALSE(_detail::level_is_active(level::trace));
61134

135+
// fix compiled in
62136
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));
64139
EXPECT_FALSE(_detail::default_level_is_active(level::trace));
65140
}
66141

67-
TEST_F(LoggingTest, levels_to_string) {
142+
TEST_F(LoggingTest, change_single_level) {
68143
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+
}
69148

149+
TEST_F(LoggingTest, levels_to_string) {
150+
using namespace ext::logging;
70151
EXPECT_STREQ(_detail::level_to_str(level::fatal).c_str(), "fatal");
71152
EXPECT_STREQ(_detail::level_to_str(level::error).c_str(), "error");
72153
EXPECT_STREQ(_detail::level_to_str(level::warn).c_str(), "warning");

0 commit comments

Comments
 (0)