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.
This commit is contained in:
Johann Dreo 2021-12-19 08:30:19 +01:00
commit 44ffe6309a
4 changed files with 63 additions and 2 deletions

View file

@ -345,16 +345,33 @@ Note: the log levels constants are lower case (for example: `clutchlog::level::x
Limitations Limitations
=========== ===========
### System-dependent stack depth
Because the call stack depth and program name access are system-dependent, 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 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: are only available for operating systems having the following headers:
`execinfo.h`, `stdlib.h` and `libgen.h` (so far, tested with Linux). `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. Some colors/styles may not be supported by some exotic terminal emulators.
Clutchlog needs `C++-17` with the `filesystem` feature. Clutchlog needs `C++-17` with the `filesystem` feature.
You may need to indicate `-std=c++17 -lstdc++fs` to some compilers. 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): What Clutchlog do not provide at the moment (but may in a near future):
- Super fast log writing. - Super fast log writing.

View file

@ -26,6 +26,7 @@ namespace fs = std::filesystem;
#include <execinfo.h> // execinfo #include <execinfo.h> // execinfo
#include <stdlib.h> // getenv #include <stdlib.h> // getenv
#include <libgen.h> // basename #include <libgen.h> // basename
//! POSIX headers necessary for stack depth management are available.
#define CLUTCHLOG_HAVE_UNIX_SYSINFO 1 #define CLUTCHLOG_HAVE_UNIX_SYSINFO 1
#else #else
#define CLUTCHLOG_HAVE_UNIX_SYSINFO 0 #define CLUTCHLOG_HAVE_UNIX_SYSINFO 0
@ -36,6 +37,7 @@ namespace fs = std::filesystem;
**********************************************************************/ **********************************************************************/
#ifndef WITH_CLUTCHLOG #ifndef WITH_CLUTCHLOG
#ifndef NDEBUG #ifndef NDEBUG
//! Actually enable clutchlog features.
#define WITH_CLUTCHLOG #define WITH_CLUTCHLOG
#endif #endif
#endif #endif
@ -662,7 +664,7 @@ class clutchlog
#if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
format = replace(format, "\\{name\\}", name); format = replace(format, "\\{name\\}", name);
format = replace(format, "\\{depth\\}", depth); format = replace(format, "\\{depth\\}", depth - _strip_calls);
std::ostringstream chevrons; std::ostringstream chevrons;
for(size_t i = _strip_calls; i < depth; ++i) { for(size_t i = _strip_calls; i < depth; ++i) {

42
tests/t-assert.cpp Normal file
View file

@ -0,0 +1,42 @@
#include <iostream>
#include <cassert>
#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();
}

View file

@ -49,7 +49,7 @@ int main(const int argc, char* argv[])
} else { } else {
try { try {
log.threshold(log.level_of(argv[1])); log.threshold(log.level_of(argv[1]));
} catch(std::out_of_range err) { } catch(std::out_of_range& err) {
CLUTCHLOG(critical,err.what()); CLUTCHLOG(critical,err.what());
exit(100); exit(100);
} }