nestlog: + explicit+max indent width

This commit is contained in:
Roland Conybeare 2023-09-15 15:25:17 -04:00
commit 5a47539dbb
4 changed files with 51 additions and 17 deletions

View file

@ -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));
}

View file

@ -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>(contents)} {}
std::uint32_t color() const { return color_; }
Contents const & contents() const { return contents_; }
@ -77,17 +77,17 @@ namespace xo {
template <typename Contents>
color_impl<Contents> with_ansi_color(std::uint32_t color, Contents && contents) {
return color_impl<Contents>(CF_All, CE_Ansi, color, std::move(contents));
return color_impl<Contents>(CF_All, CE_Ansi, color, std::forward(contents));
} /*with_ansi_color*/
template <typename Contents>
color_impl<Contents> with_xterm_color(std::uint32_t color, Contents && contents) {
return color_impl<Contents>(CF_All, CE_Xterm, color, std::move(contents));
return color_impl<Contents>(CF_All, CE_Xterm, color, std::forward(contents));
} /*with_ansi_color*/
template <typename Contents>
color_impl<Contents> with_color(color_encoding encoding, std::uint32_t color, Contents && contents) {
return color_impl<Contents>(CF_All, encoding, color, std::move(contents));
return color_impl<Contents>(CF_All, encoding, color, std::forward<Contents>(contents));
} /*with_color*/
inline color_impl<int>

View file

@ -10,8 +10,14 @@ namespace xo {
/* Tag here b/c we want header-only library */
template <typename Tag>
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<Tag>::indent_width = 1;
template <typename Tag>
std::uint32_t
log_config_impl<Tag>::max_indent_width = 32;
template <typename Tag>
bool
log_config_impl<Tag>::nesting_level_enabled = true;
template <typename Tag>
std::uint32_t
log_config_impl<Tag>::nesting_level_color = 195;
template <typename Tag>
function_style
log_config_impl<Tag>::style = FS_Streamlined;

View file

@ -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 <typename CharT, typename Traits>
@ -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;