clutchlog  0.9
Modules | Classes | Macros | Enumerations | Enumerator | Functions | Variables | Friends
High-level API macros
Collaboration diagram for High-level API macros:

Modules

 Formating tools
 

Classes

struct  clutchlog::scope_t
 Structure holding a location matching. More...
 

Macros

#define CLUTCHLOC   __FILE__, __FUNCTION__, __LINE__
 Handy shortcuts to location.
 
#define CLUTCHLOG(LEVEL, WHAT)
 Log a message at the given level. More...
 
#define CLUTCHDUMP(LEVEL, CONTAINER, FILENAME)
 Dump the given container. More...
 
#define CLUTCHFUNC(LEVEL, FUNC, ...)
 Call any function if the scope matches. More...
 
#define CLUTCHCODE(LEVEL, ...)
 Run any code if the scope matches. More...
 
#define CLUTCHLOG_DEFAULT_FORMAT   "{level_letter} {msg}\t\t\t\t\t{func} @ {file}:{line}\n"
 Compile-time default format of the messages (debug mode: with absolute location).
 
#define CLUTCHDUMP_DEFAULT_FORMAT   "# {level} in {func} @ {file}:{line}"
 Compile-time default format of the comment line in file dump.
 
#define CLUTCHDUMP_DEFAULT_SEP   "\n"
 Compile-time default item separator for dump.
 
#define CLUTCHLOG_DEFAULT_DEPTH_MARK   ">"
 Compile-time default mark for stack depth.
 
#define CLUTCHLOG_STRIP_CALLS   5
 Compile-time number of call stack levels to remove from depth display by default.
 
#define CLUTCHLOG_HFILL_MARK   '.'
 Character used as a filling for right-align the right part of messages with "{hfill}".
 

Enumerations

enum  clutchlog::fmt::fg {
  black = 30, red = 31, green = 32, yellow = 33,
  blue = 34, magenta = 35, cyan = 36, white = 37,
  none
}
 Foreground color codes.
 
enum  clutchlog::fmt::bg {
  black = 40, red = 41, green = 42, yellow = 43,
  blue = 44, magenta = 45, cyan = 46, white = 47,
  none
}
 Background color codes.
 
enum  clutchlog::fmt::typo {
  reset = 0, bold = 1, underline = 4, inverse = 7,
  none
}
 Typographic style codes.
 

Functions

 clutchlog::fmt::fmt ()
  Empty constructor, only useful for a no-op formatter.
 
std::ostream & clutchlog::fmt::print_on (std::ostream &os) const
 Print the currently encoded format escape code on the given output stream.
 
std::string clutchlog::fmt::operator() (const std::string &msg) const
 Format the given string with the currently encoded format. More...
 
 clutchlog::scope_t::scope_t ()
 Constructor.
 

Variables

static std::string clutchlog::default_format = CLUTCHLOG_DEFAULT_FORMAT
 Default format of the messages.
 
static std::string clutchlog::dump_default_format = CLUTCHDUMP_DEFAULT_FORMAT
 Default format of the comment line in file dump.
 
static std::string clutchlog::dump_default_sep = CLUTCHDUMP_DEFAULT_SEP
 Default item separator for dump.
 
static std::string clutchlog::default_depth_mark = CLUTCHLOG_DEFAULT_DEPTH_MARK
 Default mark for stack depth.
 
static unsigned int clutchlog::default_strip_calls = CLUTCHLOG_STRIP_CALLS
 Number of call stack levels to remove from depth display by default.
 
static char clutchlog::default_hfill_char = CLUTCHLOG_HFILL_MARK
 Default character used as a filling for right-align the right part of messages with "{hfill}".
 
static unsigned short clutchlog::default_hfill_max = CLUTCHLOG_HFILL_MAX
 Default maximum number of character used as a filling for right-align the right part of messages with "{hfill}".
 
enum clutchlog::fmt::fg clutchlog::fmt::fore
 Foreground color.
 
enum clutchlog::fmt::bg clutchlog::fmt::back
 Background color.
 
enum clutchlog::fmt::typo clutchlog::fmt::style
 Typographic style.
 
