From 44ffe6309a705d47b505298fe106bbc2bb45a6e6 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sun, 19 Dec 2021 08:30:19 +0100 Subject: [PATCH] fix {depth} display, warnings and doc - fix call stack depth display with {depth}: remove (5) stripped depths. - fix warnings in tests - Explain CLUTCHLOG_HAVE_UNIX_SYSINFO in the README. --- README.md | 17 +++++++++++++++++ clutchlog/clutchlog.h | 4 +++- tests/t-assert.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/t-demo.cpp | 2 +- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/t-assert.cpp diff --git a/README.md b/README.md index 983b6e4..c9893f5 100644 --- a/README.md +++ b/README.md @@ -345,16 +345,33 @@ Note: the log levels constants are lower case (for example: `clutchlog::level::x Limitations =========== +### System-dependent stack depth + Because the call stack depth and program name access are system-dependent, the features relying on the depth of the call stack and the display of the program name are only available for operating systems having the following headers: `execinfo.h`, `stdlib.h` and `libgen.h` (so far, tested with Linux). +Clutchlog sets the `CLUTCHLOG_HAVE_UNIX_SYSINFO` to 1 if the headers are +available, and to 0 if they are not. +You can make portable code using something like: +```cpp +#if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1 + log.depth( x ); +#endif +``` + + +### Dependencies + Some colors/styles may not be supported by some exotic terminal emulators. Clutchlog needs `C++-17` with the `filesystem` feature. You may need to indicate `-std=c++17 -lstdc++fs` to some compilers. + +### Features + What Clutchlog do not provide at the moment (but may in a near future): - Super fast log writing. diff --git a/clutchlog/clutchlog.h b/clutchlog/clutchlog.h index f1469f6..9a2fd9e 100644 --- a/clutchlog/clutchlog.h +++ b/clutchlog/clutchlog.h @@ -26,6 +26,7 @@ namespace fs = std::filesystem; #include // execinfo #include // getenv #include // basename +//! POSIX headers necessary for stack depth management are available. #define CLUTCHLOG_HAVE_UNIX_SYSINFO 1 #else #define CLUTCHLOG_HAVE_UNIX_SYSINFO 0 @@ -36,6 +37,7 @@ namespace fs = std::filesystem; **********************************************************************/ #ifndef WITH_CLUTCHLOG #ifndef NDEBUG +//! Actually enable clutchlog features. #define WITH_CLUTCHLOG #endif #endif @@ -662,7 +664,7 @@ class clutchlog #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1 format = replace(format, "\\{name\\}", name); - format = replace(format, "\\{depth\\}", depth); + format = replace(format, "\\{depth\\}", depth - _strip_calls); std::ostringstream chevrons; for(size_t i = _strip_calls; i < depth; ++i) { diff --git a/tests/t-assert.cpp b/tests/t-assert.cpp new file mode 100644 index 0000000..b240ac5 --- /dev/null +++ b/tests/t-assert.cpp @@ -0,0 +1,42 @@ +#include +#include + +#include "../clutchlog/clutchlog.h" + +// Make asserts (de)clutchable. +#define ASSERT(LEVEL, ...) { CLUTCHFUNC(LEVEL, assert, __VA_ARGS__) } + +void h() +{ + CLUTCHLOG(info, "!"); + ASSERT(info, true == true); + std::clog << "--" << std::endl; +} + +void g() +{ + CLUTCHLOG(warning, "world"); + ASSERT(warning, strcmp("life","life") == 0); + h(); +} + +void f() +{ + CLUTCHLOG(error, "hello "); + ASSERT(error, strcmp("no more","please")!=0); + g(); +} + +int main(/*const int argc, char* argv[]*/) +{ + auto& log = clutchlog::logger(); + + log.func("f"); + f(); + + log.func("g"); + f(); + + log.func("h"); + f(); +} diff --git a/tests/t-demo.cpp b/tests/t-demo.cpp index 76ca4fe..bfc1ea8 100644 --- a/tests/t-demo.cpp +++ b/tests/t-demo.cpp @@ -49,7 +49,7 @@ int main(const int argc, char* argv[]) } else { try { log.threshold(log.level_of(argv[1])); - } catch(std::out_of_range err) { + } catch(std::out_of_range& err) { CLUTCHLOG(critical,err.what()); exit(100); }