Skip to content

Commit 7e12e31

Browse files
committed
sdl3.zig logging API
1 parent e413393 commit 7e12e31

File tree

1 file changed

+280
-15
lines changed

1 file changed

+280
-15
lines changed

src/sdl3.zig

Lines changed: 280 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@ const builtin = @import("builtin");
55

66
const std = @import("std");
77
const assert = std.debug.assert;
8-
const log = std.log.scoped(.zsdl3);
9-
10-
const sdl3 = @This();
118

129
test {
13-
_ = std.testing.refAllDeclsRecursive(sdl3);
10+
_ = std.testing.refAllDeclsRecursive(@This());
11+
}
12+
13+
pub const Error = error{SdlError};
14+
15+
pub fn makeError() error{SdlError} {
16+
if (getError()) |str| {
17+
logError(.@"error", "{s}", .{str});
18+
}
19+
return error.SdlError;
1420
}
1521

1622
//--------------------------------------------------------------------------------------------------
1723
//
1824
// Application entry points (SDL_main.h)
1925
//
2026
//--------------------------------------------------------------------------------------------------
21-
2227
pub const AppResult = enum(c_int) {
2328
@"continue" = 0,
2429
success = 1,
@@ -139,21 +144,281 @@ pub fn getError() ?[:0]const u8 {
139144
}
140145
extern fn SDL_GetError() [*c]const u8;
141146

142-
pub const Error = error{SdlError};
143-
144-
pub fn makeError() error{SdlError} {
145-
if (getError()) |str| {
146-
log.debug("{s}", .{str});
147-
}
148-
return error.SdlError;
149-
}
150-
151147
//--------------------------------------------------------------------------------------------------
152148
//
153149
// Log Handling (SDL_log.h)
154150
//
155151
//--------------------------------------------------------------------------------------------------
156-
// TODO
152+
pub const max_log_message = 4096;
153+
154+
/// The predefined log categories
155+
///
156+
/// By default the application and gpu categories are enabled at the INFO
157+
/// level, the assert category is enabled at the WARN level, test is enabled at
158+
/// the VERBOSE level and all other categories are enabled at the ERROR level.
159+
pub const LogCategory = enum(c_int) {
160+
application = 0,
161+
@"error",
162+
assert,
163+
system,
164+
audio,
165+
video,
166+
render,
167+
input,
168+
@"test",
169+
gpu,
170+
reserved2,
171+
reserved3,
172+
reserved4,
173+
reserved5,
174+
reserved6,
175+
reserved7,
176+
reserved8,
177+
reserved9,
178+
reserved10,
179+
custom,
180+
_,
181+
};
182+
183+
/// The predefined log priorities
184+
pub const LogPriority = enum(c_int) {
185+
invalid = 0,
186+
trace,
187+
verbose,
188+
debug,
189+
info,
190+
warn,
191+
@"error",
192+
critical,
193+
};
194+
195+
/// Set the priority of all log categories.
196+
pub fn setLogPriorities(priority: LogPriority) void {
197+
SDL_SetLogPriorities(priority);
198+
}
199+
extern fn SDL_SetLogPriorities(priority: LogPriority) void;
200+
201+
/// Set the priority of a particular log category.
202+
pub fn setLogPriority(category: LogCategory, priority: LogPriority) void {
203+
SDL_SetLogPriority(@intFromEnum(category), priority);
204+
}
205+
extern fn SDL_SetLogPriority(category: c_int, priority: LogPriority) void;
206+
207+
/// Get the priority of a particular log category.
208+
pub fn getLogPriority(category: LogCategory) LogPriority {
209+
return SDL_GetLogPriority(@intFromEnum(category));
210+
}
211+
extern fn SDL_GetLogPriority(category: c_int) LogPriority;
212+
213+
/// Reset all priorities to default.
214+
///
215+
/// This is called by quit().
216+
pub const resetLogPriorities = SDL_ResetLogPriorities;
217+
extern fn SDL_ResetLogPriorities() void;
218+
219+
/// Set the text prepended to log messages of a given priority.
220+
///
221+
/// By default INFO and below have no prefix, and WARN and higher have a prefix
222+
/// showing their priority, e.g. "WARNING: ".
223+
pub fn setLogPriorityPrefix(priority: LogPriority, prefix: ?[:0]const u8) Error!void {
224+
if (!SDL_SetLogPriorityPrefix(
225+
priority,
226+
if (prefix) |p| @as([*c]const u8, @ptrCast(p.ptr)) else null,
227+
)) {
228+
return makeError();
229+
}
230+
}
231+
extern fn SDL_SetLogPriorityPrefix(priority: LogPriority, prefix: [*c]const u8) bool;
232+
233+
/// Log a message with SDL_LOG_CATEGORY_APPLICATION and SDL_LOG_PRIORITY_INFO.
234+
pub fn log(comptime fmt: []const u8, args: anytype) void {
235+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
236+
var buf: [max_log_message]u8 = undefined;
237+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
238+
std.fmt.BufPrintError.NoSpaceLeft => {
239+
SDL_LogError(
240+
@intFromEnum(LogCategory.assert),
241+
"(log message exceeded %d characters)",
242+
.{max_log_message},
243+
);
244+
return;
245+
},
246+
};
247+
SDL_Log(message.ptr);
248+
}
249+
extern fn SDL_Log(fmt: [*:0]const u8, ...) void;
250+
251+
/// Log a message with SDL_LOG_PRIORITY_TRACE.
252+
pub fn logTrace(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
253+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
254+
var buf: [max_log_message]u8 = undefined;
255+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
256+
std.fmt.BufPrintError.NoSpaceLeft => {
257+
SDL_LogError(
258+
@intFromEnum(LogCategory.assert),
259+
"(log message exceeded %d characters)",
260+
.{max_log_message},
261+
);
262+
return;
263+
},
264+
};
265+
SDL_LogTrace(@intFromEnum(category), message.ptr);
266+
}
267+
extern fn SDL_LogTrace(category: c_int, fmt: [*:0]const u8, ...) void;
268+
269+
/// Log a message with SDL_LOG_PRIORITY_VERBOSE.
270+
pub fn logVerbose(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
271+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
272+
var buf: [max_log_message]u8 = undefined;
273+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
274+
std.fmt.BufPrintError.NoSpaceLeft => {
275+
SDL_LogError(
276+
@intFromEnum(LogCategory.assert),
277+
"(log message exceeded %d characters)",
278+
.{max_log_message},
279+
);
280+
return;
281+
},
282+
};
283+
SDL_LogVerbose(@intFromEnum(category), message.ptr);
284+
}
285+
extern fn SDL_LogVerbose(category: c_int, fmt: [*:0]const u8, ...) void;
286+
287+
/// Log a message with SDL_LOG_PRIORITY_DEBUG.
288+
pub fn logDebug(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
289+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
290+
var buf: [max_log_message]u8 = undefined;
291+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
292+
std.fmt.BufPrintError.NoSpaceLeft => {
293+
SDL_LogError(
294+
@intFromEnum(LogCategory.assert),
295+
"(log message exceeded %d characters)",
296+
.{max_log_message},
297+
);
298+
return;
299+
},
300+
};
301+
SDL_LogDebug(@intFromEnum(category), message.ptr);
302+
}
303+
extern fn SDL_LogDebug(category: c_int, fmt: [*:0]const u8, ...) void;
304+
305+
/// Log a message with SDL_LOG_PRIORITY_INFO.
306+
pub fn logInfo(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
307+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
308+
var buf: [max_log_message]u8 = undefined;
309+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
310+
std.fmt.BufPrintError.NoSpaceLeft => {
311+
SDL_LogError(
312+
@intFromEnum(LogCategory.assert),
313+
"(log message exceeded %d characters)",
314+
.{max_log_message},
315+
);
316+
return;
317+
},
318+
};
319+
SDL_LogInfo(@intFromEnum(category), message.ptr);
320+
}
321+
extern fn SDL_LogInfo(category: c_int, fmt: [*:0]const u8, ...) void;
322+
323+
/// Log a message with SDL_LOG_PRIORITY_WARN.
324+
pub fn logWarn(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
325+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
326+
var buf: [max_log_message]u8 = undefined;
327+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
328+
std.fmt.BufPrintError.NoSpaceLeft => {
329+
SDL_LogError(
330+
@intFromEnum(LogCategory.assert),
331+
"(log message exceeded %d characters)",
332+
.{max_log_message},
333+
);
334+
return;
335+
},
336+
};
337+
SDL_LogWarn(@intFromEnum(category), message.ptr);
338+
}
339+
extern fn SDL_LogWarn(category: c_int, fmt: [*:0]const u8, ...) void;
340+
341+
/// Log a message with SDL_LOG_PRIORITY_ERROR.
342+
pub fn logError(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
343+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
344+
var buf: [max_log_message]u8 = undefined;
345+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
346+
std.fmt.BufPrintError.NoSpaceLeft => {
347+
SDL_LogError(
348+
@intFromEnum(LogCategory.assert),
349+
"(log message exceeded %d characters)",
350+
.{max_log_message},
351+
);
352+
return;
353+
},
354+
};
355+
SDL_LogError(@intFromEnum(category), message.ptr);
356+
}
357+
extern fn SDL_LogError(category: c_int, fmt: [*:0]const u8, ...) void;
358+
359+
/// Log a message with SDL_LOG_PRIORITY_CRITICAL.
360+
pub fn logCritical(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
361+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
362+
var buf: [max_log_message]u8 = undefined;
363+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
364+
std.fmt.BufPrintError.NoSpaceLeft => {
365+
SDL_LogError(
366+
@intFromEnum(LogCategory.assert),
367+
"(log message exceeded %d characters)",
368+
.{max_log_message},
369+
);
370+
return;
371+
},
372+
};
373+
SDL_LogCritical(@intFromEnum(category), message.ptr);
374+
}
375+
extern fn SDL_LogCritical(category: c_int, fmt: [*:0]const u8, ...) void;
376+
377+
/// Log a message with the specified category and priority.
378+
pub fn logMessage(category: LogCategory, priority: LogPriority, comptime fmt: []const u8, args: anytype) void {
379+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
380+
var buf: [max_log_message]u8 = undefined;
381+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
382+
std.fmt.BufPrintError.NoSpaceLeft => {
383+
SDL_LogError(
384+
@intFromEnum(LogCategory.assert),
385+
"(log message exceeded %d characters)",
386+
.{max_log_message},
387+
);
388+
return;
389+
},
390+
};
391+
SDL_LogMessage(@intFromEnum(category), priority, message.ptr);
392+
}
393+
extern fn SDL_LogMessage(category: c_int, priority: LogPriority, fmt: [*:0]const u8, ...) void;
394+
395+
/// The prototype for the log output callback function.
396+
///
397+
/// This function is called by SDL when there is new text to be logged. A mutex
398+
/// is held so that this function is never called by more than one thread at
399+
/// once.
400+
pub const LogOutputFunction = *const fn (
401+
userdata: ?*anyopaque,
402+
category: c_int,
403+
priority: LogPriority,
404+
message: [*:0]const u8,
405+
) callconv(.c) void;
406+
407+
/// Get the default log output function.
408+
pub const getDefaultLogOutputFunction = SDL_GetDefaultLogOutputFunction;
409+
extern fn SDL_GetDefaultLogOutputFunction() LogOutputFunction;
410+
411+
/// Get the current log output function.
412+
pub fn getLogOutputFunction(callback: *LogOutputFunction, userdata: *?*anyopaque) void {
413+
SDL_GetLogOutputFunction(callback, userdata);
414+
}
415+
extern fn SDL_GetLogOutputFunction(callback: *LogOutputFunction, userdata: *?*anyopaque) void;
416+
417+
/// Replace the default log output function with one of your own.
418+
pub fn setLogOutputFunction(callback: LogOutputFunction, userdata: ?*anyopaque) void {
419+
SDL_SetLogOutputFunction(callback, userdata);
420+
}
421+
extern fn SDL_SetLogOutputFunction(callback: LogOutputFunction, userdata: ?*anyopaque) void;
157422

158423
//--------------------------------------------------------------------------------------------------
159424
//

0 commit comments

Comments
 (0)