libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
LoggerFile.cpp
1/****************************************************************************
2 * Platform Abstraction Layer
3 * Daryl Borth 2026
4 * LoggerFile.cpp
5 ***************************************************************************/
6#include "LoggerFile.h"
7
8bool LoggerFile::init(const LogConfig & config)
9{
10 shutdown();
11
12 // Append-only - never truncates a prior run's log
13 file = fopen(config.filePath, "a");
14 if (!file)
15 return false;
16
17 // Line-buffer rather than the default full-buffering libc picks for
18 // a regular file - keeps a torn/half-written line out of the file if
19 // a flush is skipped (see below) and the console loses power.
20 setvbuf(file, nullptr, _IOLBF, 0);
21
22 flushPolicy = config.flushPolicy;
23 flushEveryNWrites = config.flushEveryNWrites > 0 ? config.flushEveryNWrites : 1;
24 writesSinceFlush = 0;
25 return true;
26}
27
29{
30 if (file)
31 {
32 fflush(file);
33 fclose(file);
34 file = nullptr;
35 }
36 writesSinceFlush = 0;
37}
38
39void LoggerFile::write(LogLevel /*level*/, const char * line, size_t len)
40{
41 if (!file)
42 return;
43
44 fwrite(line, 1, len, file);
45
46 switch (flushPolicy)
47 {
49 fflush(file);
50 break;
51
53 if (++writesSinceFlush >= flushEveryNWrites)
54 {
55 fflush(file);
56 writesSinceFlush = 0;
57 }
58 break;
59
61 default:
62 break;
63 }
64}
void shutdown() override
Releases whatever init() acquired.
void write(LogLevel level, const char *line, size_t len) override
Writes one already-formatted, newline-terminated line.
bool init(const LogConfig &config) override
Opens whatever handle/socket/file this backend needs, using only fields from config (nothing hardcode...
Definition LoggerFile.cpp:8
LogLevel
Severity of a single log call.
Definition Logger.h:45
@ Immediate
fflush() after every write - safest against a crash/power loss, slowest
Definition Logger.h:89
@ Never
rely on the C library / fclose() at shutdown - fastest, least crash-safe
Definition Logger.h:91
@ EveryNWrites
fflush() every LogConfig::flushEveryNWrites writes
Definition Logger.h:90
Full runtime configuration for the Logger.
Definition Logger.h:101
LogFlushPolicy flushPolicy
How often the file backend flushes to storage.
Definition Logger.h:136
char filePath[1024]
Path of the log file for the file backend. Each platform's init() points this at debug....
Definition Logger.h:134
uint32_t flushEveryNWrites
Flush interval in writes; only used when flushPolicy is LogFlushPolicy::EveryNWrites.
Definition Logger.h:138