clutchlog  0.14
clutchlog.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef CLUTCHLOG_H
3 #define CLUTCHLOG_H
5 
7 #include <ciso646>
8  #ifdef FSEXPERIMENTAL
9  #include <experimental/filesystem>
10  namespace fs = std::experimental::filesystem;
11 #else
12  #include <filesystem>
13  namespace fs = std::filesystem;
14 #endif
15 
16 #include <iostream>
17 #include <sstream>
18 #include <fstream>
19 #include <cassert>
20 #include <cstdlib>
21 #include <string>
22 #include <limits>
23 #include <regex>
24 #include <map>
25 
27 #if __has_include(<execinfo.h>) && __has_include(<stdlib.h>) && __has_include(<libgen.h>)
28  #include <execinfo.h> // execinfo
29  #include <stdlib.h> // getenv
30  #include <libgen.h> // basename
31  #define CLUTCHLOG_HAVE_UNIX_SYSINFO 1
32 #else
33  #define CLUTCHLOG_HAVE_UNIX_SYSINFO 0
34 #endif
35 
37 #if __has_include(<sys/ioctl.h>) && __has_include(<stdio.h>) && __has_include(<unistd.h>)
38  #include <sys/ioctl.h>
39  #include <stdio.h>
40  #include <unistd.h>
41  #define CLUTCHLOG_HAVE_UNIX_SYSIOCTL 1
42 #else
43  #define CLUTCHLOG_HAVE_UNIX_SYSIOCTL 0
44 #endif
45 
46 
47 /**********************************************************************
48  * Enable by default in Debug builds.
49  **********************************************************************/
50 #ifndef WITH_CLUTCHLOG
51  #ifndef NDEBUG
52  #define WITH_CLUTCHLOG
54  #endif
55 #endif
56 
57 /**********************************************************************
58  * Macros definitions
59  **********************************************************************/
60 #ifdef WITH_CLUTCHLOG
61 
65 #ifndef CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG
66  #define CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG clutchlog::level::progress
68 #endif // CLUTCHLOG_DEFAULT_DEPTH_BUILT
69 
76 #define CLUTCHLOC __FILE__, __FUNCTION__, __LINE__
78 
80 #ifndef NDEBUG
81  #define CLUTCHLOG( LEVEL, WHAT ) do { \
82  auto& clutchlog__logger = clutchlog::logger(); \
83  std::ostringstream clutchlog__msg ; clutchlog__msg << WHAT; \
84  clutchlog__logger.log(clutchlog::level::LEVEL, clutchlog__msg.str(), CLUTCHLOC); \
85  } while(0)
86 #else // not Debug build.
87  #define CLUTCHLOG( LEVEL, WHAT ) do { \
88  if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
89  auto& clutchlog__logger = clutchlog::logger(); \
90  std::ostringstream clutchlog__msg ; clutchlog__msg << WHAT; \
91  clutchlog__logger.log(clutchlog::level::LEVEL, clutchlog__msg.str(), CLUTCHLOC); \
92  } \
93  } while(0)
94 #endif // NDEBUG
95 
97 #ifndef NDEBUG
98  #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do { \
99  auto& clutchlog__logger = clutchlog::logger(); \
100  clutchlog__logger.dump(clutchlog::level::LEVEL, std::begin(CONTAINER), std::end(CONTAINER), \
101  CLUTCHLOC, FILENAME, CLUTCHDUMP_DEFAULT_SEP); \
102  } while(0)
103 #else // not Debug build.
104  #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do { \
105  if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
106  auto& clutchlog__logger = clutchlog::logger(); \
107  clutchlog__logger.dump(clutchlog::level::LEVEL, std::begin(CONTAINER), std::end(CONTAINER), \
108  CLUTCHLOC, FILENAME, CLUTCHDUMP_DEFAULT_SEP); \
109  } \
110  } while(0)
111 #endif // NDEBUG
112 
114 #ifndef NDEBUG
115  #define CLUTCHFUNC( LEVEL, FUNC, ... ) do { \
116  auto& clutchlog__logger = clutchlog::logger(); \
117  clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
118  if(clutchlog__scope.matches) { \
119  FUNC(__VA_ARGS__); \
120  } \
121  } while(0)
122 #else // not Debug build.
123  #define CLUTCHFUNC( LEVEL, FUNC, ... ) do { \
124  if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
125  auto& clutchlog__logger = clutchlog::logger(); \
126  clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
127  if(clutchlog__scope.matches) { \
128  FUNC(__VA_ARGS__); \
129  } \
130  } \
131  } while(0)
132 #endif // NDEBUG
133 
135 #ifndef NDEBUG
136  #define CLUTCHCODE( LEVEL, ... ) do { \
137  auto& clutchlog__logger = clutchlog::logger(); \
138  clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
139  if(clutchlog__scope.matches) { \
140  __VA_ARGS__ \
141  } \
142  } while(0)
143 #else // not Debug build.
144  #define CLUTCHCODE( LEVEL, CODE ) do { \
145  if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
146  auto& clutchlog__logger = clutchlog::logger(); \
147  clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
148  if(clutchlog__scope.matches) { \
149  CODE \
150  } \
151  } \
152  } while(0)
153 #endif // NDEBUG
154 
157 #else // not WITH_CLUTCHLOG
158  // Disabled macros can still be called in Release builds.
159  #define CLUTCHLOG( LEVEL, WHAT ) do {/*nothing*/} while(0)
160  #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do {/*nothing*/} while(0)
161  #define CLUTCHFUNC( LEVEL, FUNC, ... ) do {/*nothing*/} while(0)
162  #define CLUTCHCODE( LEVEL, CODE ) do {/*nothing*/} while(0)
163 #endif // WITH_CLUTCHLOG
164 
165 /**********************************************************************
166  * Implementation
167  **********************************************************************/
168 
169 #ifdef WITH_CLUTCHLOG
170 
178 {
179  protected:
180 
185  #ifndef NDEBUG
186  #ifndef CLUTCHLOG_DEFAULT_FORMAT
187  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1 // Enables: name, depth and depth_marks
189  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1 // Enables: hfill
190  #define CLUTCHLOG_DEFAULT_FORMAT "[{name}] {level_letter}:{depth_marks} {msg} {hfill} {func} @ {file}:{line}\n"
191  #else
192  #define CLUTCHLOG_DEFAULT_FORMAT "[{name}] {level_letter}:{depth_marks} {msg}\t\t\t\t\t{func} @ {file}:{line}\n"
193  #endif
194  #else
195  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
196  #define CLUTCHLOG_DEFAULT_FORMAT "{level_letter} {msg} {hfill} {func} @ {file}:{line}\n"
197  #else
198  #define CLUTCHLOG_DEFAULT_FORMAT "{level_letter} {msg}\t\t\t\t\t{func} @ {file}:{line}\n"
199  #endif
200  #endif
201  #endif
202  #else
203  #ifndef CLUTCHLOG_DEFAULT_FORMAT
204  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
206  #define CLUTCHLOG_DEFAULT_FORMAT "[{name}] {level_letter}:{depth_marks} {msg} {hfill} {func}\n"
207  #else
208  #define CLUTCHLOG_DEFAULT_FORMAT "{level_letter} {msg}\t\t\t\t\t{func}\n"
209  #endif
210  #endif
211  #endif
212  static inline std::string default_format = CLUTCHLOG_DEFAULT_FORMAT;
214 
215  #ifndef NDEBUG
216  #ifndef CLUTCHDUMP_DEFAULT_FORMAT
217  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
219  #define CLUTCHDUMP_DEFAULT_FORMAT "# [{name}] {level} in {func} (at depth {depth}) @ {file}:{line}"
220  #else
221  #define CLUTCHDUMP_DEFAULT_FORMAT "# {level} in {func} @ {file}:{line}"
222  #endif
223  #endif // CLUTCHDUMP_DEFAULT_FORMAT
224  #else
225  #ifndef CLUTCHDUMP_DEFAULT_FORMAT
226  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
228  #define CLUTCHDUMP_DEFAULT_FORMAT "# [{name}] {level} in {func} (at depth {depth})"
229  #else
230  #define CLUTCHDUMP_DEFAULT_FORMAT "# {level} in {func}"
231  #endif
232  #endif // CLUTCHDUMP_DEFAULT_FORMAT
233  #endif
234  static inline std::string dump_default_format = CLUTCHDUMP_DEFAULT_FORMAT;
236 
237  #ifndef CLUTCHDUMP_DEFAULT_SEP
238  #define CLUTCHDUMP_DEFAULT_SEP "\n"
240  #endif // CLUTCHDUMP_DEFAULT_SEP
241  static inline std::string dump_default_sep = CLUTCHDUMP_DEFAULT_SEP;
243 
244  #ifndef CLUTCHLOG_DEFAULT_DEPTH_MARK
245  #define CLUTCHLOG_DEFAULT_DEPTH_MARK ">"
247  #endif // CLUTCHLOG_DEFAULT_DEPTH_MARK
248  static inline std::string default_depth_mark = CLUTCHLOG_DEFAULT_DEPTH_MARK;
250 
251  #ifndef CLUTCHLOG_STRIP_CALLS
252  #define CLUTCHLOG_STRIP_CALLS 5
254  #endif // CLUTCHLOG_STRIP_CALLS
255  static inline unsigned int default_strip_calls = CLUTCHLOG_STRIP_CALLS;
257 
258  #ifndef CLUTCHLOG_DEFAULT_HFILL_MARK
259  #define CLUTCHLOG_DEFAULT_HFILL_MARK '.'
261  #endif // CLUTCHLOG_DEFAULT_HFILL_MARK
264 
265 
266  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
267  #ifndef CLUTCHLOG_DEFAULT_HFILL_MAX
268  #define CLUTCHLOG_DEFAULT_HFILL_MAX 300
269  #endif
270  #ifndef CLUTCHLOG_DEFAULT_HFILL_MIN
271  #define CLUTCHLOG_DEFAULT_HFILL_MIN 150
272  #endif
273  #endif
274  static inline size_t default_hfill_max = CLUTCHLOG_DEFAULT_HFILL_MAX;
277  static inline size_t default_hfill_min = CLUTCHLOG_DEFAULT_HFILL_MIN;
278 
279  // NOTE: there is no CLUTCHLOG_HFILL_STYLE for defaulting,
280  // but you can still set `hfill_style(...)` on the logger singleton.
281  /* @} DefaultConfig */
282  /* @} */
283 
284 
285  public:
286 
296  static clutchlog& logger()
297  {
298  static clutchlog instance;
299  return instance;
300  }
301 
303  enum level {critical=0, error=1, warning=2, progress=3, note=4, info=5, debug=6, xdebug=7};
304 
366  class fmt {
367  public:
369  enum class ansi {
371  colors_16 = -1, // Not supposed to be casted.
373  colors_256 = 5, // Casted as short in color::operator<<.
375  colors_16M = 2 // Casted as short in color::operator<<
376  } mode;
377 
379  enum class typo {
380  reset = 0,
381  bold = 1,
382  underline = 4,
383  inverse = 7,
384  none = -1
385  } style;
386 
389  enum class fg {
391  black = 30,
392  red = 31,
393  green = 32,
394  yellow = 33,
395  blue = 34,
396  magenta = 35,
397  cyan = 36,
398  white = 37,
399  bright_black = 90,
400  bright_red = 91,
401  bright_green = 92,
402  bright_yellow = 93,
403  bright_blue = 94,
404  bright_magenta = 95,
405  bright_cyan = 96,
406  bright_white = 97,
407  none = -1
408  } fore;
409 
411  enum class bg {
412  black = 40,
413  red = 41,
414  green = 42,
415  yellow = 43,
416  blue = 44,
417  magenta = 45,
418  cyan = 46,
419  white = 47,
420  bright_black = 100,
421  bright_red = 101,
422  bright_green = 102,
423  bright_yellow = 103,
424  bright_blue = 104,
425  bright_magenta = 105,
426  bright_cyan = 106,
427  bright_white = 107,
428  none = -1
429  } back;
430 
431  protected:
433  friend std::ostream& operator<<(std::ostream& os, const std::tuple<fg,bg,typo>& fbs)
434  {
435  auto [f,b,s] = fbs;
436  std::vector<short> codes; codes.reserve(3);
437  if(f != fg::none) { codes.push_back(static_cast<short>(f));}
438  if(b != bg::none) { codes.push_back(static_cast<short>(b));}
439  if(s != typo::none) { codes.push_back(static_cast<short>(s));}
440  if(codes.size() == 0) {
441  return os;
442 
443  } else {
444  os << "\033[";
445  os << codes[0];
446  for(size_t i=1; i < codes.size(); ++i) {
447  os << ";" << codes[i];
448  }
449  os << "m";
450  }
451  return os;
452  }
453 
455  friend std::ostream& operator<<(std::ostream& os, const typo& s)
456  {
457  if(s != typo::none) {
458  os << "\033[" << static_cast<short>(s) << "m";
459  }
460  return os;
461  }
462 
465  protected:
470  struct color {
471  ansi mode; // Not const to allow for the implicit copy assignemnt operator.
472 
474  enum class ground { // idem.
475  fore = 38,
476  back = 48
477  } type;
478 
484  color(ansi a, ground g) : mode(a), type(g) {}
485 
487  virtual bool is_set() const = 0;
488 
490  virtual std::ostream& print_on( std::ostream& os) const = 0;
491 
493  friend std::ostream& operator<<(std::ostream& os, const color& c)
494  {
495  if(c.is_set()) {
496  os << "\033[" << static_cast<short>(c.type) << ";" << static_cast<short>(c.mode) << ";";
497  c.print_on(os);
498  os << "m";
499  }
500  return os;
501  }
502  };
503 
504  // There is no color_16 because it would be the same as color_256, only with different indices,
505  // hence making it more complicated for the user to select the right constructor.
506  // Here, we just use enum for 16 colors, and indices for 256 colors.
507 
509  struct color_256 : public color {
513  short index;
514 
518  color_256(ground t) : color(ansi::colors_256, t), index(-1) {}
519 
525  color_256(ground t, const short i) : color(ansi::colors_256, t), index(i) {assert(-1 <= i and i <= 255);}
526 
528  bool is_set() const {return index > -1;}
529 
531  std::ostream& print_on( std::ostream& os) const
532  {
533  os << index;
534  return os;
535  }
536  };
537 
539  struct fg_256 : public color_256 {
541  fg_256() : color_256(ground::fore) {}
542 
546  fg_256(const short f) : color_256(ground::fore, f) {}
547 
551  fg_256(const fg&) : color_256(ground::fore, -1) {}
552 
553  } fore_256;
554 
556  struct bg_256 : public color_256 {
558  bg_256() : color_256(ground::back) {}
559 
563  bg_256(const short b) : color_256(ground::back, b) {}
564 
568  bg_256(const bg&) : color_256(ground::back, -1) {}
569 
570  } back_256;
571 
573  struct color_16M : public color {
577  short red, green, blue;
578 
582  color_16M(ground t) : color(ansi::colors_16M, t), red(-1), green(-1), blue(-1) {}
583 
591  color_16M(ground t, short r, short g, short b)
592  : color(ansi::colors_16M, t), red(r), green(g), blue(b) {}
593 
601  color_16M(ground t, const std::string& srgb) : color(ansi::colors_16M, t)
602  {
603  assert(srgb.size() == 7);
604  if(srgb.size() != 7) {
605  red = -1;
606  green = -1;
607  blue = -1;
608  } else {
609  char i = 0;
610  if(srgb.at(0) == '#') {
611  i = 1;
612  }
613  std::istringstream(srgb.substr(0+i,2)) >> std::hex >> red;
614  std::istringstream(srgb.substr(2+i,2)) >> std::hex >> green;
615  std::istringstream(srgb.substr(4+i,2)) >> std::hex >> blue;
616  }
617  assert(-1 <= red and red <= 255);
618  assert(-1 <= green and green <= 255);
619  assert(-1 <= blue and blue <= 255);
620  }
621 
623  bool is_set() const {return red > -1 and green > -1 and blue > -1;}
624 
626  std::ostream& print_on( std::ostream& os) const
627  {
628  os << red << ";" << green << ";" << blue;
629  return os;
630  }
631  };
632 
634  struct fg_16M : public color_16M {
636  fg_16M() : color_16M(ground::fore) {}
637 
646  fg_16M(short r, short g, short b) : color_16M(ground::fore, r,g,b) {}
647 
654  fg_16M(const std::string& srgb) : color_16M(ground::fore, srgb) {}
655 
659  fg_16M(const fg&) : color_16M(ground::fore, -1,-1,-1) {}
660 
661  } fore_16M;
662 
664  struct bg_16M : public color_16M {
666  bg_16M() : color_16M(ground::back) {}
667 
676  bg_16M(short r, short g, short b) : color_16M(ground::back, r,g,b) {}
677 
684  bg_16M(const std::string& srgb) : color_16M(ground::back, srgb) {}
685 
689  bg_16M(const bg&) : color_16M(ground::back, -1,-1,-1) {}
690 
691  } back_16M;
692 
695  public:
697  fmt() : mode(ansi::colors_16), style(typo::none), fore(fg::none), back(bg::none) {}
698 
701  fmt( fg f, bg b = bg::none, typo s = typo::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
702  fmt( fg f, typo s , bg b = bg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
703  fmt( bg b, fg f = fg::none, typo s = typo::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
704  fmt( bg b, typo s , fg f = fg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
705  fmt(typo s, fg f = fg::none, bg b = bg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
706  fmt(typo s, bg b , fg f = fg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
711  fmt(fg_256 f, bg_256 b, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(f), back_256(b) {}
712  fmt(fg_256 f, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(f), back_256(bg::none) {}
713  fmt(fg, bg_256 b, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(fg::none), back_256(b) {}
718  fmt(const short fr, const short fg, const short fb,
719  const short gr, const short gg, const short gb,
720  typo s = typo::none)
721  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(gr,gg,gb) {}
722  fmt(fg,
723  const short gr, const short gg, const short gb,
724  typo s = typo::none)
725  : mode(ansi::colors_16M), style(s), fore_16M(fg::none), back_16M(gr,gg,gb) {}
726  fmt(const short fr, const short fg, const short fb,
727  bg, typo s = typo::none)
728  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(bg::none) {}
729  fmt(const short fr, const short fg, const short fb,
730  typo s = typo::none)
731  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(bg::none) {}
732 
733  fmt(const std::string& f, const std::string& b, typo s = typo::none)
734  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(b) {}
735  fmt(fg, const std::string& b, typo s = typo::none)
736  : mode(ansi::colors_16M), style(s), fore_16M(fg::none), back_16M(b) {}
737  fmt(const std::string& f, bg, typo s = typo::none)
738  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(bg::none) {}
739  fmt(const std::string& f, typo s = typo::none)
740  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(bg::none) {}
743  protected:
744 
746  std::ostream& print_on( std::ostream& os) const
747  {
748  if(mode == ansi::colors_16) {
749  // Print all in a single escape.
750  os << std::make_tuple(fore,back,style);
751 
752  } else {
753  // 256 or 16M: always print separated escapes for foreground/background.
754  if(mode == ansi::colors_256) {
755  os << fore_256;
756  os << back_256;
757 
758  } else if(mode == ansi::colors_16M) {
759  os << fore_16M;
760  os << back_16M;
761  }
762  // In any case, print the style separately.
763  os << style;
764  }
765  return os;
766  }
767 
768  public:
780  friend std::ostream& operator<<(std::ostream& os, const fmt& fmt)
781  {
782  return fmt.print_on(os);
783  }
784 
795  std::string operator()( const std::string& msg ) const
796  {
797  std::ostringstream os;
798  this->print_on(os);
799  fmt reset(fmt::typo::reset);
800  os << msg;
801  reset.print_on(os);
802  return os.str();
803  }
804 
807  std::string str() const
808  {
809  std::ostringstream os;
810  this->print_on(os);
811  return os.str();
812  }
813 
814  static fmt hash( const std::string& str, const std::vector<fmt> domain = {})
815  {
816  size_t h = std::hash<std::string>{}(str);
817  if(domain.size() == 0) {
818  return fmt(static_cast<short>(h % 256));
819  } else {
820  return fmt(domain[h % domain.size()]);
821  }
822  }
823  }; // fmt class
824 
830  public:
831  clutchlog(clutchlog const&) = delete;
832  void operator=(clutchlog const&) = delete;
833 
834  private:
835  clutchlog() :
836  // system, main, log
838  _level_word({
839  {level::critical,"Critical"},
840  {level::error ,"Error"},
841  {level::warning ,"Warning"},
842  {level::progress,"Progress"},
843  {level::note ,"Note"},
844  {level::info ,"Info"},
845  {level::debug ,"Debug"},
846  {level::xdebug ,"XDebug"}
847  }),
848  _level_short({
849  {level::critical, "Crit"},
850  {level::error , "Erro"},
851  {level::warning , "Warn"},
852  {level::progress, "Prog"},
853  {level::note , "Note"},
854  {level::info , "Info"},
855  {level::debug , "Dbug"},
856  {level::xdebug , "XDbg"}
857  }),
858  _level_fmt({
859  {level::critical,fmt(fmt::fg::red, fmt::typo::underline)},
860  {level::error ,fmt(fmt::fg::red, fmt::typo::bold)},
861  {level::warning ,fmt(fmt::fg::magenta, fmt::typo::bold)},
862  {level::progress,fmt()},
863  {level::note ,fmt()},
864  {level::info ,fmt()},
865  {level::debug ,fmt()},
866  {level::xdebug ,fmt()}
867  }),
870  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
871  _hfill_char(clutchlog::default_hfill_char),
872  _hfill_fmt(fmt::fg::none),
873  _hfill_max(clutchlog::default_hfill_max),
874  _hfill_min(clutchlog::default_hfill_min),
875  #endif
876  _out(&std::clog),
877  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
878  _depth(std::numeric_limits<size_t>::max() - _strip_calls),
879  _depth_mark(clutchlog::default_depth_mark),
880  #endif
881  _stage(level::error),
882  _in_file(".*"),
883  _in_func(".*"),
884  _in_line(".*")
885  // Empty vectors by default:
886  // _filehash_fmts
887  // _funchash_fmts
888  // _depth_fmts
889  {
890  // Reverse the level->word map into a word->level map.
891  for(auto& lw : _level_word) {
892  _word_level[lw.second] = lw.first;
893  }
894 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
895  struct winsize w;
896  ioctl(STDERR_FILENO, TIOCGWINSZ, &w);
897  _nb_columns = std::max(std::min((size_t)w.ws_col, default_hfill_max), default_hfill_min);
898 #endif
899  }
900 
901  protected:
903  size_t _strip_calls;
905  const std::map<level,std::string> _level_word;
907  std::map<std::string,level> _word_level;
909  std::map<level,std::string> _level_short;
911  std::map<level,fmt> _level_fmt;
913  std::string _format_log;
915  std::string _format_dump;
916  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
917 
918  char _hfill_char;
920  fmt _hfill_fmt;
922  size_t _hfill_max;
924  size_t _hfill_min;
925  #endif
926 
927  std::ostream* _out;
928  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
929 
930  size_t _depth;
932  std::string _depth_mark;
933  #endif
934 
937  std::regex _in_file;
939  std::regex _in_func;
941  std::regex _in_line;
942 
944  std::vector<fmt> _filehash_fmts;
946  std::vector<fmt> _funchash_fmts;
947 
948 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
949 
950  static const size_t _max_buffer = 4096;
952  std::vector<fmt> _depth_fmts;
953 #endif
954 
955 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
956 
957  size_t _nb_columns;
958 #endif
959 
961  public:
962 
966  void format(const std::string& format) {_format_log = format;}
969  std::string format() const {return _format_log;}
970 
972  void format_comment(const std::string& format) {_format_dump = format;}
974  std::string format_comment() const {return _format_dump;}
975 
977  void out(std::ostream& out) {_out = &out;}
979  std::ostream& out() {return *_out;}
980 
981 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
982  void depth(size_t d) {_depth = d;}
985  size_t depth() const {return _depth;}
986 
988  void depth_mark(const std::string mark) {_depth_mark = mark;}
990  std::string depth_mark() const {return _depth_mark;}
991 
993  void strip_calls(const size_t n) {_strip_calls = n;}
995  size_t strip_calls() const {return _strip_calls;}
996 #endif
997 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
998  void hfill_mark(const char mark) {_hfill_char = mark;}
1001  char hfill_mark() const {return _hfill_char;}
1003  void hfill_style(fmt style) {_hfill_fmt = style;}
1008  template<class ... FMT>
1009  void hfill_style(FMT... styles) { this->hfill_style(fmt(styles...)); }
1011  fmt hfill_style() const {return _hfill_fmt;}
1013  void hfill_max(const size_t nmax) {_hfill_max = nmax;}
1015  size_t hfill_max() {return _hfill_max;}
1017  void hfill_min(const size_t nmin) {_hfill_min = nmin;}
1019  size_t hfill_min() {return _hfill_min;}
1020 #endif
1021 
1028  void filehash_styles(std::vector<fmt> styles) {_filehash_fmts = styles;}
1036  void funchash_styles(std::vector<fmt> styles) {_funchash_fmts = styles;}
1045  void depth_styles(std::vector<fmt> styles) {_depth_fmts = styles;}
1046 
1048  void threshold(level l) {_stage = l;}
1050  void threshold(const std::string& l) {_stage = this->level_of(l);}
1052  level threshold() const {return _stage;}
1054  const std::map<std::string,level>& levels() const { return _word_level;}
1055 
1060  level level_of(const std::string name)
1061  {
1062  const auto ilevel = _word_level.find(name);
1063  if( ilevel != std::end(_word_level)) {
1064  return ilevel->second;
1065  } else {
1066  throw std::out_of_range("'" + name + "' is not a valid log level name");
1067  }
1068  }
1069 
1071  void file(std::string file) {_in_file = file;}
1073  void func(std::string func) {_in_func = func;}
1075  void line(std::string line) {_in_line = line;}
1076 
1078  void location(
1079  const std::string& in_file,
1080  const std::string& in_function=".*",
1081  const std::string& in_line=".*"
1082  )
1083  {
1084  file(in_file);
1085  func(in_function);
1086  line(in_line);
1087  }
1088 
1093  template<class ... FMT>
1094  void style(level stage, FMT... styles) { this->style(stage,fmt(styles...)); }
1096  void style(level stage, fmt style) { _level_fmt.at(stage) = style; }
1098  fmt style(level stage) const { return _level_fmt.at(stage); }
1099 
1102  public:
1103 
1107  struct scope_t {
1110  bool matches;
1113 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1114 
1115  size_t depth;
1116 #endif
1117 
1118  bool there;
1121  matches(false),
1122  stage(level::xdebug),
1124  depth(0),
1125 #endif
1126  there(false)
1127  {}
1128  }; // scope_t
1129 
1130 
1133  const level& stage,
1134  const std::string& file,
1135  const std::string& func,
1136  const size_t line
1137  ) const
1138  {
1139  scope_t scope; // False scope by default.
1140 
1141  /***** Log level stage *****/
1142  // Test stage first, because it's fastest.
1143  scope.stage = stage;
1144  if(not (scope.stage <= _stage)) {
1145  // Bypass useless computations if no match
1146  // because of the stage.
1147  return scope;
1148  }
1149 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1150  /***** Stack depth *****/
1151  // Backtrace in second, quite fast.
1152  size_t stack_depth;
1153  void *buffer[_max_buffer];
1154  stack_depth = backtrace(buffer, _max_buffer);
1155  scope.depth = stack_depth;
1156  if(not (scope.depth <= _depth + _strip_calls)) {
1157  // Bypass if no match.
1158  return scope;
1159  }
1160 #endif
1161 
1162  /***** Location *****/
1163  // Location last, slowest.
1164  std::ostringstream sline; sline << line;
1165  scope.there =
1166  std::regex_search(file, _in_file)
1167  and std::regex_search(func, _in_func)
1168  and std::regex_search(sline.str(), _in_line);
1169 
1170  // No need to retest stage and depth, which are true here.
1171  scope.matches = scope.there;
1172 
1173  return scope;
1174  } // locate
1175 
1183  std::string replace(
1184  const std::string& form,
1185  const std::string& mark,
1186  const std::string& tag
1187  ) const
1188  {
1189  // Useless debug code, unless something fancy would be done with name tags.
1190  // std::regex re;
1191  // try {
1192  // re = std::regex(mark);
1193  //
1194  // } catch(const std::regex_error& e) {
1195  // std::cerr << "ERROR with a regular expression \"" << mark << "\": ";
1196  // switch(e.code()) {
1197  // case std::regex_constants::error_collate:
1198  // std::cerr << "the expression contains an invalid collating element name";
1199  // break;
1200  // case std::regex_constants::error_ctype:
1201  // std::cerr << "the expression contains an invalid character class name";
1202  // break;
1203  // case std::regex_constants::error_escape:
1204  // std::cerr << "the expression contains an invalid escaped character or a trailing escape";
1205  // break;
1206  // case std::regex_constants::error_backref:
1207  // std::cerr << "the expression contains an invalid back reference";
1208  // break;
1209  // case std::regex_constants::error_brack:
1210  // std::cerr << "the expression contains mismatched square brackets ('[' and ']')";
1211  // break;
1212  // case std::regex_constants::error_paren:
1213  // std::cerr << "the expression contains mismatched parentheses ('(' and ')')";
1214  // break;
1215  // case std::regex_constants::error_brace:
1216  // std::cerr << "the expression contains mismatched curly braces ('{' and '}')";
1217  // break;
1218  // case std::regex_constants::error_badbrace:
1219  // std::cerr << "the expression contains an invalid range in a {} expression";
1220  // break;
1221  // case std::regex_constants::error_range:
1222  // std::cerr << "the expression contains an invalid character range (e.g. [b-a])";
1223  // break;
1224  // case std::regex_constants::error_space:
1225  // std::cerr << "there was not enough memory to convert the expression into a finite state machine";
1226  // break;
1227  // case std::regex_constants::error_badrepeat:
1228  // std::cerr << "one of *?+{ was not preceded by a valid regular expression";
1229  // break;
1230  // case std::regex_constants::error_complexity:
1231  // std::cerr << "the complexity of an attempted match exceeded a predefined level";
1232  // break;
1233  // case std::regex_constants::error_stack:
1234  // std::cerr << "there was not enough memory to perform a match";
1235  // break;
1236  // default:
1237  // std::cerr << "unknown error";
1238  // }
1239  // std::cerr << std::endl;
1240  // throw;
1241  // } // catch
1242 
1243  const std::regex re(mark);
1244  return std::regex_replace(form, re, tag);
1245  }
1246 
1248  std::string replace(
1249  const std::string& form,
1250  const std::string& mark,
1251  const size_t tag
1252  ) const
1253  {
1254  std::ostringstream stag; stag << tag;
1255  return replace(form, mark, stag.str());
1256  }
1257 
1259  std::string format(
1260  std::string row,
1261  const std::string& what,
1263  const std::string& name,
1264 #endif
1265  const level& stage,
1266  const std::string& file,
1267  const std::string& func,
1268  const size_t line
1270  ,
1271  const size_t depth
1272 #endif
1273  ) const
1274  {
1275  row = replace(row, "\\{msg\\}", what);
1276  row = replace(row, "\\{file\\}", file);
1277  row = replace(row, "\\{func\\}", func);
1278  row = replace(row, "\\{line\\}", line);
1279 
1280  row = replace(row, "\\{level\\}", _level_word.at(stage));
1281  std::string letter(1, _level_word.at(stage).at(0)); // char -> string
1282  row = replace(row, "\\{level_letter\\}", letter);
1283 
1284  row = replace(row, "\\{level_short\\}", _level_short.at(stage));
1285 
1286 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1287  size_t actual_depth = depth - _strip_calls;
1288  row = replace(row, "\\{name\\}", name);
1289  row = replace(row, "\\{depth\\}", actual_depth);
1290 
1291  std::ostringstream chevrons;
1292  for(size_t i = _strip_calls; i < depth; ++i) {
1293  chevrons << _depth_mark;
1294  }
1295  row = replace(row, "\\{depth_marks\\}", chevrons.str());
1296 
1297  if(_depth_fmts.size() == 0) {
1298  row = replace(row, "\\{depth_fmt\\}", fmt(actual_depth % 256).str() );
1299  } else {
1300  row = replace(row, "\\{depth_fmt\\}",
1301  _depth_fmts[std::min(actual_depth,_depth_fmts.size()-1)].str() );
1302  }
1303 #endif
1304  row = replace(row, "\\{level_fmt\\}", _level_fmt.at(stage).str());
1305  row = replace(row, "\\{filehash_fmt\\}", fmt::hash(file, _filehash_fmts).str() );
1306  row = replace(row, "\\{funchash_fmt\\}", fmt::hash(func, _funchash_fmts).str() );
1307 
1308 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
1309  // hfill is replaced last to allow for correct line width estimation.
1310  const std::string raw_row = replace(row, "\\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]", "");
1311  const std::string hfill_tag = "{hfill}";
1312  const size_t hfill_pos = row.find(hfill_tag);
1313  const size_t raw_hfill_pos = raw_row.find(hfill_tag);
1314  const size_t nb_columns = std::max(std::min((size_t)_nb_columns, _hfill_max), _hfill_min);
1315  if(hfill_pos != std::string::npos) {
1316  assert(raw_hfill_pos != std::string::npos);
1317  if(nb_columns > 0) {
1318  const size_t left_len = raw_hfill_pos;
1319  const size_t right_len = raw_row.size() - raw_hfill_pos - hfill_tag.size();
1320  if(right_len+left_len > nb_columns) {
1321  // The right part would go over the terminal width: add a new row.
1322  if(right_len < nb_columns) {
1323  // There is room for the right part on a new line.
1324  const std::string hfill(std::max((size_t)0, nb_columns-right_len), _hfill_char);
1325  const std::string hfill_styled = _hfill_fmt(hfill);
1326  row = replace(row, "\\{hfill\\}", "\n"+hfill_styled);
1327  } else {
1328  // Right part still goes over columns: let it go.
1329  const std::string hfill(1, _hfill_char);
1330  const std::string hfill_styled = _hfill_fmt(hfill);
1331  row = replace(row, "\\{hfill\\}", "\n"+hfill_styled);
1332  }
1333  } else {
1334  // There is some space in between left and right parts.
1335  const std::string hfill(std::max((size_t)0, nb_columns - (right_len+left_len)), _hfill_char);
1336  const std::string hfill_styled = _hfill_fmt(hfill);
1337  row = replace(row, "\\{hfill\\}", hfill_styled);
1338  }
1339  } else {
1340  // We don't know the terminal width.
1341  const std::string hfill(1, _hfill_char);
1342  const std::string hfill_styled = _hfill_fmt(hfill);
1343  row = replace(row, "\\{hfill\\}", hfill_styled);
1344  }
1345  }
1346 #else
1347  // We cannot know the terminal width.
1348  const std::string hfill(1, _hfill_char);
1349  const std::string hfill_styled = _hfill_fmt(hfill);
1350  row = replace(row, "\\{hfill\\}", hfill_styled);
1351 #endif
1352  return _level_fmt.at(stage)(row);
1353  }
1354 
1356  void log(
1357  const level& stage,
1358  const std::string& what,
1359  const std::string& file, const std::string& func, size_t line
1360  ) const
1361  {
1362  scope_t scope = locate(stage, file, func, line);
1363 
1364  if(scope.matches) {
1365 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1366  *_out << format(_format_log, what, basename(getenv("_")),
1367  stage, file, func,
1368  line, scope.depth );
1369 #else
1370  *_out << format(_format_log, what,
1371  stage, file, func,
1372  line );
1373 
1374 #endif
1375  _out->flush();
1376  } // if scopes.matches
1377  }
1378 
1380  template<class In>
1381  void dump(
1382  const level& stage,
1383  const In container_begin, const In container_end,
1384  const std::string& file, const std::string& func, size_t line,
1385  const std::string& filename_template = "dump_{n}.dat",
1386  const std::string sep = dump_default_sep
1387  ) const
1388  {
1389  scope_t scope = locate(stage, file, func, line);
1390 
1391  if(scope.matches) {
1392  const std::string tag = "\\{n\\}";
1393  const std::regex re(tag);
1394  std::string outfile = "";
1395 
1396  // If the file name template has the {n} tag.
1397  if(std::regex_search(filename_template, re)) {
1398  // Increment n until a free one is found.
1399  size_t n = 0;
1400  do {
1401  outfile = replace(filename_template, tag, n);
1402  n++;
1403  } while( fs::exists( outfile ) );
1404 
1405  } else {
1406  // Use the parameter as is.
1407  outfile = filename_template;
1408  }
1409 
1410  std::ofstream fd(outfile);
1411 
1412  if(_format_dump.size() > 0) {
1413 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1414  fd << format(_format_dump, "", basename(getenv("_")),
1415  stage, file, func,
1416  line, scope.depth );
1417 #else
1418  fd << format(_format_dump, "",
1419  stage, file, func,
1420  line );
1421 #endif
1422  fd << sep; // sep after comment line.
1423  }
1424 
1425  std::copy(container_begin, container_end,
1426  std::ostream_iterator<typename In::value_type>(fd, sep.c_str()));
1427 
1428  fd.close();
1429  } // if scopes.matches
1430  }
1431 
1433 };
1434 
1437 #else // not WITH_CLUTCHLOG
1438 
1439 
1440 /**********************************************************************
1441  * Fake implementation
1442  **********************************************************************/
1443 
1444 // Equivalent class with empty methods, will be optimized out
1445 // while allowing to actually have calls implemented without WITH_CLUTCHLOG guards.
1446 #pragma GCC diagnostic push
1447 #pragma GCC diagnostic ignored "-Wreturn-type"
1448 class clutchlog
1449 {
1450  public:
1451  static clutchlog& logger() {}
1452  enum level {critical=0, error=1, warning=2, progress=3, note=4, info=5, debug=6, xdebug=7};
1453  class fmt {
1454  public:
1455  enum class ansi { colors_16, colors_256, colors_16M} mode;
1456  enum class typo { reset, bold, underline, inverse, none} style;
1457  enum class fg { black, red, green, yellow, blue, magenta, cyan, white, bright_black, bright_red, bright_green, bright_yellow, bright_blue, bright_magenta, bright_cyan, bright_white, none} fore;
1458  enum class bg { black, red, green, yellow, blue, magenta, cyan, white, bright_black, bright_red, bright_green, bright_yellow, bright_blue, bright_magenta, bright_cyan, bright_white, none } back;
1459  protected:
1460  friend std::ostream& operator<<(std::ostream&, const std::tuple<fg,bg,typo>&) {}
1461  friend std::ostream& operator<<(std::ostream&, const typo&) {}
1462  protected:
1463  struct color {
1464  ansi mode;
1465  enum class ground { fore, back } type;
1466  color(ansi a, ground g) : mode(a), type(g) {}
1467  virtual bool is_set() const = 0;
1468  virtual std::ostream& print_on( std::ostream&) const = 0;
1469  friend std::ostream& operator<<(std::ostream&, const color&) {}
1470  };
1471  struct color_256 : public color {
1472  short index;
1473  color_256(ground t) : color(ansi::colors_256, t), index(-1) {}
1474  color_256(ground t, const short i) : color(ansi::colors_256, t), index(i) {}
1475  bool is_set() const {}
1476  std::ostream& print_on( std::ostream&) const {}
1477  };
1478  struct fg_256 : public color_256 {
1479  fg_256() : color_256(ground::fore) {}
1480  fg_256(const short f) : color_256(ground::fore, f) {}
1481  fg_256(const fg&) : color_256(ground::fore, -1) {}
1482  } fore_256;
1483  struct bg_256 : public color_256 {
1484  bg_256() : color_256(ground::back) {}
1485  bg_256(const short b) : color_256(ground::back, b) {}
1486  bg_256(const bg&) : color_256(ground::back, -1) {}
1487  } back_256;
1488  struct color_16M : public color {
1489  short red, green, blue;
1490  color_16M(ground t) : color(ansi::colors_16M, t), red(-1), green(-1), blue(-1) {}
1491  color_16M(ground t, short r, short g, short b) : color(ansi::colors_16M, t), red(r), green(g), blue(b) {}
1492  color_16M(ground t, const std::string&) : color(ansi::colors_16M, t) {}
1493  bool is_set() const {return red > -1 and green > -1 and blue > -1;}
1494  std::ostream& print_on( std::ostream&) const {}
1495  };
1496  struct fg_16M : public color_16M {
1497  fg_16M() : color_16M(ground::fore) {}
1498  fg_16M(short r, short g, short b) : color_16M(ground::fore, r,g,b) {}
1499  fg_16M(const std::string& srgb) : color_16M(ground::fore, srgb) {}
1500  fg_16M(const fg&) : color_16M(ground::fore, -1,-1,-1) {}
1501  } fore_16M;
1502  struct bg_16M : public color_16M {
1503  bg_16M() : color_16M(ground::back) {}
1504  bg_16M(short r, short g, short b) : color_16M(ground::back, r,g,b) {}
1505  bg_16M(const std::string& srgb) : color_16M(ground::back, srgb) {}
1506  bg_16M(const bg&) : color_16M(ground::back, -1,-1,-1) {}
1507  } back_16M;
1508  public:
1509  fmt() : mode(ansi::colors_16), style(typo::none), fore(fg::none), back(bg::none) {}
1510  fmt( fg f, bg b = bg::none, typo s = typo::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1511  fmt( fg f, typo s , bg b = bg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1512  fmt( bg b, fg f = fg::none, typo s = typo::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1513  fmt( bg b, typo s , fg f = fg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1514  fmt(typo s, fg f = fg::none, bg b = bg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1515  fmt(typo s, bg b , fg f = fg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1516  fmt(fg_256 f, bg_256 b, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(f), back_256(b) {}
1517  fmt(fg_256 f, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(f), back_256(bg::none) {}
1518  fmt(fg, bg_256 b, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(fg::none), back_256(b) {}
1519  fmt(const short fr, const short fg, const short fb,
1520  const short gr, const short gg, const short gb,
1521  typo s = typo::none)
1522  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(gr,gg,gb) {}
1523  fmt(fg,
1524  const short gr, const short gg, const short gb,
1525  typo s = typo::none)
1526  : mode(ansi::colors_16M), style(s), fore_16M(fg::none), back_16M(gr,gg,gb) {}
1527  fmt(const short fr, const short fg, const short fb,
1528  bg, typo s = typo::none)
1529  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(bg::none) {}
1530  fmt(const short fr, const short fg, const short fb,
1531  typo s = typo::none)
1532  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(bg::none) {}
1533 
1534  fmt(const std::string& f, const std::string& b, typo s = typo::none)
1535  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(b) {}
1536  fmt(fg, const std::string& b, typo s = typo::none)
1537  : mode(ansi::colors_16M), style(s), fore_16M(fg::none), back_16M(b) {}
1538  fmt(const std::string& f, bg, typo s = typo::none)
1539  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(bg::none) {}
1540  fmt(const std::string& f, typo s = typo::none)
1541  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(bg::none) {}
1542  protected:
1543  std::ostream& print_on( std::ostream&) const {}
1544  public:
1545  friend std::ostream& operator<<(std::ostream&, const fmt&) {}
1546  std::string operator()( const std::string&) const {}
1547  std::string str() const {}
1548  static fmt hash( const std::string&, const std::vector<fmt>) {}
1549  };
1550  public:
1551  clutchlog(clutchlog const&) = delete;
1552  void operator=(clutchlog const&) = delete;
1553  private:
1554  clutchlog() {}
1555  protected:
1556  struct scope_t {};
1557  scope_t locate(
1558  const level&,
1559  const std::string&,
1560  const std::string&,
1561  const size_t
1562  ) const
1563  {}
1564  public:
1565  void format(const std::string&) {}
1566  std::string format() const {}
1567 
1568  void format_comment(const std::string&) {}
1569  std::string format_comment() const {}
1570 
1571  void out(std::ostream&) {}
1572  std::ostream& out() {}
1573 
1574 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1575  void depth(size_t) {}
1576  size_t depth() const {}
1577 
1578  void depth_mark(const std::string) {}
1579  std::string depth_mark() const {}
1580  void strip_calls(const size_t) {}
1581  size_t strip_calls() const {}
1582 #endif
1583 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
1584  void hfill_mark(const char) {}
1585  char hfill_mark() const {}
1586  void hfill_fmt(fmt) {}
1587  fmt hfill_fmt() const {}
1588  void hfill_min(const size_t) {}
1589  size_t hfill_min() {}
1590  void hfill_max(const size_t) {}
1591  size_t hfill_max() {}
1592 #endif
1593  void filehash_styles(std::vector<fmt> ) {}
1594  void funchash_styles(std::vector<fmt> ) {}
1595  void depth_styles(std::vector<fmt>) {}
1596 
1597  void threshold(level) {}
1598  void threshold(const std::string&) {}
1599  level threshold() const {}
1600  const std::map<std::string,level> levels() const {}
1601  level level_of(const std::string) {}
1602 
1603  void file(std::string) {}
1604  void func(std::string) {}
1605  void line(std::string) {}
1606 
1607 #pragma GCC diagnostic push
1608 #pragma GCC diagnostic ignored "-Wunused-parameter"
1609  void location(
1610  const std::string&,
1611  const std::string& in_function=".*",
1612  const std::string& in_line=".*"
1613  )
1614  {}
1615 #pragma GCC diagnostic pop
1616  template<class ... FMT>
1617  void style(level, FMT...) {}
1618  void style(level, fmt) {}
1619  fmt style(level) const {}
1620  public:
1621  std::string replace(
1622  const std::string&,
1623  const std::string&,
1624  const std::string&
1625  ) const
1626  {}
1627 
1628  std::string replace(
1629  const std::string&,
1630  const std::string&,
1631  const size_t
1632  ) const
1633  {}
1634 
1635  std::string format(
1636  std::string,
1637  const std::string&,
1639  const std::string&,
1640 #endif
1641  const level&,
1642  const std::string&,
1643  const std::string&,
1644  const size_t
1646  ,
1647  const size_t
1648 #endif
1649  ) const
1650  {}
1651 
1652  void log(
1653  const level&,
1654  const std::string&,
1655  const std::string&, const std::string&, size_t
1656  ) const
1657  {}
1658 
1659  template<class In>
1660  void dump(
1661  const level&,
1662  const In, const In,
1663  const std::string&, const std::string&, size_t,
1664  const std::string&,
1665  const std::string
1666  ) const
1667  {}
1668 };
1669 #pragma GCC diagnostic pop
1670 #endif // WITH_CLUTCHLOG
1671 
1672 #endif // CLUTCHLOG_H
clutchlog::fmt::operator<<
friend std::ostream & operator<<(std::ostream &os, const std::tuple< fg, bg, typo > &fbs)
Output stream operator for a 3-tuple of 16-colors mode tags.
Definition: clutchlog.h:433
clutchlog::default_depth_mark
static std::string default_depth_mark
Default mark for stack depth.
Definition: clutchlog.h:249
clutchlog::_format_log
std::string _format_log
Current format of the standard output.
Definition: clutchlog.h:913
clutchlog::fmt::color_16M::color_16M
color_16M(ground t, const std::string &srgb)
Hex triplet string constructor.
Definition: clutchlog.h:601
clutchlog::depth_styles
void depth_styles(std::vector< fmt > styles)
Set the styles for value-dependant depth formatting.
Definition: clutchlog.h:1045
clutchlog::_level_fmt
std::map< level, fmt > _level_fmt
Dictionary of level identifier to their format.
Definition: clutchlog.h:911
clutchlog::log
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:1356
clutchlog::fmt::back
enum clutchlog::fmt::bg back
Background color.
clutchlog::_funchash_fmts
std::vector< fmt > _funchash_fmts
List of candidate format objects for value-dependant function name styling.
Definition: clutchlog.h:946
clutchlog::fmt::str
std::string str() const
Return the formatting code as a string.
Definition: clutchlog.h:807
clutchlog::fmt::fg_256
Foreground in 256-colors mode.
Definition: clutchlog.h:539
clutchlog::line
void line(std::string line)
Set the regular expression filtering the line location.
Definition: clutchlog.h:1075
clutchlog::fmt::color::operator<<
friend std::ostream & operator<<(std::ostream &os, const color &c)
Print the actually encoded escaped color sequence on the given stream.
Definition: clutchlog.h:493
clutchlog::fmt::operator<<
friend std::ostream & operator<<(std::ostream &os, const typo &s)
Output stream operator for a typo tag alone, in 16-colors mode.
Definition: clutchlog.h:455
clutchlog::dump_default_format
static std::string dump_default_format
Default format of the comment line in file dump.
Definition: clutchlog.h:235
clutchlog::out
void out(std::ostream &out)
Set the output stream on which to print.
Definition: clutchlog.h:977
clutchlog::fmt::color_16M::is_set
bool is_set() const
Returns true if the underying representation encodes an existing color.
Definition: clutchlog.h:623
clutchlog::fmt::fg_16M::fg_16M
fg_16M(const fg &)
Conversion constructor from 16-colors mode.
Definition: clutchlog.h:659
clutchlog::dump_default_sep
static std::string dump_default_sep
Default item separator for dump.
Definition: clutchlog.h:242
clutchlog::format
std::string format(std::string row, 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:1259
clutchlog::fmt::bg_256
Background in 256-colors mode.
Definition: clutchlog.h:556
CLUTCHLOG_DEFAULT_DEPTH_MARK
#define CLUTCHLOG_DEFAULT_DEPTH_MARK
Compile-time default mark for stack depth.
Definition: clutchlog.h:246
clutchlog::default_strip_calls
static unsigned int default_strip_calls
Number of call stack levels to remove from depth display by default.
Definition: clutchlog.h:256
clutchlog::default_hfill_min
static size_t default_hfill_min
Default minimum width (number of characters) at which to fill for right-aligning the right part of me...
Definition: clutchlog.h:277
clutchlog::replace
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:1183
clutchlog::fmt::color_256::color_256
color_256(ground t)
Constructor.
Definition: clutchlog.h:518
clutchlog::fmt::back_16M
clutchlog::fmt::bg_16M back_16M
Current background in 16M-colors mode.
clutchlog::fmt::color
Interface class for colors representation.
Definition: clutchlog.h:470
clutchlog::fmt::ansi::colors_16
@ colors_16
16 colors mode.
clutchlog::fmt::bg_16M
background in 256-colors mode.
Definition: clutchlog.h:664
clutchlog::default_hfill_char
static char default_hfill_char
Default character used as a filling for right-align the right part of messages with "{hfill}".
Definition: clutchlog.h:263
clutchlog::scope_t::matches
bool matches
Everything is compatible.
Definition: clutchlog.h:1110
clutchlog::fmt::style
enum clutchlog::fmt::typo style
Typographic style.
clutchlog::fmt::color_16M
Abstract base class for 16M colors objects (24-bits ANSI).
Definition: clutchlog.h:573
clutchlog::fmt::fg_256::fg_256
fg_256(const fg &)
Conversion constructor from 16-colors mode.
Definition: clutchlog.h:551
clutchlog::format_comment
void format_comment(const std::string &format)
Set the template string for dumps.
Definition: clutchlog.h:972
clutchlog::fmt::bg_16M::bg_16M
bg_16M(const std::string &srgb)
Hex triplet string constructor.
Definition: clutchlog.h:684
clutchlog::file
void file(std::string file)
Set the regular expression filtering the file location.
Definition: clutchlog.h:1071
clutchlog::locate
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:1132
clutchlog::fmt::fore_16M
clutchlog::fmt::fg_16M fore_16M
Current foreground in 16M-colors mode.
clutchlog::fmt::color_256
Abstract base class for 256 colors objects (8-bits ANSI).
Definition: clutchlog.h:509
clutchlog::fmt::fmt
fmt()
Empty constructor, only useful for a no-op formatter.
Definition: clutchlog.h:697
clutchlog::style
void style(level stage, fmt style)
Set the style (color and typo) of the given log level, passing a fmt instance.
Definition: clutchlog.h:1096
clutchlog::threshold
void threshold(level l)
Set the log level (below which logs are not printed) with an identifier.
Definition: clutchlog.h:1048
clutchlog::threshold
level threshold() const
Get the log level below which logs are not printed.
Definition: clutchlog.h:1052
clutchlog::fmt::color::is_set
virtual bool is_set() const =0
Should return true if the underying representation encodes an existing color.
clutchlog::_level_short
std::map< level, std::string > _level_short
dictionary of level identifier to their 4-letters representation.
Definition: clutchlog.h:909
clutchlog::level
level
Available log levels.
Definition: clutchlog.h:303
clutchlog::fmt::color_256::color_256
color_256(ground t, const short i)
Constructor.
Definition: clutchlog.h:525
clutchlog::fmt::color_256::print_on
std::ostream & print_on(std::ostream &os) const
Print the color index on the given stream.
Definition: clutchlog.h:531
clutchlog::fmt::color_16M::color_16M
color_16M(ground t)
Constructor.
Definition: clutchlog.h:582
clutchlog::default_hfill_max
static size_t default_hfill_max
Default maximum width (number of characters) for which to fill for right-aligning the right part of m...
Definition: clutchlog.h:275
clutchlog::funchash_styles
void funchash_styles(std::vector< fmt > styles)
Set the candidate styles for value-dependant function name formatting.
Definition: clutchlog.h:1036
clutchlog::scope_t::scope_t
scope_t()
Constructor.
Definition: clutchlog.h:1120
clutchlog::_in_func
std::regex _in_func
Current function location filter.
Definition: clutchlog.h:939
clutchlog::default_format
static std::string default_format
Default format of the messages.
Definition: clutchlog.h:213
clutchlog::fmt::bg_16M::bg_16M
bg_16M(short r, short g, short b)
Numeric triplet constructor.
Definition: clutchlog.h:676
clutchlog::logger
static clutchlog & logger()
Get the logger instance.
Definition: clutchlog.h:296
clutchlog::fmt::fg_16M
Foreground in 256-colors mode.
Definition: clutchlog.h:634
clutchlog::dump
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:1381
clutchlog::fmt::fore_256
clutchlog::fmt::fg_256 fore_256
Current foreground in 256-colors mode.
clutchlog::fmt::fore
enum clutchlog::fmt::fg fore
Foreground color.
clutchlog::filehash_styles
void filehash_styles(std::vector< fmt > styles)
Set the candidate styles for value-dependant file name formatting.
Definition: clutchlog.h:1028
clutchlog::fmt::color::type
enum clutchlog::fmt::color::ground type
Type of color (foreground or background).
clutchlog::fmt
Color and style formatter for ANSI terminal escape sequences.
Definition: clutchlog.h:366
clutchlog::fmt::color_256::index
short index
The encoded color index in 4-bits ANSI.
Definition: clutchlog.h:513
clutchlog::fmt::bg_256::bg_256
bg_256()
Empty constructor: no color.
Definition: clutchlog.h:558
clutchlog::func
void func(std::string func)
Set the regular expression filtering the function location.
Definition: clutchlog.h:1073
clutchlog::format
std::string format() const
Get the template string.
Definition: clutchlog.h:969
clutchlog::_in_file
std::regex _in_file
Current file location filter.
Definition: clutchlog.h:937
clutchlog::style
void style(level stage, FMT... styles)
Set the style (color and typo) of the given log level.
Definition: clutchlog.h:1094
clutchlog::fmt::bg_256::bg_256
bg_256(const bg &)
Conversion constructor from 16-colors mode.
Definition: clutchlog.h:568
clutchlog::fmt::fg_16M::fg_16M
fg_16M()
Empty constructor: no color.
Definition: clutchlog.h:636
clutchlog::level_of
level level_of(const std::string name)
Return the log level tag corresponding to the given pre-configured name.
Definition: clutchlog.h:1060
clutchlog::_level_word
const std::map< level, std::string > _level_word
Dictionary of level identifier to their string representation.
Definition: clutchlog.h:905
clutchlog::fmt::operator()
std::string operator()(const std::string &msg) const
Format the given string with the currently encoded format.
Definition: clutchlog.h:795
clutchlog::fmt::color_16M::color_16M
color_16M(ground t, short r, short g, short b)
Numeric triplet constructor.
Definition: clutchlog.h:591
clutchlog::fmt::color_16M::print_on
std::ostream & print_on(std::ostream &os) const
Print the color RGB triplet on the given stream.
Definition: clutchlog.h:626
clutchlog::fmt::bg
bg
Background color codes.
Definition: clutchlog.h:411
CLUTCHLOG_DEFAULT_FORMAT
#define CLUTCHLOG_DEFAULT_FORMAT
Compile-time default format of the messages (debug mode: with absolute location).
Definition: clutchlog.h:198
clutchlog::_in_line
std::regex _in_line
Current line location filter.
Definition: clutchlog.h:941
clutchlog::fmt::fg_256::fg_256
fg_256(const short f)
Constructor.
Definition: clutchlog.h:546
clutchlog::fmt::ansi::colors_256
@ colors_256
256 colors mode.
clutchlog::fmt::mode
enum clutchlog::fmt::ansi mode
Current ANSI color mode.
clutchlog::replace
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:1248
clutchlog::format_comment
std::string format_comment() const
Get the template string for dumps.
Definition: clutchlog.h:974
clutchlog::fmt::fg_256::fg_256
fg_256()
Empty constructor: no color.
Definition: clutchlog.h:541
clutchlog::fmt::ansi::colors_16M
@ colors_16M
16 millions ("true") colors mode.
clutchlog::_format_dump
std::string _format_dump
Current format of the file output.
Definition: clutchlog.h:915
CLUTCHDUMP_DEFAULT_SEP
#define CLUTCHDUMP_DEFAULT_SEP
Compile-time default item separator for dump.
Definition: clutchlog.h:239
clutchlog::fmt::bg_256::bg_256
bg_256(const short b)
Constructor.
Definition: clutchlog.h:563
clutchlog::fmt::color::ground
ground
Codes for representing foreground or background.
Definition: clutchlog.h:474
clutchlog::scope_t
Structure holding a location matching.
Definition: clutchlog.h:1108
CLUTCHLOG_DEFAULT_HFILL_MARK
#define CLUTCHLOG_DEFAULT_HFILL_MARK
Character used as a filling for right-align the right part of messages with "{hfill}".
Definition: clutchlog.h:260
clutchlog::fmt::print_on
std::ostream & print_on(std::ostream &os) const
Print the currently encoded format escape code on the given output stream.
Definition: clutchlog.h:746
clutchlog::fmt::color_256::is_set
bool is_set() const
Returns true if the underying representation encodes an existing color.
Definition: clutchlog.h:528
clutchlog::_out
std::ostream * _out
Standard output.
Definition: clutchlog.h:927
clutchlog::out
std::ostream & out()
Get the output stream on which to print.
Definition: clutchlog.h:979
clutchlog::threshold
void threshold(const std::string &l)
Set the log level (below which logs are not printed) with a string.
Definition: clutchlog.h:1050
clutchlog::fmt::color::print_on
virtual std::ostream & print_on(std::ostream &os) const =0
Should print the underlying representation on the given stream.
clutchlog::levels
const std::map< std::string, level > & levels() const
Get the map of available log levels string representations toward their identifier....
Definition: clutchlog.h:1054
clutchlog::_strip_calls
size_t _strip_calls
Current number of call stack levels to remove from depth display.
Definition: clutchlog.h:903
clutchlog::scope_t::stage
level stage
Current log level.
Definition: clutchlog.h:1112
clutchlog::scope_t::there
bool there
Location is compatible.
Definition: clutchlog.h:1118
clutchlog::fmt::fg_16M::fg_16M
fg_16M(const std::string &srgb)
Hex triplet string constructor.
Definition: clutchlog.h:654
CLUTCHLOG_STRIP_CALLS
#define CLUTCHLOG_STRIP_CALLS
Compile-time number of call stack levels to remove from depth display by default.
Definition: clutchlog.h:253
clutchlog::fmt::operator<<
friend std::ostream & operator<<(std::ostream &os, const fmt &fmt)
Output stream overload.
Definition: clutchlog.h:780
clutchlog::fmt::bg_16M::bg_16M
bg_16M(const bg &)
Conversion constructor from 16-colors mode.
Definition: clutchlog.h:689
clutchlog::fmt::bg_16M::bg_16M
bg_16M()
Empty constructor: no color.
Definition: clutchlog.h:666
clutchlog::_word_level
std::map< std::string, level > _word_level
Dictionary of level string to their identifier.
Definition: clutchlog.h:907
clutchlog::_stage
level _stage
Current log level.
Definition: clutchlog.h:935
clutchlog::style
fmt style(level stage) const
Get the configured fmt instance of the given log level.
Definition: clutchlog.h:1098
clutchlog::fmt::typo
typo
Typographic style codes.
Definition: clutchlog.h:379
clutchlog::location
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:1078
CLUTCHLOG_HAVE_UNIX_SYSINFO
#define CLUTCHLOG_HAVE_UNIX_SYSINFO
True if POSIX headers necessary for stack depth management are available.
Definition: clutchlog.h:33
clutchlog::_filehash_fmts
std::vector< fmt > _filehash_fmts
List of candidate format objects for value-dependant file name styling.
Definition: clutchlog.h:944
clutchlog
The single class which holds everything.
Definition: clutchlog.h:177
clutchlog::fmt::ansi
ansi
ANSI code configuring the available number of colors.
Definition: clutchlog.h:369
clutchlog::fmt::fg_16M::fg_16M
fg_16M(short r, short g, short b)
Numeric triplet constructor.
Definition: clutchlog.h:646
clutchlog::fmt::fg
fg
Foreground color codes.
Definition: clutchlog.h:390
clutchlog::fmt::color_16M::red
short red
The encoded RGB indices.
Definition: clutchlog.h:577
CLUTCHDUMP_DEFAULT_FORMAT
#define CLUTCHDUMP_DEFAULT_FORMAT
Compile-time default format of the comment line in file dump.
Definition: clutchlog.h:221
clutchlog::fmt::color::color
color(ansi a, ground g)
Constructor.
Definition: clutchlog.h:484
clutchlog::fmt::back_256
clutchlog::fmt::bg_256 back_256
Current background in 256-colors mode.