feat: add hfill_style

This commit is contained in:
Johann Dreo 2022-09-05 16:08:55 +02:00
commit 9904034020
2 changed files with 37 additions and 8 deletions

View file

@ -306,6 +306,12 @@ log.format(format.str());
Note: messages at the "critical", "error" and "warning" log levels are colored by default. Note: messages at the "critical", "error" and "warning" log levels are colored by default.
You may want to set their style to `none` if you want to stay in control of inserted colors in the format template. You may want to set their style to `none` if you want to stay in control of inserted colors in the format template.
The horizontal filling line (the `{hfill}` tag) can be configured separately with `hfill_style`,
for example:
```cpp
log.hfill_style(clutchlog::fmt::fg::black);
```
Disabled calls Disabled calls
-------------- --------------

View file

@ -1,6 +1,6 @@
#ifndef __CLUTCHLOG_H__
#define __CLUTCHLOG_H__
#pragma once #pragma once
#ifndef CLUTCHLOG_H
#define CLUTCHLOG_H
/** @file */ /** @file */
#include <ciso646> #include <ciso646>
@ -257,10 +257,14 @@ class clutchlog
#endif // CLUTCHLOG_HFILL_MARK #endif // CLUTCHLOG_HFILL_MARK
//! Default character used as a filling for right-align the right part of messages with "{hfill}". //! Default character used as a filling for right-align the right part of messages with "{hfill}".
static inline char default_hfill_char = CLUTCHLOG_HFILL_MARK; static inline char default_hfill_char = CLUTCHLOG_HFILL_MARK;
// NOTE: there is no CLUTCHLOG_HFILL_STYLE for defaulting,
// but you can still set `hfill_style(...)` on the logger singleton.
/* @} */ /* @} */
public: public:
/** @name High-level API /** @name High-level API
* @{ */ * @{ */
@ -436,6 +440,7 @@ class clutchlog
_format_log(clutchlog::default_format), _format_log(clutchlog::default_format),
_format_dump(clutchlog::dump_default_format), _format_dump(clutchlog::dump_default_format),
_hfill_char(clutchlog::default_hfill_char), _hfill_char(clutchlog::default_hfill_char),
_hfill_fmt(fmt::fg::none),
_out(&std::clog), _out(&std::clog),
#if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
_depth(std::numeric_limits<size_t>::max() - _strip_calls), _depth(std::numeric_limits<size_t>::max() - _strip_calls),
@ -472,6 +477,8 @@ class clutchlog
std::string _format_dump; std::string _format_dump;
/** Character for filling. */ /** Character for filling. */
char _hfill_char; char _hfill_char;
/** Style of the filling. */
fmt _hfill_fmt;
/** Standard output. */ /** Standard output. */
std::ostream* _out; std::ostream* _out;
#if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1 #if CLUTCHLOG_HAVE_UNIX_SYSINFO == 1
@ -536,6 +543,16 @@ class clutchlog
void hfill_mark(const char mark) {_hfill_char = mark;} void hfill_mark(const char mark) {_hfill_char = mark;}
//! Get the character for the stretching hfill marker. //! Get the character for the stretching hfill marker.
char hfill_mark() const {return _hfill_char;} char hfill_mark() const {return _hfill_char;}
//! Set the style for the stretching hfill marker, with a `fmt` object.
void hfill_style(fmt style) {_hfill_fmt = style;}
/** Set the style for the stretching hfill marker.
*
* This version accept style arguments as if they were passed to `clutchlog::fmt`.
*/
template<class ... FMT>
void style(FMT... styles) { this->hfill_style(fmt(styles...)); }
//! Get the character for the stretching hfill marker.
fmt hfill_style() const {return _hfill_fmt;}
#endif #endif
//! Set the log level (below which logs are not printed) with an identifier. //! Set the log level (below which logs are not printed) with an identifier.
@ -795,15 +812,19 @@ class clutchlog
if(right_len+left_len > _nb_columns) { if(right_len+left_len > _nb_columns) {
// The right part would go over the terminal width: add a new line. // The right part would go over the terminal width: add a new line.
const std::string hfill(std::max((size_t)0, _nb_columns-right_len), _hfill_char); const std::string hfill(std::max((size_t)0, _nb_columns-right_len), _hfill_char);
format = replace(format, "\\{hfill\\}", "\n"+hfill); const std::string hfill_styled = _hfill_fmt(hfill);
format = replace(format, "\\{hfill\\}", "\n"+hfill_styled);
} else { } else {
// There is some space in between left and right parts. // There is some space in between left and right parts.
const std::string hfill(std::max((size_t)0, _nb_columns - (right_len+left_len)), _hfill_char); const std::string hfill(std::max((size_t)0, _nb_columns - (right_len+left_len)), _hfill_char);
format = replace(format, "\\{hfill\\}", hfill); const std::string hfill_styled = _hfill_fmt(hfill);
format = replace(format, "\\{hfill\\}", hfill_styled);
} }
} else { } else {
// We don't know the terminal width. // We don't know the terminal width.
format = replace(format, "\\{hfill\\}", _hfill_char); const std::string hfill(1, _hfill_char);
const std::string hfill_styled = _hfill_fmt(hfill);
format = replace(format, "\\{hfill\\}", hfill_styled);
} }
} }
#endif #endif
@ -960,12 +981,14 @@ class clutchlog
#if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1 #if CLUTCHLOG_HAVE_UNIX_SYSIOCTL == 1
void hfill_mark(const char) {} void hfill_mark(const char) {}
char hfill_mark() const {} char hfill_mark() const {}
void hfill_fmt(fmt) {}
fmt hfill_fmt() const {}
#endif #endif
void threshold(level) {} void threshold(level) {}
void threshold(const std::string&) {} void threshold(const std::string&) {}
level threshold() const {} level threshold() const {}
const std::map<std::string,level> levels() const {}; const std::map<std::string,level> levels() const {}
level level_of(const std::string) {} level level_of(const std::string) {}
void file(std::string) {} void file(std::string) {}
@ -982,7 +1005,7 @@ class clutchlog
{} {}
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
template<class ... FMT> template<class ... FMT>
void style(level stage, FMT... styles) {} void style(level, FMT...) {}
void style(level, fmt) {} void style(level, fmt) {}
fmt style(level) const {} fmt style(level) const {}
public: public:
@ -1037,4 +1060,4 @@ class clutchlog
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif // WITH_CLUTCHLOG #endif // WITH_CLUTCHLOG
#endif // __CLUTCHLOG_H__ #endif // CLUTCHLOG_H