Skip to content

Commit 012ff5e

Browse files
committed
sdl2.zig add bindings for Logging API
1 parent 1253537 commit 012ff5e

File tree

1 file changed

+176
-3
lines changed

1 file changed

+176
-3
lines changed

src/sdl2.zig

Lines changed: 176 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ pub const Error = error{SdlError};
66

77
pub fn makeError() error{SdlError} {
88
if (getError()) |str| {
9-
std.log.debug("SDL2: {s}", .{str});
9+
logError(.@"error", "{s}", .{str});
1010
}
1111
return error.SdlError;
1212
}
1313

1414
const sdl2 = @This();
1515

1616
test {
17-
_ = std.testing.refAllDeclsRecursive(sdl2);
17+
_ = std.testing.refAllDeclsRecursive(@This());
1818
}
1919

2020
//--------------------------------------------------------------------------------------------------
@@ -93,7 +93,180 @@ extern fn SDL_GetError() ?[*:0]const u8;
9393
// Log Handling (SDL_log.h)
9494
//
9595
//--------------------------------------------------------------------------------------------------
96-
// TODO
96+
pub const max_log_message = 4096;
97+
98+
/// The predefined log categories
99+
pub const LogCategory = enum(c_int) {
100+
application = 0,
101+
@"error",
102+
assert,
103+
system,
104+
audio,
105+
video,
106+
render,
107+
input,
108+
@"test",
109+
reserved1,
110+
reserved2,
111+
reserved3,
112+
reserved4,
113+
reserved5,
114+
reserved6,
115+
reserved7,
116+
reserved8,
117+
reserved9,
118+
reserved10,
119+
custom,
120+
_,
121+
};
122+
123+
/// The predefined log priorities
124+
pub const LogPriority = enum(c_int) {
125+
verbose = 1,
126+
debug,
127+
info,
128+
warn,
129+
@"error",
130+
critical,
131+
};
132+
133+
/// Set the priority of all log categories.
134+
pub fn logSetAllPriority(priority: LogPriority) void {
135+
SDL_LogSetAllPriority(priority);
136+
}
137+
extern fn SDL_LogSetAllPriority(priority: LogPriority) void;
138+
139+
/// Set the priority of a particular log category.
140+
pub fn logSetPriority(category: LogCategory, priority: LogPriority) void {
141+
SDL_LogSetPriority(@intFromEnum(category), priority);
142+
}
143+
extern fn SDL_LogSetPriority(category: c_int, priority: LogPriority) void;
144+
145+
/// Get the priority of a particular log category.
146+
pub fn logGetPriority(category: LogCategory) LogPriority {
147+
return SDL_LogGetPriority(@intFromEnum(category));
148+
}
149+
extern fn SDL_LogGetPriority(category: c_int) LogPriority;
150+
151+
/// Reset all priorities to default.
152+
pub const logResetPriorities = SDL_LogResetPriorities;
153+
extern fn SDL_LogResetPriorities() void;
154+
155+
/// Log a message with SDL_LOG_CATEGORY_APPLICATION and SDL_LOG_PRIORITY_INFO.
156+
pub fn log(comptime fmt: []const u8, args: anytype) void {
157+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
158+
var buf: [max_log_message]u8 = undefined;
159+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
160+
SDL_LogError(.assert, "Log message too long!");
161+
};
162+
assert(message.len <= max_log_message);
163+
SDL_Log(message.ptr);
164+
}
165+
extern fn SDL_Log(fmt: [*:0]const u8, ...) void;
166+
167+
/// Log a message with SDL_LOG_PRIORITY_VERBOSE.
168+
pub fn logVerbose(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
169+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
170+
var buf: [max_log_message]u8 = undefined;
171+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
172+
SDL_LogError(.assert, "Log message too long!");
173+
};
174+
assert(message.len <= max_log_message);
175+
SDL_LogVerbose(@intFromEnum(category), message.ptr);
176+
}
177+
extern fn SDL_LogVerbose(category: c_int, fmt: [*:0]const u8, ...) void;
178+
179+
/// Log a message with SDL_LOG_PRIORITY_DEBUG.
180+
pub fn logDebug(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
181+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
182+
var buf: [max_log_message]u8 = undefined;
183+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
184+
SDL_LogError(.assert, "Log message too long!");
185+
};
186+
assert(message.len <= max_log_message);
187+
SDL_LogDebug(@intFromEnum(category), message.ptr);
188+
}
189+
extern fn SDL_LogDebug(category: c_int, fmt: [*:0]const u8, ...) void;
190+
191+
/// Log a message with SDL_LOG_PRIORITY_INFO.
192+
pub fn logInfo(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
193+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
194+
var buf: [max_log_message]u8 = undefined;
195+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
196+
SDL_LogError(.assert, "Log message too long!");
197+
};
198+
assert(message.len <= max_log_message);
199+
SDL_LogInfo(@intFromEnum(category), message.ptr);
200+
}
201+
extern fn SDL_LogInfo(category: c_int, fmt: [*:0]const u8, ...) void;
202+
203+
/// Log a message with SDL_LOG_PRIORITY_WARN.
204+
pub fn logWarn(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
205+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
206+
var buf: [max_log_message]u8 = undefined;
207+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
208+
SDL_LogError(.assert, "Log message too long!");
209+
};
210+
assert(message.len <= max_log_message);
211+
SDL_LogWarn(@intFromEnum(category), message.ptr);
212+
}
213+
extern fn SDL_LogWarn(category: c_int, fmt: [*:0]const u8, ...) void;
214+
215+
/// Log a message with SDL_LOG_PRIORITY_ERROR.
216+
pub fn logError(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
217+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
218+
var buf: [max_log_message]u8 = undefined;
219+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
220+
SDL_LogError(.assert, "Log message too long!");
221+
};
222+
assert(message.len <= max_log_message);
223+
SDL_LogError(@intFromEnum(category), message.ptr);
224+
}
225+
extern fn SDL_LogError(category: c_int, fmt: [*:0]const u8, ...) void;
226+
227+
/// Log a message with SDL_LOG_PRIORITY_CRITICAL.
228+
pub fn logCritical(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
229+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
230+
var buf: [max_log_message]u8 = undefined;
231+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
232+
SDL_LogError(.assert, "Log message too long!");
233+
};
234+
assert(message.len <= max_log_message);
235+
SDL_LogCritical(@intFromEnum(category), message.ptr);
236+
}
237+
extern fn SDL_LogCritical(category: c_int, fmt: [*:0]const u8, ...) void;
238+
239+
/// Log a message with the specified category and priority.
240+
pub fn logMessage(category: LogCategory, priority: LogPriority, comptime fmt: []const u8, args: anytype) void {
241+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
242+
var buf: [max_log_message]u8 = undefined;
243+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
244+
SDL_LogError(.assert, "Log message too long!");
245+
};
246+
assert(message.len <= max_log_message);
247+
SDL_LogMessage(@intFromEnum(category), priority, message.ptr);
248+
}
249+
extern fn SDL_LogMessage(category: c_int, priority: LogPriority, fmt: [*:0]const u8, ...) void;
250+
251+
/// The prototype for the log output callback function.
252+
pub const LogOutputFunction = *const fn (
253+
userdata: ?*anyopaque,
254+
category: c_int,
255+
priority: LogPriority,
256+
message: [*c]const u8,
257+
) callconv(.c) void;
258+
259+
/// Get the current log output function.
260+
pub fn logGetOutputFunction(callback: *LogOutputFunction, userdata: *?*anyopaque) void {
261+
SDL_LogGetOutputFunction(callback, userdata);
262+
}
263+
extern fn SDL_LogGetOutputFunction(callback: *LogOutputFunction, userdata: *?*anyopaque) void;
264+
265+
/// Replace the default log output function with one of your own.
266+
pub fn logSetOutputFunction(callback: LogOutputFunction, userdata: ?*anyopaque) void {
267+
SDL_LogSetOutputFunction(callback, userdata);
268+
}
269+
extern fn SDL_LogSetOutputFunction(callback: LogOutputFunction, userdata: ?*anyopaque) void;
97270

98271
//--------------------------------------------------------------------------------------------------
99272
//

0 commit comments

Comments
 (0)