// SPDX-FileCopyrightText: © 2024 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! Logs a known set of entries to whatever journal is actually running, so //! that the NixOS check in `tests/nixos` can read them back out of //! `journalctl` and see that systemd-journald accepted and indexed them. //! //! It writes nothing to stdout: everything it has to say, it says through the //! journal, which is the point. const std = @import("std"); const journalz = @import("journalz"); /// What the test script greps for. const identifier = "journalz-e2e"; pub fn main() !void { var debug_allocator: std.heap.DebugAllocator(.{}) = .init; defer _ = debug_allocator.deinit(); const gpa = debug_allocator.allocator(); var threaded: std.Io.Threaded = .init(gpa, .{}); defer threaded.deinit(); const io = threaded.io(); const logger: journalz.Logger = try .init(io, identifier, .LOCAL3); defer logger.deinit(); // The convenience methods, one per priority, so the test can check that // each lands at the severity it claims. logger.err("an error from journalz", .{}); logger.warn("a warning from journalz", .{}); logger.info("an informational note from journalz", .{}); logger.debug("a debug note from journalz", .{}); // Formatted arguments. logger.info("listening on port {d} as {s}", .{ 8080, identifier }); // A structured entry with fields of its own. journald indexes these, so // `journalctl ANSWER=42` finds this entry and nothing else. { const msg = try logger.message(.NOTICE); errdefer msg.cancel(); var buf: [512]u8 = undefined; var w = msg.writer(&buf); try w.writeAll("MESSAGE", "a structured entry"); try w.print("ANSWER", "{d}", .{42}); try w.writeAll("UNIT_UNDER_TEST", "journalz"); try w.writeSrc(@src()); try w.flush(); try msg.send(); } // A value with embedded newlines. This is the reason the native protocol // sends a length rather than a terminator, and journald has to give it // back whole. { const msg = try logger.message(.INFO); errdefer msg.cancel(); var buf: [512]u8 = undefined; var w = msg.writer(&buf); try w.writeAll("MESSAGE", "first line\nsecond line\nthird line"); try w.writeAll("MULTILINE", "yes"); try w.flush(); try msg.send(); } // A payload far larger than a datagram would carry. Passing a sealed // memfd rather than bytes in the datagram is what makes this work at all, // so it is worth proving against the real journald. { const big = try gpa.alloc(u8, 256 * 1024); defer gpa.free(big); @memset(big, 'x'); const msg = try logger.message(.INFO); errdefer msg.cancel(); var buf: [4096]u8 = undefined; var w = msg.writer(&buf); try w.writeAll("MESSAGE", "a large entry"); try w.writeAll("BIG", big); try w.print("BIG_LEN", "{d}", .{big.len}); try w.flush(); try msg.send(); } }