2014-10-28 03:36:00 -04:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 00:38:14 -05:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-10-28 03:36:00 -04:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstdio>
|
|
|
|
|
2014-11-03 22:42:34 -05:00
|
|
|
#ifdef _WIN32
|
2016-09-17 20:38:01 -04:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
2014-11-03 22:42:34 -05:00
|
|
|
#endif
|
|
|
|
|
2014-10-28 03:36:00 -04:00
|
|
|
#include "common/logging/backend.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/logging/text_formatter.h"
|
|
|
|
|
2015-08-02 12:55:31 -04:00
|
|
|
#include "common/assert.h"
|
2015-05-06 03:06:12 -04:00
|
|
|
#include "common/common_funcs.h"
|
2014-11-04 00:03:19 -05:00
|
|
|
#include "common/string_util.h"
|
|
|
|
|
2014-10-28 03:36:00 -04:00
|
|
|
namespace Log {
|
|
|
|
|
2014-11-04 00:03:19 -05:00
|
|
|
// TODO(bunnei): This should be moved to a generic path manipulation library
|
|
|
|
const char* TrimSourcePath(const char* path, const char* root) {
|
|
|
|
const char* p = path;
|
|
|
|
|
|
|
|
while (*p != '\0') {
|
|
|
|
const char* next_slash = p;
|
|
|
|
while (*next_slash != '\0' && *next_slash != '/' && *next_slash != '\\') {
|
|
|
|
++next_slash;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_src = Common::ComparePartialString(p, next_slash, root);
|
|
|
|
p = next_slash;
|
|
|
|
|
|
|
|
if (*p != '\0') {
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
if (is_src) {
|
|
|
|
path = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2014-10-28 03:36:00 -04:00
|
|
|
void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len) {
|
2016-09-17 20:38:01 -04:00
|
|
|
unsigned int time_seconds = static_cast<unsigned int>(entry.timestamp.count() / 1000000);
|
2014-10-28 03:36:00 -04:00
|
|
|
unsigned int time_fractional = static_cast<unsigned int>(entry.timestamp.count() % 1000000);
|
|
|
|
|
2015-05-12 01:19:44 -04:00
|
|
|
const char* class_name = GetLogClassName(entry.log_class);
|
|
|
|
const char* level_name = GetLevelName(entry.log_level);
|
2014-10-28 03:36:00 -04:00
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
snprintf(out_text, text_len, "[%4u.%06u] %s <%s> %s: %s", time_seconds, time_fractional,
|
|
|
|
class_name, level_name, TrimSourcePath(entry.location.c_str()), entry.message.c_str());
|
2014-10-28 03:36:00 -04:00
|
|
|
}
|
|
|
|
|
2014-12-13 23:09:56 -05:00
|
|
|
void PrintMessage(const Entry& entry) {
|
|
|
|
std::array<char, 4 * 1024> format_buffer;
|
|
|
|
FormatLogMessage(entry, format_buffer.data(), format_buffer.size());
|
|
|
|
fputs(format_buffer.data(), stderr);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintColoredMessage(const Entry& entry) {
|
2014-11-03 22:42:34 -05:00
|
|
|
#ifdef _WIN32
|
|
|
|
static HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
|
2014-12-13 23:09:56 -05:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO original_info = {0};
|
|
|
|
GetConsoleScreenBufferInfo(console_handle, &original_info);
|
|
|
|
|
2014-11-03 22:42:34 -05:00
|
|
|
WORD color = 0;
|
2014-12-13 23:09:56 -05:00
|
|
|
switch (entry.log_level) {
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Trace: // Grey
|
2016-09-17 20:38:01 -04:00
|
|
|
color = FOREGROUND_INTENSITY;
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Debug: // Cyan
|
2016-09-17 20:38:01 -04:00
|
|
|
color = FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Info: // Bright gray
|
2016-09-17 20:38:01 -04:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Warning: // Bright yellow
|
2016-09-17 20:38:01 -04:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Error: // Bright red
|
2016-09-17 20:38:01 -04:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Critical: // Bright magenta
|
2016-09-17 20:38:01 -04:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
2015-08-02 12:55:31 -04:00
|
|
|
case Level::Count:
|
2015-08-02 18:30:24 -04:00
|
|
|
UNREACHABLE();
|
2014-11-03 22:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(console_handle, color);
|
|
|
|
#else
|
2016-09-17 20:38:01 -04:00
|
|
|
#define ESC "\x1b"
|
2014-11-03 22:42:34 -05:00
|
|
|
const char* color = "";
|
2014-12-13 23:09:56 -05:00
|
|
|
switch (entry.log_level) {
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Trace: // Grey
|
2016-09-17 20:38:01 -04:00
|
|
|
color = ESC "[1;30m";
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Debug: // Cyan
|
2016-09-17 20:38:01 -04:00
|
|
|
color = ESC "[0;36m";
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Info: // Bright gray
|
2016-09-17 20:38:01 -04:00
|
|
|
color = ESC "[0;37m";
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Warning: // Bright yellow
|
2016-09-17 20:38:01 -04:00
|
|
|
color = ESC "[1;33m";
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Error: // Bright red
|
2016-09-17 20:38:01 -04:00
|
|
|
color = ESC "[1;31m";
|
|
|
|
break;
|
2014-11-03 22:42:34 -05:00
|
|
|
case Level::Critical: // Bright magenta
|
2016-09-17 20:38:01 -04:00
|
|
|
color = ESC "[1;35m";
|
|
|
|
break;
|
2015-08-02 12:55:31 -04:00
|
|
|
case Level::Count:
|
2015-08-02 18:30:24 -04:00
|
|
|
UNREACHABLE();
|
2014-11-03 22:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs(color, stderr);
|
|
|
|
#endif
|
|
|
|
|
2014-12-13 23:09:56 -05:00
|
|
|
PrintMessage(entry);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetConsoleTextAttribute(console_handle, original_info.wAttributes);
|
|
|
|
#else
|
|
|
|
fputs(ESC "[0m", stderr);
|
2016-09-17 20:38:01 -04:00
|
|
|
#undef ESC
|
2014-12-13 23:09:56 -05:00
|
|
|
#endif
|
2014-10-28 03:36:00 -04:00
|
|
|
}
|
|
|
|
}
|