bool clutchlog::scope_t::matches
 Everything is compatible.
 
level clutchlog::scope_t::stage
 Current log level.
 
bool clutchlog::scope_t::there
 Location is compatible.
 

Friends

std::ostream & clutchlog::fmt::operator<< (std::ostream &os, const fmt &fmt)
 Output stream overload. More...
 

High-level API

enum  clutchlog::level {
  critical =0, error =1, warning =2, progress =3,
  note =4, info =5, debug =6, xdebug =7
}
 Available log levels.
 
static clutchlogclutchlog::logger ()
 Get the logger instance. More...
 

Internal details

size_t clutchlog::_strip_calls
 Current number of call stack levels to remove from depth display.
 
const std::map< level, std::string > clutchlog::_level_word
 Dictionary of level identifier to their string representation.
 
std::map< std::string, levelclutchlog::_word_level
 Dictionary of level string to their identifier.
 
std::map< level, fmtclutchlog::_level_fmt
 Dictionary of level identifier to their format.
 
std::string clutchlog::_format_log
 Current format of the standard output.
 
std::string clutchlog::_format_dump
 Current format of the file output.
 
std::ostream * clutchlog::_out
 Standard output.
 
level clutchlog::_stage
 Current log level.
 
std::regex clutchlog::_in_file
 Current file location filter.
 
std::regex clutchlog::_in_func
 Current function location filter.
 
std::regex clutchlog::_in_line
 Current line location filter.
 
 clutchlog::clutchlog (clutchlog const &)=delete
 
void clutchlog::operator= (clutchlog const &)=delete
 

Configuration accessors

void clutchlog::format (const std::string &format)
 Set the template string.
 
std::string clutchlog::format () const
 Get the template string.
 
void clutchlog::format_comment (const std::string &format)
 Set the template string for dumps.
 
std::string clutchlog::format_comment () const
 Get the template string for dumps.
 
void clutchlog::out (std::ostream &out)
 Set the output stream on which to print.
 
std::ostream & clutchlog::out ()
 Get the output stream on which to print.
 
void clutchlog::threshold (level l)
 Set the log level (below which logs are not printed) with an identifier.
 
void clutchlog::threshold (const std::string &l)
 Set the log level (below which logs are not printed) with a string.
 
level clutchlog::threshold () const
 Get the log level below which logs are not printed.
 
const std::map< std::string, level > & clutchlog::levels () const
 Get the map of available log levels string representations toward their identifier. *‍/.
 
level clutchlog::level_of (const std::string name)
 Return the log level tag corresponding to the given pre-configured name. More...
 
void clutchlog::file (std::string file)
 Set the regular expression filtering the file location.
 
void clutchlog::func (std::string func)
 Set the regular expression filtering the function location.
 
void clutchlog::line (std::string line)
 Set the regular expression filtering the line location.
 
void clutchlog::location (const std::string &in_file, const std::string &in_function=".*", const std::string &in_line=".*")
 Set the regular expressions filtering the location.
 
template<class ... FMT>
void clutchlog::style (level stage, FMT... styles)
 Set the style (color and typo) of the given log level. More...
 
void clutchlog::style (level stage, fmt style)
 Set the style (color and typo) of the given log level, passing a fmt instance.
 
fmt clutchlog::style (level stage) const
 Get the configured fmt instance of the given log level.
 

Low-level API

scope_t clutchlog::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.
 
std::string clutchlog::replace (const std::string &form, const std::string &mark, const std::string &tag) const
 Replace mark by tag in form. More...
 
std::string clutchlog::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.
 
std::string clutchlog::format (std::string format, const std::string &what, const level &stage, const std::string &file, const std::string &func, const size_t line) const
 Substitute all tags in the format string with the corresponding information and apply the style corresponding to the log level.
 
void clutchlog::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.
 
template<class In >
void clutchlog::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.
 

