diff --git a/include/xo/indentlog/scope.hpp b/include/xo/indentlog/scope.hpp index 3217e0e8..42a664c9 100644 --- a/include/xo/indentlog/scope.hpp +++ b/include/xo/indentlog/scope.hpp @@ -171,10 +171,27 @@ namespace xo { return true; } /*log*/ + /** re-enable + log (args...). + * No-op if scope already enabled. + * Useful in verify_ok() methods, to conditionally enable + * logging only when a test fails + **/ + template + void retroactively_enable(Tn&&... args) { + if (finalized_) { + this->finalized_ = false; + this->begin_scope(std::forward(args)...); + } + } + /** Log argument in pack @p args **/ template bool operator()(Tn&&... args) { return this->log(std::forward(args)...); } + /** If enabled, writes initial banner **/ + template + void begin_scope(Tn&&... args); + /** Optionally, call once to end scope before dtor. * Logs arguments in pack @p args **/ @@ -231,27 +248,7 @@ namespace xo { line_{setup.line_}, finalized_{!(setup.is_enabled())} { - if(setup.is_enabled()) { - state_impl_type * logstate = basic_scope::require_thread_local_state(); - std::ostream & os = logstate2stream(logstate); - - logstate->preamble(this->style_, this->name1_, this->name2_); - - tosn(os, " ", std::forward(args)...); - - if (log_config::location_enabled) { - /* prints on next call to flush2sbuf */ - logstate->set_location(this->file_, this->line_); - //tosn(os, " [", basename(this->file_), ":", this->line_, "]"); - } - - logstate->flush2sbuf(std::clog.rdbuf()); - - ///* next call to scope::log() can reset to beginning of buffer space */ - //logstate->ss().seekp(0); - - logstate->incr_nesting(); - } + this->begin_scope(std::forward(args)...); } /*ctor*/ template @@ -314,6 +311,34 @@ namespace xo { logstate->flush2sbuf(this->dest_sbuf_); } /*flush2sbuf*/ + template + template + void + basic_scope::begin_scope(Tn&&... args) + { + if(this->enabled()) { + state_impl_type * logstate = basic_scope::require_thread_local_state(); + std::ostream & os = logstate2stream(logstate); + + logstate->preamble(this->style_, this->name1_, this->name2_); + + tosn(os, " ", std::forward(args)...); + + if (log_config::location_enabled) { + /* prints on next call to flush2sbuf */ + logstate->set_location(this->file_, this->line_); + //tosn(os, " [", basename(this->file_), ":", this->line_, "]"); + } + + logstate->flush2sbuf(std::clog.rdbuf()); + + ///* next call to scope::log() can reset to beginning of buffer space */ + //logstate->ss().seekp(0); + + logstate->incr_nesting(); + } + } + template template void