persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
logger.cpp
Go to the documentation of this file.
1#include "logger.hpp"
2
3#include "core/globals.hpp"
4#include <cstdint>
5#include <string>
6
7namespace
8{
9struct LogStats
10{
11 uint32_t total = 0;
12 uint32_t debug = 0;
13 uint32_t warning = 0;
14 uint32_t error = 0;
15 uint32_t fatal = 0;
16};
17
18LogStats stats;
19bool isLogSessionStarted = false;
20
27const char* severityName(LogSeverity severity)
28{
29 const char* severityStr = "UNKNOWN";
30 switch (severity)
31 {
33 {
34 severityStr = "DEBUG";
35 break;
36 }
37
39 {
40 severityStr = "WARNING";
41 break;
42 }
43
45 {
46 severityStr = "ERROR";
47 break;
48 }
49
51 {
52 severityStr = "FATAL";
53 break;
54 }
55 }
56
57 return severityStr;
58}
59
65void incrementStats(LogSeverity severity)
66{
67 stats.total++;
68 switch (severity)
69 {
71 {
72 stats.debug++;
73 break;
74 }
75
77 {
78 stats.warning++;
79 break;
80 }
81
83 {
84 stats.error++;
85 break;
86 }
87
89 {
90 stats.fatal++;
91 break;
92 }
93 }
94}
95} // namespace
96
98{
99 const std::string logPath = fatBasePath.empty() ? "fat:/data/log.txt" : (fatBasePath + "log.txt");
100 FILE* logFile = std::fopen(logPath.c_str(), "w");
101 if (logFile != nullptr)
102 {
103 std::fflush(logFile);
104 std::fclose(logFile);
105 }
106 stats = LogStats{};
107 isLogSessionStarted = true;
108}
109
110void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
111{
112 const char* file = location.file_name();
113 const int line = static_cast<int>(location.line());
114 const char* func = location.function_name();
115
116 incrementStats(severity);
117 const char* name = severityName(severity);
118
119 if (severity == LogSeverity::DEBUG)
120 {
122 std::fprintf(stderr,
123 "frame=%ld stats(total=%lu debug=%lu warning=%lu error=%lu fatal=%lu)\n"
124 "[%s] file: %s(%d) `%s`: %.*s\n",
125 static_cast<long>(frame),
126 static_cast<unsigned long>(stats.total),
127 static_cast<unsigned long>(stats.debug),
128 static_cast<unsigned long>(stats.warning),
129 static_cast<unsigned long>(stats.error),
130 static_cast<unsigned long>(stats.fatal),
131 name,
132 file,
133 line,
134 func,
135 static_cast<int>(message.size()),
136 message.data());
137 }
138 else if (severity != LogSeverity::FATAL)
139 {
141 std::fprintf(stderr,
142 "[%s] file: %s(%d) `%s`: %.*s\n",
143 name,
144 file,
145 line,
146 func,
147 static_cast<int>(message.size()),
148 message.data());
149 }
150
152 const std::string logPath = fatBasePath + "log.txt";
153 const char* openMode = isLogSessionStarted ? "a" : "w";
154 FILE* logFile = std::fopen(logPath.c_str(), openMode);
155
156 if (logFile != nullptr)
157 {
158 isLogSessionStarted = true;
159 std::fprintf(logFile,
160 "frame=%ld stats(total=%lu debug=%lu warning=%lu error=%lu fatal=%lu)\n"
161 "[%s] file: %s(%d) `%s`: %.*s\n",
162 static_cast<long>(frame),
163 static_cast<unsigned long>(stats.total),
164 static_cast<unsigned long>(stats.debug),
165 static_cast<unsigned long>(stats.warning),
166 static_cast<unsigned long>(stats.error),
167 static_cast<unsigned long>(stats.fatal),
168 name,
169 file,
170 line,
171 func,
172 static_cast<int>(message.size()),
173 message.data());
174 std::fflush(logFile);
175 std::fclose(logFile);
176 }
177
178 if (severity == LogSeverity::FATAL)
179 {
181 consoleDemoInit();
182 std::fprintf(stderr,
183 "frame=%ld stats(total=%lu debug=%lu warning=%lu error=%lu fatal=%lu)\n"
184 "[%s] file: %s(%d) `%s`: %.*s\n",
185 static_cast<long>(frame),
186 static_cast<unsigned long>(stats.total),
187 static_cast<unsigned long>(stats.debug),
188 static_cast<unsigned long>(stats.warning),
189 static_cast<unsigned long>(stats.error),
190 static_cast<unsigned long>(stats.fatal),
191 name,
192 file,
193 line,
194 func,
195 static_cast<int>(message.size()),
196 message.data());
197 while (1)
198 {
199 swiWaitForVBlank();
200 }
201 }
202}
Holds global variables used game-wide.
std::string fatBasePath
Definition main.cpp:38
volatile int32_t frame
Definition main.cpp:35
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
void clearLog()
Clears the log file for a fresh game session.
Definition logger.cpp:97
Util to handle error logging.
LogSeverity
Definition logger.hpp:14