refactor and add configurable decorations
This commit is contained in:
parent
aea665ee51
commit
7ae6c40d27
1 changed files with 87 additions and 24 deletions
|
|
@ -31,6 +31,8 @@
|
||||||
class clutchlog
|
class clutchlog
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/** High-level API @{ */
|
||||||
|
|
||||||
static clutchlog& logger()
|
static clutchlog& logger()
|
||||||
{
|
{
|
||||||
static clutchlog instance;
|
static clutchlog instance;
|
||||||
|
|
@ -39,6 +41,10 @@ class clutchlog
|
||||||
|
|
||||||
enum level {quiet, error, warning, info, debug, xdebug};
|
enum level {quiet, error, warning, info, debug, xdebug};
|
||||||
|
|
||||||
|
/** }@ High-level API */
|
||||||
|
|
||||||
|
/** Internal details @{ */
|
||||||
|
|
||||||
public:
|
public:
|
||||||
clutchlog(clutchlog const&) = delete;
|
clutchlog(clutchlog const&) = delete;
|
||||||
void operator=(clutchlog const&) = delete;
|
void operator=(clutchlog const&) = delete;
|
||||||
|
|
@ -47,10 +53,13 @@ class clutchlog
|
||||||
clutchlog() :
|
clutchlog() :
|
||||||
_out(&std::clog),
|
_out(&std::clog),
|
||||||
_depth(std::numeric_limits<size_t>::max()),
|
_depth(std::numeric_limits<size_t>::max()),
|
||||||
_level(level::error),
|
_stage(level::error),
|
||||||
_in_file(".*"),
|
_in_file(".*"),
|
||||||
_in_func(".*"),
|
_in_func(".*"),
|
||||||
_in_line(".*")
|
_in_line(".*"),
|
||||||
|
_show_name(true),
|
||||||
|
_show_depth(true),
|
||||||
|
_show_location(true)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
@ -58,20 +67,58 @@ class clutchlog
|
||||||
const size_t _strip_calls = 3;
|
const size_t _strip_calls = 3;
|
||||||
std::ostream* _out;
|
std::ostream* _out;
|
||||||
size_t _depth;
|
size_t _depth;
|
||||||
level _level;
|
level _stage;
|
||||||
std::regex _in_file;
|
std::regex _in_file;
|
||||||
std::regex _in_func;
|
std::regex _in_func;
|
||||||
std::regex _in_line;
|
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:
|
public:
|
||||||
|
|
||||||
|
/** Configuration accessors @{ */
|
||||||
|
|
||||||
void out(std::ostream& out) {_out = &out;}
|
void out(std::ostream& out) {_out = &out;}
|
||||||
std::ostream& out() {return *_out;}
|
std::ostream& out() {return *_out;}
|
||||||
|
|
||||||
void depth(size_t d) {_depth = d;}
|
void depth(size_t d) {_depth = d;}
|
||||||
size_t depth() const {return _depth;}
|
size_t depth() const {return _depth;}
|
||||||
|
|
||||||
void threshold(level l) {_level = l;}
|
void threshold(level l) {_stage = l;}
|
||||||
level threshold() const {return _level;}
|
level threshold() const {return _stage;}
|
||||||
|
|
||||||
void file(std::string file) {_in_file = file;}
|
void file(std::string file) {_in_file = file;}
|
||||||
void func(std::string func) {_in_func = func;}
|
void func(std::string func) {_in_func = func;}
|
||||||
|
|
@ -84,34 +131,50 @@ class clutchlog
|
||||||
line(in_line);
|
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;
|
scope_t scope = locate(stage, file, func, line);
|
||||||
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)) {
|
|
||||||
|
|
||||||
|
if(scope.matches) {
|
||||||
|
if(_show_name) {
|
||||||
*_out << "[" << basename(getenv("_")) << "] ";
|
*_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 << ">";
|
*_out << ">";
|
||||||
}
|
}
|
||||||
if(stack_depth > _strip_calls) {
|
if(scope.depth > _strip_calls) {
|
||||||
*_out << " ";
|
*_out << " ";
|
||||||
}
|
}
|
||||||
*_out << what;
|
}
|
||||||
|
|
||||||
|
*_out << what;
|
||||||
|
|
||||||
|
if(_show_location) {
|
||||||
*_out << "\t\t\t\t\t" << file << ":" << line << " (" << func << ")";
|
*_out << "\t\t\t\t\t" << file << ":" << line << " (" << func << ")";
|
||||||
if(newline) {
|
}
|
||||||
*_out << std::endl;
|
if(newline) {
|
||||||
}
|
*_out << std::endl;
|
||||||
} // regex location
|
}
|
||||||
} // log level and stack depth
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** }@ Low-level API */
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef WITH_CLUTCHLOG
|
#ifdef WITH_CLUTCHLOG
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue