Something went wrong. Try again.
A Zig library for writing structured log messages directly to the systemd journal over its native datagram socket.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133// SPDX-FileCopyrightText: © 2024 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
const Logger = @This();
const std = @import("std");const Allocator = std.mem.Allocator;const assert = std.debug.assert;const errno = std.os.linux.errno;
const Message = @import("Message.zig");const constants = @import("constants.zig");const Facility = constants.Facility;const Priority = constants.Priority;
const linux = std.os.linux;const AF = linux.AF;const SOCK = linux.SOCK;const socket_t = linux.socket_t;const sockaddr = linux.sockaddr;
const JOURNAL_SOCKET: [:0]const u8 = "/run/systemd/journal/socket";
io: std.Io,identifier: ?[]const u8,facility: ?Facility,socket: socket_t,path: sockaddr.un = undefined,
/// Open a logger onto the system journal at its well known socket,/// `/run/systemd/journal/socket`.pub fn init(io: std.Io, identifier: ?[]const u8, facility: ?Facility) error{Failed}!Logger { return initPath(io, identifier, facility, JOURNAL_SOCKET) catch |e| switch (e) { // The one path this can be called with is a compile time constant // that comfortably fits, so the length error is unreachable here. error.PathTooLong => unreachable, error.Failed => error.Failed, };}
/// Open a logger onto a journal socket at `path`, for a journal that is not/// the calling process' own: a container's socket bind mounted into view, or/// a fake journal in a test.////// `path` must be shorter than the 108 byte `sun_path` of a Unix domain/// socket address, which it is by a wide margin for the sockets systemd/// creates.pub fn initPath( io: std.Io, identifier: ?[]const u8, facility: ?Facility, path: []const u8,) error{ Failed, PathTooLong }!Logger { var logger: Logger = .{ .io = io, .identifier = identifier, .facility = facility, .socket = undefined, };
// One byte is spent on the terminator, so the path itself has to be // strictly shorter than the field. Checked before the socket is created // so that the error path has nothing to clean up. if (path.len >= logger.path.path.len) return error.PathTooLong;
logger.socket = socket: { const rc = linux.socket( AF.UNIX, SOCK.DGRAM | SOCK.CLOEXEC, 0, ); switch (errno(rc)) { .SUCCESS => break :socket @intCast(rc), else => return error.Failed, } }; errdefer _ = linux.close(logger.socket);
logger.path.family = AF.UNIX; @memcpy(logger.path.path[0..path.len], path); logger.path.path[path.len] = 0;
return logger;}
pub fn deinit(self: *const Logger) void { _ = linux.close(self.socket);}
pub fn message(self: *const Logger, priority: Priority) error{ MemFDCreationFailed, WriteFailed }!Message { const msg: Message = try .init(self); errdefer msg.cancel();
var buf: [1024]u8 = undefined; var writer = msg.writer(&buf);
if (self.identifier) |i| try writer.writeSyslogIdentifier(i); if (self.facility) |f| try writer.writeFacility(f); try writer.writePriority(priority);
try writer.flush();
return msg;}
fn _msg(self: *const Logger, priority: Priority, comptime fmt: []const u8, args: anytype) error{ MemFDCreationFailed, WriteFailed, SealFailed, SendFailed }!void { var buf: [4096]u8 = undefined;
const msg = try self.message(priority); errdefer msg.cancel(); var writer = msg.writer(&buf); try writer.print("MESSAGE", fmt, args); try writer.flush(); try msg.send();}
pub fn err(self: *const Logger, comptime fmt: []const u8, args: anytype) void { self._msg(.ERROR, fmt, args) catch {};}
pub fn warn(self: *const Logger, comptime fmt: []const u8, args: anytype) void { self._msg(.WARNING, fmt, args) catch {};}
pub fn info(self: *const Logger, comptime fmt: []const u8, args: anytype) void { self._msg(.INFO, fmt, args) catch {};}
pub fn debug(self: *const Logger, comptime fmt: []const u8, args: anytype) void { self._msg(.DEBUG, fmt, args) catch {};}