From 4a9683a256b3c14bc5829cccef332b4b9794d624 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 6 Oct 2025 17:48:22 +1100 Subject: [PATCH] Added log tool --- halyde/apps/helpdb/log.txt | 15 +++++ halyde/apps/log.lua | 110 +++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 halyde/apps/helpdb/log.txt create mode 100644 halyde/apps/log.lua diff --git a/halyde/apps/helpdb/log.txt b/halyde/apps/helpdb/log.txt new file mode 100644 index 0000000..bfaa230 --- /dev/null +++ b/halyde/apps/helpdb/log.txt @@ -0,0 +1,15 @@ +Usage: log [OPERATION] [ARGS] +Tool to manage system logs. + + OPERATION Operation to do with the system logs. + view [LOG] View a log file. + list List all logs. + clear [LOG*] Clear a log file, or all if none specified. + info/warn/error [LOG] [TEXT] Create a log entry for the specified log at the specified log level. + ARGS Arguments (specified under OPERATION) + +Examples: + log view example + log list + log clear example + log info example This is an example. \ No newline at end of file diff --git a/halyde/apps/log.lua b/halyde/apps/log.lua new file mode 100644 index 0000000..6efc8fb --- /dev/null +++ b/halyde/apps/log.lua @@ -0,0 +1,110 @@ +local log = require("log") +local shell = require("shell") +local fs = require("filesystem") + +local args = {...} +if #args == 0 then + shell.run("help log") + return +end + +local function viewlog(logname) + local logpath = "/halyde/logs/" .. logname .. ".log" + if not fs.exists(logpath) then + print("Log not found.") + return + end + local handle = fs.open(logpath) + local entry = "" + local byte + while true do + byte = handle:read(1) + if not byte then return end + if string.byte(byte) == 0x0a then --check for newline + if string.byte(string.sub(entry, -1, -1)) == 0x0d then --failsafe in case line endings are CRLF + entry = string.sub(entry, 1, -2) + else + entry = string.sub(entry, 1, -1) + end + if entry:sub(1, 4) == "INFO" then + print(entry) + elseif entry:sub(1, 4) == "WARN" then + print("\x1b[93m" .. entry) + elseif entry:sub(1, 5) == "ERROR" then + print("\x1b[91m" .. entry) + end + entry = "" + else + entry = entry .. byte + end + end +end + +local function listlogs() + local files = fs.list("/halyde/logs") + local logs = {} + local j = 1 + for i in ipairs(files) do + if not(string.sub(files[i], -1, -1) == "/") and string.sub(files[i], -4, -1) == ".log" then + logs[j] = string.sub(files[i], 1, -5) + j = j + 1 + end + end + return logs +end + +local function listlogs2() + local logs = listlogs() + print("Found \x1b[93m" .. #logs .. "\x1b[0m logs.") + for i in ipairs(logs) do + if i == #logs then + print("\x1b[93m└ \x1b[0m" .. logs[i] .. "\x1b[90m.log") + else + print("\x1b[93m├ \x1b[0m" .. logs[i] .. "\x1b[90m.log") + end + end +end + +local function clearlog(logname) + if logname then + local logpath = "/halyde/logs/" .. logname .. ".log" + if not fs.exists(logpath) then + print("Log file not found.") + return + end + local success, err = fs.remove(logpath) + if not success then + print("Failed to remove log file: " .. err) + return + end + else + local logs = listlogs() + local j + for i in ipairs(logs) do + local success, err = fs.remove("/halyde/logs/" .. logs[i] .. ".log") + if not success then + print("Failed to remove log " .. logs[i] .. ": " .. err) + print("Removed" .. i - 1 .. "logs.") + return + end + j = i + end + print("Removed " .. j .. " log(s) successfully.") + end +end + +if args[1] == "view" then + viewlog(args[2]) +elseif args[1] == "list" then + listlogs2() +elseif args[1] == "clear" then + clearlog(args[2]) +elseif args[1] == "info" or args[1] == "warn" or args[1] == "error" then + local loglevel = args[1] + local logname = args[2] + local logtext = args[3] + for i = 4, #args do + logtext = logtext .. " " .. args[i] + end + log[logname][loglevel](logtext) +end \ No newline at end of file