citra/src/common/logging/text_formatter.cpp

137 lines
4.1 KiB
C++
Raw Normal View History

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>
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
#endif
2014-10-28 08:36:00 +01:00
#include "common/logging/backend.h"
#include "common/logging/filter.h"
2014-10-28 08:36:00 +01:00
#include "common/logging/log.h"
#include "common/logging/text_formatter.h"
#include "common/string_util.h"
2014-10-28 08:36:00 +01:00
namespace Log {
// 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 08:36:00 +01:00
void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len) {
unsigned int time_seconds = static_cast<unsigned int>(entry.timestamp.count() / 1000000);
unsigned int time_fractional = static_cast<unsigned int>(entry.timestamp.count() % 1000000);
const char* class_name = Logger::GetLogClassName(entry.log_class);
const char* level_name = Logger::GetLevelName(entry.log_level);
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 08:36:00 +01: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) {
#ifdef _WIN32
static HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO original_info = {0};
GetConsoleScreenBufferInfo(console_handle, &original_info);
WORD color = 0;
switch (entry.log_level) {
case Level::Trace: // Grey
color = FOREGROUND_INTENSITY; break;
case Level::Debug: // Cyan
color = FOREGROUND_GREEN | FOREGROUND_BLUE; break;
case Level::Info: // Bright gray
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
case Level::Warning: // Bright yellow
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
case Level::Error: // Bright red
color = FOREGROUND_RED | FOREGROUND_INTENSITY; break;
case Level::Critical: // Bright magenta
color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
}
SetConsoleTextAttribute(console_handle, color);
#else
# define ESC "\x1b"
const char* color = "";
switch (entry.log_level) {
case Level::Trace: // Grey
color = ESC "[1;30m"; break;
case Level::Debug: // Cyan
color = ESC "[0;36m"; break;
case Level::Info: // Bright gray
color = ESC "[0;37m"; break;
case Level::Warning: // Bright yellow
color = ESC "[1;33m"; break;
case Level::Error: // Bright red
color = ESC "[1;31m"; break;
case Level::Critical: // Bright magenta
color = ESC "[1;35m"; break;
}
fputs(color, stderr);
#endif
PrintMessage(entry);
#ifdef _WIN32
SetConsoleTextAttribute(console_handle, original_info.wAttributes);
#else
fputs(ESC "[0m", stderr);
# undef ESC
#endif
2014-10-28 08:36:00 +01:00
}
void TextLoggingLoop(std::shared_ptr<Logger> logger, const Filter* filter) {
2014-10-28 08:36:00 +01:00
std::array<Entry, 256> entry_buffer;
while (true) {
size_t num_entries = logger->GetEntries(entry_buffer.data(), entry_buffer.size());
if (num_entries == Logger::QUEUE_CLOSED) {
break;
}
for (size_t i = 0; i < num_entries; ++i) {
const Entry& entry = entry_buffer[i];
if (filter->CheckMessage(entry.log_class, entry.log_level)) {
PrintColoredMessage(entry);
}
2014-10-28 08:36:00 +01:00
}
}
}
}