const std = @import("std"); const runtime = @import("runtime.zig"); const env = @import("utilities/env.zig"); const terminal = @import("terminal.zig"); const time_util = @import("utilities/time.zig"); pub const Level = enum(u8) { debug = 10, info = 20, warning = 30, err = 40, critical = 50, pub fn fromString(s: []const u8) Level { if (std.ascii.eqlIgnoreCase(s, "DEBUG")) return .debug; if (std.ascii.eqlIgnoreCase(s, "INFO")) return .info; if (std.ascii.eqlIgnoreCase(s, "WARNING") or std.ascii.eqlIgnoreCase(s, "WARN")) return .warning; if (std.ascii.eqlIgnoreCase(s, "ERROR")) return .err; if (std.ascii.eqlIgnoreCase(s, "CRITICAL")) return .critical; return .warning; } pub fn name(self: Level) []const u8 { return switch (self) { .debug => "DEBUG", .info => "INFO", .warning => "WARNING", .err => "ERROR", .critical => "CRITICAL", }; } }; var server_level: Level = .warning; var output_mutex: std.Io.Mutex = .init; var display: ?terminal.Display = null; var animation: ?std.Thread = null; var debug_ready = false; var color_enabled = false; pub const Stage = terminal.Stage; pub fn startupBegin() void { if (server_level == .debug) { info("server", "starting", .{}); return; } const io = runtime.appIo(); output_mutex.lockUncancelable(io); display = terminal.Display.init(io, server_level == .debug); display.?.begin(); const animate = display.?.redraw; output_mutex.unlock(io); if (animate) animation = std.Thread.spawn(.{}, animatePresentation, .{}) catch null; } pub fn shutdownBegin() void { const io = runtime.appIo(); output_mutex.lockUncancelable(io); const begin = if (display) |d| d.was_ready and !d.shutdown else false; output_mutex.unlock(io); if (!begin) return; if (animation) |thread| thread.join(); animation = null; output_mutex.lockUncancelable(io); display = terminal.Display.init(io, server_level == .debug); display.?.shutdown = true; display.?.begin(); display.?.stage(.api, "draining connections"); const animate = display.?.redraw; output_mutex.unlock(io); if (animate) animation = std.Thread.spawn(.{}, animatePresentation, .{}) catch null; } pub fn shutdownProgress(stage: Stage, detail: []const u8, complete: bool) void { const io = runtime.appIo(); output_mutex.lockUncancelable(io); defer output_mutex.unlock(io); if (display) |*d| { if (!d.shutdown) return; if (complete) d.done(stage, detail) else d.stage(stage, detail); } } pub fn shutdownComponent(name: []const u8, complete: bool) void { const io = runtime.appIo(); output_mutex.lockUncancelable(io); defer output_mutex.unlock(io); if (display) |*d| { if (d.shutdown) d.component(name, complete); } } fn animatePresentation() void { const io = runtime.appIo(); while (true) { std.Io.sleep(io, .fromMilliseconds(100), .awake) catch return; output_mutex.lockUncancelable(io); const active = if (display) |*d| blk: { if (d.active) d.render(); break :blk d.active and d.redraw; } else false; output_mutex.unlock(io); if (!active) return; } } pub fn finishPresentation() void { if (server_level == .debug) { if (debug_ready) info("server", "prefect-server - stopped", .{}); return; } const io = runtime.appIo(); output_mutex.lockUncancelable(io); if (display) |*d| { if (d.active) { if (d.shutdown) d.stopped() else d.fail(error.StartupInterrupted); } } output_mutex.unlock(io); if (animation) |thread| thread.join(); animation = null; } pub fn startupStage(stage: Stage, detail: []const u8) void { const io = runtime.appIo(); output_mutex.lockUncancelable(io); defer output_mutex.unlock(io); if (display) |*d| d.stage(stage, detail); } pub fn startupDone(stage: Stage, detail: []const u8) void { const io = runtime.appIo(); output_mutex.lockUncancelable(io); defer output_mutex.unlock(io); if (display) |*d| d.done(stage, detail); } pub fn startupComponent(name: []const u8) void { updateComponent(name, false); } pub fn startupComponentDone(name: []const u8) void { updateComponent(name, true); } fn updateComponent(name: []const u8, complete: bool) void { const io = runtime.appIo(); output_mutex.lockUncancelable(io); defer output_mutex.unlock(io); if (display) |*d| d.component(name, complete); } pub fn startupFail(failure: anyerror) void { if (server_level == .debug) { err("server", "startup failed: {}", .{failure}); return; } const io = runtime.appIo(); output_mutex.lockUncancelable(io); defer output_mutex.unlock(io); if (display) |*d| d.fail(failure); } pub fn startupReady(label: []const u8, host: []const u8, port: u16) void { if (server_level == .debug) { var buf: [512]u8 = undefined; info("server", "prefect-server - ready: {s}", .{terminal.browserAddress(&buf, label, host, port)}); debug_ready = true; return; } const io = runtime.appIo(); output_mutex.lockUncancelable(io); defer output_mutex.unlock(io); if (display) |*d| d.ready(label, host, port); } pub fn stdLog(comptime level: std.log.Level, comptime scope: @EnumLiteral(), comptime fmt: []const u8, args: anytype) void { log(switch (level) { .debug => .debug, .info => .info, .warn => .warning, .err => .err, }, @tagName(scope), fmt, args); } pub fn init() void { color_enabled = terminal.supportsColor(runtime.appIo()); if (env.getenv("PREFECT_SERVER_LOGGING_LEVEL")) |level_str| { server_level = Level.fromString(level_str); } else if (env.getenv("PREFECT_LOGGING_LEVEL")) |level_str| { server_level = Level.fromString(level_str); } } pub fn setLevel(level: Level) void { server_level = level; } pub fn getLevel() Level { return server_level; } fn getTimestamp(buf: *[12]u8) []const u8 { const ms_total = time_util.milliTimestamp(runtime.appIo()); const ms: u64 = @intCast(@mod(ms_total, 1000)); const epoch_secs: u64 = @intCast(@divFloor(ms_total, 1000)); const secs_today = epoch_secs % 86400; const hours = secs_today / 3600; const mins = (secs_today % 3600) / 60; const secs = secs_today % 60; return std.fmt.bufPrint(buf, "{d:0>2}:{d:0>2}:{d:0>2}.{d:0>3}", .{ hours, mins, secs, ms, }) catch "00:00:00.000"; } fn writeStderr(data: []const u8) void { std.debug.print("{s}", .{data}); } pub fn log(level: Level, component: []const u8, comptime fmt: []const u8, args: anytype) void { if (@backingInt(level) < @backingInt(server_level)) return; const io = runtime.appIo(); output_mutex.lockUncancelable(io); defer output_mutex.unlock(io); if (display) |*d| { if (d.active and server_level != .debug and @backingInt(level) < @backingInt(Level.warning)) return; if (d.active and @backingInt(level) >= @backingInt(Level.err)) d.had_error = true; d.clear(); } defer if (display) |*d| { if (d.active) d.render(); }; var ts_buf: [12]u8 = undefined; const ts = getTimestamp(&ts_buf); const color = color_enabled; const tint = if (!color) "" else switch (level) { .warning => "\x1b[1;33m", .err, .critical => "\x1b[1;35m", .info => "", .debug => "\x1b[36m", }; var buf: [4096]u8 = undefined; const len = (std.fmt.bufPrint(&buf, "{s} | {s}{s: <8}{s} | {s} - " ++ fmt ++ "\n", .{ ts, tint, level.name(), if (color) "\x1b[0m" else "", component, } ++ args) catch return).len; writeStderr(buf[0..len]); } pub fn debug(component: []const u8, comptime fmt: []const u8, args: anytype) void { log(.debug, component, fmt, args); } pub fn info(component: []const u8, comptime fmt: []const u8, args: anytype) void { log(.info, component, fmt, args); } pub fn warn(component: []const u8, comptime fmt: []const u8, args: anytype) void { log(.warning, component, fmt, args); } pub fn err(component: []const u8, comptime fmt: []const u8, args: anytype) void { log(.err, component, fmt, args); } pub fn critical(component: []const u8, comptime fmt: []const u8, args: anytype) void { log(.critical, component, fmt, args); }