nestlog: colorize tag inserter

This commit is contained in:
Roland Conybeare 2023-09-16 14:21:43 -04:00
commit d46c734950
4 changed files with 77 additions and 3 deletions

View file

@ -0,0 +1,37 @@
/* @file concat.hpp */
#pragma once
#include <ostream>
#include <utility> // for std::move()
namespace xo {
template <typename T1, typename T2>
struct concat_impl {
public:
concat_impl(T1 && x1, T2 && x2)
: x1_{std::forward<T1>(x1)}, x2_{std::forward<T2>(x2)} {}
T1 const & x1() const { return x1_; }
T2 const & x2() const { return x2_; }
private:
T1 x1_;
T2 x2_;
}; /*concat_impl*/
template <typename T1, typename T2>
concat_impl<T1,T2> concat(T1 && x1, T2 && x2) {
return concat_impl<T1,T2>(std::move(x1), std::move(x2));
} /*concat*/
template <typename T1, typename T2>
inline std::ostream &
operator<<(std::ostream & os, concat_impl<T1, T2> const & x) {
os << x.x1() << x.x2();
return os;
} /*operator<<*/
} /*namespace xo*/
/* end concat.hpp */

View file

@ -4,7 +4,7 @@
#include "log_level.hpp"
#include "function.hpp"
#include "nestlog/color.hpp"
#include "color.hpp"
#include <cstdint>
namespace xo {

View file

@ -2,7 +2,10 @@
#pragma once
#include "nestlog/quoted.hpp"
#include "tag_config.hpp"
#include "concat.hpp"
#include "quoted.hpp"
#include "color.hpp"
#include <iostream>
// STRINGIFY(xyz) -> "xyz"
@ -93,7 +96,7 @@ namespace xo {
if(PrefixSpace)
s << " ";
s << ":" << tag.name()
s << with_color(tag_config::encoding, tag_config::tag_color, concat((char const *)":", tag.name()))
<< " " << unq(tag.value());
return s;

View file

@ -0,0 +1,34 @@
/* @file tag_config.hpp */
#pragma once
#include "color.hpp"
#include <cstdint>
namespace xo {
/* Tag here b/c we want header-only library */
template <typename Tag>
struct tag_config_impl {
/* color encoding */
static color_encoding encoding;
/* color to use for tags
* os << tag("foo", foovalue)
* to produces output like
* :foo foovalue
* with :foo using .tag_color
*/
static std::uint32_t tag_color;
}; /*tag_config_impl*/
template <typename Tag>
color_encoding
tag_config_impl<Tag>::encoding = CE_Xterm;
template <typename Tag>
std::uint32_t
tag_config_impl<Tag>::tag_color = 245;
using tag_config = tag_config_impl<class tag_config_tag>;
} /*namespace xo*/
/* end tag_config.hpp */