Go to the documentation of this file. 1 #ifndef __CLUTCHLOG_H__
2 #define __CLUTCHLOG_H__
8 #include <experimental/filesystem>
9 namespace fs = std::experimental::filesystem;
12 namespace fs = std::filesystem;
26 #if __has_include(<execinfo.h>) && __has_include(<stdlib.h>) && __has_include(<libgen.h>)
30 #define CLUTCHLOG_HAVE_UNIX_SYSINFO 1
32 #define CLUTCHLOG_HAVE_UNIX_SYSINFO 0
35 #if __has_include(<sys/ioctl.h>) && __has_include(<stdio.h>) && __has_include(<unistd.h>)
36 #include <sys/ioctl.h>
39 #define CLUTCHLOG_HAVE_UNIX_SYSIOCTL 1
41 #define CLUTCHLOG_HAVE_UNIX_SYSIOCTL 0
48 #ifndef WITH_CLUTCHLOG
50 #define WITH_CLUTCHLOG
63 #ifndef CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG
64 #define CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG clutchlog::level::progress
66 #endif // CLUTCHLOG_DEFAULT_DEPTH_BUILT
74 #define CLUTCHLOC __FILE__, __FUNCTION__, __LINE__
79 #define CLUTCHLOG( LEVEL, WHAT ) do { \
80 auto& clutchlog__logger = clutchlog::logger(); \
81 std::ostringstream clutchlog__msg ; clutchlog__msg << WHAT; \
82 clutchlog__logger.log(clutchlog::level::LEVEL, clutchlog__msg.str(), CLUTCHLOC); \
84 #else // not Debug build.
85 #define CLUTCHLOG( LEVEL, WHAT ) do { \
86 if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
87 auto& clutchlog__logger = clutchlog::logger(); \
88 std::ostringstream clutchlog__msg ; clutchlog__msg << WHAT; \
89 clutchlog__logger.log(clutchlog::level::LEVEL, clutchlog__msg.str(), CLUTCHLOC); \
96 #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do { \
97 auto& clutchlog__logger = clutchlog::logger(); \
98 clutchlog__logger.dump(clutchlog::level::LEVEL, std::begin(CONTAINER), std::end(CONTAINER), \
99 CLUTCHLOC, FILENAME, CLUTCHDUMP_DEFAULT_SEP); \
101 #else // not Debug build.
102 #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do { \
103 if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
104 auto& clutchlog__logger = clutchlog::logger(); \
105 clutchlog__logger.dump(clutchlog::level::LEVEL, std::begin(CONTAINER), std::end(CONTAINER), \
106 CLUTCHLOC, FILENAME, CLUTCHDUMP_DEFAULT_SEP); \
113 #define CLUTCHFUNC( LEVEL, FUNC, ... ) do { \
114 auto& clutchlog__logger = clutchlog::logger(); \
115 clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
116 if(clutchlog__scope.matches) { \
120 #else // not Debug build.
121 #define CLUTCHFUNC( LEVEL, FUNC, ... ) do { \
122 if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
123 auto& clutchlog__logger = clutchlog::logger(); \
124 clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
125 if(clutchlog__scope.matches) { \
134 #define CLUTCHCODE( LEVEL, ... ) do { \
135 auto& clutchlog__logger = clutchlog::logger(); \
136 clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
137 if(clutchlog__scope.matches) { \
141 #else // not Debug build.
142 #define CLUTCHCODE( LEVEL, CODE ) do { \
143 if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
144 auto& clutchlog__logger = clutchlog::logger(); \
145 clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
146 if(clutchlog__scope.matches) { \
155 #else // not WITH_CLUTCHLOG
157 #define CLUTCHLOG( LEVEL, WHAT ) do {} while(0)
158 #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do {} while(0)
159 #define CLUTCHFUNC( LEVEL, FUNC, ... ) do {} while(0)
160 #define CLUTCHCODE( LEVEL, CODE ) do {} while(0)
161 #endif // WITH_CLUTCHLOG
167 #ifdef WITH_CLUTCHLOG
182 #ifndef CLUTCHLOG_DEFAULT_FORMAT
183 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
185 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
186 #define CLUTCHLOG_DEFAULT_FORMAT "[{name}] {level_letter}:{depth_marks} {msg} {hfill} {func} @ {file}:{line}\n"
188 #define CLUTCHLOG_DEFAULT_FORMAT "[{name}] {level_letter}:{depth_marks} {msg}\t\t\t\t\t{func} @ {file}:{line}\n"
191 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
192 #define CLUTCHLOG_DEFAULT_FORMAT "{level_letter} {msg} {hfill} {func} @ {file}:{line}\n"
194 #define CLUTCHLOG_DEFAULT_FORMAT "{level_letter} {msg}\t\t\t\t\t{func} @ {file}:{line}\n"
199 #ifndef CLUTCHLOG_DEFAULT_FORMAT
200 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
202 #define CLUTCHLOG_DEFAULT_FORMAT "[{name}] {level_letter}:{depth_marks} {msg}\n"
204 #define CLUTCHLOG_DEFAULT_FORMAT "{level_letter} {msg}\n"
211 #ifndef CLUTCHDUMP_DEFAULT_FORMAT
212 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
214 #define CLUTCHDUMP_DEFAULT_FORMAT "# [{name}] {level} in {func} (at depth {depth}) @ {file}:{line}"
216 #define CLUTCHDUMP_DEFAULT_FORMAT "# {level} in {func} @ {file}:{line}"
218 #endif // CLUTCHDUMP_DEFAULT_FORMAT
222 #ifndef CLUTCHDUMP_DEFAULT_SEP
223 #define CLUTCHDUMP_DEFAULT_SEP "\n"
225 #endif // CLUTCHDUMP_DEFAULT_SEP
229 #ifndef CLUTCHLOG_DEFAULT_DEPTH_MARK
230 #define CLUTCHLOG_DEFAULT_DEPTH_MARK ">"
232 #endif // CLUTCHLOG_DEFAULT_DEPTH_MARK
236 #ifndef CLUTCHLOG_STRIP_CALLS
237 #define CLUTCHLOG_STRIP_CALLS 5
239 #endif // CLUTCHLOG_STRIP_CALLS
243 #ifndef CLUTCHLOG_HFILL_CHAR
244 #define CLUTCHLOG_HFILL_CHAR '.'
246 #endif // CLUTCHLOG_HFILL_CHAR
269 enum level {critical=0, error=1, warning=2, progress=3, note=4, info=5, debug=6, xdebug=7};
337 std::vector<int> codes; codes.reserve(3);
338 if(this->
fore != fg::none) { codes.push_back(
static_cast<int>(this->
fore ));}
339 if(this->
back != bg::none) { codes.push_back(
static_cast<int>(this->
back ));}
340 if(this->
style != typo::none) { codes.push_back(
static_cast<int>(this->
style));}
341 if(codes.size() == 0) {
return os;}
344 assert(codes.size() > 0);
346 for(
size_t i=1; i < codes.size(); ++i) {
347 os <<
";" << codes[i];
382 std::ostringstream os;
384 fmt reset(fmt::typo::reset);
399 void operator=(
clutchlog const&) =
delete;
406 {level::critical,
"Critical"},
407 {level::error ,
"Error"},
408 {level::warning ,
"Warning"},
409 {level::progress,
"Progress"},
410 {level::note ,
"Note"},
411 {level::info ,
"Info"},
412 {level::debug ,
"Debug"},
413 {level::xdebug ,
"XDebug"}
416 {level::critical,fmt(fmt::fg::red, fmt::typo::underline)},
417 {level::error ,fmt(fmt::fg::red, fmt::typo::bold)},
418 {level::warning ,fmt(fmt::fg::magenta, fmt::typo::bold)},
419 {level::progress,fmt()},
420 {level::note ,fmt()},
421 {level::info ,fmt()},
422 {level::debug ,fmt()},
423 {level::xdebug ,fmt()}
429 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
430 _depth(std::numeric_limits<size_t>::max() -
_strip_calls),
442 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
444 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
445 _nb_columns = w.ws_col;
466 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
470 std::string _depth_mark;
481 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
483 static const size_t _max_buffer = 4096;
486 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
512 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
513 void depth(
size_t d) {_depth = d;}
516 size_t depth()
const {
return _depth;}
519 void depth_mark(std::string mark) {_depth_mark = mark;}
521 std::string depth_mark()
const {
return _depth_mark;}
541 return ilevel->second;
543 throw std::out_of_range(
"'" + name +
"' is not a valid log level name");
556 const std::string& in_file,
557 const std::string& in_function=
".*",
558 const std::string& in_line=
".*"
570 template<
class ... FMT>
590 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
611 const std::string&
file,
612 const std::string&
func,
621 if(not (scope.stage <=
_stage)) {
626 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
630 void *buffer[_max_buffer];
631 stack_depth = backtrace(buffer, _max_buffer);
632 scope.depth = stack_depth;
641 std::ostringstream sline; sline <<
line;
645 and std::regex_search(sline.str(),
_in_line);
648 scope.matches = scope.there;
661 const std::string& form,
662 const std::string& mark,
663 const std::string& tag
720 const std::regex re(mark);
721 return std::regex_replace(form, re, tag);
726 const std::string& form,
727 const std::string& mark,
731 std::ostringstream stag; stag << tag;
732 return replace(form, mark, stag.str());
738 const std::string& what,
740 const std::string& name,
743 const std::string&
file,
744 const std::string&
func,
758 std::string letter(1,
_level_word.at(stage).at(0));
761 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
765 std::ostringstream chevrons;
767 chevrons << _depth_mark;
771 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
772 const std::string hfill_tag =
"{hfill}";
773 const size_t hfill_pos =
format.find(hfill_tag);
774 if(hfill_pos != std::string::npos) {
775 const size_t left_len = hfill_pos;
776 const size_t right_len =
format.size() - hfill_pos - hfill_tag.size();
777 if(right_len+left_len > _nb_columns) {
778 const std::string hfill(_nb_columns-right_len,
_hfill_char);
781 const std::string hfill(_nb_columns - (right_len+left_len),
_hfill_char);
792 const std::string& what,
793 const std::string&
file,
const std::string&
func,
size_t line
799 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
817 const In container_begin,
const In container_end,
818 const std::string&
file,
const std::string&
func,
size_t line,
819 const std::string& filename_template =
"dump_{n}.dat",
826 const std::string tag =
"\\{n\\}";
827 const std::regex re(tag);
828 std::string outfile =
"";
831 if(std::regex_search(filename_template, re)) {
835 outfile =
replace(filename_template, tag, n);
837 }
while( fs::exists( outfile ) );
841 outfile = filename_template;
844 std::ofstream fd(outfile);
847 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
859 std::copy(container_begin, container_end,
860 std::ostream_iterator<typename In::value_type>(fd, sep.c_str()));
871 #else // not WITH_CLUTCHLOG
880 #pragma GCC diagnostic push
881 #pragma GCC diagnostic ignored "-Wreturn-type"
886 enum level {critical=0, error=1, warning=2, progress=3, note=4, info=5, debug=6, xdebug=7};
889 enum class fg { black, red, green, yellow, blue, magenta, cyan, white, none }
fore;
890 enum class bg { black, red, green, yellow, blue, magenta, cyan, white, none }
back;
891 enum class typo { reset, bold, underline, inverse, none }
style;
900 std::ostream&
print_on(std::ostream&)
const {}
902 friend std::ostream&
operator<<(std::ostream&,
const fmt&) {}
903 std::string
operator()(
const std::string&)
const {}
907 void operator=(
clutchlog const&) =
delete;
920 void format(
const std::string&) {}
921 std::string
format()
const {}
926 void out(std::ostream&) {}
927 std::ostream&
out() {}
929 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
930 void depth(
size_t) {}
931 size_t depth()
const {}
933 void depth_mark(std::string) {}
934 std::string depth_mark()
const {}
940 const std::map<std::string,level>
levels()
const {};
943 void file(std::string) {}
944 void func(std::string) {}
945 void line(std::string) {}
947 #pragma GCC diagnostic push
948 #pragma GCC diagnostic ignored "-Wunused-parameter"
951 const std::string& in_function=
".*",
952 const std::string& in_line=
".*"
955 #pragma GCC diagnostic pop
993 const std::string&,
const std::string&,
size_t
1001 const std::string&,
const std::string&,
size_t,
1007 #pragma GCC diagnostic pop
1008 #endif // WITH_CLUTCHLOG
1010 #endif // __CLUTCHLOG_H__
void file(std::string file)
Set the regular expression filtering the file location.
Definition: clutchlog.h:548
#define CLUTCHDUMP_DEFAULT_FORMAT
Compile-time default format of the comment line in file dump.
Definition: clutchlog.h:216
fg
Foreground color codes.
Definition: clutchlog.h:286
static std::string dump_default_format
Default format of the comment line in file dump.
Definition: clutchlog.h:220
const std::map< std::string, level > & levels() const
Get the map of available log levels string representations toward their identifier....
Definition: clutchlog.h:531
void out(std::ostream &out)
Set the output stream on which to print.
Definition: clutchlog.h:508
fmt()
 Empty constructor, only useful for a no-op formatter.
Definition: clutchlog.h:321
static clutchlog & logger()
Get the logger instance.
Definition: clutchlog.h:262
void line(std::string line)
Set the regular expression filtering the line location.
Definition: clutchlog.h:552
void format_comment(const std::string &format)
Set the template string for dumps.
Definition: clutchlog.h:503
typo
Typographic style codes.
Definition: clutchlog.h:312
#define CLUTCHDUMP_DEFAULT_SEP
Compile-time default item separator for dump.
Definition: clutchlog.h:224
static std::string default_depth_mark
Default mark for stack depth.
Definition: clutchlog.h:234
#define CLUTCHLOG_DEFAULT_FORMAT
Compile-time default format of the messages (debug mode: with absolute location).
Definition: clutchlog.h:194
std::string format() const
Get the template string.
Definition: clutchlog.h:500
void log(const level &stage, const std::string &what, const std::string &file, const std::string &func, size_t line) const
Print a log message IF the location matches the given one.
Definition: clutchlog.h:790
level level_of(const std::string name)
Return the log level tag corresponding to the given pre-configured name.
Definition: clutchlog.h:537
std::string _format_dump
Current format of the file output.
Definition: clutchlog.h:461
std::string operator()(const std::string &msg) const
Format the given string with the currently encoded format.
Definition: clutchlog.h:380
std::string format(std::string format, const std::string &what, const level &stage, const std::string &file, const std::string &func, const size_t line) const
Substitute all tags in the format string with the corresponding information and apply the style corre...
Definition: clutchlog.h:736
std::string _format_log
Current format of the standard output.
Definition: clutchlog.h:459
level
Available log levels.
Definition: clutchlog.h:269
level stage
Current log level.
Definition: clutchlog.h:589
#define CLUTCHLOG_HFILL_CHAR
Character used as a filling for right-align the right part of messages with "{hfill}".
Definition: clutchlog.h:245
#define CLUTCHLOG_DEFAULT_DEPTH_MARK
Compile-time default mark for stack depth.
Definition: clutchlog.h:231
friend std::ostream & operator<<(std::ostream &os, const fmt &fmt)
Output stream overload.
Definition: clutchlog.h:365
std::map< level, fmt > _level_fmt
Dictionary of level identifier to their format.
Definition: clutchlog.h:457
std::regex _in_line
Current line location filter.
Definition: clutchlog.h:479
enum clutchlog::fmt::typo style
Typographic style.
level _stage
Current log level.
Definition: clutchlog.h:473
const size_t _strip_calls
Current number of call stack levels to remove from depth display.
Definition: clutchlog.h:451
std::string format_comment() const
Get the template string for dumps.
Definition: clutchlog.h:505
const char _hfill_char
Character for filling.
Definition: clutchlog.h:463
#define CLUTCHLOG_STRIP_CALLS
Compile-time number of call stack levels to remove from depth display by default.
Definition: clutchlog.h:238
std::ostream * _out
Standard output.
Definition: clutchlog.h:465
void func(std::string func)
Set the regular expression filtering the function location.
Definition: clutchlog.h:550
void location(const std::string &in_file, const std::string &in_function=".*", const std::string &in_line=".*")
Set the regular expressions filtering the location.
Definition: clutchlog.h:555
static char default_hfill_char
Default character used as a filling for right-align the right part of messages with "{hfill}".
Definition: clutchlog.h:248
void dump(const level &stage, const In container_begin, const In container_end, const std::string &file, const std::string &func, size_t line, const std::string &filename_template="dump_{n}.dat", const std::string sep=dump_default_sep) const
Dump a serializable container after a comment line with log information.
Definition: clutchlog.h:815
std::string replace(const std::string &form, const std::string &mark, const size_t tag) const
Replace mark by tag in form, converting tag to its string representation first.
Definition: clutchlog.h:725
Color and style formatter for ANSI terminal escape sequences.
Definition: clutchlog.h:283
std::regex _in_file
Current file location filter.
Definition: clutchlog.h:475
std::ostream & out()
Get the output stream on which to print.
Definition: clutchlog.h:510
std::string replace(const std::string &form, const std::string &mark, const std::string &tag) const
Replace mark by tag in form.
Definition: clutchlog.h:660
static unsigned int default_strip_calls
Number of call stack levels to remove from depth display by default.
Definition: clutchlog.h:241
static std::string default_format
Default format of the messages.
Definition: clutchlog.h:209
enum clutchlog::fmt::fg fore
Foreground color.
void threshold(const std::string &l)
Set the log level (below which logs are not printed) with a string.
Definition: clutchlog.h:527
bool there
Location is compatible.
Definition: clutchlog.h:595
Structure holding a location matching.
Definition: clutchlog.h:585
std::regex _in_func
Current function location filter.
Definition: clutchlog.h:477
void threshold(level l)
Set the log level (below which logs are not printed) with an identifier.
Definition: clutchlog.h:525
void style(level stage, fmt style)
Set the style (color and typo) of the given log level, passing a fmt instance.
Definition: clutchlog.h:573
const std::map< level, std::string > _level_word
Dictionary of level identifier to their string representation.
Definition: clutchlog.h:453
scope_t locate(const level &stage, const std::string &file, const std::string &func, const size_t line) const
Gather information on the current location of the call.
Definition: clutchlog.h:609
bool matches
Everything is compatible.
Definition: clutchlog.h:587
static std::string dump_default_sep
Default item separator for dump.
Definition: clutchlog.h:227
level threshold() const
Get the log level below which logs are not printed.
Definition: clutchlog.h:529
std::ostream & print_on(std::ostream &os) const
Print the currently encoded format escape code on the given output stream.
Definition: clutchlog.h:335
std::map< std::string, level > _word_level
Dictionary of level string to their identifier.
Definition: clutchlog.h:455
void style(level stage, FMT... styles)
Set the style (color and typo) of the given log level.
Definition: clutchlog.h:571
scope_t()
Constructor.
Definition: clutchlog.h:597
#define CLUTCHLOG_HAVE_UNIX_SYSINFO
POSIX headers necessary for stack depth management are available.
Definition: clutchlog.h:32
Definition: clutchlog.h:175
fmt style(level stage) const
Get the configured fmt instance of the given log level.
Definition: clutchlog.h:575
bg
Background color codes.
Definition: clutchlog.h:299
enum clutchlog::fmt::bg back
Background color.