mirror of
https://github.com/fairyglade/ly.git
synced 2026-05-06 07:10:36 +00:00
Implement syslog functionality (closes #
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
@@ -3,50 +3,85 @@ const interop = @import("interop.zig");
|
|||||||
|
|
||||||
const LogFile = @This();
|
const LogFile = @This();
|
||||||
|
|
||||||
path: []const u8,
|
maybe_path: ?[]const u8,
|
||||||
could_open_log_file: bool = undefined,
|
could_open_log_file: bool = undefined,
|
||||||
file: std.Io.File = undefined,
|
maybe_file: ?std.Io.File = null,
|
||||||
buffer: []u8,
|
buffer: []u8,
|
||||||
file_writer: std.Io.File.Writer = undefined,
|
maybe_file_writer: ?std.Io.File.Writer = null,
|
||||||
|
|
||||||
|
pub fn init(io: std.Io, path: ?[]const u8, buffer: []u8) !LogFile {
|
||||||
|
var log_file = LogFile{
|
||||||
|
.maybe_path = path,
|
||||||
|
.buffer = buffer,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (path) |p| {
|
||||||
|
log_file.could_open_log_file = try openLogFile(io, p, &log_file);
|
||||||
|
} else {
|
||||||
|
std.posix.system.openlog("ly", 0, 0);
|
||||||
|
log_file.could_open_log_file = true;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn init(io: std.Io, path: []const u8, buffer: []u8) !LogFile {
|
|
||||||
var log_file = LogFile{ .path = path, .buffer = buffer };
|
|
||||||
log_file.could_open_log_file = try openLogFile(io, path, &log_file);
|
|
||||||
return log_file;
|
return log_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reinit(self: *LogFile, io: std.Io) !void {
|
pub fn reinit(self: *LogFile, io: std.Io) !void {
|
||||||
self.could_open_log_file = try openLogFile(io, self.path, self);
|
if (self.maybe_path) |path| {
|
||||||
|
self.could_open_log_file = try openLogFile(io, path, self);
|
||||||
|
} else {
|
||||||
|
std.posix.system.openlog("ly", 0, 0);
|
||||||
|
self.could_open_log_file = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *LogFile, io: std.Io) void {
|
pub fn deinit(self: *LogFile, io: std.Io) void {
|
||||||
self.file.close(io);
|
if (self.maybe_file) |file| {
|
||||||
|
file.close(io);
|
||||||
|
} else {
|
||||||
|
std.posix.system.closelog();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn info(self: *LogFile, io: std.Io, category: []const u8, comptime message: []const u8, args: anytype) !void {
|
pub fn info(self: *LogFile, io: std.Io, category: []const u8, comptime message: []const u8, args: anytype) !void {
|
||||||
var buffer: [128:0]u8 = undefined;
|
if (self.maybe_file_writer) |*writer| {
|
||||||
const time = interop.timeAsString(io, &buffer, "%Y-%m-%d %H:%M:%S");
|
var buffer: [128:0]u8 = undefined;
|
||||||
|
const time = interop.timeAsString(io, &buffer, "%Y-%m-%d %H:%M:%S");
|
||||||
|
|
||||||
try self.file_writer.interface.print("{s} [info/{s}] ", .{ time, category });
|
try writer.interface.print("{s} [info/{s}] ", .{ time, category });
|
||||||
try self.file_writer.interface.print(message, args);
|
try writer.interface.print(message, args);
|
||||||
try self.file_writer.interface.writeByte('\n');
|
try writer.interface.writeByte('\n');
|
||||||
try self.file_writer.interface.flush();
|
try writer.interface.flush();
|
||||||
|
} else {
|
||||||
|
var buffer: [1024]u8 = undefined;
|
||||||
|
const slice = try std.fmt.bufPrint(&buffer, message, args);
|
||||||
|
const msg = try std.fmt.bufPrintZ(buffer[slice.len..], "[info/{s}] {s}", .{ category, slice });
|
||||||
|
|
||||||
|
std.posix.system.syslog(std.posix.LOG.INFO, msg.ptr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn err(self: *LogFile, io: std.Io, category: []const u8, comptime message: []const u8, args: anytype) !void {
|
pub fn err(self: *LogFile, io: std.Io, category: []const u8, comptime message: []const u8, args: anytype) !void {
|
||||||
var buffer: [128:0]u8 = undefined;
|
if (self.maybe_file_writer) |*writer| {
|
||||||
const time = interop.timeAsString(io, &buffer, "%Y-%m-%d %H:%M:%S");
|
var buffer: [128:0]u8 = undefined;
|
||||||
|
const time = interop.timeAsString(io, &buffer, "%Y-%m-%d %H:%M:%S");
|
||||||
|
|
||||||
try self.file_writer.interface.print("{s} [err/{s}] ", .{ time, category });
|
try writer.interface.print("{s} [err/{s}] ", .{ time, category });
|
||||||
try self.file_writer.interface.print(message, args);
|
try writer.interface.print(message, args);
|
||||||
try self.file_writer.interface.writeByte('\n');
|
try writer.interface.writeByte('\n');
|
||||||
try self.file_writer.interface.flush();
|
try writer.interface.flush();
|
||||||
|
} else {
|
||||||
|
var buffer: [1024]u8 = undefined;
|
||||||
|
const slice = try std.fmt.bufPrint(&buffer, message, args);
|
||||||
|
const msg = try std.fmt.bufPrintZ(buffer[slice.len..], "[info/{s}] {s}", .{ category, slice });
|
||||||
|
|
||||||
|
std.posix.system.syslog(std.posix.LOG.ERR, msg.ptr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn openLogFile(io: std.Io, path: []const u8, log_file: *LogFile) !bool {
|
fn openLogFile(io: std.Io, path: []const u8, log_file: *LogFile) !bool {
|
||||||
var could_open_log_file = true;
|
var could_open_log_file = true;
|
||||||
open_log_file: {
|
open_log_file: {
|
||||||
log_file.file = std.Io.Dir.cwd().openFile(io, path, .{ .mode = .write_only }) catch std.Io.Dir.cwd().createFile(io, path, .{ .permissions = .fromMode(0o666) }) catch {
|
log_file.maybe_file = std.Io.Dir.cwd().openFile(io, path, .{ .mode = .write_only }) catch std.Io.Dir.cwd().createFile(io, path, .{ .permissions = .fromMode(0o666) }) catch {
|
||||||
// If we could neither open an existing log file nor create a new
|
// If we could neither open an existing log file nor create a new
|
||||||
// one, abort.
|
// one, abort.
|
||||||
could_open_log_file = false;
|
could_open_log_file = false;
|
||||||
@@ -55,17 +90,17 @@ fn openLogFile(io: std.Io, path: []const u8, log_file: *LogFile) !bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!could_open_log_file) {
|
if (!could_open_log_file) {
|
||||||
log_file.file = try std.Io.Dir.openFileAbsolute(io, "/dev/null", .{ .mode = .write_only });
|
log_file.maybe_file = try std.Io.Dir.openFileAbsolute(io, "/dev/null", .{ .mode = .write_only });
|
||||||
}
|
}
|
||||||
|
|
||||||
var log_file_writer = log_file.file.writer(io, log_file.buffer);
|
var log_file_writer = log_file.maybe_file.?.writer(io, log_file.buffer);
|
||||||
|
|
||||||
// Seek to the end of the log file
|
// Seek to the end of the log file
|
||||||
if (could_open_log_file) {
|
if (could_open_log_file) {
|
||||||
const stat = try log_file.file.stat(io);
|
const stat = try log_file.maybe_file.?.stat(io);
|
||||||
try log_file_writer.seekTo(stat.size);
|
try log_file_writer.seekTo(stat.size);
|
||||||
}
|
}
|
||||||
|
|
||||||
log_file.file_writer = log_file_writer;
|
log_file.maybe_file_writer = log_file_writer;
|
||||||
return could_open_log_file;
|
return could_open_log_file;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -299,6 +299,7 @@ login_defs_path = /etc/login.defs
|
|||||||
logout_cmd = null
|
logout_cmd = null
|
||||||
|
|
||||||
# General log file path
|
# General log file path
|
||||||
|
# If null, syslog will be used instead
|
||||||
ly_log = /var/log/ly.log
|
ly_log = /var/log/ly.log
|
||||||
|
|
||||||
# Main box horizontal margin
|
# Main box horizontal margin
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ lang: []const u8 = "en",
|
|||||||
login_cmd: ?[]const u8 = null,
|
login_cmd: ?[]const u8 = null,
|
||||||
login_defs_path: []const u8 = "/etc/login.defs",
|
login_defs_path: []const u8 = "/etc/login.defs",
|
||||||
logout_cmd: ?[]const u8 = null,
|
logout_cmd: ?[]const u8 = null,
|
||||||
ly_log: []const u8 = "/var/log/ly.log",
|
ly_log: ?[]const u8 = "/var/log/ly.log",
|
||||||
margin_box_h: u8 = 2,
|
margin_box_h: u8 = 2,
|
||||||
margin_box_v: u8 = 1,
|
margin_box_v: u8 = 1,
|
||||||
numlock: bool = false,
|
numlock: bool = false,
|
||||||
|
|||||||
Reference in New Issue
Block a user