Skip to content

Commit 47883f0

Browse files
Allow to chose stdout/stderr for console appender (#162, #117)
* add stderr/stdcout choice * add stderr handle * use correct streams * fix last handle * don't use too modern c++
2 parents fda4a26 + 9a5b2d8 commit 47883f0

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

include/plog/Appenders/ColorConsoleAppender.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ namespace plog
99
{
1010
public:
1111
#ifdef _WIN32
12-
ColorConsoleAppender() : m_originalAttr()
12+
ColorConsoleAppender(OutputStream outStream = streamStdOut)
13+
: ConsoleAppender<Formatter>(outStream)
14+
, m_originalAttr()
1315
{
1416
if (this->m_isatty)
1517
{
1618
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
17-
GetConsoleScreenBufferInfo(this->m_stdoutHandle, &csbiInfo);
19+
GetConsoleScreenBufferInfo(this->m_outputHandle, &csbiInfo);
1820

1921
m_originalAttr = csbiInfo.wAttributes;
2022
}
2123
}
2224
#else
23-
ColorConsoleAppender() {}
25+
ColorConsoleAppender(OutputStream outStream = streamStdOut)
26+
: ConsoleAppender<Formatter>(outStream)
27+
{}
2428
#endif
2529

2630
virtual void write(const Record& record)
@@ -42,37 +46,37 @@ namespace plog
4246
{
4347
#ifdef _WIN32
4448
case fatal:
45-
SetConsoleTextAttribute(this->m_stdoutHandle, foreground::kRed | foreground::kGreen | foreground::kBlue | foreground::kIntensity | background::kRed); // white on red background
49+
SetConsoleTextAttribute(this->m_outputHandle, foreground::kRed | foreground::kGreen | foreground::kBlue | foreground::kIntensity | background::kRed); // white on red background
4650
break;
4751

4852
case error:
49-
SetConsoleTextAttribute(this->m_stdoutHandle, static_cast<WORD>(foreground::kRed | foreground::kIntensity | (m_originalAttr & 0xf0))); // red
53+
SetConsoleTextAttribute(this->m_outputHandle, static_cast<WORD>(foreground::kRed | foreground::kIntensity | (m_originalAttr & 0xf0))); // red
5054
break;
5155

5256
case warning:
53-
SetConsoleTextAttribute(this->m_stdoutHandle, static_cast<WORD>(foreground::kRed | foreground::kGreen | foreground::kIntensity | (m_originalAttr & 0xf0))); // yellow
57+
SetConsoleTextAttribute(this->m_outputHandle, static_cast<WORD>(foreground::kRed | foreground::kGreen | foreground::kIntensity | (m_originalAttr & 0xf0))); // yellow
5458
break;
5559

5660
case debug:
5761
case verbose:
58-
SetConsoleTextAttribute(this->m_stdoutHandle, static_cast<WORD>(foreground::kGreen | foreground::kBlue | foreground::kIntensity | (m_originalAttr & 0xf0))); // cyan
62+
SetConsoleTextAttribute(this->m_outputHandle, static_cast<WORD>(foreground::kGreen | foreground::kBlue | foreground::kIntensity | (m_originalAttr & 0xf0))); // cyan
5963
break;
6064
#else
6165
case fatal:
62-
std::cout << "\x1B[97m\x1B[41m"; // white on red background
66+
this->m_outputStream << "\x1B[97m\x1B[41m"; // white on red background
6367
break;
6468

6569
case error:
66-
std::cout << "\x1B[91m"; // red
70+
this->m_outputStream << "\x1B[91m"; // red
6771
break;
6872

6973
case warning:
70-
std::cout << "\x1B[93m"; // yellow
74+
this->m_outputStream << "\x1B[93m"; // yellow
7175
break;
7276

7377
case debug:
7478
case verbose:
75-
std::cout << "\x1B[96m"; // cyan
79+
this->m_outputStream << "\x1B[96m"; // cyan
7680
break;
7781
#endif
7882
default:
@@ -86,9 +90,9 @@ namespace plog
8690
if (this->m_isatty)
8791
{
8892
#ifdef _WIN32
89-
SetConsoleTextAttribute(this->m_stdoutHandle, m_originalAttr);
93+
SetConsoleTextAttribute(this->m_outputHandle, m_originalAttr);
9094
#else
91-
std::cout << "\x1B[0m\x1B[0K";
95+
this->m_outputStream << "\x1B[0m\x1B[0K";
9296
#endif
9397
}
9498
}

include/plog/Appenders/ConsoleAppender.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@
66

77
namespace plog
88
{
9+
enum OutputStream
10+
{
11+
streamStdOut,
12+
streamStdErr
13+
};
14+
915
template<class Formatter>
1016
class ConsoleAppender : public IAppender
1117
{
1218
public:
1319
#ifdef _WIN32
14-
ConsoleAppender() : m_isatty(!!_isatty(_fileno(stdout))), m_stdoutHandle()
20+
ConsoleAppender(OutputStream outStream = streamStdOut)
21+
: m_isatty(!!_isatty(_fileno(outStream == streamStdOut ? stdout : stderr)))
22+
, m_outputStream(outStream == streamStdOut ? std::cout : std::cerr)
23+
, m_outputHandle()
1524
{
1625
if (m_isatty)
1726
{
18-
m_stdoutHandle = GetStdHandle(stdHandle::kOutput);
27+
m_outputHandle = GetStdHandle(outStream == streamStdOut ? stdHandle::kOutput : stdHandle::kErrorOutput);
1928
}
2029
}
2130
#else
22-
ConsoleAppender() : m_isatty(!!isatty(fileno(stdout))) {}
31+
ConsoleAppender(OutputStream outStream = streamStdOut)
32+
: m_isatty(!!isatty(fileno(outStream == streamStdOut ? stdout : stderr)))
33+
, m_outputStream(outStream == streamStdOut ? std::cout : std::cerr)
34+
{}
2335
#endif
2436

2537
virtual void write(const Record& record)
@@ -36,14 +48,14 @@ namespace plog
3648
#ifdef _WIN32
3749
if (m_isatty)
3850
{
39-
WriteConsoleW(m_stdoutHandle, str.c_str(), static_cast<DWORD>(str.size()), NULL, NULL);
51+
WriteConsoleW(m_outputHandle, str.c_str(), static_cast<DWORD>(str.size()), NULL, NULL);
4052
}
4153
else
4254
{
43-
std::cout << util::toNarrow(str, codePage::kActive) << std::flush;
55+
m_outputStream << util::toNarrow(str, codePage::kActive) << std::flush;
4456
}
4557
#else
46-
std::cout << str << std::flush;
58+
m_outputStream << str << std::flush;
4759
#endif
4860
}
4961

@@ -55,8 +67,9 @@ namespace plog
5567
protected:
5668
util::Mutex m_mutex;
5769
const bool m_isatty;
70+
std::ostream& m_outputStream;
5871
#ifdef _WIN32
59-
HANDLE m_stdoutHandle;
72+
HANDLE m_outputHandle;
6073
#endif
6174
};
6275
}

include/plog/WinApi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ namespace plog
8787
const DWORD kDword = 4;
8888
}
8989

90-
namespace stdHandle
90+
namespace stdHandle
9191
{
9292
const DWORD kOutput = static_cast<DWORD>(-11);
93+
const DWORD kErrorOutput = static_cast<DWORD>(-12);
9394
}
9495

9596
namespace foreground

0 commit comments

Comments
 (0)