mzte-mpv: clear line before logging

This commit is contained in:
LordMZTE 2024-08-06 20:48:58 +02:00
parent 4ca0521a5f
commit 43f2ac0901
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 23 additions and 9 deletions

View file

@ -1,7 +1,15 @@
const std = @import("std"); const std = @import("std");
const root = @import("root");
pub usingnamespace @import("delimited_writer.zig"); pub usingnamespace @import("delimited_writer.zig");
pub const Opts = struct {
log_pfx: ?[]const u8 = null,
log_clear_line: bool = false,
};
const opts: Opts = if (@hasDecl(root, "mztecommon_opts")) root.mztecommon_opts else .{};
var stderr_isatty: ?bool = null; var stderr_isatty: ?bool = null;
pub var log_file: ?std.fs.File = null; pub var log_file: ?std.fs.File = null;
@ -12,11 +20,6 @@ pub fn logFn(
comptime fmt: []const u8, comptime fmt: []const u8,
args: anytype, args: anytype,
) void { ) void {
const log_pfx: ?[]const u8 = if (@hasDecl(@import("root"), "mztecommon_log_pfx"))
@import("root").mztecommon_log_pfx
else
null;
const color = log_file == null and stderr_isatty orelse blk: { const color = log_file == null and stderr_isatty orelse blk: {
const isatty = std.posix.isatty(std.posix.STDERR_FILENO); const isatty = std.posix.isatty(std.posix.STDERR_FILENO);
stderr_isatty = isatty; stderr_isatty = isatty;
@ -25,7 +28,11 @@ pub fn logFn(
const logfile = log_file orelse std.io.getStdErr(); const logfile = log_file orelse std.io.getStdErr();
const scope_prefix = if (log_pfx) |lpfx| if (opts.log_clear_line) {
logfile.writer().writeAll("\x1b[2K\r") catch {};
}
const scope_prefix = if (opts.log_pfx) |lpfx|
if (scope != .default) if (scope != .default)
"[" ++ lpfx ++ " " ++ @tagName(scope) ++ "] " "[" ++ lpfx ++ " " ++ @tagName(scope) ++ "] "
else else

View file

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const common = @import("common");
const c = ffi.c; const c = ffi.c;
const ffi = @import("ffi.zig"); const ffi = @import("ffi.zig");
@ -6,10 +7,13 @@ const util = @import("util.zig");
pub const std_options = std.Options{ pub const std_options = std.Options{
.log_level = .debug, .log_level = .debug,
.logFn = @import("common").logFn, .logFn = common.logFn,
}; };
pub const mztecommon_log_pfx = "mzte-mpv"; pub const mztecommon_opts = common.Opts{
.log_pfx = "mzte-mpv",
.log_clear_line = true,
};
export fn mpv_open_cplugin(handle: *c.mpv_handle) callconv(.C) c_int { export fn mpv_open_cplugin(handle: *c.mpv_handle) callconv(.C) c_int {
tryMain(handle) catch |e| { tryMain(handle) catch |e| {

View file

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const common = @import("common");
const opts = @import("opts"); const opts = @import("opts");
const init = @import("init.zig").init; const init = @import("init.zig").init;
@ -11,7 +12,9 @@ pub const std_options = std.Options{
.logFn = @import("common").logFn, .logFn = @import("common").logFn,
}; };
pub const mztecommon_log_pfx = "mzteriver"; pub const mztecommon_opts = common.Opts{
.log_pfx = "mzteriver",
};
pub fn main() !void { pub fn main() !void {
var dbg_gpa = if (@import("builtin").mode == .Debug) std.heap.GeneralPurposeAllocator(.{}){} else {}; var dbg_gpa = if (@import("builtin").mode == .Debug) std.heap.GeneralPurposeAllocator(.{}){} else {};