From 271b1563a86efde89d23028a356515b0e6a16159 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 13 May 2021 14:25:34 +0200 Subject: [PATCH] river: make server a global variable --- river/main.zig | 95 +++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/river/main.zig b/river/main.zig index 20529b8..ce8c4dd 100644 --- a/river/main.zig +++ b/river/main.zig @@ -26,7 +26,7 @@ const util = @import("util.zig"); const Server = @import("Server.zig"); -const log_server = std.log.scoped(.server); +pub var server: Server = undefined; pub var level: std.log.Level = switch (std.builtin.mode) { .Debug => .debug, @@ -44,47 +44,6 @@ const usage: []const u8 = \\ ; -fn testConfigPath(comptime fmt: []const u8, args: anytype) std.fmt.AllocPrintError!?[:0]const u8 { - const path = try std.fmt.allocPrintZ(util.gpa, fmt, args); - os.access(path, os.X_OK) catch { - util.gpa.free(path); - return null; - }; - return path; -} - -fn getStartupCommand() std.fmt.AllocPrintError!?[:0]const u8 { - if (os.getenv("XDG_CONFIG_HOME")) |xdg_config_home| { - if (try testConfigPath("{}/river/init", .{xdg_config_home})) |path| { - return path; - } - } else if (os.getenv("HOME")) |home| { - if (try testConfigPath("{}/.config/river/init", .{home})) |path| { - return path; - } - } - if (try testConfigPath(build_options.default_config_path, .{})) |path| { - return path; - } - return null; -} - -pub fn log( - comptime message_level: std.log.Level, - comptime scope: @TypeOf(.foobar), - comptime format: []const u8, - args: anytype, -) void { - if (@enumToInt(message_level) <= @enumToInt(level)) { - // Don't store/log messages in release small mode to save space - if (std.builtin.mode != .ReleaseSmall) { - const stderr = std.io.getStdErr().writer(); - stderr.print(@tagName(message_level) ++ ": (" ++ @tagName(scope) ++ ") " ++ - format ++ "\n", args) catch return; - } - } -} - pub fn main() anyerror!void { var startup_command: ?[:0]const u8 = null; { @@ -131,8 +90,7 @@ pub fn main() anyerror!void { if (try getStartupCommand()) |path| startup_command = path; } - log_server.info("initializing server", .{}); - var server: Server = undefined; + std.log.info("initializing server", .{}); try server.init(); defer server.deinit(); @@ -141,7 +99,7 @@ pub fn main() anyerror!void { // Run the child in a new process group so that we can send SIGTERM to all // descendants on exit. const child_pgid = if (startup_command) |cmd| blk: { - log_server.info("running startup command '{}'", .{cmd}); + std.log.info("running startup command '{}'", .{cmd}); const child_args = [_:null]?[*:0]const u8{ "/bin/sh", "-c", cmd, null }; const pid = try os.fork(); if (pid == 0) { @@ -154,13 +112,13 @@ pub fn main() anyerror!void { break :blk pid; } else null; defer if (child_pgid) |pgid| - os.kill(-pgid, os.SIGTERM) catch |e| log_server.err("failed to kill startup process: {}", .{e}); + os.kill(-pgid, os.SIGTERM) catch |e| std.log.err("failed to kill startup process: {}", .{e}); - log_server.info("running server", .{}); + std.log.info("running server", .{}); server.wl_server.run(); - log_server.info("shutting down", .{}); + std.log.info("shutting down", .{}); } fn printErrorExit(comptime format: []const u8, args: anytype) noreturn { @@ -168,3 +126,44 @@ fn printErrorExit(comptime format: []const u8, args: anytype) noreturn { stderr.print(format ++ "\n", args) catch os.exit(1); os.exit(1); } + +fn testConfigPath(comptime fmt: []const u8, args: anytype) std.fmt.AllocPrintError!?[:0]const u8 { + const path = try std.fmt.allocPrintZ(util.gpa, fmt, args); + os.access(path, os.X_OK) catch { + util.gpa.free(path); + return null; + }; + return path; +} + +fn getStartupCommand() std.fmt.AllocPrintError!?[:0]const u8 { + if (os.getenv("XDG_CONFIG_HOME")) |xdg_config_home| { + if (try testConfigPath("{}/river/init", .{xdg_config_home})) |path| { + return path; + } + } else if (os.getenv("HOME")) |home| { + if (try testConfigPath("{}/.config/river/init", .{home})) |path| { + return path; + } + } + if (try testConfigPath(build_options.default_config_path, .{})) |path| { + return path; + } + return null; +} + +pub fn log( + comptime message_level: std.log.Level, + comptime scope: @TypeOf(.foobar), + comptime format: []const u8, + args: anytype, +) void { + if (@enumToInt(message_level) <= @enumToInt(level)) { + // Don't store/log messages in release small mode to save space + if (std.builtin.mode != .ReleaseSmall) { + const stderr = std.io.getStdErr().writer(); + stderr.print(@tagName(message_level) ++ ": (" ++ @tagName(scope) ++ ") " ++ + format ++ "\n", args) catch return; + } + } +}