All combination of constructors with different parameters orders.

 clutchlog::fmt::fmt (fg f, bg b=bg::none, typo s=typo::none)
 
 clutchlog::fmt::fmt (fg f, typo s, bg b=bg::none)
 
 clutchlog::fmt::fmt (bg b, fg f=fg::none, typo s=typo::none)
 
 clutchlog::fmt::fmt (bg b, typo s, fg f=fg::none)
 
 clutchlog::fmt::fmt (typo s, fg f=fg::none, bg b=bg::none)
 
 clutchlog::fmt::fmt (typo s, bg b, fg f=fg::none)
 

Detailed Description

Macro Definition Documentation

◆ CLUTCHCODE

#define CLUTCHCODE (   LEVEL,
  ... 
)
Value:
do { \
auto& clutchlog__logger = clutchlog::logger(); \
clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
if(clutchlog__scope.matches) { \
__VA_ARGS__ \
} \
} while(0)

Run any code if the scope matches.

◆ CLUTCHDUMP

#define CLUTCHDUMP (   LEVEL,
  CONTAINER,
  FILENAME 
)
Value:
do { \
auto& clutchlog__logger = clutchlog::logger(); \
clutchlog__logger.dump(clutchlog::level::LEVEL, std::begin(CONTAINER), std::end(CONTAINER), \
} while(0)

Dump the given container.

◆ CLUTCHFUNC

#define CLUTCHFUNC (   LEVEL,
  FUNC,
  ... 
)
Value:
do { \
auto& clutchlog__logger = clutchlog::logger(); \
clutchlog::scope_t clutchlog__scope = clutchlog__logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \
if(clutchlog__scope.matches) { \
FUNC(__VA_ARGS__); \
} \
} while(0)

Call any function if the scope matches.

◆ CLUTCHLOG

#define CLUTCHLOG (   LEVEL,
  WHAT 
)
Value:
do { \
auto& clutchlog__logger = clutchlog::logger(); \
std::ostringstream clutchlog__msg ; clutchlog__msg << WHAT; \
clutchlog__logger.log(clutchlog::level::LEVEL, clutchlog__msg.str(), CLUTCHLOC); \
} while(0)

Log a message at the given level.

Function Documentation

◆ level_of()

level clutchlog::level_of ( const std::string  name)
inline

Return the log level tag corresponding to the given pre-configured name.

Note
This is case sensitive, see the pre-configured _level_word.

◆ logger()

static clutchlog& clutchlog::logger ( )
inlinestatic

Get the logger instance.

◆ operator()()

std::string clutchlog::fmt::operator() ( const std::string &  msg) const
inline

Format the given string with the currently encoded format.

Allow to use a formatter as a function:

clutchlog::fmt error(clutchlog::fmt::fg::red, clutchlog::fmt::typo::bold);
std::cout << error("ERROR") << std::endl;
Note
A formatter called this way WILL output a reset escape code at the end.

◆ replace()

std::string clutchlog::replace ( const std::string &  form,
const std::string &  mark,
const std::string &  tag 
) const
inline

Replace mark by tag in form.

log.replace("{greet} {world}", "\\{greet\\}", "hello");
// returns "hello {world}"

◆ style()

template<class ... FMT>
void clutchlog::style ( level  stage,
FMT...  styles 
)
inline

Set the style (color and typo) of the given log level.

This version accept style arguments as if they were passed to clutchlog::fmt.

Friends

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const fmt fmt 
)
friend

Output stream overload.

Allow to use a formatter as a tag within a stream:

clutchlog::fmt end(clutchlog::fmt::typo::reset);
clutchlog::fmt error(clutchlog::fmt::fg::red, clutchlog::fmt::typo::bold);
std::cout << error << "ERROR" << end << std::endl;
Note
An formatter called this way will NOT output a reset escape code.
clutchlog::logger
static clutchlog & logger()
Get the logger instance.
Definition: clutchlog.h:286
CLUTCHDUMP_DEFAULT_SEP
#define CLUTCHDUMP_DEFAULT_SEP
Compile-time default item separator for dump.
Definition: clutchlog.h:235
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:860
CLUTCHLOC
#define CLUTCHLOC
Handy shortcuts to location.
Definition: clutchlog.h:75
clutchlog::fmt
Color and style formatter for ANSI terminal escape sequences.
Definition: clutchlog.h:307