xrpld
Loading...
Searching...
No Matches
TestSink.cpp
1#include <helpers/TestSink.h>
2
3#include <xrpl/beast/utility/Journal.h>
4
5#include <boost/predef.h>
6
7#include <cstdlib> // for getenv
8#include <string>
9
10#if BOOST_OS_WINDOWS
11#include <io.h> // for _isatty, _fileno
12#include <stdio.h> // for stdout
13#else
14#include <unistd.h> // for isatty, STDOUT_FILENO
15#endif
16
17#include <iostream>
18
19namespace xrpl {
20
24
25void
27{
28 if (level < threshold())
29 return;
30 writeAlways(level, text);
31}
32
33void
35{
36 auto supportsColor = [] {
37 // 1. Check for "NO_COLOR" environment variable (Standard convention)
38 if (std::getenv("NO_COLOR") != nullptr)
39 {
40 return false;
41 }
42
43 // 2. Check for "CLICOLOR_FORCE" (Force color)
44 if (std::getenv("CLICOLOR_FORCE") != nullptr)
45 {
46 return true;
47 }
48
49 // 3. Platform-specific check to see if stdout is a terminal
50#if BOOST_OS_WINDOWS
51 // Windows: Check if the output handle is a character device
52 // _fileno(stdout) is usually 1
53 // _isatty returns non-zero if the handle is a character device, 0
54 // otherwise.
55 return _isatty(_fileno(stdout)) != 0;
56#else
57 // Linux/macOS: Check if file descriptor 1 (stdout) is a TTY
58 // STDOUT_FILENO is 1
59 // isatty returns 1 if the file descriptor is a TTY, 0 otherwise.
60 return isatty(STDOUT_FILENO) != 0;
61#endif
62 }();
63
64 auto color = [level]() {
65 switch (level)
66 {
68 return "\033[34m"; // blue
70 return "\033[32m"; // green
72 return "\033[36m"; // cyan
74 return "\033[33m"; // yellow
76 return "\033[31m"; // red
78 default:
79 break;
80 }
81 return "\033[31m"; // red
82 }();
83
84 auto prefix = [level]() {
85 switch (level)
86 {
88 return "TRC:";
90 return "DBG:";
92 return "INF:";
94 return "WRN:";
96 return "ERR:";
98 default:
99 break;
100 }
101 return "FTL:";
102 }();
103
104 auto& stream = [level]() -> std::ostream& {
105 switch (level)
106 {
109 return std::cerr;
110 default:
111 return std::cout;
112 }
113 }();
114
115 static constexpr auto kReset = "\033[0m";
116
117 if (supportsColor)
118 {
119 stream << color << prefix << " " << text << kReset << std::endl;
120 }
121 else
122 {
123 stream << prefix << " " << text << std::endl;
124 }
125}
126
127} // namespace xrpl
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Sink(Sink const &sink)=default
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition TestSink.cpp:26
TestSink(beast::Severity threshold=beast::Severity::Debug)
Definition TestSink.cpp:21
void writeAlways(beast::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition TestSink.cpp:34
T endl(T... args)
T getenv(T... args)
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:11
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5