2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
#include <ctime>
|
2020-01-21 10:09:55 +08:00
|
|
|
#include <fstream>
|
|
|
|
|
#include <iostream>
|
2019-11-13 14:33:15 +08:00
|
|
|
#include <kiwano/core/Logger.h>
|
2019-10-10 15:09:38 +08:00
|
|
|
|
2019-04-24 13:33:19 +08:00
|
|
|
namespace
|
|
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
std::streambuf *cin_buffer, *cout_buffer, *cerr_buffer;
|
|
|
|
|
std::fstream console_input, console_output, console_error;
|
|
|
|
|
|
|
|
|
|
std::wstreambuf *wcin_buffer, *wcout_buffer, *wcerr_buffer;
|
|
|
|
|
std::wfstream wconsole_input, wconsole_output, wconsole_error;
|
|
|
|
|
|
|
|
|
|
void RedirectStdIO()
|
|
|
|
|
{
|
|
|
|
|
cin_buffer = std::cin.rdbuf();
|
|
|
|
|
cout_buffer = std::cout.rdbuf();
|
|
|
|
|
cerr_buffer = std::cerr.rdbuf();
|
|
|
|
|
wcin_buffer = std::wcin.rdbuf();
|
|
|
|
|
wcout_buffer = std::wcout.rdbuf();
|
|
|
|
|
wcerr_buffer = std::wcerr.rdbuf();
|
|
|
|
|
|
|
|
|
|
console_input.open("CONIN$", std::ios::in);
|
|
|
|
|
console_output.open("CONOUT$", std::ios::out);
|
|
|
|
|
console_error.open("CONOUT$", std::ios::out);
|
|
|
|
|
wconsole_input.open("CONIN$", std::ios::in);
|
|
|
|
|
wconsole_output.open("CONOUT$", std::ios::out);
|
|
|
|
|
wconsole_error.open("CONOUT$", std::ios::out);
|
|
|
|
|
|
|
|
|
|
FILE* dummy;
|
|
|
|
|
freopen_s(&dummy, "CONOUT$", "w+t", stdout);
|
|
|
|
|
freopen_s(&dummy, "CONIN$", "r+t", stdin);
|
|
|
|
|
freopen_s(&dummy, "CONOUT$", "w+t", stderr);
|
|
|
|
|
(void)dummy;
|
|
|
|
|
|
|
|
|
|
std::cin.rdbuf(console_input.rdbuf());
|
|
|
|
|
std::cout.rdbuf(console_output.rdbuf());
|
|
|
|
|
std::cerr.rdbuf(console_error.rdbuf());
|
|
|
|
|
std::wcin.rdbuf(wconsole_input.rdbuf());
|
|
|
|
|
std::wcout.rdbuf(wconsole_output.rdbuf());
|
|
|
|
|
std::wcerr.rdbuf(wconsole_error.rdbuf());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResetStdIO()
|
|
|
|
|
{
|
|
|
|
|
console_input.close();
|
|
|
|
|
console_output.close();
|
|
|
|
|
console_error.close();
|
|
|
|
|
wconsole_input.close();
|
|
|
|
|
wconsole_output.close();
|
|
|
|
|
wconsole_error.close();
|
|
|
|
|
|
|
|
|
|
std::cin.rdbuf(cin_buffer);
|
|
|
|
|
std::cout.rdbuf(cout_buffer);
|
|
|
|
|
std::cerr.rdbuf(cerr_buffer);
|
|
|
|
|
std::wcin.rdbuf(wcin_buffer);
|
|
|
|
|
std::wcout.rdbuf(wcout_buffer);
|
|
|
|
|
std::wcerr.rdbuf(wcerr_buffer);
|
|
|
|
|
|
|
|
|
|
fclose(stdout);
|
|
|
|
|
fclose(stdin);
|
|
|
|
|
fclose(stderr);
|
|
|
|
|
|
|
|
|
|
cin_buffer = nullptr;
|
|
|
|
|
cout_buffer = nullptr;
|
|
|
|
|
cerr_buffer = nullptr;
|
|
|
|
|
wcin_buffer = nullptr;
|
|
|
|
|
wcout_buffer = nullptr;
|
|
|
|
|
wcerr_buffer = nullptr;
|
2019-04-24 13:33:19 +08:00
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
HWND allocated_console = nullptr;
|
|
|
|
|
|
|
|
|
|
HWND AllocateConsole()
|
|
|
|
|
{
|
|
|
|
|
if (::AllocConsole())
|
|
|
|
|
{
|
|
|
|
|
allocated_console = ::GetConsoleWindow();
|
|
|
|
|
|
|
|
|
|
if (allocated_console)
|
|
|
|
|
{
|
|
|
|
|
RedirectStdIO();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return allocated_console;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FreeAllocatedConsole()
|
|
|
|
|
{
|
|
|
|
|
if (allocated_console)
|
|
|
|
|
{
|
|
|
|
|
ResetStdIO();
|
|
|
|
|
::FreeConsole();
|
|
|
|
|
allocated_console = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HWND GetAllocatedConsole()
|
|
|
|
|
{
|
|
|
|
|
return allocated_console;
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
namespace console_colors
|
|
|
|
|
{
|
|
|
|
|
const WORD blue = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
|
|
|
|
|
const WORD green = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
|
|
|
|
const WORD red = FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
|
const WORD yellow = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
|
const WORD white = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
|
|
|
|
|
|
const WORD blue_bg = white | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
|
|
|
|
|
const WORD green_bg = white | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
|
|
|
|
|
const WORD red_bg = white | BACKGROUND_RED | BACKGROUND_INTENSITY;
|
|
|
|
|
const WORD yellow_bg = BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
|
|
|
|
|
const WORD white_bg = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
const WORD Reset = white;
|
2020-01-21 10:09:55 +08:00
|
|
|
} // namespace console_colors
|
|
|
|
|
|
|
|
|
|
#define DECLARE_HANDLE_COLOR(NAME, HANDLE_NAME, COLOR) \
|
2020-02-10 13:47:00 +08:00
|
|
|
inline OutputStream&(NAME)(OutputStream & _out) \
|
2020-01-21 10:09:55 +08:00
|
|
|
{ \
|
|
|
|
|
::SetConsoleTextAttribute(::GetStdHandle(HANDLE_NAME), console_colors::##COLOR); \
|
|
|
|
|
return _out; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define DECLARE_COLOR(COLOR) \
|
|
|
|
|
DECLARE_HANDLE_COLOR(stdout_##COLOR, STD_OUTPUT_HANDLE, COLOR) \
|
|
|
|
|
DECLARE_HANDLE_COLOR(stderr_##COLOR, STD_ERROR_HANDLE, COLOR)
|
|
|
|
|
|
|
|
|
|
#define DECLARE_BG_COLOR(COLOR) \
|
|
|
|
|
DECLARE_HANDLE_COLOR(stdout_##COLOR##_bg, STD_OUTPUT_HANDLE, COLOR##_bg) \
|
|
|
|
|
DECLARE_HANDLE_COLOR(stderr_##COLOR##_bg, STD_ERROR_HANDLE, COLOR##_bg)
|
|
|
|
|
|
|
|
|
|
namespace console_colors
|
|
|
|
|
{
|
|
|
|
|
DECLARE_COLOR(red);
|
|
|
|
|
DECLARE_COLOR(green);
|
|
|
|
|
DECLARE_COLOR(yellow);
|
|
|
|
|
DECLARE_COLOR(blue);
|
|
|
|
|
DECLARE_COLOR(white);
|
2020-02-15 17:32:32 +08:00
|
|
|
DECLARE_COLOR(Reset);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
DECLARE_BG_COLOR(red);
|
|
|
|
|
DECLARE_BG_COLOR(green);
|
|
|
|
|
DECLARE_BG_COLOR(yellow);
|
|
|
|
|
DECLARE_BG_COLOR(blue);
|
|
|
|
|
DECLARE_BG_COLOR(white);
|
|
|
|
|
} // namespace console_colors
|
|
|
|
|
|
|
|
|
|
Logger::Logger()
|
|
|
|
|
: enabled_(true)
|
|
|
|
|
, default_stdout_color_(0)
|
|
|
|
|
, default_stderr_color_(0)
|
2020-02-10 13:47:00 +08:00
|
|
|
, output_stream_(std::cout.rdbuf())
|
|
|
|
|
, error_stream_(std::cerr.rdbuf())
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
ResetOutputStream();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger::~Logger()
|
|
|
|
|
{
|
|
|
|
|
FreeAllocatedConsole();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
void Logger::Printf(Level level, const char* format, ...)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
if (!enabled_)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
StringStream sstream;
|
|
|
|
|
Prepare(level, sstream);
|
|
|
|
|
|
|
|
|
|
// Format message
|
|
|
|
|
if (format)
|
|
|
|
|
{
|
|
|
|
|
va_list args = nullptr;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
static char temp_buffer[1024 * 3 + 1];
|
|
|
|
|
const auto len = ::_vscprintf(format, args) + 1;
|
|
|
|
|
::_vsnprintf_s(temp_buffer, len, len, format, args);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
sstream << ' ' << temp_buffer << "\r\n";
|
2019-04-24 13:33:19 +08:00
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Output(level, sstream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::Prepare(Level level, StringStream& sstream)
|
|
|
|
|
{
|
|
|
|
|
String prefix;
|
|
|
|
|
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
|
|
|
|
case Level::Info:
|
2020-02-10 13:47:00 +08:00
|
|
|
prefix = "[INFO] ";
|
2020-01-21 10:09:55 +08:00
|
|
|
break;
|
|
|
|
|
case Level::System:
|
2020-02-10 13:47:00 +08:00
|
|
|
prefix = "[SYSTEM] ";
|
2020-01-21 10:09:55 +08:00
|
|
|
break;
|
|
|
|
|
case Level::Warning:
|
2020-02-10 13:47:00 +08:00
|
|
|
prefix = "[WARN] ";
|
2020-01-21 10:09:55 +08:00
|
|
|
break;
|
|
|
|
|
case Level::Error:
|
2020-02-10 13:47:00 +08:00
|
|
|
prefix = "[ERROR] ";
|
2020-01-21 10:09:55 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Timestamp
|
|
|
|
|
time_t unix = std::time(nullptr);
|
|
|
|
|
std::tm tmbuf;
|
|
|
|
|
localtime_s(&tmbuf, &unix);
|
2020-02-10 13:47:00 +08:00
|
|
|
sstream << prefix << std::put_time(&tmbuf, "%H:%M:%S ");
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::Output(Level level, StringStream& sstream)
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
using ConsoleColor = Function<OutputStream&(OutputStream&)>;
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
OutputStream* ostream = nullptr;
|
|
|
|
|
ConsoleColor color = nullptr;
|
|
|
|
|
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
|
|
|
|
case Level::Info:
|
|
|
|
|
ostream = &output_stream_;
|
|
|
|
|
color = Closure(this, &Logger::DefaultOutputColor);
|
|
|
|
|
break;
|
|
|
|
|
case Level::System:
|
|
|
|
|
ostream = &output_stream_;
|
|
|
|
|
color = console_colors::stdout_blue;
|
|
|
|
|
break;
|
|
|
|
|
case Level::Warning:
|
|
|
|
|
ostream = &output_stream_;
|
|
|
|
|
color = console_colors::stdout_yellow_bg;
|
|
|
|
|
break;
|
|
|
|
|
case Level::Error:
|
|
|
|
|
ostream = &error_stream_;
|
|
|
|
|
color = console_colors::stderr_red_bg;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Printing
|
|
|
|
|
if (ostream)
|
|
|
|
|
{
|
|
|
|
|
auto output = sstream.str();
|
|
|
|
|
color(*ostream) << output << std::flush;
|
2020-02-10 13:47:00 +08:00
|
|
|
::OutputDebugStringA(output.c_str());
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
ResetConsoleColor();
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
void Logger::ResetOutputStream()
|
|
|
|
|
{
|
|
|
|
|
bool has_console = ::GetConsoleWindow() != nullptr;
|
|
|
|
|
if (has_console)
|
|
|
|
|
{
|
|
|
|
|
default_stdout_color_ = default_stderr_color_ =
|
|
|
|
|
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO stdout_info;
|
|
|
|
|
if (::GetConsoleScreenBufferInfo(::GetStdHandle(STD_OUTPUT_HANDLE), &stdout_info))
|
|
|
|
|
{
|
|
|
|
|
default_stdout_color_ = stdout_info.wAttributes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO stderr_info;
|
|
|
|
|
if (::GetConsoleScreenBufferInfo(::GetStdHandle(STD_ERROR_HANDLE), &stderr_info))
|
|
|
|
|
{
|
|
|
|
|
default_stderr_color_ = stderr_info.wAttributes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// replace the C++ global locale with the user-preferred locale
|
|
|
|
|
(void)std::locale::global(std::locale(""));
|
2020-02-10 13:47:00 +08:00
|
|
|
(void)std::cout.imbue(std::locale());
|
|
|
|
|
(void)std::cerr.imbue(std::locale());
|
2020-01-21 10:09:55 +08:00
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
RedirectOutputStreamBuffer(std::cout.rdbuf());
|
|
|
|
|
RedirectErrorStreamBuffer(std::cerr.rdbuf());
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
std::streambuf* Logger::RedirectOutputStreamBuffer(std::streambuf* buf)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
return output_stream_.rdbuf(buf);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
std::streambuf* Logger::RedirectErrorStreamBuffer(std::streambuf* buf)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
return error_stream_.rdbuf(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::ShowConsole(bool show)
|
|
|
|
|
{
|
|
|
|
|
HWND current_console = ::GetConsoleWindow();
|
|
|
|
|
if (show)
|
|
|
|
|
{
|
|
|
|
|
if (current_console)
|
|
|
|
|
{
|
|
|
|
|
::ShowWindow(current_console, SW_SHOW);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HWND console = ::AllocateConsole();
|
|
|
|
|
if (!console)
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
KGE_WARN("AllocConsole failed");
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// disable the close button of console
|
|
|
|
|
HMENU hmenu = ::GetSystemMenu(console, FALSE);
|
|
|
|
|
::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (current_console)
|
|
|
|
|
{
|
|
|
|
|
if (current_console == GetAllocatedConsole())
|
|
|
|
|
{
|
|
|
|
|
FreeAllocatedConsole();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
::ShowWindow(current_console, SW_HIDE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResetOutputStream();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace kiwano
|