xo-umbrella2/xo-indentlog2/example/ex3f/ex3f.cpp
Roland Conybeare fb013ea267
All checks were successful
cmake-docker / cmake-build (clang, clang++) (push) Successful in 30m28s
cmake-docker / cmake-build (gcc, g++) (push) Successful in 29m12s
CI / smoke-test (push) Successful in 53m49s
xo-ppsink +: use appcx for pretty printer config [REFACTOR]
2026-09-02 00:10:49 -04:00

68 lines
1.8 KiB
C++

/* example ex3f/ex3f.cpp
*
* @author Roland Conybeare, Jul 2026
*
* Colored nesting counters, routed through the arena-backed PrettySink
* (from xo-indentlog2) -- like xo-ppsink/example/ex3f but exercising the real
* pretty-print engine rather than the degenerate FlatSink.
*
* scope_config::nesting_level_enabled shows the depth "(N)" after the +/-
* banner marker; nesting_level_color colors just the depth (xterm 195).
* Run in a terminal to see color; piped, the raw \033[...m escapes show.
*/
#include <xo/indentlog2/print/PrettySink.hpp>
#include <xo/arena/ArenaConfig.hpp>
#include <xo/ppsink/color.hpp>
#include <xo/ppsink/scope.hpp>
#include <xo/ppsink/scope_macros.hpp>
#include <xo/ppsink/tag.hpp>
#include <iostream>
#include <string>
/* XO_ENTER0_ (trailing underscore) coexists with legacy xo-indentlog's
* XO_ENTER0, which PrettySink -> xo-arena pulls into this TU.
*/
using xo::pp::scope;
using xo::pp::scope_config;
using xo::pp::log_level;
using xo::pp::tag;
using xo::pp::xtag;
using xo::pp::PrettySink;
using xo::pp::PpConfig;
using xo::pp::ThreadLogState;
using xo::mm::ArenaConfig;
int
fib(int n) {
scope log(XO_ENTER0_(info), tag("n", n));
int retval = 1;
if (n >= 2)
retval = fib(n - 1) + fib(n - 2);
log.end_scope(tag("n", n), " <-", xtag("retval", retval));
return retval;
}
int
main(int argc, char ** argv) {
scope_config::min_log_level = log_level::info;
scope_config::indent_width = 2;
scope_config::nesting_level_enabled = true; /* show "(N)" depth (default light-blue color) */
auto pp = std::make_unique<PrettySink>(PpConfig::colored(), nullptr /*out*/);
ThreadLogState::log_set_sink(std::move(pp));
fib(3);
ThreadLogState::log_set_sink(nullptr); /* restore default (clog) */
std::cout << pp->output();
return 0;
}
/* end ex3f.cpp */