clutchlog  0.15
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 CLUTCHLOGD( LEVEL, WHAT, DEPTH_DELTA ) 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, DEPTH_DELTA); \
85  } while(0)
86 #else // not Debug build.
87  #define CLUTCHLOGD( LEVEL, WHAT, DEPTH_DELTA ) 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, DEPTH_DELTA); \
92  } \
93  } while(0)
94 #endif // NDEBUG
95 
97 #ifndef NDEBUG
98  #define CLUTCHLOG( LEVEL, WHAT ) \
99  CLUTCHLOGD(LEVEL, WHAT, 0)
100 #else // not Debug build.
101  #define CLUTCHLOG( LEVEL, WHAT ) \
102  CLUTCHLOGD(LEVEL, WHAT, 0)
103 #endif // NDEBUG
104 
106 #ifndef NDEBUG
107  #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do { \
108  auto& clutchlog__logger = clutchlog::logger(); \
109  clutchlog__logger.dump(clutchlog::level::LEVEL, std::begin(CONTAINER), std::end(CONTAINER), \
110  CLUTCHLOC, FILENAME, CLUTCHDUMP_DEFAULT_SEP); \
111  } while(0)
112 #else // not Debug build.
113  #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do { \
114  if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
115  auto& clutchlog__logger = clutchlog::logger(); \
116  clutchlog__logger.dump(clutchlog::level::LEVEL, std::begin(CONTAINER), std::end(CONTAINER), \
117  CLUTCHLOC, FILENAME, CLUTCHDUMP_DEFAULT_SEP); \
118  } \
119  } while(0)
120 #endif // NDEBUG
121 
123 #ifndef NDEBUG
124  #define CLUTCHFUNC( LEVEL, FUNC, ... ) do { \
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  } while(0)
131 #else // not Debug build.
132  #define CLUTCHFUNC( LEVEL, FUNC, ... ) do { \
133  if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
134  auto& clutchlog__logger = clutchlog::logger(); \
135  clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
136  if(clutchlog__scope.matches) { \
137  FUNC(__VA_ARGS__); \
138  } \
139  } \
140  } while(0)
141 #endif // NDEBUG
142 
144 #ifndef NDEBUG
145  #define CLUTCHCODE( LEVEL, ... ) do { \
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  __VA_ARGS__ \
150  } \
151  } while(0)
152 #else // not Debug build.
153  #define CLUTCHCODE( LEVEL, CODE ) do { \
154  if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \
155  auto& clutchlog__logger = clutchlog::logger(); \
156  clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
157  if(clutchlog__scope.matches) { \
158  CODE \
159  } \
160  } \
161  } while(0)
162 #endif // NDEBUG
163 
166 #else // not WITH_CLUTCHLOG
167  // Disabled macros can still be called in Release builds.
168  #define CLUTCHLOG( LEVEL, WHAT ) do {/*nothing*/} while(0)
169  #define CLUTCHLOGD( LEVEL, WHAT, DEPTH_DELTA ) do {/*nothing*/} while(0)
170  #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) do {/*nothing*/} while(0)
171  #define CLUTCHFUNC( LEVEL, FUNC, ... ) do {/*nothing*/} while(0)
172  #define CLUTCHCODE( LEVEL, CODE ) do {/*nothing*/} while(0)
173 #endif // WITH_CLUTCHLOG
174 
175 /**********************************************************************
176  * Implementation
177  **********************************************************************/
178 
179 #ifdef WITH_CLUTCHLOG
180 
188 {
189  protected:
190 
195  #ifndef NDEBUG
196  #ifndef CLUTCHLOG_DEFAULT_FORMAT
197  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1 // Enables: name, depth and depth_marks
199  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1 // Enables: hfill
200  #define CLUTCHLOG_DEFAULT_FORMAT "[{name}] {level_letter}:{depth_marks} {msg} {hfill} {func} @ {file}:{line}\n"
201  #else
202  #define CLUTCHLOG_DEFAULT_FORMAT "[{name}] {level_letter}:{depth_marks} {msg}\t\t\t\t\t{func} @ {file}:{line}\n"
203  #endif
204  #else
205  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
206  #define CLUTCHLOG_DEFAULT_FORMAT "{level_letter} {msg} {hfill} {func} @ {file}:{line}\n"
207  #else
208  #define CLUTCHLOG_DEFAULT_FORMAT "{level_letter} {msg}\t\t\t\t\t{func} @ {file}:{line}\n"
209  #endif
210  #endif
211  #endif
212  #else
213  #ifndef CLUTCHLOG_DEFAULT_FORMAT
214  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
216  #define CLUTCHLOG_DEFAULT_FORMAT "[{name}] {level_letter}:{depth_marks} {msg} {hfill} {func}\n"
217  #else
218  #define CLUTCHLOG_DEFAULT_FORMAT "{level_letter} {msg}\t\t\t\t\t{func}\n"
219  #endif
220  #endif
221  #endif
222  static inline std::string default_format = CLUTCHLOG_DEFAULT_FORMAT;
224 
225  #ifndef NDEBUG
226  #ifndef CLUTCHDUMP_DEFAULT_FORMAT
227  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
229  #define CLUTCHDUMP_DEFAULT_FORMAT "# [{name}] {level} in {func} (at depth {depth}) @ {file}:{line}"
230  #else
231  #define CLUTCHDUMP_DEFAULT_FORMAT "# {level} in {func} @ {file}:{line}"
232  #endif
233  #endif // CLUTCHDUMP_DEFAULT_FORMAT
234  #else
235  #ifndef CLUTCHDUMP_DEFAULT_FORMAT
236  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
238  #define CLUTCHDUMP_DEFAULT_FORMAT "# [{name}] {level} in {func} (at depth {depth})"
239  #else
240  #define CLUTCHDUMP_DEFAULT_FORMAT "# {level} in {func}"
241  #endif
242  #endif // CLUTCHDUMP_DEFAULT_FORMAT
243  #endif
244  static inline std::string dump_default_format = CLUTCHDUMP_DEFAULT_FORMAT;
246 
247  #ifndef CLUTCHDUMP_DEFAULT_SEP
248  #define CLUTCHDUMP_DEFAULT_SEP "\n"
250  #endif // CLUTCHDUMP_DEFAULT_SEP
251  static inline std::string dump_default_sep = CLUTCHDUMP_DEFAULT_SEP;
253 
254  #ifndef CLUTCHLOG_DEFAULT_DEPTH_MARK
255  #define CLUTCHLOG_DEFAULT_DEPTH_MARK ">"
257  #endif // CLUTCHLOG_DEFAULT_DEPTH_MARK
258  static inline std::string default_depth_mark = CLUTCHLOG_DEFAULT_DEPTH_MARK;
260 
261  #ifndef CLUTCHLOG_STRIP_CALLS
262  #define CLUTCHLOG_STRIP_CALLS 5
264  #endif // CLUTCHLOG_STRIP_CALLS
265  static inline unsigned int default_strip_calls = CLUTCHLOG_STRIP_CALLS;
267 
268  #ifndef CLUTCHLOG_DEFAULT_HFILL_MARK
269  #define CLUTCHLOG_DEFAULT_HFILL_MARK '.'
271  #endif // CLUTCHLOG_DEFAULT_HFILL_MARK
274 
275 
276  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
277  #ifndef CLUTCHLOG_DEFAULT_HFILL_MAX
278  #define CLUTCHLOG_DEFAULT_HFILL_MAX 300
279  #endif
280  #ifndef CLUTCHLOG_DEFAULT_HFILL_MIN
281  #define CLUTCHLOG_DEFAULT_HFILL_MIN 150
282  #endif
283  #endif
284  static inline size_t default_hfill_max = CLUTCHLOG_DEFAULT_HFILL_MAX;
287  static inline size_t default_hfill_min = CLUTCHLOG_DEFAULT_HFILL_MIN;
288 
289  // NOTE: there is no CLUTCHLOG_HFILL_STYLE for defaulting,
290  // but you can still set `hfill_style(...)` on the logger singleton.
291  /* @} DefaultConfig */
292  /* @} */
293 
294 
295  public:
296 
306  static clutchlog& logger()
307  {
308  static clutchlog instance;
309  return instance;
310  }
311 
313  enum level {critical=0, error=1, warning=2, progress=3, note=4, info=5, debug=6, xdebug=7};
314 
376  class fmt {
377  public:
379  enum class ansi {
381  colors_16 = -1, // Not supposed to be casted.
383  colors_256 = 5, // Casted as short in color::operator<<.
385  colors_16M = 2 // Casted as short in color::operator<<
386  } mode;
387 
389  enum class typo {
390  reset = 0,
391  bold = 1,
392  underline = 4,
393  inverse = 7,
394  none = -1
395  } style;
396 
399  enum class fg {
401  black = 30,
402  red = 31,
403  green = 32,
404  yellow = 33,
405  blue = 34,
406  magenta = 35,
407  cyan = 36,
408  white = 37,
409  bright_black = 90,
410  bright_red = 91,
411  bright_green = 92,
412  bright_yellow = 93,
413  bright_blue = 94,
414  bright_magenta = 95,
415  bright_cyan = 96,
416  bright_white = 97,
417  none = -1
418  } fore;
419 
421  enum class bg {
422  black = 40,
423  red = 41,
424  green = 42,
425  yellow = 43,
426  blue = 44,
427  magenta = 45,
428  cyan = 46,
429  white = 47,
430  bright_black = 100,
431  bright_red = 101,
432  bright_green = 102,
433  bright_yellow = 103,
434  bright_blue = 104,
435  bright_magenta = 105,
436  bright_cyan = 106,
437  bright_white = 107,
438  none = -1
439  } back;
440 
441  protected:
443  friend std::ostream& operator<<(std::ostream& os, const std::tuple<fg,bg,typo>& fbs)
444  {
445  auto [f,b,s] = fbs;
446  std::vector<short> codes; codes.reserve(3);
447  if(f != fg::none) { codes.push_back(static_cast<short>(f));}
448  if(b != bg::none) { codes.push_back(static_cast<short>(b));}
449  if(s != typo::none) { codes.push_back(static_cast<short>(s));}
450  if(codes.size() == 0) {
451  return os;
452 
453  } else {
454  os << "\033[";
455  os << codes[0];
456  for(size_t i=1; i < codes.size(); ++i) {
457  os << ";" << codes[i];
458  }
459  os << "m";
460  }
461  return os;
462  }
463 
465  friend std::ostream& operator<<(std::ostream& os, const typo& s)
466  {
467  if(s != typo::none) {
468  os << "\033[" << static_cast<short>(s) << "m";
469  }
470  return os;
471  }
472 
475  protected:
480  struct color {
481  ansi mode; // Not const to allow for the implicit copy assignemnt operator.
482 
484  enum class ground { // idem.
485  fore = 38,
486  back = 48
487  } type;
488 
494  color(ansi a, ground g) : mode(a), type(g) {}
495 
497  virtual bool is_set() const = 0;
498 
500  virtual std::ostream& print_on( std::ostream& os) const = 0;
501 
503  friend std::ostream& operator<<(std::ostream& os, const color& c)
504  {
505  if(c.is_set()) {
506  os << "\033[" << static_cast<short>(c.type) << ";" << static_cast<short>(c.mode) << ";";
507  c.print_on(os);
508  os << "m";
509  }
510  return os;
511  }
512  };
513 
514  // There is no color_16 because it would be the same as color_256, only with different indices,
515  // hence making it more complicated for the user to select the right constructor.
516  // Here, we just use enum for 16 colors, and indices for 256 colors.
517 
519  struct color_256 : public color {
523  short index;
524 
528  color_256(ground t) : color(ansi::colors_256, t), index(-1) {}
529 
535  color_256(ground t, const short i) : color(ansi::colors_256, t), index(i) {assert(-1 <= i and i <= 255);}
536 
538  bool is_set() const {return index > -1;}
539 
541  std::ostream& print_on( std::ostream& os) const
542  {
543  os << index;
544  return os;
545  }
546  };
547 
549  struct fg_256 : public color_256 {
551  fg_256() : color_256(ground::fore) {}
552 
556  fg_256(const short f) : color_256(ground::fore, f) {}
557 
561  fg_256(const fg&) : color_256(ground::fore, -1) {}
562 
563  } fore_256;
564 
566  struct bg_256 : public color_256 {
568  bg_256() : color_256(ground::back) {}
569 
573  bg_256(const short b) : color_256(ground::back, b) {}
574 
578  bg_256(const bg&) : color_256(ground::back, -1) {}
579 
580  } back_256;
581 
583  struct color_16M : public color {
587  short red, green, blue;
588 
592  color_16M(ground t) : color(ansi::colors_16M, t), red(-1), green(-1), blue(-1) {}
593 
601  color_16M(ground t, short r, short g, short b)
602  : color(ansi::colors_16M, t), red(r), green(g), blue(b) {}
603 
611  color_16M(ground t, const std::string& srgb) : color(ansi::colors_16M, t)
612  {
613  assert(srgb.size() == 7);
614  if(srgb.size() != 7) {
615  red = -1;
616  green = -1;
617  blue = -1;
618  } else {
619  char i = 0;
620  if(srgb.at(0) == '#') {
621  i = 1;
622  }
623  std::istringstream(srgb.substr(0+i,2)) >> std::hex >> red;
624  std::istringstream(srgb.substr(2+i,2)) >> std::hex >> green;
625  std::istringstream(srgb.substr(4+i,2)) >> std::hex >> blue;
626  }
627  assert(-1 <= red and red <= 255);
628  assert(-1 <= green and green <= 255);
629  assert(-1 <= blue and blue <= 255);
630  }
631 
633  bool is_set() const {return red > -1 and green > -1 and blue > -1;}
634 
636  std::ostream& print_on( std::ostream& os) const
637  {
638  os << red << ";" << green << ";" << blue;
639  return os;
640  }
641  };
642 
644  struct fg_16M : public color_16M {
646  fg_16M() : color_16M(ground::fore) {}
647 
656  fg_16M(short r, short g, short b) : color_16M(ground::fore, r,g,b) {}
657 
664  fg_16M(const std::string& srgb) : color_16M(ground::fore, srgb) {}
665 
669  fg_16M(const fg&) : color_16M(ground::fore, -1,-1,-1) {}
670 
671  } fore_16M;
672 
674  struct bg_16M : public color_16M {
676  bg_16M() : color_16M(ground::back) {}
677 
686  bg_16M(short r, short g, short b) : color_16M(ground::back, r,g,b) {}
687 
694  bg_16M(const std::string& srgb) : color_16M(ground::back, srgb) {}
695 
699  bg_16M(const bg&) : color_16M(ground::back, -1,-1,-1) {}
700 
701  } back_16M;
702 
705  public:
707  fmt() : mode(ansi::colors_16), style(typo::none), fore(fg::none), back(bg::none) {}
708 
711  fmt( fg f, bg b = bg::none, typo s = typo::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
712  fmt( fg f, typo s , bg b = bg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
713  fmt( bg b, fg f = fg::none, typo s = typo::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
714  fmt( bg b, typo s , fg f = fg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
715  fmt(typo s, fg f = fg::none, bg b = bg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
716  fmt(typo s, bg b , fg f = fg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
721  fmt(fg_256 f, bg_256 b, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(f), back_256(b) {}
722  fmt(fg_256 f, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(f), back_256(bg::none) {}
723  fmt(fg, bg_256 b, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(fg::none), back_256(b) {}
728  fmt(const short fr, const short fg, const short fb,
729  const short gr, const short gg, const short gb,
730  typo s = typo::none)
731  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(gr,gg,gb) {}
732  fmt(fg,
733  const short gr, const short gg, const short gb,
734  typo s = typo::none)
735  : mode(ansi::colors_16M), style(s), fore_16M(fg::none), back_16M(gr,gg,gb) {}
736  fmt(const short fr, const short fg, const short fb,
737  bg, typo s = typo::none)
738  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(bg::none) {}
739  fmt(const short fr, const short fg, const short fb,
740  typo s = typo::none)
741  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(bg::none) {}
742 
743  fmt(const std::string& f, const std::string& b, typo s = typo::none)
744  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(b) {}
745  fmt(fg, const std::string& b, typo s = typo::none)
746  : mode(ansi::colors_16M), style(s), fore_16M(fg::none), back_16M(b) {}
747  fmt(const std::string& f, bg, typo s = typo::none)
748  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(bg::none) {}
749  fmt(const std::string& f, typo s = typo::none)
750  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(bg::none) {}
753  protected:
754 
756  std::ostream& print_on( std::ostream& os) const
757  {
758  if(mode == ansi::colors_16) {
759  // Print all in a single escape.
760  os << std::make_tuple(fore,back,style);
761 
762  } else {
763  // 256 or 16M: always print separated escapes for foreground/background.
764  if(mode == ansi::colors_256) {
765  os << fore_256;
766  os << back_256;
767 
768  } else if(mode == ansi::colors_16M) {
769  os << fore_16M;
770  os << back_16M;
771  }
772  // In any case, print the style separately.
773  os << style;
774  }
775  return os;
776  }
777 
778  public:
790  friend std::ostream& operator<<(std::ostream& os, const fmt& fmt)
791  {
792  return fmt.print_on(os);
793  }
794 
805  std::string operator()( const std::string& msg ) const
806  {
807  std::ostringstream os;
808  this->print_on(os);
809  fmt reset(fmt::typo::reset);
810  os << msg;
811  reset.print_on(os);
812  return os.str();
813  }
814 
817  std::string str() const
818  {
819  std::ostringstream os;
820  this->print_on(os);
821  return os.str();
822  }
823 
824  static fmt hash( const std::string& str, const std::vector<fmt> domain = {})
825  {
826  size_t h = std::hash<std::string>{}(str);
827  if(domain.size() == 0) {
828  return fmt(static_cast<short>(h % 256));
829  } else {
830  return fmt(domain[h % domain.size()]);
831  }
832  }
833  }; // fmt class
834 
840  public:
841  clutchlog(clutchlog const&) = delete;
842  void operator=(clutchlog const&) = delete;
843 
844  private:
845  clutchlog() :
846  // system, main, log
848  _level_word({
849  {level::critical,"Critical"},
850  {level::error ,"Error"},
851  {level::warning ,"Warning"},
852  {level::progress,"Progress"},
853  {level::note ,"Note"},
854  {level::info ,"Info"},
855  {level::debug ,"Debug"},
856  {level::xdebug ,"XDebug"}
857  }),
858  _level_short({
859  {level::critical, "Crit"},
860  {level::error , "Erro"},
861  {level::warning , "Warn"},
862  {level::progress, "Prog"},
863  {level::note , "Note"},
864  {level::info , "Info"},
865  {level::debug , "Dbug"},
866  {level::xdebug , "XDbg"}
867  }),
868  _level_fmt({
869  {level::critical,fmt(fmt::fg::red, fmt::typo::underline)},
870  {level::error ,fmt(fmt::fg::red, fmt::typo::bold)},
871  {level::warning ,fmt(fmt::fg::magenta, fmt::typo::bold)},
872  {level::progress,fmt()},
873  {level::note ,fmt()},
874  {level::info ,fmt()},
875  {level::debug ,fmt()},
876  {level::xdebug ,fmt()}
877  }),
880  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
881  _hfill_char(clutchlog::default_hfill_char),
882  _hfill_fmt(fmt::fg::none),
883  _hfill_max(clutchlog::default_hfill_max),
884  _hfill_min(clutchlog::default_hfill_min),
885  #endif
886  _out(&std::clog),
887  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
888  _depth(std::numeric_limits<size_t>::max() - _strip_calls),
889  _depth_mark(clutchlog::default_depth_mark),
890  #endif
891  _stage(level::error),
892  _in_file(".*"),
893  _in_func(".*"),
894  _in_line(".*")
895  // Empty vectors by default:
896  // _filehash_fmts
897  // _funchash_fmts
898  // _depth_fmts
899  {
900  // Reverse the level->word map into a word->level map.
901  for(auto& lw : _level_word) {
902  _word_level[lw.second] = lw.first;
903  }
904 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
905  struct winsize w;
906  ioctl(STDERR_FILENO, TIOCGWINSZ, &w);
907  _nb_columns = std::max(std::min((size_t)w.ws_col, default_hfill_max), default_hfill_min);
908 #endif
909  }
910 
911  protected:
913  size_t _strip_calls;
915  const std::map<level,std::string> _level_word;
917  std::map<std::string,level> _word_level;
919  std::map<level,std::string> _level_short;
921  std::map<level,fmt> _level_fmt;
923  std::string _format_log;
925  std::string _format_dump;
926  #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
927 
928  char _hfill_char;
930  fmt _hfill_fmt;
932  size_t _hfill_max;
934  size_t _hfill_min;
935  #endif
936 
937  std::ostream* _out;
938  #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
939 
940  size_t _depth;
942  std::string _depth_mark;
943  #endif
944 
947  std::regex _in_file;
949  std::regex _in_func;
951  std::regex _in_line;
952 
954  std::vector<fmt> _filehash_fmts;
956  std::vector<fmt> _funchash_fmts;
957 
958 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
959 
960  static const size_t _max_buffer = 4096;
962  std::vector<fmt> _depth_fmts;
963 #endif
964 
965 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
966 
967  size_t _nb_columns;
968 #endif
969 
971  public:
972 
976  void format(const std::string& format) {_format_log = format;}
979  std::string format() const {return _format_log;}
980 
982  void format_comment(const std::string& format) {_format_dump = format;}
984  std::string format_comment() const {return _format_dump;}
985 
987  void out(std::ostream& out) {_out = &out;}
989  std::ostream& out() {return *_out;}
990 
991 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
992  void depth(size_t d) {_depth = d;}
995  size_t depth() const {return _depth;}
996 
998  void depth_mark(const std::string mark) {_depth_mark = mark;}
1000  std::string depth_mark() const {return _depth_mark;}
1001 
1003  void strip_calls(const size_t n) {_strip_calls = n;}
1005  size_t strip_calls() const {return _strip_calls;}
1006 #endif
1007 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
1008  void hfill_mark(const char mark) {_hfill_char = mark;}
1011  char hfill_mark() const {return _hfill_char;}
1013  void hfill_style(fmt style) {_hfill_fmt = style;}
1018  template<class ... FMT>
1019  void hfill_style(FMT... styles) { this->hfill_style(fmt(styles...)); }
1021  fmt hfill_style() const {return _hfill_fmt;}
1023  void hfill_max(const size_t nmax) {_hfill_max = nmax;}
1025  size_t hfill_max() {return _hfill_max;}
1027  void hfill_min(const size_t nmin) {_hfill_min = nmin;}
1029  size_t hfill_min() {return _hfill_min;}
1030 #endif
1031 
1038  void filehash_styles(std::vector<fmt> styles) {_filehash_fmts = styles;}
1046  void funchash_styles(std::vector<fmt> styles) {_funchash_fmts = styles;}
1055  void depth_styles(std::vector<fmt> styles) {_depth_fmts = styles;}
1056 
1058  void threshold(level l) {_stage = l;}
1060  void threshold(const std::string& l) {_stage = this->level_of(l);}
1062  level threshold() const {return _stage;}
1064  const std::map<std::string,level>& levels() const { return _word_level;}
1065 
1070  level level_of(const std::string name)
1071  {
1072  const auto ilevel = _word_level.find(name);
1073  if( ilevel != std::end(_word_level)) {
1074  return ilevel->second;
1075  } else {
1076  throw std::out_of_range("'" + name + "' is not a valid log level name");
1077  }
1078  }
1079 
1081  void file(std::string file) {_in_file = file;}
1083  void func(std::string func) {_in_func = func;}
1085  void line(std::string line) {_in_line = line;}
1086 
1088  void location(
1089  const std::string& in_file,
1090  const std::string& in_function=".*",
1091  const std::string& in_line=".*"
1092  )
1093  {
1094  file(in_file);
1095  func(in_function);
1096  line(in_line);
1097  }
1098 
1103  template<class ... FMT>
1104  void style(level stage, FMT... styles) { this->style(stage,fmt(styles...)); }
1106  void style(level stage, fmt style) { _level_fmt.at(stage) = style; }
1108  fmt style(level stage) const { return _level_fmt.at(stage); }
1109 
1112  public:
1113 
1117  struct scope_t {
1120  bool matches;
1123 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1124 
1125  size_t depth;
1126 #endif
1127 
1128  bool there;
1131  matches(false),
1132  stage(level::xdebug),
1134  depth(0),
1135 #endif
1136  there(false)
1137  {}
1138  }; // scope_t
1139 
1140 
1143  const level& stage,
1144  const std::string& file,
1145  const std::string& func,
1146  const size_t line
1147  ) const
1148  {
1149  scope_t scope; // False scope by default.
1150 
1151  /***** Log level stage *****/
1152  // Test stage first, because it's fastest.
1153  scope.stage = stage;
1154  if(not (scope.stage <= _stage)) {
1155  // Bypass useless computations if no match
1156  // because of the stage.
1157  return scope;
1158  }
1159 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1160  /***** Stack depth *****/
1161  // Backtrace in second, quite fast.
1162  size_t stack_depth;
1163  void *buffer[_max_buffer];
1164  stack_depth = backtrace(buffer, _max_buffer);
1165  scope.depth = stack_depth;
1166  if(not (scope.depth <= _depth + _strip_calls)) {
1167  // Bypass if no match.
1168  return scope;
1169  }
1170 #endif
1171 
1172  /***** Location *****/
1173  // Location last, slowest.
1174  std::ostringstream sline; sline << line;
1175  scope.there =
1176  std::regex_search(file, _in_file)
1177  and std::regex_search(func, _in_func)
1178  and std::regex_search(sline.str(), _in_line);
1179 
1180  // No need to retest stage and depth, which are true here.
1181  scope.matches = scope.there;
1182 
1183  return scope;
1184  } // locate
1185 
1193  std::string replace(
1194  const std::string& form,
1195  const std::string& mark,
1196  const std::string& tag
1197  ) const
1198  {
1199  // Useless debug code, unless something fancy would be done with name tags.
1200  // std::regex re;
1201  // try {
1202  // re = std::regex(mark);
1203  //
1204  // } catch(const std::regex_error& e) {
1205  // std::cerr << "ERROR with a regular expression \"" << mark << "\": ";
1206  // switch(e.code()) {
1207  // case std::regex_constants::error_collate:
1208  // std::cerr << "the expression contains an invalid collating element name";
1209  // break;
1210  // case std::regex_constants::error_ctype:
1211  // std::cerr << "the expression contains an invalid character class name";
1212  // break;
1213  // case std::regex_constants::error_escape:
1214  // std::cerr << "the expression contains an invalid escaped character or a trailing escape";
1215  // break;
1216  // case std::regex_constants::error_backref:
1217  // std::cerr << "the expression contains an invalid back reference";
1218  // break;
1219  // case std::regex_constants::error_brack:
1220  // std::cerr << "the expression contains mismatched square brackets ('[' and ']')";
1221  // break;
1222  // case std::regex_constants::error_paren:
1223  // std::cerr << "the expression contains mismatched parentheses ('(' and ')')";
1224  // break;
1225  // case std::regex_constants::error_brace:
1226  // std::cerr << "the expression contains mismatched curly braces ('{' and '}')";
1227  // break;
1228  // case std::regex_constants::error_badbrace:
1229  // std::cerr << "the expression contains an invalid range in a {} expression";
1230  // break;
1231  // case std::regex_constants::error_range:
1232  // std::cerr << "the expression contains an invalid character range (e.g. [b-a])";
1233  // break;
1234  // case std::regex_constants::error_space:
1235  // std::cerr << "there was not enough memory to convert the expression into a finite state machine";
1236  // break;
1237  // case std::regex_constants::error_badrepeat:
1238  // std::cerr << "one of *?+{ was not preceded by a valid regular expression";
1239  // break;
1240  // case std::regex_constants::error_complexity:
1241  // std::cerr << "the complexity of an attempted match exceeded a predefined level";
1242  // break;
1243  // case std::regex_constants::error_stack:
1244  // std::cerr << "there was not enough memory to perform a match";
1245  // break;
1246  // default:
1247  // std::cerr << "unknown error";
1248  // }
1249  // std::cerr << std::endl;
1250  // throw;
1251  // } // catch
1252 
1253  const std::regex re(mark);
1254  return std::regex_replace(form, re, tag);
1255  }
1256 
1258  std::string replace(
1259  const std::string& form,
1260  const std::string& mark,
1261  const size_t tag
1262  ) const
1263  {
1264  std::ostringstream stag; stag << tag;
1265  return replace(form, mark, stag.str());
1266  }
1267 
1269  std::string format(
1270  std::string row,
1271  const std::string& what,
1273  const std::string& name,
1274 #endif
1275  const level& stage,
1276  const std::string& file,
1277  const std::string& func,
1278  const size_t line
1280  ,
1281  const size_t depth
1282 #endif
1283  ) const
1284  {
1285  row = replace(row, "\\{msg\\}", what);
1286  row = replace(row, "\\{file\\}", file);
1287  row = replace(row, "\\{func\\}", func);
1288  row = replace(row, "\\{line\\}", line);
1289 
1290  row = replace(row, "\\{level\\}", _level_word.at(stage));
1291  std::string letter(1, _level_word.at(stage).at(0)); // char -> string
1292  row = replace(row, "\\{level_letter\\}", letter);
1293 
1294  row = replace(row, "\\{level_short\\}", _level_short.at(stage));
1295 
1296 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1297  size_t actual_depth = depth - _strip_calls;
1298  row = replace(row, "\\{name\\}", name);
1299  row = replace(row, "\\{depth\\}", actual_depth);
1300 
1301  if(_depth_fmts.size() == 0) {
1302  row = replace(row, "\\{depth_fmt\\}", fmt(actual_depth % 256).str() );
1303 
1304  std::ostringstream chevrons;
1305  for(size_t i = 0; i < actual_depth; ++i) {
1306  chevrons << _depth_mark;
1307  }
1308  row = replace(row, "\\{depth_marks\\}", chevrons.str());
1309 
1310  } else {
1311  row = replace(row, "\\{depth_fmt\\}",
1312  _depth_fmts[std::min(actual_depth,_depth_fmts.size()-1)].str() );
1313 
1314  std::ostringstream chevrons;
1315  for(size_t i = 0; i < actual_depth; ++i) {
1316  chevrons << _depth_fmts[std::min(i+1,_depth_fmts.size()-1)].str()
1317  << _depth_mark;
1318  }
1319  row = replace(row, "\\{depth_marks\\}", chevrons.str());
1320  }
1321 #endif
1322  row = replace(row, "\\{level_fmt\\}", _level_fmt.at(stage).str());
1323  row = replace(row, "\\{filehash_fmt\\}", fmt::hash(file, _filehash_fmts).str() );
1324  row = replace(row, "\\{funchash_fmt\\}", fmt::hash(func, _funchash_fmts).str() );
1325 
1326 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL
1327  // hfill is replaced last to allow for correct line width estimation.
1328  const std::string raw_row = replace(row, "\\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]", "");
1329  const std::string hfill_tag = "{hfill}";
1330  const size_t hfill_pos = row.find(hfill_tag);
1331  const size_t raw_hfill_pos = raw_row.find(hfill_tag);
1332  const size_t nb_columns = std::max(std::min((size_t)_nb_columns, _hfill_max), _hfill_min);
1333  if(hfill_pos != std::string::npos) {
1334  assert(raw_hfill_pos != std::string::npos);
1335  if(nb_columns > 0) {
1336  const size_t left_len = raw_hfill_pos;
1337  const size_t right_len = raw_row.size() - raw_hfill_pos - hfill_tag.size();
1338  if(right_len+left_len > nb_columns) {
1339  // The right part would go over the terminal width: add a new row.
1340  if(right_len < nb_columns) {
1341  // There is room for the right part on a new line.
1342  const std::string hfill(std::max((size_t)0, nb_columns-right_len), _hfill_char);
1343  const std::string hfill_styled = _hfill_fmt(hfill);
1344  row = replace(row, "\\{hfill\\}", "\n"+hfill_styled);
1345  } else {
1346  // Right part still goes over columns: let it go.
1347  const std::string hfill(1, _hfill_char);
1348  const std::string hfill_styled = _hfill_fmt(hfill);
1349  row = replace(row, "\\{hfill\\}", "\n"+hfill_styled);
1350  }
1351  } else {
1352  // There is some space in between left and right parts.
1353  const std::string hfill(std::max((size_t)0, nb_columns - (right_len+left_len)), _hfill_char);
1354  const std::string hfill_styled = _hfill_fmt(hfill);
1355  row = replace(row, "\\{hfill\\}", hfill_styled);
1356  }
1357  } else {
1358  // We don't know the terminal width.
1359  const std::string hfill(1, _hfill_char);
1360  const std::string hfill_styled = _hfill_fmt(hfill);
1361  row = replace(row, "\\{hfill\\}", hfill_styled);
1362  }
1363  }
1364 #else
1365  // We cannot know the terminal width.
1366  const std::string hfill(1, _hfill_char);
1367  const std::string hfill_styled = _hfill_fmt(hfill);
1368  row = replace(row, "\\{hfill\\}", hfill_styled);
1369 #endif
1370  return _level_fmt.at(stage)(row);
1371  }
1372 
1374  void log(
1375  const level& stage,
1376  const std::string& what,
1377  const std::string& file, const std::string& func, const size_t line,
1378  const size_t depth_delta = 0
1379  ) const
1380  {
1381  scope_t scope = locate(stage, file, func, line);
1382 
1383  if(scope.matches) {
1384 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1385  *_out << format(_format_log, what, basename(getenv("_")),
1386  stage, file, func,
1387  line, scope.depth + depth_delta );
1388 #else
1389  *_out << format(_format_log, what,
1390  stage, file, func,
1391  line );
1392 #endif
1393  _out->flush();
1394  } // if scopes.matches
1395  }
1396 
1398  template<class In>
1399  void dump(
1400  const level& stage,
1401  const In container_begin, const In container_end,
1402  const std::string& file, const std::string& func, const size_t line,
1403  const std::string& filename_template = "dump_{n}.dat",
1404  const std::string sep = dump_default_sep
1405  ) const
1406  {
1407  scope_t scope = locate(stage, file, func, line);
1408 
1409  if(scope.matches) {
1410  const std::string tag = "\\{n\\}";
1411  const std::regex re(tag);
1412  std::string outfile = "";
1413 
1414  // If the file name template has the {n} tag.
1415  if(std::regex_search(filename_template, re)) {
1416  // Increment n until a free one is found.
1417  size_t n = 0;
1418  do {
1419  outfile = replace(filename_template, tag, n);
1420  n++;
1421  } while( fs::exists( outfile ) );
1422 
1423  } else {
1424  // Use the parameter as is.
1425  outfile = filename_template;
1426  }
1427 
1428  std::ofstream fd(outfile);
1429 
1430  if(_format_dump.size() > 0) {
1431 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1432  fd << format(_format_dump, "", basename(getenv("_")),
1433  stage, file, func,
1434  line, scope.depth );
1435 #else
1436  fd << format(_format_dump, "",
1437  stage, file, func,
1438  line );
1439 #endif
1440  fd << sep; // sep after comment line.
1441  }
1442 
1443  std::copy(container_begin, container_end,
1444  std::ostream_iterator<typename In::value_type>(fd, sep.c_str()));
1445 
1446  fd.close();
1447  } // if scopes.matches
1448  }
1449 
1451 };
1452 
1455 #else // not WITH_CLUTCHLOG
1456 
1457 
1458 /**********************************************************************
1459  * Fake implementation
1460  **********************************************************************/
1461 
1462 // Equivalent class with empty methods, will be optimized out
1463 // while allowing to actually have calls implemented without WITH_CLUTCHLOG guards.
1464 #pragma GCC diagnostic push
1465 #pragma GCC diagnostic ignored "-Wreturn-type"
1466 class clutchlog
1467 {
1468  public:
1469  static clutchlog& logger() {}
1470  enum level {critical=0, error=1, warning=2, progress=3, note=4, info=5, debug=6, xdebug=7};
1471  class fmt {
1472  public:
1473  enum class ansi { colors_16, colors_256, colors_16M} mode;
1474  enum class typo { reset, bold, underline, inverse, none} style;
1475  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;
1476  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;
1477  protected:
1478  friend std::ostream& operator<<(std::ostream&, const std::tuple<fg,bg,typo>&) {}
1479  friend std::ostream& operator<<(std::ostream&, const typo&) {}
1480  protected:
1481  struct color {
1482  ansi mode;
1483  enum class ground { fore, back } type;
1484  color(ansi a, ground g) : mode(a), type(g) {}
1485  virtual bool is_set() const = 0;
1486  virtual std::ostream& print_on( std::ostream&) const = 0;
1487  friend std::ostream& operator<<(std::ostream&, const color&) {}
1488  };
1489  struct color_256 : public color {
1490  short index;
1491  color_256(ground t) : color(ansi::colors_256, t), index(-1) {}
1492  color_256(ground t, const short i) : color(ansi::colors_256, t), index(i) {}
1493  bool is_set() const {}
1494  std::ostream& print_on( std::ostream&) const {}
1495  };
1496  struct fg_256 : public color_256 {
1497  fg_256() : color_256(ground::fore) {}
1498  fg_256(const short f) : color_256(ground::fore, f) {}
1499  fg_256(const fg&) : color_256(ground::fore, -1) {}
1500  } fore_256;
1501  struct bg_256 : public color_256 {
1502  bg_256() : color_256(ground::back) {}
1503  bg_256(const short b) : color_256(ground::back, b) {}
1504  bg_256(const bg&) : color_256(ground::back, -1) {}
1505  } back_256;
1506  struct color_16M : public color {
1507  short red, green, blue;
1508  color_16M(ground t) : color(ansi::colors_16M, t), red(-1), green(-1), blue(-1) {}
1509  color_16M(ground t, short r, short g, short b) : color(ansi::colors_16M, t), red(r), green(g), blue(b) {}
1510  color_16M(ground t, const std::string&) : color(ansi::colors_16M, t) {}
1511  bool is_set() const {return red > -1 and green > -1 and blue > -1;}
1512  std::ostream& print_on( std::ostream&) const {}
1513  };
1514  struct fg_16M : public color_16M {
1515  fg_16M() : color_16M(ground::fore) {}
1516  fg_16M(short r, short g, short b) : color_16M(ground::fore, r,g,b) {}
1517  fg_16M(const std::string& srgb) : color_16M(ground::fore, srgb) {}
1518  fg_16M(const fg&) : color_16M(ground::fore, -1,-1,-1) {}
1519  } fore_16M;
1520  struct bg_16M : public color_16M {
1521  bg_16M() : color_16M(ground::back) {}
1522  bg_16M(short r, short g, short b) : color_16M(ground::back, r,g,b) {}
1523  bg_16M(const std::string& srgb) : color_16M(ground::back, srgb) {}
1524  bg_16M(const bg&) : color_16M(ground::back, -1,-1,-1) {}
1525  } back_16M;
1526  public:
1527  fmt() : mode(ansi::colors_16), style(typo::none), fore(fg::none), back(bg::none) {}
1528  fmt( fg f, bg b = bg::none, typo s = typo::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1529  fmt( fg f, typo s , bg b = bg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1530  fmt( bg b, fg f = fg::none, typo s = typo::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1531  fmt( bg b, typo s , fg f = fg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1532  fmt(typo s, fg f = fg::none, bg b = bg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1533  fmt(typo s, bg b , fg f = fg::none) : mode(ansi::colors_16), style(s), fore(f), back(b) {}
1534  fmt(fg_256 f, bg_256 b, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(f), back_256(b) {}
1535  fmt(fg_256 f, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(f), back_256(bg::none) {}
1536  fmt(fg, bg_256 b, typo s = typo::none) : mode(ansi::colors_256), style(s), fore_256(fg::none), back_256(b) {}
1537  fmt(const short fr, const short fg, const short fb,
1538  const short gr, const short gg, const short gb,
1539  typo s = typo::none)
1540  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(gr,gg,gb) {}
1541  fmt(fg,
1542  const short gr, const short gg, const short gb,
1543  typo s = typo::none)
1544  : mode(ansi::colors_16M), style(s), fore_16M(fg::none), back_16M(gr,gg,gb) {}
1545  fmt(const short fr, const short fg, const short fb,
1546  bg, typo s = typo::none)
1547  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(bg::none) {}
1548  fmt(const short fr, const short fg, const short fb,
1549  typo s = typo::none)
1550  : mode(ansi::colors_16M), style(s), fore_16M(fr,fg,fb), back_16M(bg::none) {}
1551 
1552  fmt(const std::string& f, const std::string& b, typo s = typo::none)
1553  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(b) {}
1554  fmt(fg, const std::string& b, typo s = typo::none)
1555  : mode(ansi::colors_16M), style(s), fore_16M(fg::none), back_16M(b) {}
1556  fmt(const std::string& f, bg, typo s = typo::none)
1557  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(bg::none) {}
1558  fmt(const std::string& f, typo s = typo::none)
1559  : mode(ansi::colors_16M), style(s), fore_16M(f), back_16M(bg::none) {}
1560  protected:
1561  std::ostream& print_on( std::ostream&) const {}
1562  public:
1563  friend std::ostream& operator<<(std::ostream&, const fmt&) {}
1564  std::string operator()( const std::string&) const {}
1565  std::string str() const {}
1566  static fmt hash( const std::string&, const std::vector<fmt>) {}
1567  };
1568  public:
1569  clutchlog(clutchlog const&) = delete;
1570  void operator=(clutchlog const&) = delete;
1571  private:
1572  clutchlog() {}
1573  protected:
1574  struct scope_t {};
1575  scope_t locate(
1576  const level&,
1577  const std::string&,
1578  const std::string&,
1579  const size_t
1580  ) const
1581  {}
1582  public:
1583  void format(const std::string&) {}
1584  std::string format() const {}
1585 
1586  void format_comment(const std::string&) {}
1587  std::string format_comment() const {}
1588 
1589  void out(std::ostream&) {}
1590  std::ostream& out() {}
1591 
1592 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
1593  void depth(size_t) {}
1594  size_t depth() const {}
1595 
1596  void depth_mark(const std::string) {}
1597  std::string depth_mark() const {}
1598  void strip_calls(const size_t) {}
1599  size_t strip_calls() const {}
1600 #endif
1601 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
1602  void hfill_mark(const char) {}
1603  char hfill_mark() const {}
1604  void hfill_fmt(fmt) {}
1605  fmt hfill_fmt() const {}
1606  void hfill_min(const size_t) {}
1607  size_t hfill_min() {}
1608  void hfill_max(const size_t) {}
1609  size_t hfill_max() {}
1610 #endif
1611  void filehash_styles(std::vector<fmt> ) {}
1612  void funchash_styles(std::vector<fmt> ) {}
1613  void depth_styles(std::vector<fmt>) {}
1614 
1615  void threshold(level) {}
1616  void threshold(const std::string&) {}
1617  level threshold() const {}
1618  const std::map<std::string,level> levels() const {}
1619  level level_of(const std::string) {}
1620 
1621  void file(std::string) {}
1622  void func(std::string) {}
1623  void line(std::string) {}
1624 
1625 #pragma GCC diagnostic push
1626 #pragma GCC diagnostic ignored "-Wunused-parameter"
1627  void location(
1628  const std::string&,
1629  const std::string& in_function=".*",
1630  const std::string& in_line=".*"
1631  )
1632  {}
1633 #pragma GCC diagnostic pop
1634  template<class ... FMT>
1635  void style(level, FMT...) {}
1636  void style(level, fmt) {}
1637  fmt style(level) const {}
1638  public:
1639  std::string replace(
1640  const std::string&,
1641  const std::string&,
1642  const std::string&
1643  ) const
1644  {}
1645 
1646  std::string replace(
1647  const std::string&,
1648  const std::string&,
1649  const size_t
1650  ) const
1651  {}
1652 
1653  std::string format(
1654  std::string,
1655  const std::string&,
1657  const std::string&,
1658 #endif
1659  const level&,
1660  const std::string&,
1661  const std::string&,
1662  const size_t
1664  ,
1665  const size_t
1666 #endif
1667  ) const
1668  {}
1669 
1670  void log(
1671  const level&,
1672  const std::string&,
1673  const std::string&, const std::string&, size_t
1674  ) const
1675  {}
1676 
1677  template<class In>
1678  void dump(
1679  const level&,
1680  const In, const In,
1681  const std::string&, const std::string&, size_t,
1682  const std::string&,
1683  const std::string
1684  ) const
1685  {}
1686 };
1687 #pragma GCC diagnostic pop
1688 #endif // WITH_CLUTCHLOG
1689 
1690 #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:443
clutchlog::default_depth_mark
static std::string default_depth_mark
Default mark for stack depth.
Definition: clutchlog.h:259
clutchlog::_format_log
std::string _format_log
Current format of the standard output.
Definition: clutchlog.h:923
clutchlog::fmt::color_16M::color_16M
color_16M(ground t, const std::string &srgb)
Hex triplet string constructor.
Definition: clutchlog.h:611
clutchlog::dump
void dump(const level &stage, const In container_begin, const In container_end, const std::string &file, const std::string &func, const 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:1399
clutchlog::depth_styles
void depth_styles(std::vector< fmt > styles)
Set the styles for value-dependant depth formatting.
Definition: clutchlog.h:1055
clutchlog::_level_fmt
std::map< level, fmt > _level_fmt
Dictionary of level identifier to their format.
Definition: clutchlog.h:921
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:956
clutchlog::fmt::str
std::string str() const
Return the formatting code as a string.
Definition: clutchlog.h:817
clutchlog::fmt::fg_256
Foreground in 256-colors mode.
Definition: clutchlog.h:549
clutchlog::line
void line(std::string line)
Set the regular expression filtering the line location.
Definition: clutchlog.h:1085
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:503
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:465
clutchlog::dump_default_format
static std::string dump_default_format
Default format of the comment line in file dump.
Definition: clutchlog.h:245
clutchlog::out
void out(std::ostream &out)
Set the output stream on which to print.
Definition: clutchlog.h:987
clutchlog::fmt::color_16M::is_set
bool is_set() const
Returns true if the underying representation encodes an existing color.
Definition: clutchlog.h:633
clutchlog::fmt::fg_16M::fg_16M
fg_16M(const fg &)
Conversion constructor from 16-colors mode.
Definition: clutchlog.h:669
clutchlog::dump_default_sep
static std::string dump_default_sep
Default item separator for dump.
Definition: clutchlog.h:252
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:1269
clutchlog::fmt::bg_256
Background in 256-colors mode.
Definition: clutchlog.h:566
CLUTCHLOG_DEFAULT_DEPTH_MARK
#define CLUTCHLOG_DEFAULT_DEPTH_MARK
Compile-time default mark for stack depth.
Definition: clutchlog.h:256
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:266
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:287
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:1193
clutchlog::fmt::color_256::color_256
color_256(ground t)
Constructor.
Definition: clutchlog.h:528
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:480
clutchlog::fmt::ansi::colors_16
@ colors_16
16 colors mode.
clutchlog::fmt::bg_16M
background in 256-colors mode.
Definition: clutchlog.h:674
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:273
clutchlog::scope_t::matches
bool matches
Everything is compatible.
Definition: clutchlog.h:1120
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:583
clutchlog::fmt::fg_256::fg_256
fg_256(const fg &)
Conversion constructor from 16-colors mode.
Definition: clutchlog.h:561
clutchlog::format_comment
void format_comment(const std::string &format)
Set the template string for dumps.
Definition: clutchlog.h:982
clutchlog::fmt::bg_16M::bg_16M
bg_16M(const std::string &srgb)
Hex triplet string constructor.
Definition: clutchlog.h:694
clutchlog::file
void file(std::string file)
Set the regular expression filtering the file location.
Definition: clutchlog.h:1081
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:1142
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:519
clutchlog::fmt::fmt
fmt()
Empty constructor, only useful for a no-op formatter.
Definition: clutchlog.h:707
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:1106
clutchlog::threshold
void threshold(level l)
Set the log level (below which logs are not printed) with an identifier.
Definition: clutchlog.h:1058
clutchlog::threshold
level threshold() const
Get the log level below which logs are not printed.
Definition: clutchlog.h:1062
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:919
clutchlog::level
level
Available log levels.
Definition: clutchlog.h:313
clutchlog::fmt::color_256::color_256
color_256(ground t, const short i)
Constructor.
Definition: clutchlog.h:535
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:541
clutchlog::fmt::color_16M::color_16M
color_16M(ground t)
Constructor.
Definition: clutchlog.h:592
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:285
clutchlog::funchash_styles
void funchash_styles(std::vector< fmt > styles)
Set the candidate styles for value-dependant function name formatting.
Definition: clutchlog.h:1046
clutchlog::scope_t::scope_t
scope_t()
Constructor.
Definition: clutchlog.h:1130
clutchlog::_in_func
std::regex _in_func
Current function location filter.
Definition: clutchlog.h:949
clutchlog::default_format
static std::string default_format
Default format of the messages.
Definition: clutchlog.h:223
clutchlog::fmt::bg_16M::bg_16M
bg_16M(short r, short g, short b)
Numeric triplet constructor.
Definition: clutchlog.h:686
clutchlog::logger
static clutchlog & logger()
Get the logger instance.
Definition: clutchlog.h:306
clutchlog::fmt::fg_16M
Foreground in 256-colors mode.
Definition: clutchlog.h:644
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:1038
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:376
clutchlog::fmt::color_256::index
short index
The encoded color index in 4-bits ANSI.
Definition: clutchlog.h:523
clutchlog::fmt::bg_256::bg_256
bg_256()
Empty constructor: no color.
Definition: clutchlog.h:568
clutchlog::func
void func(std::string func)
Set the regular expression filtering the function location.
Definition: clutchlog.h:1083
clutchlog::format
std::string format() const
Get the template string.
Definition: clutchlog.h:979
clutchlog::_in_file
std::regex _in_file
Current file location filter.
Definition: clutchlog.h:947
clutchlog::style
void style(level stage, FMT... styles)
Set the style (color and typo) of the given log level.
Definition: clutchlog.h:1104
clutchlog::fmt::bg_256::bg_256
bg_256(const bg &)
Conversion constructor from 16-colors mode.
Definition: clutchlog.h:578
clutchlog::fmt::fg_16M::fg_16M
fg_16M()
Empty constructor: no color.
Definition: clutchlog.h:646
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:1070
clutchlog::_level_word
const std::map< level, std::string > _level_word
Dictionary of level identifier to their string representation.
Definition: clutchlog.h:915
clutchlog::fmt::operator()
std::string operator()(const std::string &msg) const
Format the given string with the currently encoded format.
Definition: clutchlog.h:805
clutchlog::fmt::color_16M::color_16M
color_16M(ground t, short r, short g, short b)
Numeric triplet constructor.
Definition: clutchlog.h:601
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:636
clutchlog::fmt::bg
bg
Background color codes.
Definition: clutchlog.h:421
CLUTCHLOG_DEFAULT_FORMAT
#define CLUTCHLOG_DEFAULT_FORMAT
Compile-time default format of the messages (debug mode: with absolute location).
Definition: clutchlog.h:208
clutchlog::_in_line
std::regex _in_line
Current line location filter.
Definition: clutchlog.h:951
clutchlog::fmt::fg_256::fg_256
fg_256(const short f)
Constructor.
Definition: clutchlog.h:556
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:1258
clutchlog::format_comment
std::string format_comment() const
Get the template string for dumps.
Definition: clutchlog.h:984
clutchlog::fmt::fg_256::fg_256
fg_256()
Empty constructor: no color.
Definition: clutchlog.h:551
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:925
CLUTCHDUMP_DEFAULT_SEP
#define CLUTCHDUMP_DEFAULT_SEP
Compile-time default item separator for dump.
Definition: clutchlog.h:249
clutchlog::fmt::bg_256::bg_256
bg_256(const short b)
Constructor.
Definition: clutchlog.h:573
clutchlog::fmt::color::ground
ground
Codes for representing foreground or background.
Definition: clutchlog.h:484
clutchlog::scope_t
Structure holding a location matching.
Definition: clutchlog.h:1118
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:270
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:756
clutchlog::fmt::color_256::is_set
bool is_set() const
Returns true if the underying representation encodes an existing color.
Definition: clutchlog.h:538
clutchlog::_out
std::ostream * _out
Standard output.
Definition: clutchlog.h:937
clutchlog::out
std::ostream & out()
Get the output stream on which to print.
Definition: clutchlog.h:989
clutchlog::threshold
void threshold(const std::string &l)
Set the log level (below which logs are not printed) with a string.
Definition: clutchlog.h:1060
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:1064
clutchlog::_strip_calls
size_t _strip_calls
Current number of call stack levels to remove from depth display.
Definition: clutchlog.h:913
clutchlog::scope_t::stage
level stage
Current log level.
Definition: clutchlog.h:1122
clutchlog::scope_t::there
bool there
Location is compatible.
Definition: clutchlog.h:1128
clutchlog::fmt::fg_16M::fg_16M
fg_16M(const std::string &srgb)
Hex triplet string constructor.
Definition: clutchlog.h:664
CLUTCHLOG_STRIP_CALLS
#define CLUTCHLOG_STRIP_CALLS
Compile-time number of call stack levels to remove from depth display by default.
Definition: clutchlog.h:263
clutchlog::fmt::operator<<
friend std::ostream & operator<<(std::ostream &os, const fmt &fmt)
Output stream overload.
Definition: clutchlog.h:790
clutchlog::log
void log(const level &stage, const std::string &what, const std::string &file, const std::string &func, const size_t line, const size_t depth_delta=0) const
Print a log message IF the location matches the given one.
Definition: clutchlog.h:1374
clutchlog::fmt::bg_16M::bg_16M
bg_16M(const bg &)
Conversion constructor from 16-colors mode.
Definition: clutchlog.h:699
clutchlog::fmt::bg_16M::bg_16M
bg_16M()
Empty constructor: no color.
Definition: clutchlog.h:676
clutchlog::_word_level
std::map< std::string, level > _word_level
Dictionary of level string to their identifier.
Definition: clutchlog.h:917
clutchlog::_stage
level _stage
Current log level.
Definition: clutchlog.h:945
clutchlog::style
fmt style(level stage) const
Get the configured fmt instance of the given log level.
Definition: clutchlog.h:1108
clutchlog::fmt::typo
typo
Typographic style codes.
Definition: clutchlog.h:389
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:1088
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:954
clutchlog
The single class which holds everything.
Definition: clutchlog.h:187
clutchlog::fmt::ansi
ansi
ANSI code configuring the available number of colors.
Definition: clutchlog.h:379
clutchlog::fmt::fg_16M::fg_16M
fg_16M(short r, short g, short b)
Numeric triplet constructor.
Definition: clutchlog.h:656
clutchlog::fmt::fg
fg
Foreground color codes.
Definition: clutchlog.h:400
clutchlog::fmt::color_16M::red
short red
The encoded RGB indices.
Definition: clutchlog.h:587
CLUTCHDUMP_DEFAULT_FORMAT
#define CLUTCHDUMP_DEFAULT_FORMAT
Compile-time default format of the comment line in file dump.
Definition: clutchlog.h:231
clutchlog::fmt::color::color
color(ansi a, ground g)
Constructor.
Definition: clutchlog.h:494
clutchlog::fmt::back_256
clutchlog::fmt::bg_256 back_256
Current background in 256-colors mode.