libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
OgcLoggerUdp.cpp
1/****************************************************************************
2 * Platform Abstraction Layer
3 * Daryl Borth 2026
4 * OgcLoggerUdp.cpp
5 ***************************************************************************/
6#include "OgcLoggerUdp.h"
7
8#ifdef HW_RVL
9
10#include <cstring>
11
12#include "../Platform.h"
13
14OgcLoggerUdp * OgcLoggerUdp::self = nullptr;
15
16bool OgcLoggerUdp::init(const LogConfig & config)
17{
18 shutdown();
19 pendingConfig = config;
20
21 if (WiiNetwork::isUp())
22 return activateSocket();
23
24 WiiNetwork::notifyWhenUp(&OgcLoggerUdp::OnNetworkUp);
25 return false;
26}
27
28bool OgcLoggerUdp::activateSocket()
29{
30 sock = net_socket(AF_INET, SOCK_DGRAM, 0);
31 if (sock < 0)
32 return false;
33
34 if (pendingConfig.nonBlocking)
35 {
36 u32 nonBlocking = 1;
37 net_ioctl(sock, FIONBIO, &nonBlocking);
38 }
39
40 memset(&serverAddr, 0, sizeof(serverAddr));
41 serverAddr.sin_len = 8;
42 serverAddr.sin_family = AF_INET;
43 serverAddr.sin_port = htons(pendingConfig.targetPort);
44
45 if (inet_aton(pendingConfig.targetIp, &serverAddr.sin_addr) == 0)
46 {
47 // Malformed targetIp - fail activation loudly rather than silently
48 // sending to a zeroed/broadcast address.
49 net_close(sock);
50 sock = -1;
51 return false;
52 }
53
54 return true;
55}
56
57void OgcLoggerUdp::OnNetworkUp()
58{
59 // Only ever reached from WiiNetwork's background thread
60 if (!self || !self->activateSocket())
61 return;
62
63 if (platform && platform->getLogger())
64 platform->getLogger()->activateDeferred(LOGGER_UDP);
65}
66
68{
69 if (sock >= 0)
70 {
71 net_close(sock);
72 sock = -1;
73 }
74}
75
76void OgcLoggerUdp::write(LogLevel, const char * line, size_t len)
77{
78 if (sock < 0)
79 return;
80
81 // Non-blocking send: a dropped packet or an offline listener returns
82 // immediately (typically -EAGAIN) rather than stalling the caller -
83 // deliberately not checked/retried, since UDP logging is inherently
84 // best-effort and retrying here would reintroduce the exact stall
85 // this backend exists to avoid.
86 net_sendto(sock, line, len, 0, (struct sockaddr *)&serverAddr, 8);
87}
88
89#else // !HW_RVL - GameCube: no network hardware supported by this backend
90
91bool OgcLoggerUdp::init(const LogConfig &) { return false; }
93void OgcLoggerUdp::write(LogLevel, const char *, size_t) { }
94
95#endif
void activateDeferred(LogBackendId id)
Called by a backend that deferred its own activation on some background precondition (eg.
Definition Logger.cpp:147
Non-blocking UDP log backend.
void shutdown() override
Releases whatever init() acquired.
bool init(const LogConfig &config) override
Opens whatever handle/socket/file this backend needs, using only fields from config (nothing hardcode...
void write(LogLevel level, const char *line, size_t len) override
Writes one already-formatted, newline-terminated line.
virtual Logger * getLogger()=0
May return nullptr on a Platform that hasn't finished init() yet - LogPrintf()/LOG_*() already guard ...
static void notifyWhenUp(void(*callback)())
Registers callback to run exactly once.
static bool isUp()
Non-blocking: true if the console currently has a usable IP.
LogLevel
Severity of a single log call.
Definition Logger.h:45
Full runtime configuration for the Logger.
Definition Logger.h:101