Skip to content

Commit bb7db4f

Browse files
committed
Add more tests and fix some erros.
1 parent 8cfe269 commit bb7db4f

File tree

5 files changed

+190
-89
lines changed

5 files changed

+190
-89
lines changed

README.rst

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
|travis|
22

3-
logging
4-
=======
3+
Simple Logging Library
4+
======================
55

6-
Simple logging library.
6+
Advantages:
7+
- Has variable and constexpr activation level checks. The constexpr check
8+
compares to the compiled in activation level. It is therefor less flexible
9+
but will be fully optimized away in case the level is not high enough to
10+
trigger log output.
11+
- Makes sure that static initialization happens very early. The logger can
12+
safely be used in other classes' constructors and destructors even if those
13+
classes are used in a static storage context.
14+
- Uses log-ids that enable to relate between log output and code location.
15+
(Uniqueness is not enforced by this code. A tool might be added in the
16+
future)
17+
- Has extra log macros (`EXT_DEV`, `EXT_DEV_IF`, ...) for development. This
18+
allows for efficient removal of development artifacts.
19+
- Has optional support to output locations in formats understood by gdb and
20+
vim. (Has to be requested at compile time.)
21+
22+
23+
Disadvantages:
24+
- Works only with supported compilers (gcc, clang, msvc).
25+
- Setup **MUST** happen in single threaded part of application (may change).
726

827
.. |travis| image:: https://travis-ci.org/extcpp/logging.svg?branch=master
928
:target: https://travis-ci.org/extcpp/logging

examples/logging.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
struct test {
1111
test() {
12-
EXT_LOG("test class", error) << "hello form ctor";
12+
EXT_LOG("test class", error) << "hello from ctor";
1313
}
1414
};
1515

