xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
helpers
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
19
namespace
xrpl
{
20
21
TestSink::TestSink
(
beast::Severity
threshold
) :
Sink
(
threshold
, false)
22
{
23
}
24
25
void
26
TestSink::write
(
beast::Severity
level,
std::string
const
& text)
27
{
28
if
(level <
threshold
())
29
return
;
30
writeAlways
(level, text);
31
}
32
33
void
34
TestSink::writeAlways
(
beast::Severity
level,
std::string
const
& text)
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
{
67
case
beast::Severity::Trace
:
68
return
"\033[34m"
;
// blue
69
case
beast::Severity::Debug
:
70
return
"\033[32m"
;
// green
71
case
beast::Severity::Info
:
72
return
"\033[36m"
;
// cyan
73
case
beast::Severity::Warning
:
74
return
"\033[33m"
;
// yellow
75
case
beast::Severity::Error
:
76
return
"\033[31m"
;
// red
77
case
beast::Severity::Fatal
:
78
default
:
79
break
;
80
}
81
return
"\033[31m"
;
// red
82
}();
83
84
auto
prefix = [level]() {
85
switch
(level)
86
{
87
case
beast::Severity::Trace
:
88
return
"TRC:"
;
89
case
beast::Severity::Debug
:
90
return
"DBG:"
;
91
case
beast::Severity::Info
:
92
return
"INF:"
;
93
case
beast::Severity::Warning
:
94
return
"WRN:"
;
95
case
beast::Severity::Error
:
96
return
"ERR:"
;
97
case
beast::Severity::Fatal
:
98
default
:
99
break
;
100
}
101
return
"FTL:"
;
102
}();
103
104
auto
& stream = [level]() ->
std::ostream
& {
105
switch
(level)
106
{
107
case
beast::Severity::Error
:
108
case
beast::Severity::Fatal
:
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
std::ostream
std::string
std::cerr
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition
beast_Journal.cpp:96
beast::Journal::Sink::Sink
Sink(Sink const &sink)=default
xrpl::TestSink::write
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition
TestSink.cpp:26
xrpl::TestSink::TestSink
TestSink(beast::Severity threshold=beast::Severity::Debug)
Definition
TestSink.cpp:21
xrpl::TestSink::writeAlways
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
std::cout
cstdlib
std::endl
T endl(T... args)
std::getenv
T getenv(T... args)
iostream
beast::Severity
Severity
Severity level / threshold of a Journal message.
Definition
Journal.h:16
beast::Severity::Warning
@ Warning
Definition
Journal.h:22
beast::Severity::Info
@ Info
Definition
Journal.h:21
beast::Severity::Fatal
@ Fatal
Definition
Journal.h:24
beast::Severity::Error
@ Error
Definition
Journal.h:23
beast::Severity::Debug
@ Debug
Definition
Journal.h:20
beast::Severity::Trace
@ Trace
Definition
Journal.h:19
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
string
Generated by
1.17.0