Skip to content

Commit 6494e87

Browse files
D-Bergdoawoo
authored andcommitted
logger: use array concatenation instead of heap allocation
There is no need to allocate the formatted string. Also the string would live as long as the process did which is unnecessary.
1 parent 3372daa commit 6494e87

File tree

1 file changed

+6
-28
lines changed

1 file changed

+6
-28
lines changed

src/logger.zig

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,31 @@
11
const std = @import("std");
22

3-
var log = std.log;
4-
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
5-
var allocator = arena.allocator();
6-
73
pub fn query(comptime message: []const u8, args: anytype) void {
84
var stdout = std.io.getStdOut().writer();
9-
const out_string = std.fmt.allocPrint(allocator, message, args) catch {
10-
return;
11-
};
12-
stdout.print("[?] {s}", .{out_string}) catch {};
5+
stdout.print("[?] " ++ message, args) catch {};
136
}
147

158
pub fn log_stderr(comptime message: []const u8, args: anytype) void {
169
var stderr = std.io.getStdErr().writer();
17-
const out_string = std.fmt.allocPrint(allocator, message, args) catch {
18-
return;
19-
};
20-
stderr.print("[l] {s}\n", .{out_string}) catch {};
10+
stderr.print("[l] " ++ message ++ "\n", args) catch {};
2111
}
2212

2313
pub fn info(comptime message: []const u8, args: anytype) void {
2414
var stdout = std.io.getStdOut().writer();
25-
const out_string = std.fmt.allocPrint(allocator, message, args) catch {
26-
return;
27-
};
28-
stdout.print("[i] {s}\n", .{out_string}) catch {};
15+
stdout.print("[i] " ++ message ++ "\n", args) catch {};
2916
}
3017

3118
pub fn warn(comptime message: []const u8, args: anytype) void {
3219
var stderr = std.io.getStdErr().writer();
33-
const out_string = std.fmt.allocPrint(allocator, message, args) catch {
34-
return;
35-
};
36-
stderr.print("[w] {s}\n", .{out_string}) catch {};
20+
stderr.print("[w] " ++ message ++ "\n", args) catch {};
3721
}
3822

3923
pub fn err(comptime message: []const u8, args: anytype) void {
4024
var stderr = std.io.getStdErr().writer();
41-
const out_string = std.fmt.allocPrint(allocator, message, args) catch {
42-
return;
43-
};
44-
stderr.print("[!] {s}\n", .{out_string}) catch {};
25+
stderr.print("[!] " ++ message ++ "\n", args) catch {};
4526
}
4627

4728
pub fn crit(comptime message: []const u8, args: anytype) void {
4829
var stderr = std.io.getStdErr().writer();
49-
const out_string = std.fmt.allocPrint(allocator, message, args) catch {
50-
return;
51-
};
52-
stderr.print("[!!] {s}\n", .{out_string}) catch {};
30+
stderr.print("[!!] " ++ message ++ "\n", args) catch {};
5331
}

0 commit comments

Comments
 (0)