From 5e84d69237d731149a15d673ef1069fb892d4725 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sun, 29 Mar 2020 21:05:34 +0200 Subject: [PATCH] Add a log utility --- src/log.zig | 24 ++++++++++++++++++++++++ src/main.zig | 8 ++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/log.zig diff --git a/src/log.zig b/src/log.zig new file mode 100644 index 0000000..4d7aae9 --- /dev/null +++ b/src/log.zig @@ -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); + } + } +}; diff --git a/src/main.zig b/src/main.zig index 2bbaf8e..ec9d5ca 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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(); }