refactor(wezterm): add LogOpts for logging lib

This commit is contained in:
Price Hiller 2023-08-29 23:23:43 -05:00
parent 376e581ba5
commit f19671abbd
Signed by: Price
SSH Key Fingerprint: SHA256:Y4S9ZzYphRn1W1kbJerJFO6GGsfu9O70VaBSxJO7dF8

View File

@ -1,53 +1,97 @@
local wezterm = require("wezterm") local wezterm = require("wezterm")
local wlib = require("lib.wlib")
local os_detected = require("lib.wlib").get_os_arch().name:lower() local os_detected = require("lib.wlib").get_os_arch().name:lower()
local M = {} local M = {}
---comment ---@class LogOpts
---@param message string ---@field level string?
---@param log_level string
---| "info" ---| "info"
---| "debug" ---| "debug"
---| "warning" ---| "warning"
---| "err" ---| "err"
local function system_log(message, log_level) ---@field prefix string?
---@field ignore_result boolean?
---Log to systemd log
---@param message string
---@param opts LogOpts
local function system_log(message, opts)
local log_unit = "wezterm" local log_unit = "wezterm"
if os_detected == "linux" then if os_detected == "linux" then
local handle = io.popen(string.format("systemd-cat -t %s -p %s echo '%s'", log_unit, log_level:lower(), message)) local handle = io.popen(string.format("systemd-cat -t %s -p %s echo '%s'", log_unit, opts.level:lower(), message))
---@diagnostic disable-next-line: need-check-nil if not opts.ignore_result or opts.ignore_result == nil then
local output = handle:read("*a") ---@diagnostic disable-next-line: need-check-nil
local output = handle:read("*a")
if output == nil then if output == nil then
output = "Handle did not return any content!" output = "Handle did not return any content!"
end end
---@diagnostic disable-next-line: need-check-nil ---@diagnostic disable-next-line: need-check-nil
local success = handle:close() local success = handle:close()
if success == nil or not success then if success == nil or not success then
wezterm.log_warn( wezterm.log_warn(
string.format("'systemd-cat' did not indicate a successful run!\nHandle Output:\n%s", output) string.format("'systemd-cat' did not indicate a successful run!\nHandle Output:\n%s", output)
) )
end
end end
end end
end end
function M.debug(message)
system_log(message, "debug") ---Format message with log options
---@param message string
---@param opts LogOpts
local function format_message(message, opts)
local formatted_str = message
if opts.prefix then
formatted_str = string.format("%s: %s", opts.prefix, message)
end
return formatted_str
end end
function M.info(message) ---Log a debug message
---@param message string
---@param opts LogOpts?
function M.debug(message, opts)
opts = opts or {}
message = format_message(message, opts)
local merged_opts = wlib.Table.merge(opts, { level = "debug" })
system_log(message, merged_opts)
end
---Log a info message
---@param message string
---@param opts LogOpts?
function M.info(message, opts)
opts = opts or {}
message = format_message(message, opts)
wezterm.log_info(message) wezterm.log_info(message)
system_log(message, "info") local merged_opts = wlib.Table.merge(opts, { level = "info" })
system_log(message, merged_opts)
end end
function M.warn(message) ---Log a warning message
---@param message string
---@param opts LogOpts?
function M.warn(message, opts)
opts = opts or {}
message = format_message(message, opts)
wezterm.log_warn(message) wezterm.log_warn(message)
system_log(message, "warning") local merged_opts = wlib.Table.merge(opts, { level = "warning" })
system_log(message, merged_opts)
end end
function M.error(message) ---Log a error message
---@param message string
---@param opts LogOpts?
function M.error(message, opts)
opts = opts or {}
message = format_message(message, opts)
wezterm.log_error(message) wezterm.log_error(message)
system_log(message, "err") local merged_opts = wlib.Table.merge(opts, { level = "err" })
system_log(message, merged_opts)
end end
return M return M