Skip to content

Commit 9927935

Browse files
committed
sdl2.zig better buf print error handling
1 parent 22e5df7 commit 9927935

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

src/sdl2.zig

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ extern fn SDL_LogResetPriorities() void;
156156
pub fn log(comptime fmt: []const u8, args: anytype) void {
157157
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
158158
var buf: [max_log_message]u8 = undefined;
159-
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
160-
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
161-
return;
159+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
160+
std.fmt.BufPrintError.NoSpaceLeft => {
161+
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
162+
return;
163+
},
162164
};
163165
SDL_Log(message.ptr);
164166
}
@@ -168,9 +170,11 @@ extern fn SDL_Log(fmt: [*:0]const u8, ...) void;
168170
pub fn logVerbose(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
169171
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
170172
var buf: [max_log_message]u8 = undefined;
171-
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
172-
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
173-
return;
173+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
174+
std.fmt.BufPrintError.NoSpaceLeft => {
175+
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
176+
return;
177+
},
174178
};
175179
SDL_LogVerbose(@intFromEnum(category), message.ptr);
176180
}
@@ -180,9 +184,11 @@ extern fn SDL_LogVerbose(category: c_int, fmt: [*:0]const u8, ...) void;
180184
pub fn logDebug(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
181185
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
182186
var buf: [max_log_message]u8 = undefined;
183-
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
184-
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
185-
return;
187+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
188+
std.fmt.BufPrintError.NoSpaceLeft => {
189+
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
190+
return;
191+
},
186192
};
187193
SDL_LogDebug(@intFromEnum(category), message.ptr);
188194
}
@@ -192,9 +198,11 @@ extern fn SDL_LogDebug(category: c_int, fmt: [*:0]const u8, ...) void;
192198
pub fn logInfo(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
193199
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
194200
var buf: [max_log_message]u8 = undefined;
195-
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
196-
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
197-
return;
201+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
202+
std.fmt.BufPrintError.NoSpaceLeft => {
203+
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
204+
return;
205+
},
198206
};
199207
SDL_LogInfo(@intFromEnum(category), message.ptr);
200208
}
@@ -204,9 +212,11 @@ extern fn SDL_LogInfo(category: c_int, fmt: [*:0]const u8, ...) void;
204212
pub fn logWarn(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
205213
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
206214
var buf: [max_log_message]u8 = undefined;
207-
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
208-
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
209-
return;
215+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
216+
std.fmt.BufPrintError.NoSpaceLeft => {
217+
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
218+
return;
219+
},
210220
};
211221
SDL_LogWarn(@intFromEnum(category), message.ptr);
212222
}
@@ -216,9 +226,11 @@ extern fn SDL_LogWarn(category: c_int, fmt: [*:0]const u8, ...) void;
216226
pub fn logError(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
217227
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
218228
var buf: [max_log_message]u8 = undefined;
219-
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
220-
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
221-
return;
229+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
230+
std.fmt.BufPrintError.NoSpaceLeft => {
231+
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
232+
return;
233+
},
222234
};
223235
SDL_LogError(@intFromEnum(category), message.ptr);
224236
}
@@ -228,9 +240,11 @@ extern fn SDL_LogError(category: c_int, fmt: [*:0]const u8, ...) void;
228240
pub fn logCritical(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
229241
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
230242
var buf: [max_log_message]u8 = undefined;
231-
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
232-
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
233-
return;
243+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
244+
std.fmt.BufPrintError.NoSpaceLeft => {
245+
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
246+
return;
247+
},
234248
};
235249
SDL_LogCritical(@intFromEnum(category), message.ptr);
236250
}
@@ -240,9 +254,11 @@ extern fn SDL_LogCritical(category: c_int, fmt: [*:0]const u8, ...) void;
240254
pub fn logMessage(category: LogCategory, priority: LogPriority, comptime fmt: []const u8, args: anytype) void {
241255
assert(fmt.len > 0 and fmt.len < max_log_message - 1);
242256
var buf: [max_log_message]u8 = undefined;
243-
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch {
244-
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
245-
return;
257+
const message = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
258+
std.fmt.BufPrintError.NoSpaceLeft => {
259+
SDL_LogError(@intFromEnum(LogCategory.assert), "Log message too long!");
260+
return;
261+
},
246262
};
247263
SDL_LogMessage(@intFromEnum(category), priority, message.ptr);
248264
}

0 commit comments

Comments
 (0)