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