log: move Color, printLog as private

This commit is contained in:
Adrien Béraud committed 2023-05-17 23:43:55 -04:00
1 parent 24628ff13d
commit da6579132c
2 files changed
+32 -38

No files matched your search

+32 -2
View File
@@ -30,6 +30,36 @@
namespace dht {
namespace log {
/**
* Terminal colors for logging
*/
namespace Color {
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_YELLOW = 33,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_BLUE = 44,
BG_DEFAULT = 49
};
class Modifier {
const Code code;
public:
constexpr Modifier(Code pCode) : code(pCode) {}
friend std::ostream&
operator<<(std::ostream& os, const Modifier& mod) {
return os << "\033[" << mod.code << 'm';
}
};
}
constexpr const Color::Modifier def(Color::FG_DEFAULT);
constexpr const Color::Modifier red(Color::FG_RED);
constexpr const Color::Modifier yellow(Color::FG_YELLOW);
/**
* Print va_list to std::ostream (used for logging).
*/
@@ -46,8 +76,8 @@ printLog(std::ostream& s, char const *m, va_list args) {
using log_precision = microseconds;
constexpr auto den = log_precision::period::den;
auto num = duration_cast<log_precision>(steady_clock::now().time_since_epoch()).count();
s << "[" << std::setfill('0') << std::setw(6) << num / den << "."
<< std::setfill('0') << std::setw(6) << num % den << "]" << " ";
s << '[' << std::setfill('0') << std::setw(6) << num / den << '.'
<< std::setfill('0') << std::setw(6) << num % den << "] ";
// write log
s.write(buffer.data(), std::min((size_t) ret, buffer.size()));