LogManager: Use colors for logging when possible

This commit is contained in:
Tony Wasserka committed 2025-06-13 15:14:47 +02:00
1 parent febce21b21
commit 3e85e60a30
3 files changed
+33 -13

No files matched your search

@@ -6,6 +6,7 @@
#include <cstdarg>
#include <fmt/format.h>
#include <fmt/color.h>
namespace LogMan {
enum DebugLevels {
@@ -31,6 +32,16 @@ static inline const char* DebugLevelStr(uint32_t Level) {
}
}
static inline fmt::text_style DebugLevelStyle(uint32_t Level) {
switch (Level) {
case LogMan::ASSERT: return fmt::bg(fmt::color::red) | fmt::emphasis::bold | fmt::fg(fmt::color::white);
case LogMan::ERROR: return fmt::fg(fmt::color::red);
case LogMan::DEBUG: return fmt::fg(fmt::color::gray);
case LogMan::INFO: return fmt::fg(fmt::color::green);
default: return {}; break;
}
}
constexpr DebugLevels MSG_LEVEL = INFO;
// Note that all logging functions with the Fmt or _FMT suffix on them expect
+7 -8
View File
@@ -68,24 +68,22 @@ namespace {
static bool SilentLog {};
static int OutputFD {STDERR_FILENO};
// Set an empty style to disable coloring when FEXServer output is e.g. piped to a file
static bool DisableOutputColors {};
void MsgHandler(LogMan::DebugLevels Level, const char* Message) {
if (SilentLog) {
return;
}
const auto Output = fextl::fmt::format("[{}] {}\n", LogMan::DebugLevelStr(Level), Message);
const auto Style = DisableOutputColors ? fmt::text_style {} : LogMan::DebugLevelStyle(Level);
const auto Output = fextl::fmt::format("{} {}\n", fmt::styled(LogMan::DebugLevelStr(Level), Style), Message);
write(OutputFD, Output.c_str(), Output.size());
fsync(OutputFD);
}
void AssertHandler(const char* Message) {
if (SilentLog) {
return;
}
const auto Output = fextl::fmt::format("[ASSERT] {}\n", Message);
write(OutputFD, Output.c_str(), Output.size());
fsync(OutputFD);
return MsgHandler(LogMan::ASSERT, Message);
}
} // Anonymous namespace
@@ -383,6 +381,7 @@ int main(int argc, char** argv, char** const envp) {
OutputFD = open(LogFile.c_str(), O_CREAT | O_CLOEXEC | O_WRONLY, USER_PERMS);
}
}
DisableOutputColors = !isatty(OutputFD);
if (StartupSleep() && (StartupSleepProcName().empty() || Program.ProgramName == StartupSleepProcName())) {
LogMan::Msg::IFmt("[{}][{}] Sleeping for {} seconds", ::getpid(), Program.ProgramName, StartupSleep());
+15 -5
View File
@@ -9,6 +9,8 @@
#include "Common/Config.h"
#include "Common/FEXServerClient.h"
#include <fmt/color.h>
#include <chrono>
#include <dirent.h>
#include <fcntl.h>
@@ -23,20 +25,23 @@
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <termios.h>
#include <thread>
#include <unistd.h>
static timespec StartTime {};
// Set an empty style to disable coloring when FEXServer output is e.g. piped to a file
static std::optional<fmt::text_style> DisableColors = isatty(STDOUT_FILENO) ? std::nullopt : std::optional {fmt::text_style {}};
namespace Logging {
void MsgHandler(LogMan::DebugLevels Level, const char* Message) {
const auto Output = fmt::format("[{}] {}\n", LogMan::DebugLevelStr(Level), Message);
const auto Output = fmt::format("{} {}\n", fmt::styled(LogMan::DebugLevelStr(Level), DisableColors.value_or(DebugLevelStyle(Level))), Message);
write(STDOUT_FILENO, Output.c_str(), Output.size());
}
void AssertHandler(const char* Message) {
const auto Output = fmt::format("[A] {}\n", Message);
write(STDOUT_FILENO, Output.c_str(), Output.size());
return MsgHandler(LogMan::ASSERT, Message);
}
void ClientMsgHandler(int FD, FEXServerClient::Logging::PacketMsg* const Msg, const char* MsgStr) {
@@ -45,8 +50,13 @@ void ClientMsgHandler(int FD, FEXServerClient::Logging::PacketMsg* const Msg, co
}
auto seconds = Msg->Header.Timestamp.tv_sec - StartTime.tv_sec - (Msg->Header.Timestamp.tv_nsec < StartTime.tv_nsec);
auto nanos = (1'000'000'000 + Msg->Header.Timestamp.tv_nsec - StartTime.tv_nsec) % 1'000'000'000;
const auto Output = fmt::format("[{}][{}.{:03}][{}|{}] {}\n", LogMan::DebugLevelStr(Msg->Level), Msg->Header.Timestamp.tv_sec,
Msg->Header.Timestamp.tv_nsec, Msg->Header.PID, Msg->Header.TID, MsgStr);
char Metadata[128];
auto Cursor =
fmt::format_to(&Metadata[0], DisableColors.value_or(LogMan::DebugLevelStyle(Msg->Level)), "{}", LogMan::DebugLevelStr(Msg->Level));
Cursor = fmt::format_to(Cursor, DisableColors.value_or(fmt::fg(fmt::color::light_gray)), " {}|{} ", Msg->Header.PID, Msg->Header.TID);
Cursor = fmt::format_to(Cursor, DisableColors.value_or(fmt::fg(fmt::color::gray)), "{}.{:03}", seconds, nanos / 1000000);
*Cursor = 0;
auto Output = fmt::format("{} {}\n", Metadata, MsgStr);
write(STDERR_FILENO, Output.c_str(), Output.size());
}
} // namespace Logging