2019-04-11 12:58:23 +00:00
|
|
|
#if defined(_WIN32)
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "win32_logger.hpp"
|
|
|
|
#include "logger_internal.hpp"
|
2019-04-11 12:58:23 +00:00
|
|
|
|
2019-04-11 15:53:21 +00:00
|
|
|
static CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
|
|
|
|
static short old_attrs;
|
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
Win32LogStream::Win32LogStream(std::ostream& out) : OStreamLogStream(true, out), m_Out(out)
|
2019-04-11 12:58:23 +00:00
|
|
|
{
|
|
|
|
// Attempt to use ANSI escapes directly
|
|
|
|
// if the modern console is active.
|
|
|
|
DWORD mode_flags;
|
|
|
|
|
|
|
|
GetConsoleMode(fd1, &mode_flags);
|
|
|
|
// since release SDKs don't have ANSI escape support yet
|
|
|
|
// we get all or nothing: if we can't get it, then we wouldn't
|
|
|
|
// be able to get any of them individually
|
|
|
|
mode_flags |= 0x0004 | 0x0008;
|
|
|
|
BOOL t = SetConsoleMode(fd1, mode_flags);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!t)
|
2019-04-11 12:58:23 +00:00
|
|
|
this->isConsoleModern = false; // fall back to setting colours manually
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Win32LogStream::PreLog(
|
|
|
|
std::stringstream& ss,
|
|
|
|
LogLevel lvl,
|
|
|
|
const char* fname,
|
|
|
|
int lineno,
|
|
|
|
const std::string& nodename) const
|
2019-08-15 06:27:40 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!isConsoleModern)
|
2019-08-15 06:27:40 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (lvl)
|
2019-08-15 06:27:40 +00:00
|
|
|
{
|
|
|
|
case eLogNone:
|
|
|
|
break;
|
2019-11-14 21:56:01 +00:00
|
|
|
case eLogTrace:
|
|
|
|
ss << "[TRC] ";
|
2020-03-03 21:23:39 +00:00
|
|
|
break;
|
2019-08-15 06:27:40 +00:00
|
|
|
case eLogDebug:
|
|
|
|
ss << "[DBG] ";
|
|
|
|
break;
|
|
|
|
case eLogInfo:
|
|
|
|
ss << "[NFO] ";
|
|
|
|
break;
|
|
|
|
case eLogWarn:
|
|
|
|
ss << "[WRN] ";
|
|
|
|
break;
|
|
|
|
case eLogError:
|
|
|
|
ss << "[ERR] ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ss << "[" << nodename << "]"
|
2020-04-07 18:38:56 +00:00
|
|
|
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << fname << ":" << lineno
|
|
|
|
<< "\t";
|
2019-08-15 06:27:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
OStreamLogStream::PreLog(ss, lvl, fname, lineno, nodename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Win32LogStream::PostLog(std::stringstream& ss) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!isConsoleModern)
|
2019-08-15 06:27:40 +00:00
|
|
|
ss << std::endl;
|
|
|
|
else
|
|
|
|
OStreamLogStream::PostLog(ss);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Win32LogStream::Print(LogLevel lvl, const char*, const std::string& msg)
|
2019-04-11 12:58:23 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!isConsoleModern)
|
2019-04-11 12:58:23 +00:00
|
|
|
{
|
2019-04-11 15:36:20 +00:00
|
|
|
GetConsoleScreenBufferInfo(fd1, &consoleInfo);
|
|
|
|
old_attrs = consoleInfo.wAttributes;
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (lvl)
|
2019-04-11 12:58:23 +00:00
|
|
|
{
|
|
|
|
case eLogNone:
|
|
|
|
break;
|
2020-03-03 21:23:39 +00:00
|
|
|
case eLogTrace:
|
2019-04-11 12:58:23 +00:00
|
|
|
case eLogDebug:
|
2020-04-07 18:38:56 +00:00
|
|
|
SetConsoleTextAttribute(
|
|
|
|
fd1,
|
|
|
|
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // low white on black
|
2019-04-11 12:58:23 +00:00
|
|
|
break;
|
|
|
|
case eLogInfo:
|
2019-04-11 15:36:20 +00:00
|
|
|
SetConsoleTextAttribute(
|
|
|
|
fd1,
|
|
|
|
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN
|
|
|
|
| FOREGROUND_BLUE); // high white on black
|
2019-04-11 12:58:23 +00:00
|
|
|
break;
|
|
|
|
case eLogWarn:
|
2020-04-07 18:38:56 +00:00
|
|
|
SetConsoleTextAttribute(
|
|
|
|
fd1,
|
|
|
|
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); // bright yellow
|
2019-04-11 12:58:23 +00:00
|
|
|
break;
|
|
|
|
case eLogError:
|
2020-04-07 18:38:56 +00:00
|
|
|
SetConsoleTextAttribute(fd1, FOREGROUND_RED | FOREGROUND_INTENSITY); // bright red
|
2019-04-11 12:58:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-15 08:41:04 +00:00
|
|
|
|
|
|
|
m_Out << msg << std::flush;
|
2019-04-11 12:58:23 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!isConsoleModern)
|
2019-04-11 15:36:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
SetConsoleTextAttribute(fd1, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
|
2019-04-11 15:36:20 +00:00
|
|
|
}
|
2019-04-11 12:58:23 +00:00
|
|
|
}
|
2019-08-15 06:27:40 +00:00
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
} // namespace llarp
|
|
|
|
#endif
|