diff --git a/README.md b/README.md index a011415..e909f2c 100644 --- a/README.md +++ b/README.md @@ -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 ======== diff --git a/clutchlog/clutchlog.h b/clutchlog/clutchlog.h index b5a09ef..7ddf721 100644 --- a/clutchlog/clutchlog.h +++ b/clutchlog/clutchlog.h @@ -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 diff --git a/tests/t-depth-delta.cpp b/tests/t-depth-delta.cpp new file mode 100644 index 0000000..0db389d --- /dev/null +++ b/tests/t-depth-delta.cpp @@ -0,0 +1,58 @@ +#include +#include + +#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 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(); +} +