Skip to content

Commit 1879652

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

File tree

1 file changed

+158
-1
lines changed

1 file changed

+158
-1
lines changed

src/sdl2.zig

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,164 @@ 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 "Log message too long!";
160+
assert(message.len <= max_log_message);
161+
SDL_Log(message.ptr);
162+
}
163+
extern fn SDL_Log(fmt: [*:0]const u8, ...) void;
164+
165+
/// Log a message with SDL_LOG_PRIORITY_VERBOSE.
166+
pub fn logVerbose(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
167+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
168+
var buf: [max_log_message]u8 = undefined;
169+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch "Log message too long!";
170+
assert(message.len <= max_log_message);
171+
SDL_LogVerbose(@intFromEnum(category), message.ptr);
172+
}
173+
extern fn SDL_LogVerbose(category: c_int, fmt: [*:0]const u8, ...) void;
174+
175+
/// Log a message with SDL_LOG_PRIORITY_DEBUG.
176+
pub fn logDebug(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
177+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
178+
var buf: [max_log_message]u8 = undefined;
179+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch "Log message too long!";
180+
assert(message.len <= max_log_message);
181+
SDL_LogDebug(@intFromEnum(category), message.ptr);
182+
}
183+
extern fn SDL_LogDebug(category: c_int, fmt: [*:0]const u8, ...) void;
184+
185+
/// Log a message with SDL_LOG_PRIORITY_INFO.
186+
pub fn logInfo(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
187+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
188+
var buf: [max_log_message]u8 = undefined;
189+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch "Log message too long!";
190+
assert(message.len <= max_log_message);
191+
SDL_LogInfo(@intFromEnum(category), message.ptr);
192+
}
193+
extern fn SDL_LogInfo(category: c_int, fmt: [*:0]const u8, ...) void;
194+
195+
/// Log a message with SDL_LOG_PRIORITY_WARN.
196+
pub fn logWarn(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
197+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
198+
var buf: [max_log_message]u8 = undefined;
199+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch "Log message too long!";
200+
assert(message.len <= max_log_message);
201+
SDL_LogWarn(@intFromEnum(category), message.ptr);
202+
}
203+
extern fn SDL_LogWarn(category: c_int, fmt: [*:0]const u8, ...) void;
204+
205+
/// Log a message with SDL_LOG_PRIORITY_ERROR.
206+
pub fn logError(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
207+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
208+
var buf: [max_log_message]u8 = undefined;
209+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch "Log message too long!";
210+
assert(message.len <= max_log_message);
211+
SDL_LogError(@intFromEnum(category), message.ptr);
212+
}
213+
extern fn SDL_LogError(category: c_int, fmt: [*:0]const u8, ...) void;
214+
215+
/// Log a message with SDL_LOG_PRIORITY_CRITICAL.
216+
pub fn logCritical(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 "Log message too long!";
220+
assert(message.len <= max_log_message);
221+
SDL_LogCritical(@intFromEnum(category), message.ptr);
222+
}
223+
extern fn SDL_LogCritical(category: c_int, fmt: [*:0]const u8, ...) void;
224+
225+
/// Log a message with the specified category and priority.
226+
pub fn logMessage(category: LogCategory, priority: LogPriority, comptime fmt: []const u8, args: anytype) void {
227+
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
228+
var buf: [max_log_message]u8 = undefined;
229+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch "Log message too long!";
230+
assert(message.len <= max_log_message);
231+
SDL_LogMessage(@intFromEnum(category), priority, message.ptr);
232+
}
233+
extern fn SDL_LogMessage(category: c_int, priority: LogPriority, fmt: [*:0]const u8, ...) void;
234+
235+
/// The prototype for the log output callback function.
236+
pub const LogOutputFunction = *const fn (
237+
userdata: ?*anyopaque,
238+
category: c_int,
239+
priority: LogPriority,
240+
message: [*c]const u8,
241+
) callconv(.c) void;
242+
243+
/// Get the current log output function.
244+
pub fn logGetOutputFunction(callback: *LogOutputFunction, userdata: *?*anyopaque) void {
245+
SDL_LogGetOutputFunction(callback, userdata);
246+
}
247+
extern fn SDL_LogGetOutputFunction(callback: *LogOutputFunction, userdata: *?*anyopaque) void;
248+
249+
/// Replace the default log output function with one of your own.
250+
pub fn logSetOutputFunction(callback: LogOutputFunction, userdata: ?*anyopaque) void {
251+
SDL_LogSetOutputFunction(callback, userdata);
252+
}
253+
extern fn SDL_LogSetOutputFunction(callback: LogOutputFunction, userdata: ?*anyopaque) void;
97254

98255
//--------------------------------------------------------------------------------------------------
99256
//

0 commit comments

Comments
 (0)