From 1e8286b8fc432cec8ad6a4441b23573f06d575a6 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Tue, 20 Feb 2024 15:39:32 +0100 Subject: [PATCH] mzteriver: make river log to file --- scripts/mzteriver/src/init.zig | 8 +++-- scripts/mzteriver/src/main.zig | 58 ++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/scripts/mzteriver/src/init.zig b/scripts/mzteriver/src/init.zig index a741ade..53c1b18 100644 --- a/scripts/mzteriver/src/init.zig +++ b/scripts/mzteriver/src/init.zig @@ -1,6 +1,8 @@ const std = @import("std"); const opts = @import("opts"); +const log = std.log.scoped(.mzteriver); + const Connection = @import("Connection.zig"); pub fn init(alloc: std.mem.Allocator) !void { @@ -162,18 +164,18 @@ pub fn init(alloc: std.mem.Allocator) !void { var init_child = std.process.Child.init(&.{init_path}, alloc); const term = init_child.spawnAndWait() catch |e| switch (e) { error.FileNotFound => b: { - std.log.info("no river_init", .{}); + log.info("no river_init", .{}); break :b std.process.Child.Term{ .Exited = 0 }; }, else => return e, }; if (!std.meta.eql(term, .{ .Exited = 0 })) { - std.log.err("river_init borked: {}", .{term}); + log.err("river_init borked: {}", .{term}); return error.InitBorked; } - std.log.info("configuration finished, spawning processes", .{}); + log.info("configuration finished, spawning processes", .{}); var child_arena = std.heap.ArenaAllocator.init(alloc); defer child_arena.deinit(); diff --git a/scripts/mzteriver/src/main.zig b/scripts/mzteriver/src/main.zig index 6bf2297..2a745e8 100644 --- a/scripts/mzteriver/src/main.zig +++ b/scripts/mzteriver/src/main.zig @@ -1,6 +1,8 @@ const std = @import("std"); const opts = @import("opts"); +const log = std.log.scoped(.mzteriver); + pub const std_options = std.Options{ .log_level = switch (@import("builtin").mode) { .Debug => .debug, @@ -9,22 +11,53 @@ pub const std_options = std.Options{ }; pub fn main() !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - defer _ = gpa.deinit(); - const alloc = gpa.allocator(); + var dbg_gpa = if (@import("builtin").mode == .Debug) std.heap.GeneralPurposeAllocator(.{}){} else {}; + defer if (@TypeOf(dbg_gpa) != void) { + _ = dbg_gpa.deinit(); + }; + const alloc = if (@TypeOf(dbg_gpa) == void) std.heap.c_allocator else dbg_gpa.allocator(); if (std.mem.endsWith(u8, std.mem.span(std.os.argv[0]), "init") or (std.os.argv.len >= 2 and std.mem.orderZ(u8, std.os.argv[1], "init") == .eq)) { - std.log.info("running in init mode", .{}); + log.info("running in init mode", .{}); try @import("init.zig").init(alloc); } else { - std.log.info("running in launch mode", .{}); + log.info("running in launch mode", .{}); - const envp = env: { - var env = try std.ArrayList(?[*:0]const u8) - .initCapacity(alloc, std.os.environ.len + 16); - errdefer env.deinit(); + const logfd = logf: { + const logf_path = try std.fmt.allocPrintZ( + alloc, + "/tmp/mzteriver-{}-{}.log", + .{ std.os.linux.getuid(), std.os.linux.getpid() }, + ); + defer alloc.free(logf_path); + + log.info("river log file: {s}", .{logf_path}); + + break :logf try std.os.openatZ( + std.os.AT.FDCWD, + logf_path.ptr, + // no CLOEXEC + .{ + .ACCMODE = .WRONLY, + .CREAT = true, + .TRUNC = true, + }, + 0o644, + ); + }; + { + errdefer std.os.close(logfd); + + // redirect river's STDERR and STDOUT to log file + try std.os.dup2(logfd, std.os.STDOUT_FILENO); + try std.os.dup2(logfd, std.os.STDERR_FILENO); + } + std.os.close(logfd); + + var env = std.BoundedArray(?[*:0]const u8, 0xff).init(0) catch unreachable; + const envp: [*:null]?[*:0]const u8 = env: { try env.appendSlice(std.os.environ); try env.append("XKB_DEFAULT_LAYOUT=de"); @@ -35,12 +68,11 @@ pub fn main() !void { try env.append("WLR_NO_HARDWARE_CURSORS=1"); } - break :env try env.toOwnedSliceSentinel(null); + // manually add sentinel + try env.append(null); + break :env @ptrCast(env.slice().ptr); }; - // techncially unreachable - defer alloc.free(envp); - return std.os.execvpeZ("river", &[_:null]?[*:0]const u8{"river"}, envp); } }