2014-10-28 08:36:00 +01:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-10-28 08:36:00 +01:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstdio>
|
|
|
|
|
2014-11-04 04:42:34 +01:00
|
|
|
#ifdef _WIN32
|
2016-12-03 21:08:02 +01:00
|
|
|
#include <windows.h>
|
2014-11-04 04:42:34 +01:00
|
|
|
#endif
|
|
|
|
|
2016-09-20 17:21:23 +02:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_funcs.h"
|
2021-06-13 16:31:47 +02:00
|
|
|
#include "common/logging/filter.h"
|
2014-10-28 08:36:00 +01:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/logging/text_formatter.h"
|
2014-11-04 06:03:19 +01:00
|
|
|
#include "common/string_util.h"
|
|
|
|
|
2021-04-15 02:19:52 +02:00
|
|
|
namespace Common::Log {
|
2014-10-28 08:36:00 +01:00
|
|
|
|
2018-03-22 11:21:29 +01:00
|
|
|
std::string FormatLogMessage(const Entry& entry) {
|
2016-09-18 02:38:01 +02:00
|
|
|
unsigned int time_seconds = static_cast<unsigned int>(entry.timestamp.count() / 1000000);
|
2014-10-28 08:36:00 +01:00
|
|
|
unsigned int time_fractional = static_cast<unsigned int>(entry.timestamp.count() % 1000000);
|
|
|
|
|
2015-05-12 07:19:44 +02:00
|
|
|
const char* class_name = GetLogClassName(entry.log_class);
|
|
|
|
const char* level_name = GetLevelName(entry.log_level);
|
2014-10-28 08:36:00 +01:00
|
|
|
|
2018-03-22 11:21:29 +01:00
|
|
|
return fmt::format("[{:4d}.{:06d}] {} <{}> {}:{}:{}: {}", time_seconds, time_fractional,
|
|
|
|
class_name, level_name, entry.filename, entry.function, entry.line_num,
|
|
|
|
entry.message);
|
2014-10-28 08:36:00 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 05:09:56 +01:00
|
|
|
void PrintMessage(const Entry& entry) {
|
2018-10-05 05:55:50 +02:00
|
|
|
const auto str = FormatLogMessage(entry).append(1, '\n');
|
2018-03-22 11:21:29 +01:00
|
|
|
fputs(str.c_str(), stderr);
|
2014-12-14 05:09:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintColoredMessage(const Entry& entry) {
|
2014-11-04 04:42:34 +01:00
|
|
|
#ifdef _WIN32
|
2018-03-22 11:21:29 +01:00
|
|
|
HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
if (console_handle == INVALID_HANDLE_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-04 04:42:34 +01:00
|
|
|
|
2018-08-21 17:28:25 +02:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO original_info = {};
|
2014-12-14 05:09:56 +01:00
|
|
|
GetConsoleScreenBufferInfo(console_handle, &original_info);
|
|
|
|
|
2014-11-04 04:42:34 +01:00
|
|
|
WORD color = 0;
|
2014-12-14 05:09:56 +01:00
|
|
|
switch (entry.log_level) {
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Trace: // Grey
|
2016-09-18 02:38:01 +02:00
|
|
|
color = FOREGROUND_INTENSITY;
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Debug: // Cyan
|
2016-09-18 02:38:01 +02:00
|
|
|
color = FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Info: // Bright gray
|
2016-09-18 02:38:01 +02:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Warning: // Bright yellow
|
2016-09-18 02:38:01 +02:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Error: // Bright red
|
2016-09-18 02:38:01 +02:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Critical: // Bright magenta
|
2016-09-18 02:38:01 +02:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
2015-08-02 18:55:31 +02:00
|
|
|
case Level::Count:
|
2015-08-03 00:30:24 +02:00
|
|
|
UNREACHABLE();
|
2014-11-04 04:42:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(console_handle, color);
|
|
|
|
#else
|
2016-09-18 02:38:01 +02:00
|
|
|
#define ESC "\x1b"
|
2014-11-04 04:42:34 +01:00
|
|
|
const char* color = "";
|
2014-12-14 05:09:56 +01:00
|
|
|
switch (entry.log_level) {
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Trace: // Grey
|
2016-09-18 02:38:01 +02:00
|
|
|
color = ESC "[1;30m";
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Debug: // Cyan
|
2016-09-18 02:38:01 +02:00
|
|
|
color = ESC "[0;36m";
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Info: // Bright gray
|
2016-09-18 02:38:01 +02:00
|
|
|
color = ESC "[0;37m";
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Warning: // Bright yellow
|
2016-09-18 02:38:01 +02:00
|
|
|
color = ESC "[1;33m";
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Error: // Bright red
|
2016-09-18 02:38:01 +02:00
|
|
|
color = ESC "[1;31m";
|
|
|
|
break;
|
2014-11-04 04:42:34 +01:00
|
|
|
case Level::Critical: // Bright magenta
|
2016-09-18 02:38:01 +02:00
|
|
|
color = ESC "[1;35m";
|
|
|
|
break;
|
2015-08-02 18:55:31 +02:00
|
|
|
case Level::Count:
|
2015-08-03 00:30:24 +02:00
|
|
|
UNREACHABLE();
|
2014-11-04 04:42:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs(color, stderr);
|
|
|
|
#endif
|
|
|
|
|
2014-12-14 05:09:56 +01:00
|
|
|
PrintMessage(entry);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetConsoleTextAttribute(console_handle, original_info.wAttributes);
|
|
|
|
#else
|
|
|
|
fputs(ESC "[0m", stderr);
|
2016-09-18 02:38:01 +02:00
|
|
|
#undef ESC
|
2014-12-14 05:09:56 +01:00
|
|
|
#endif
|
2014-10-28 08:36:00 +01:00
|
|
|
}
|
2021-04-15 02:19:52 +02:00
|
|
|
} // namespace Common::Log
|