Add a log utility

This commit is contained in:
Isaac Freund 2020-03-29 21:05:34 +02:00
parent d0bfa7bb01
commit 5e84d69237
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
2 changed files with 30 additions and 2 deletions

24
src/log.zig Normal file
View file

@ -0,0 +1,24 @@
const std = @import("std");
pub const Log = enum {
const Self = @This();
Silent,
Error,
Info,
Debug,
var verbosity = Self.Error;
pub fn init(_verbosity: Self) void {
verbosity = _verbosity;
}
fn log(level: Self, comptime format: []const u8, args: var) void {
if (@enumToInt(level) <= @enumToInt(verbosity)) {
// TODO: log the time since start in the same format as wlroots
// TODO: use color if logging to a tty
std.debug.warn("[{}] " ++ format ++ "\n", .{@tagName(level)} ++ args);
}
}
};

View file

@ -1,12 +1,14 @@
const std = @import("std");
const c = @import("c.zig");
const Log = @import("log.zig").Log;
const Server = @import("server.zig").Server;
pub fn main() !void {
std.debug.warn("Starting up.\n", .{});
Log.init(Log.Debug);
c.wlr_log_init(c.enum_wlr_log_importance.WLR_ERROR, null);
c.wlr_log_init(c.enum_wlr_log_importance.WLR_DEBUG, null);
Log.Info.log("Initializing server", .{});
var server: Server = undefined;
try server.init(std.heap.c_allocator);
@ -14,5 +16,7 @@ pub fn main() !void {
try server.start();
Log.Info.log("Running server...", .{});
server.run();
}