@@ -24,7 +24,12 @@ int main(/*int argc, const char *argv[]*/) {
2424
el::set_level_all(ext::logging::level::info);
2525

2626
EXT_DEV << "foo";
27-
EXT_DEVV << "bar";
27+
EXT_DEV_IF(true) << "bar";
28+
EXT_DEV_IF_CONST(false) << "foo";
29+
volatile bool v = false;
30+
(void) v;
31+
//EXT_DEV_IF_CONST(v) << "foo"; // if constexpr condition is not a constant expression
32+
EXT_DEV_VARIABLE << "bar";
2833

2934
// clang-format off
3035
EXT_LOG("hi mic", warn) << "1" << "2" << "3";
@@ -37,11 +42,11 @@ int main(/*int argc, const char *argv[]*/) {
3742

3843
el::set_level_all(ext::logging::level::error);
3944
EXT_LOG("2222", info) << "Hi there!";
40-
EXT_LOG_STATIC("3333", warn) << "something is wrong";
41-
EXT_LOG_STATIC("3333", warn) << "something is wrong";
45+
EXT_LOG_CONST("3333", warn) << "something is wrong";
46+
EXT_LOG_CONST("3333", warn) << "something is wrong";
4247

43-
EXT_LOG_STATIC("4444", network, error, false) << "your network is broken";
44-
EXT_LOG_STATIC("5555", network, error, true) << "your network is broken";
48+
EXT_LOG_CONST("4444", network, error, false) << "your network is broken";
49+
EXT_LOG_CONST("5555", network, error, true) << "your network is broken";
4550
EXT_LOG("music", network, warn) << "Green Day";
4651
EXT_LOG("6666", fatal) << "your app will terminate";
4752

include/ext/logging.hpp

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,23 @@
2828
#include <iostream>
2929
#include <type_traits>
3030

31-
// If use_default is true the check becomes constexpr. Therefore the compiler
32-
// can optimize the branch containing the logging instructions in case of
33-
// inactivity completely away. If you want the level to be configurable at
34-
// runtime you need to pay for it by having the instructions in your code.
31+
// helper
32+
#define _EXT_LOG_SELECT5TH_PARAMETER(_1, _2, _3, _4, NAME, ...) NAME
33+
3534

36-
// non static
35+
// variable logging - if() ...
3736
#define _EXT_LOG_INTERNAL(id_, topic_, macro_level_, cond_) \
38-
if (ext::logging::_detail::level_is_active((macro_level_), (topic_)) && cond_) \
37+
if (ext::logging::_detail::variable_level_is_active((macro_level_), (topic_)) && cond_) \
3938
ext::logging::_detail::logger(id_, (topic_), (macro_level_), __FILE__, __LINE__, __FUNCTION__)
4039

41-
// static
42-
#define _EXT_LOG_INTERNAL_STATIC(id_, topic_, macro_level_, cond_) \
43-
if constexpr (ext::logging::_detail::default_level_is_active((macro_level_)) \
44-
ext::logging::_detail::logger(id_, (topic_), (macro_level_), __FILE__, __LINE__, __FUNCTION__)
45-
4640
#define _EXT_LOG_INTERNAL_ADD_PREFIX(id_, topic_, macro_level_, cond_) \
4741
_EXT_LOG_INTERNAL(id_, (ext::logging::topic::topic_), (ext::logging::level::macro_level_), cond_)
4842

49-
#define _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id_, topic_, macro_level_, cond_) \
50-
_EXT_LOG_INTERNAL(id_, (ext::logging::topic::topic_), (ext::logging::level::macro_level_), cond_)
51-
52-
#define _EXT_LOG_SELECT5TH_PARAMETER(_1, _2, _3, _4, NAME, ...) NAME
53-
54-
// constexpr macros
55-
#define EXT_LOGVARIABLE4(id, topic_, macro_level_, cond_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, no_topic, macro_level_, cond_)
56-
43+
#define EXT_LOGVARIABLE4(id, topic_, macro_level_, cond_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, topic_, macro_level_, cond_)
5744
#define EXT_LOGVARIABLE3(id, topic_, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, topic_, macro_level_, true)
58-
5945
#define EXT_LOGVARIABLE2(id, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX(id, no_topic, macro_level_, true)
60-
6146
#define EXT_LOGVARIABLE1(id) _EXT_LOG_INTERNAL_ADD_PREFIX(id, no_topic, EXT_LOGGING_DEFAULT_LEVEL, true)
6247

63-
#define EXT_DEVC _EXT_LOG_INTERNAL_ADD_PREFIX("@@@@", dev, EXT_LOGGING_DEFAULT_LEVEL, true)
64-
6548
// 1st __VA_ARGS__ shifts the args into the correct position
6649
// macro can not be empty because of the leading `,` (fixed with __VA_OPT__ in c++20)
6750
#ifdef EXT_COMPILER_VC
@@ -76,25 +59,37 @@
7659
(__VA_ARGS__)
7760
#endif // EXT_COMPILER_VC
7861

79-
// runtime configurable macros
80-
#define EXT_LOGSTATIC4(id, topic_, macro_level_, cond_) \
81-
_EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, no_topic, macro_level_, cond_)
62+
#define EXT_DEV_VARIABLE _EXT_LOG_INTERNAL_ADD_PREFIX("$$$$", dev, EXT_LOGGING_DEFAULT_LEVEL, true)
63+
#define EXT_DEV_IF(cond_) _EXT_LOG_INTERNAL_ADD_PREFIX("$$$$", dev, EXT_LOGGING_DEFAULT_LEVEL, cond_)
64+
#define EXT_LOG EXT_LOGVARIABLE
8265

83-
#define EXT_LOGSTATIC3(id, topic_, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, topic_, macro_level_, true)
8466

85-
#define EXT_LOGSTATIC2(id, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, no_topic, macro_level_, true)
67+
// This is still a bit experimental:
68+
// constexpr if() does not seem to work in a macro is there some way that guarantees the evaluation
69+
// at compile time. Otherwise it would be better to remove this code in order to reduce the complexity.
70+
71+
#define _EXT_LOG_INTERNAL_CONST(id_, topic_, macro_level_, cond_) \
72+
if constexpr (ext::logging::_detail::constexpr_level_is_active(macro_level_) && cond_) \
73+
ext::logging::_detail::logger(id_, (topic_), (macro_level_), __FILE__, __LINE__, __FUNCTION__)
8674

87-
#define EXT_LOGSTATIC1(id) _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC(id, no_topic, EXT_LOGGING_DEFAULT_LEVEL, true)
75+
#define _EXT_LOG_INTERNAL_ADD_PREFIX_CONST(id_, topic_, macro_level_, cond_) \
76+
_EXT_LOG_INTERNAL_CONST(id_, (ext::logging::topic::topic_), (ext::logging::level::macro_level_), cond_)
8877

89-
#define EXT_DEVV _EXT_LOG_INTERNAL_ADD_PREFIX_STATIC("$$$$", dev, EXT_LOGGING_DEFAULT_LEVEL, true)
9078

91-
#define EXT_LOGSTATIC(...) \
92-
_EXT_LOG_SELECT5TH_PARAMETER(__VA_ARGS__, EXT_LOGSTATIC4, EXT_LOGSTATIC3, EXT_LOGSTATIC2, EXT_LOGSTATIC1, ) \
79+
#define EXT_LOGCONST4(id, topic_, macro_level_, cond_) \
80+
_EXT_LOG_INTERNAL_ADD_PREFIX_CONST(id, topic_, macro_level_, cond_)
81+
82+
#define EXT_LOGCONST3(id, topic_, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX_CONST(id, topic_, macro_level_, true)
83+
#define EXT_LOGCONST2(id, macro_level_) _EXT_LOG_INTERNAL_ADD_PREFIX_CONST(id, no_topic, macro_level_, true)
84+
#define EXT_LOGCONST1(id) _EXT_LOG_INTERNAL_ADD_PREFIX_CONST(id, no_topic, EXT_LOGGING_DEFAULT_LEVEL, true)
85+
86+
#define EXT_LOGCONST(...) \
87+
_EXT_LOG_SELECT5TH_PARAMETER(__VA_ARGS__, EXT_LOGCONST4, EXT_LOGCONST3, EXT_LOGCONST2, EXT_LOGCONST1, ) \
9388
(__VA_ARGS__)
9489

95-
// set default macros
96-
#define EXT_LOG EXT_LOGVARIABLE
97-
#define EXT_LOG_STATIC EXT_LOGSTATIC
98-
#define EXT_DEV EXT_DEVC
90+
#define EXT_DEV _EXT_LOG_INTERNAL_ADD_PREFIX_CONST("@@@@", dev, EXT_LOGGING_DEFAULT_LEVEL, true)
91+
#define EXT_DEV_IF_CONST(cond_) _EXT_LOG_INTERNAL_ADD_PREFIX_CONST("@@@@", dev, EXT_LOGGING_DEFAULT_LEVEL, cond_)
92+
#define EXT_LOG_CONST EXT_LOGCONST
93+
9994

10095
#endif // EXT_LOGGING_HEADER

include/ext/logging/functionality.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace ext { namespace logging {
1414

1515
namespace _detail {
16-
inline bool level_is_active(level macro_level, logtopic topic = topic::no_topic) {
16+
inline bool variable_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
@@ -27,7 +27,7 @@ inline bool level_is_active(level macro_level, logtopic topic = topic::no_topic)
2727
return topic.activation_level >= macro_level;
2828
}
2929

30-
inline constexpr bool default_level_is_active(level macro_level) {
30+
inline constexpr bool constexpr_level_is_active(level macro_level) {
3131
return _detail::logtopic::default_level >= macro_level;
3232
}
3333

0 commit comments

Comments
 (0)