libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
WutLoggerUdp.cpp
1/****************************************************************************
2 * Platform Abstraction Layer
3 * Daryl Borth 2026
4 * WutLoggerUdp.cpp
5 ***************************************************************************/
6#include "WutLoggerUdp.h"
7
8#include <sys/socket.h>
9#include <arpa/inet.h>
10#include <fcntl.h>
11#include <unistd.h>
12#include <cstring>
13
14bool WutLoggerUdp::init(const LogConfig & config)
15{
16 shutdown();
17
18 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
19 if (sock < 0)
20 return false;
21
22 if (config.nonBlocking)
23 {
24 int flags = fcntl(sock, F_GETFL, 0);
25 if (flags >= 0)
26 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
27 }
28
29 memset(&serverAddr, 0, sizeof(serverAddr));
30 serverAddr.sin_family = AF_INET;
31 serverAddr.sin_port = htons(config.targetPort);
32
33 if (inet_pton(AF_INET, config.targetIp, &serverAddr.sin_addr) != 1)
34 {
35 // Malformed targetIp - fail activation loudly rather than silently
36 // sending to a zeroed 0.0.0.0 address.
37 close(sock);
38 sock = -1;
39 return false;
40 }
41
42 return true;
43}
44
46{
47 if (sock >= 0)
48 {
49 close(sock);
50 sock = -1;
51 }
52}
53
54void WutLoggerUdp::write(LogLevel /*level*/, const char * line, size_t len)
55{
56 if (sock < 0)
57 return;
58
59 // Best-effort, non-blocking: a dropped packet or an offline listener
60 // must never stall the calling thread or trip the OS watchdog, so
61 // the return value is deliberately ignored rather than retried.
62 sendto(sock, line, len, 0, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
63}
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...
LogLevel
Severity of a single log call.
Definition Logger.h:45
Full runtime configuration for the Logger.
Definition Logger.h:101
uint16_t targetPort
Destination UDP port for the UDP backend.
Definition Logger.h:119
const char * targetIp
Destination IPv4 address for the UDP backend.
Definition Logger.h:117
bool nonBlocking
socket is always created non-blocking; kept for clarity/future use
Definition Logger.h:120