feat(stack): adds a call macro with depth delta

Implements #9
This commit is contained in:
Johann Dreo 2023-01-29 21:28:11 +01:00
commit d5aa2d829b
3 changed files with 99 additions and 15 deletions

View file

@ -566,6 +566,23 @@ CLUTCHCODE(info,
```
Manually Increase Stack Depth
-----------------------------
You may want to manually increase the stack depth for a given logging call,
for instance to subdivise a single function in sections.
To do so, you can use the `CLUTCHLOGD` macro, which take an additional argument,
in the form of the number of additional (fake) stack depths you want:
```cpp
CLUTCHLOG( debug, "Call"); // Regular macro.
CLUTCHLOGD(debug, "Sub call", 1); // Adds an additional (fake) stack depth.
CLUTCHLOGD(debug, "Sub sub!", 2); // Adds two additional (fake) stack depths.
```
That way, the depth will be rendered to the actual depth, plus the additional
depth delta. Note that the displayed function will stay the same. Any filtering
on the stack depth will take into account the fake depth and not the real one.
Examples
========

View file

@ -76,23 +76,32 @@
//! Handy shortcuts to location.
#define CLUTCHLOC __FILE__, __FUNCTION__, __LINE__
//! Log a message at the given level.
//! Log a message at the given level and with a given depth delta.
#ifndef NDEBUG
#define CLUTCHLOG( LEVEL, WHAT ) do { \
auto& clutchlog__logger = clutchlog::logger(); \
std::ostringstream clutchlog__msg ; clutchlog__msg << WHAT; \
clutchlog__logger.log(clutchlog::level::LEVEL, clutchlog__msg.str(), CLUTCHLOC); \
#define CLUTCHLOGD( LEVEL, WHAT, DEPTH_DELTA ) do { \
auto& clutchlog__logger = clutchlog::logger(); \
std::ostringstream clutchlog__msg ; clutchlog__msg << WHAT; \
clutchlog__logger.log(clutchlog::level::LEVEL, clutchlog__msg.str(), CLUTCHLOC, DEPTH_DELTA); \
} while(0)
#else // not Debug build.
#define CLUTCHLOG( LEVEL, WHAT ) do { \
if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
auto& clutchlog__logger = clutchlog::logger(); \
std::ostringstream clutchlog__msg ; clutchlog__msg << WHAT; \
clutchlog__logger.log(clutchlog::level::LEVEL, clutchlog__msg.str(), CLUTCHLOC); \
} \
#define CLUTCHLOGD( LEVEL, WHAT, DEPTH_DELTA ) do { \
if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
auto& clutchlog__logger = clutchlog::logger(); \
std::ostringstream clutchlog__msg ; clutchlog__msg << WHAT; \
clutchlog__logger.log(clutchlog::level::LEVEL, clutchlog__msg.str(), CLUTCHLOC, DEPTH_DELTA); \
} \
} while(0)
#endif // NDEBUG
//! Log a message at the given level.
#ifndef NDEBUG
#define CLUTCHLOG( LEVEL, WHAT ) \
CLUTCHLOGD(LEVEL, WHAT, 0)
#else // not Debug build.
#define CLUTCHLOG( LEVEL, WHAT ) \
CLUTCHLOGD(LEVEL, WHAT, 0)
#endif // NDEBUG
//! Dump the given container.
#ifndef NDEBUG
#define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do { \
@ -1364,7 +1373,8 @@ class clutchlog
void log(
const level& stage,
const std::string& what,
const std::string& file, const std::string& func, size_t line
const std::string& file, const std::string& func, const size_t line,
const size_t depth_delta = 0
) const
{
scope_t scope = locate(stage, file, func, line);
@ -1373,12 +1383,11 @@ class clutchlog
#if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
*_out << format(_format_log, what, basename(getenv("_")),
stage, file, func,
line, scope.depth );
line, scope.depth + depth_delta );
#else
*_out << format(_format_log, what,
stage, file, func,
line );
#endif
_out->flush();
} // if scopes.matches
@ -1389,7 +1398,7 @@ class clutchlog
void dump(
const level& stage,
const In container_begin, const In container_end,
const std::string& file, const std::string& func, size_t line,
const std::string& file, const std::string& func, const size_t line,
const std::string& filename_template = "dump_{n}.dat",
const std::string sep = dump_default_sep
) const

58
tests/t-depth-delta.cpp Normal file
View file

@ -0,0 +1,58 @@
#include <iostream>
#include <limits>
#include "../clutchlog/clutchlog.h"
void deepcall()
{
CLUTCHLOG(warning,"at depth 4");
CLUTCHLOGD(warning,"at depth 4+1", 1);
CLUTCHLOGD(warning,"at depth 4+2", 2);
}
void subsubsubcall()
{
CLUTCHLOG(warning,"at depth 3");
CLUTCHLOGD(warning,"at depth 3+1", 1);
CLUTCHLOGD(warning,"at depth 3+2", 2);
deepcall();
}
void subsubcall()
{
CLUTCHLOG(warning,"at depth 2");
CLUTCHLOGD(warning,"at depth 2+1", 1);
CLUTCHLOGD(warning,"at depth 2+2", 2);
subsubsubcall();
}
void subcall()
{
CLUTCHLOG(warning,"at depth 1");
CLUTCHLOGD(warning,"at depth 1+1", 1);
CLUTCHLOGD(warning,"at depth 1+2", 2);
subsubcall();
}
int main(/*const int argc, char* argv[]*/)
{
auto& log = clutchlog::logger();
using fmt = clutchlog::fmt;
using typo = clutchlog::fmt::typo;
fmt reset(typo::reset);
std::ostringstream tpl;
tpl << "{depth_fmt}{depth} {depth_marks}"
<< reset << "{funchash_fmt}in {func} {msg}\t\n";
log.format(tpl.str());
log.threshold(clutchlog::level::xdebug);
std::vector<fmt> greys = {fmt(15)};
for(unsigned short i=255; i > 231; i-=3) {
greys.push_back( fmt(i) ); }
log.depth_styles( greys );
log.depth_mark("| ");
CLUTCHLOG(warning,"in main");
subcall();
}