From 5a47539dbbd81881290005fc133d224c0c0629ab Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Fri, 15 Sep 2023 15:25:17 -0400 Subject: [PATCH] nestlog: + explicit+max indent width --- example/ex3/ex3.cpp | 8 ++++---- include/nestlog/color.hpp | 8 ++++---- include/nestlog/log_config.hpp | 20 +++++++++++++++++++- include/nestlog/log_state.hpp | 32 ++++++++++++++++++++++++-------- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/example/ex3/ex3.cpp b/example/ex3/ex3.cpp index 64682a8a..89d7ad2c 100644 --- a/example/ex3/ex3.cpp +++ b/example/ex3/ex3.cpp @@ -12,10 +12,9 @@ fib(int n) { if (n >= 2) { retval = fib(n - 1) + fib(n - 2); - log(tag("n", n)); } - log.end_scope("<-", xtag("retval", retval)); + log.end_scope(tag("n", n), " <-", xtag("retval", retval)); return retval; } @@ -24,7 +23,8 @@ int main(int argc, char ** argv) { log_config::style = FS_Streamlined; log_config::indent_width = 4; - log_config::location_tab = 40; + log_config::max_indent_width = 14; + log_config::location_tab = 70; log_config::encoding = CE_Xterm; log_config::function_entry_color = 69; log_config::function_exit_color = 70; @@ -36,7 +36,7 @@ main(int argc, char ** argv) { int fn = fib(n); - log(xtag("n", n)); + xtag("n", n); log("<-", xtag("fib(n)", fn)); } diff --git a/include/nestlog/color.hpp b/include/nestlog/color.hpp index 8f5a4d74..2c2419c2 100644 --- a/include/nestlog/color.hpp +++ b/include/nestlog/color.hpp @@ -25,7 +25,7 @@ namespace xo { class color_impl { public: color_impl(color_flags flags, color_encoding encoding, std::uint32_t color, Contents && contents) - : flags_{flags}, encoding_{encoding}, color_{color}, contents_{std::move(contents)} {} + : flags_{flags}, encoding_{encoding}, color_{color}, contents_{std::forward(contents)} {} std::uint32_t color() const { return color_; } Contents const & contents() const { return contents_; } @@ -77,17 +77,17 @@ namespace xo { template color_impl with_ansi_color(std::uint32_t color, Contents && contents) { - return color_impl(CF_All, CE_Ansi, color, std::move(contents)); + return color_impl(CF_All, CE_Ansi, color, std::forward(contents)); } /*with_ansi_color*/ template color_impl with_xterm_color(std::uint32_t color, Contents && contents) { - return color_impl(CF_All, CE_Xterm, color, std::move(contents)); + return color_impl(CF_All, CE_Xterm, color, std::forward(contents)); } /*with_ansi_color*/ template color_impl with_color(color_encoding encoding, std::uint32_t color, Contents && contents) { - return color_impl(CF_All, encoding, color, std::move(contents)); + return color_impl(CF_All, encoding, color, std::forward(contents)); } /*with_color*/ inline color_impl diff --git a/include/nestlog/log_config.hpp b/include/nestlog/log_config.hpp index 3046ef44..cf5ea3aa 100644 --- a/include/nestlog/log_config.hpp +++ b/include/nestlog/log_config.hpp @@ -10,8 +10,14 @@ namespace xo { /* Tag here b/c we want header-only library */ template struct log_config_impl { - /* spaces per indent level */ + /* spaces per nesting level */ static std::uint32_t indent_width; + /* max #of spaces to introduce when indenting */ + static std::uint32_t max_indent_width; + /* if true enable explicit nesting level display [nnn] */ + static bool nesting_level_enabled; + /* color to use for explicit nesting level */ + static std::uint32_t nesting_level_color; /* display style for function names. FS_Simple|FS_Pretty|FS_Streamlined */ static function_style style; /* color encoding */ @@ -33,6 +39,18 @@ namespace xo { std::uint32_t log_config_impl::indent_width = 1; + template + std::uint32_t + log_config_impl::max_indent_width = 32; + + template + bool + log_config_impl::nesting_level_enabled = true; + + template + std::uint32_t + log_config_impl::nesting_level_color = 195; + template function_style log_config_impl::style = FS_Streamlined; diff --git a/include/nestlog/log_state.hpp b/include/nestlog/log_state.hpp index fa48adf6..8273a6ec 100644 --- a/include/nestlog/log_state.hpp +++ b/include/nestlog/log_state.hpp @@ -126,8 +126,14 @@ namespace xo { } #endif - /* indent to nesting level */ - this->ss_ << pad(this->nesting_level_ * log_config::indent_width, pad_char); + /* indent to nesting level. + * + * note: see also flush2sbuf(), need special indent handling for continuation lines + * (when application sends explicit newlines to this logger) + */ + this->ss_ << pad(std::min(this->nesting_level_ * log_config::indent_width, + log_config::max_indent_width), + pad_char); } /*indent*/ template @@ -161,6 +167,15 @@ namespace xo { this->ss_ << ee_label; + if (log_config::nesting_level_enabled) { + this->ss_ + << "(" + << with_color(log_config::encoding, + log_config::nesting_level_color, + this->nesting_level_) + << ")"; + } + if (log_config::indent_width > 1) this->ss_ << ' '; @@ -257,7 +272,7 @@ namespace xo { sbuf2->sputn(s, p - s - 1); if (this->location_flag_) { - /* 'tab' to position 80 */ + /* 'tab' to position lpos for [file:line] */ sbuf2->sputc(' '); for (std::uint32_t i = lpos_on_newline + 1; i < log_config::location_tab; ++i) sbuf2->sputc(' '); @@ -280,9 +295,8 @@ namespace xo { sbuf2->sputn(s, p - s); } - if (p == e) { + if (p == e) break; - } // { // char buf[80]; @@ -293,14 +307,16 @@ namespace xo { /* at least 1 char following newline, need to indent for it * - minimum indent = nesting level; - * - however if space_after_nonspace defined, indent to that + * - however if space_after_nonspace defined, also indent for that */ - uint32_t n_indent = this->nesting_level_; + std::uint32_t n_indent = std::min(this->nesting_level_ * log_config::indent_width, + log_config::max_indent_width); + /* this is just to indent for per-line entry/exit label */ if(space_after_nonspace) n_indent += (space_after_nonspace - s); - for(uint32_t i = 0; i < n_indent; ++i) + for(std::uint32_t i = 0; i < n_indent; ++i) sbuf2->sputc(' '); s = p;