From 7ae6c40d2727d43e3d8ea49fc4f0202e5a17662f Mon Sep 17 00:00:00 2001 From: nojhan Date: Wed, 26 Aug 2020 22:03:52 +0200 Subject: [PATCH] refactor and add configurable decorations --- clutchlog/clutchlog.h | 111 +++++++++++++++++++++++++++++++++--------- 1 file changed, 87 insertions(+), 24 deletions(-) diff --git a/clutchlog/clutchlog.h b/clutchlog/clutchlog.h index 03474c1..05c841b 100644 --- a/clutchlog/clutchlog.h +++ b/clutchlog/clutchlog.h @@ -31,6 +31,8 @@ class clutchlog { public: + /** High-level API @{ */ + static clutchlog& logger() { static clutchlog instance; @@ -39,6 +41,10 @@ class clutchlog enum level {quiet, error, warning, info, debug, xdebug}; + /** }@ High-level API */ + + /** Internal details @{ */ + public: clutchlog(clutchlog const&) = delete; void operator=(clutchlog const&) = delete; @@ -47,10 +53,13 @@ class clutchlog clutchlog() : _out(&std::clog), _depth(std::numeric_limits::max()), - _level(level::error), + _stage(level::error), _in_file(".*"), _in_func(".*"), - _in_line(".*") + _in_line(".*"), + _show_name(true), + _show_depth(true), + _show_location(true) {} protected: @@ -58,20 +67,58 @@ class clutchlog const size_t _strip_calls = 3; std::ostream* _out; size_t _depth; - level _level; + level _stage; std::regex _in_file; std::regex _in_func; std::regex _in_line; + bool _show_name; + bool _show_depth; + bool _show_location; + + struct scope_t { + bool matches; + level stage; + size_t depth; + bool there; + }; + + scope_t locate(level stage, std::string file, std::string func, size_t line) + { + const size_t max_buffer = 4096; + size_t stack_depth; + void *buffer[max_buffer]; + stack_depth = backtrace(buffer, max_buffer); + + scope_t scope; + scope.stage = stage; + scope.depth = stack_depth; + + std::ostringstream sline; sline << line; + scope.there = + std::regex_search(file, _in_file) + and std::regex_search(func, _in_func) + and std::regex_search(sline.str(), _in_line); + + scope.matches = scope.stage <= _stage + and scope.depth <= _depth + _strip_calls + and scope.there; + return scope; + } + + /** }@ Internal details */ public: + + /** Configuration accessors @{ */ + void out(std::ostream& out) {_out = &out;} std::ostream& out() {return *_out;} void depth(size_t d) {_depth = d;} size_t depth() const {return _depth;} - void threshold(level l) {_level = l;} - level threshold() const {return _level;} + void threshold(level l) {_stage = l;} + level threshold() const {return _stage;} void file(std::string file) {_in_file = file;} void func(std::string func) {_in_func = func;} @@ -84,34 +131,50 @@ class clutchlog line(in_line); } - void log(std::string what, level log_level, std::string file, std::string func, size_t line, bool newline) + void show_name(bool n) {_show_name = n;} + bool show_name() const {return _show_name;} + + void show_depth(bool d) {_show_depth = d;} + bool show_depth() const {return _show_depth;} + + void show_location(bool l) {_show_location = l;} + bool show_location() const {return _show_location;} + + /** }@ Configuration */ + + public: + + /** Low-level API @{ */ + + void log(std::string what, level stage, std::string file, std::string func, size_t line, bool newline) { - const size_t max_buffer = 1024; - size_t stack_depth; - void *buffer[max_buffer]; - stack_depth = backtrace(buffer, max_buffer); - if(log_level <= _level and stack_depth <= _depth + _strip_calls) { - - std::ostringstream sline; sline << line; - if( std::regex_search(file, _in_file) - and std::regex_search(func, _in_func) - and std::regex_search(sline.str(), _in_line)) { + scope_t scope = locate(stage, file, func, line); + if(scope.matches) { + if(_show_name) { *_out << "[" << basename(getenv("_")) << "] "; - for(size_t i = _strip_calls; i < stack_depth; ++i) { + } + if(_show_depth) { + for(size_t i = _strip_calls; i < scope.depth; ++i) { *_out << ">"; } - if(stack_depth > _strip_calls) { + if(scope.depth > _strip_calls) { *_out << " "; } - *_out << what; + } + + *_out << what; + + if(_show_location) { *_out << "\t\t\t\t\t" << file << ":" << line << " (" << func << ")"; - if(newline) { - *_out << std::endl; - } - } // regex location - } // log level and stack depth + } + if(newline) { + *_out << std::endl; + } + } } + + /** }@ Low-level API */ }; #ifdef WITH_CLUTCHLOG