Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
common/logging/text_formatter.cpp
ShadPKG / common / logging / text_formatter.cpp
1// SPDX-FileCopyrightText: Copyright 2014 Citra Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3 
4#include <array>
5#include <cstdio>
6 
7#ifdef _WIN32
8#include <windows.h>
9#endif
10 
11#include "common/assert.h"
12#include "common/logging/filter.h"
13#include "common/logging/log.h"
14#include "common/logging/log_entry.h"
15#include "common/logging/text_formatter.h"
16 
17namespace Common::Log {
18 
19std::string FormatLogMessage(const Entry& entry) {
20 const u32 time_seconds = static_cast<u32>(entry.timestamp.count() / 1000000);
21 const u32 time_fractional = static_cast<u32>(entry.timestamp.count() % 1000000);
22 
23 const char* class_name = GetLogClassName(entry.log_class);
24 const char* level_name = GetLevelName(entry.log_level);
25 
26 return fmt::format("[{}] <{}> {}:{}:{}: {}", class_name, level_name, entry.filename,
27 entry.function, entry.line_num, entry.message);
28}
29 
30void PrintMessage(const Entry& entry) {
31 const auto str = FormatLogMessage(entry).append(1, '\n');
32 fputs(str.c_str(), stdout);
33}
34 
35void PrintColoredMessage(const Entry& entry) {
36#ifdef _WIN32
37 HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE);
38 if (console_handle == INVALID_HANDLE_VALUE) {
39 return;
40 }
41 
42 CONSOLE_SCREEN_BUFFER_INFO original_info{};
43 GetConsoleScreenBufferInfo(console_handle, &original_info);
44 
45 WORD color = 0;
46 switch (entry.log_level) {
47 case Level::Trace: // Grey
48 color = FOREGROUND_INTENSITY;
49 break;
50 case Level::Debug: // Cyan
51 color = FOREGROUND_GREEN | FOREGROUND_BLUE;
52 break;
53 case Level::Info: // Bright gray
54 color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
55 break;
56 case Level::Warning: // Bright yellow
57 color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
58 break;
59 case Level::Error: // Bright red
60 color = FOREGROUND_RED | FOREGROUND_INTENSITY;
61 break;
62 case Level::Critical: // Bright magenta
63 color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
64 break;
65 case Level::Count:
66 UNREACHABLE();
67 }
68 
69 SetConsoleTextAttribute(console_handle, color);
70#else
71#define ESC "\x1b"
72 const char* color = "";
73 switch (entry.log_level) {
74 case Level::Trace: // Grey
75 color = ESC "[1;30m";
76 break;
77 case Level::Debug: // Cyan
78 color = ESC "[0;36m";
79 break;
80 case Level::Info: // Bright gray
81 color = ESC "[0;37m";
82 break;
83 case Level::Warning: // Bright yellow
84 color = ESC "[1;33m";
85 break;
86 case Level::Error: // Bright red
87 color = ESC "[1;31m";
88 break;
89 case Level::Critical: // Bright magenta
90 color = ESC "[1;35m";
91 break;
92 case Level::Count:
93 UNREACHABLE();
94 }
95 
96 fputs(color, stdout);
97#endif
98 
99 PrintMessage(entry);
100 
101#ifdef _WIN32
102 SetConsoleTextAttribute(console_handle, original_info.wAttributes);
103#else
104 fputs(ESC "[0m", stdout);
105#undef ESC
106#endif
107}
108 
109} // namespace Common::Log
110