add level prefix letter, refactor test

This commit is contained in:
Johann Dreo 2020-08-26 22:33:12 +02:00
commit b3a04a88a7
2 changed files with 63 additions and 14 deletions

View file

@ -8,6 +8,7 @@
#include <limits> #include <limits>
#include <sstream> #include <sstream>
#include <regex> #include <regex>
#include <map>
// #ifdef __unix__ // #ifdef __unix__
#include <execinfo.h> #include <execinfo.h>
@ -39,7 +40,7 @@ class clutchlog
return instance; return instance;
} }
enum level {quiet, error, warning, info, debug, xdebug}; enum level {quiet=0, error=1, warning=2, info=3, debug=4, xdebug=5};
/** }@ High-level API */ /** }@ High-level API */
@ -59,7 +60,16 @@ class clutchlog
_in_line(".*"), _in_line(".*"),
_show_name(true), _show_name(true),
_show_depth(true), _show_depth(true),
_show_location(true) _show_location(true),
_show_level(true),
_level_letters({
{level::quiet,"Q"},
{level::error,"E"},
{level::warning,"W"},
{level::info,"I"},
{level::debug,"D"},
{level::xdebug,"X"}
})
{} {}
protected: protected:
@ -74,6 +84,8 @@ class clutchlog
bool _show_name; bool _show_name;
bool _show_depth; bool _show_depth;
bool _show_location; bool _show_location;
bool _show_level;
const std::map<level,std::string> _level_letters;
struct scope_t { struct scope_t {
bool matches; bool matches;
@ -140,6 +152,9 @@ class clutchlog
void show_location(bool l) {_show_location = l;} void show_location(bool l) {_show_location = l;}
bool show_location() const {return _show_location;} bool show_location() const {return _show_location;}
void show_level(bool l) {_show_level = l;}
bool show_level() const {return _show_level;}
/** }@ Configuration */ /** }@ Configuration */
public: public:
@ -154,6 +169,9 @@ class clutchlog
if(_show_name) { if(_show_name) {
*_out << "[" << basename(getenv("_")) << "]"; *_out << "[" << basename(getenv("_")) << "]";
} }
if(_show_level) {
*_out << " " << _level_letters.at(stage) << ":";
}
if(_show_depth) { if(_show_depth) {
for(size_t i = _strip_calls; i < scope.depth; ++i) { for(size_t i = _strip_calls; i < scope.depth; ++i) {
*_out << ">"; *_out << ">";

View file

@ -1,34 +1,65 @@
#include <iostream>
#include "../clutchlog/clutchlog.h" #include "../clutchlog/clutchlog.h"
void h()
{
CLUTCHLOG(info, "!");
std::clog << "--" << std::endl;
}
void g() void g()
{ {
CLUTCHLOG(info, "!"); CLUTCHLOG(warning, "world");
h();
} }
void f() void f()
{ {
CLUTCHLOG(warning, "world"); CLUTCHLOG(error, "hello ");
g(); g();
} }
int main(const int argc, char* argv[]) int main(/*const int argc, char* argv[]*/)
{ {
#ifdef WITH_CLUTCHLOG #ifdef WITH_CLUTCHLOG
auto& log = clutchlog::logger(); auto& log = clutchlog::logger();
log.out(std::clog); log.out(std::clog);
size_t below = 2;
if(argc >= 2) { below = atoi(argv[1]); }
log.depth(below);
log.threshold(clutchlog::level::warning); log.threshold(clutchlog::level::warning);
log.file("core"); log.file("core");
log.func("(main|f)"); log.func("(main|f)");
#endif
CLUTCHLOG(error, "hello " << argc);
std::clog << "depth: 99; threshold: xdebug; location: .*" << std::endl;
log.depth(99);
log.threshold(clutchlog::level::xdebug);
log.location(".*",".*");
f(); f();
std::clog << "depth: 4; threshold: xdebug; location: ,*" << std::endl;
log.depth(4);
log.threshold(clutchlog::level::xdebug);
log.location(".*");
f();
std::clog << "depth: 99; threshold: warning; location: .*" << std::endl;
log.depth(99);
log.threshold(clutchlog::level::warning);
log.location(".*");
f();
std::clog << "depth: 99; threshold: xdebug; location: 'core','g'" << std::endl;
log.depth(99);
log.threshold(clutchlog::level::xdebug);
log.location("core","g");
f();
std::clog << "depth: 99; threshold: debug; location: '.*','(g|h)'" << std::endl;
log.depth(99);
log.threshold(clutchlog::level::debug);
log.location(".*","(g|h)");
f();
#endif
} }