2019-04-11 12:58:23 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <util/win32_logger.hpp>
|
|
|
|
#include <util/logger_internal.hpp>
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
Win32LogStream::Win32LogStream(std::ostream& out) : OStreamLogStream(out)
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
if(!t)
|
|
|
|
this->isConsoleModern = false; // fall back to setting colours manually
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Win32LogStream::PreLog(std::stringstream& ss, LogLevel lvl, const char* fname,
|
|
|
|
int lineno) const
|
|
|
|
{
|
|
|
|
if(!isConsoleModern)
|
|
|
|
{
|
2019-04-11 15:36:20 +00:00
|
|
|
GetConsoleScreenBufferInfo(fd1, &consoleInfo);
|
|
|
|
old_attrs = consoleInfo.wAttributes;
|
2019-04-11 12:58:23 +00:00
|
|
|
switch(lvl)
|
|
|
|
{
|
|
|
|
case eLogNone:
|
|
|
|
break;
|
|
|
|
case eLogDebug:
|
2019-04-11 15:36:20 +00:00
|
|
|
SetConsoleTextAttribute(fd1,
|
|
|
|
FOREGROUND_RED | FOREGROUND_GREEN
|
|
|
|
| FOREGROUND_BLUE); // low white on black
|
2019-04-11 12:58:23 +00:00
|
|
|
ss << "[DBG] ";
|
|
|
|
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
|
|
|
ss << "[NFO] ";
|
|
|
|
break;
|
|
|
|
case eLogWarn:
|
2019-04-11 15:36:20 +00:00
|
|
|
SetConsoleTextAttribute(fd1,
|
|
|
|
FOREGROUND_RED | FOREGROUND_GREEN
|
|
|
|
| FOREGROUND_INTENSITY); // bright yellow
|
2019-04-11 12:58:23 +00:00
|
|
|
ss << "[WRN] ";
|
|
|
|
break;
|
|
|
|
case eLogError:
|
2019-04-11 15:36:20 +00:00
|
|
|
SetConsoleTextAttribute(
|
|
|
|
fd1, FOREGROUND_RED | FOREGROUND_INTENSITY); // bright red
|
2019-04-11 12:58:23 +00:00
|
|
|
ss << "[ERR] ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname
|
|
|
|
<< ":" << lineno << "\t";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
OStreamLogStream::PreLog(ss, lvl, fname, lineno);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Win32LogStream::PostLog(std::stringstream& ss) const
|
|
|
|
{
|
2019-04-11 15:36:20 +00:00
|
|
|
if(!isConsoleModern)
|
|
|
|
{
|
|
|
|
SetConsoleTextAttribute(
|
|
|
|
fd1, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
|
2019-04-11 12:58:23 +00:00
|
|
|
ss << std::endl;
|
2019-04-11 15:36:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
OStreamLogStream::PostLog(ss);
|
2019-04-11 12:58:23 +00:00
|
|
|
}
|
|
|
|
} // namespace llarp
|
|
|
|
#endif
|