Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- dctail.lua — self-backgrounding direct GPU log viewer for dcmaster
- Normal use:
- dctail.lua
- dcmaster
- Debug / foreground:
- dctail.lua --fg
- dctail.lua --foreground
- Hardware list:
- dctail.lua --list
- Optional:
- dctail.lua /home/dcmaster_log.txt
- dctail.lua --screen=<screen address prefix>
- dctail.lua --gpu=<gpu address prefix>
- Notes:
- - Do NOT use: dctail.lua &
- - OpenOS does not background jobs that way.
- - This script backgrounds itself using the OpenOS thread library.
- - For true two-screen use, you still need two GPUs.
- ]]
- local component = require("component")
- local event = require("event")
- local fs = require("filesystem")
- local computer = require("computer")
- local unpack = table.unpack or unpack
- --------------------------------------------------------------------------------
- -- BASIC ARG PARSE
- --------------------------------------------------------------------------------
- local function parseArgs(rawArgs)
- local opts = {
- logFile = "/home/dcmaster_log.txt",
- listOnly = false,
- foreground = false,
- forceScreen = nil,
- forceGpu = nil,
- }
- for i = 1, #rawArgs do
- local a = tostring(rawArgs[i] or "")
- if a == "--list" then
- opts.listOnly = true
- elseif a == "--fg" or a == "--foreground" then
- opts.foreground = true
- elseif a:sub(1, 9) == "--screen=" then
- opts.forceScreen = a:sub(10)
- elseif a:sub(1, 6) == "--gpu=" then
- opts.forceGpu = a:sub(7)
- elseif a:sub(1, 1) ~= "-" then
- opts.logFile = a
- end
- end
- return opts
- end
- local function startsWith(s, p)
- s = tostring(s or "")
- p = tostring(p or "")
- return p == "" or s:sub(1, #p) == p
- end
- local function short(a)
- a = tostring(a or "nil")
- if #a <= 12 then return a end
- return a:sub(1, 8) .. ".." .. a:sub(-4)
- end
- local function keyCount(screenAddr)
- local ok, s = pcall(component.proxy, screenAddr)
- if not ok or not s or not s.getKeyboards then return 0 end
- local ok2, kbs = pcall(function()
- return s.getKeyboards()
- end)
- if not ok2 or not kbs then return 0 end
- return #kbs
- end
- local function gpuScreen(g)
- local ok, scr = pcall(function()
- return g.getScreen()
- end)
- if ok then return scr end
- return nil
- end
- local function listHardware()
- print("DCTAIL SELF-BG LOADED")
- print("")
- print("Screens:")
- local anyScreen = false
- for addr in component.list("screen") do
- anyScreen = true
- print(" " .. addr .. " keyboards=" .. tostring(keyCount(addr)))
- end
- if not anyScreen then print(" none") end
- print("")
- print("GPUs:")
- local anyGpu = false
- for addr in component.list("gpu") do
- anyGpu = true
- local ok, g = pcall(component.proxy, addr)
- local bound = "?"
- if ok and g then bound = tostring(gpuScreen(g)) end
- print(" " .. addr .. " bound=" .. bound)
- end
- if not anyGpu then print(" none") end
- end
- --------------------------------------------------------------------------------
- -- REAL TAIL VIEWER
- --------------------------------------------------------------------------------
- local function runTail(rawArgs)
- local opts = parseArgs(rawArgs)
- local LOG_FILE = opts.logFile
- local primaryGpu = component.isAvailable("gpu") and component.gpu or nil
- local primaryGpuAddr = primaryGpu and primaryGpu.address or nil
- local primaryScreen = primaryGpu and gpuScreen(primaryGpu) or nil
- local screenAddr = nil
- if opts.forceScreen then
- for addr in component.list("screen") do
- if startsWith(addr, opts.forceScreen) then
- screenAddr = addr
- break
- end
- end
- else
- -- Prefer a non-primary screen with no keyboard.
- for addr in component.list("screen") do
- if addr ~= primaryScreen and keyCount(addr) == 0 then
- screenAddr = addr
- break
- end
- end
- -- Fallback: any non-primary screen.
- if not screenAddr then
- for addr in component.list("screen") do
- if addr ~= primaryScreen then
- screenAddr = addr
- break
- end
- end
- end
- -- Final fallback: current primary screen.
- if not screenAddr then
- screenAddr = primaryScreen
- end
- end
- if not screenAddr then
- error("dctail: no screen found")
- end
- local gpu = nil
- local gpuAddr = nil
- if opts.forceGpu then
- for addr in component.list("gpu") do
- if startsWith(addr, opts.forceGpu) then
- local ok, g = pcall(component.proxy, addr)
- if ok and g then
- gpu = g
- gpuAddr = addr
- break
- end
- end
- end
- else
- -- Prefer a GPU already bound to the target screen.
- for addr in component.list("gpu") do
- local ok, g = pcall(component.proxy, addr)
- if ok and g and gpuScreen(g) == screenAddr then
- gpu = g
- gpuAddr = addr
- break
- end
- end
- -- Then prefer a GPU that is not the primary terminal GPU.
- if not gpu then
- for addr in component.list("gpu") do
- local ok, g = pcall(component.proxy, addr)
- if ok and g and addr ~= primaryGpuAddr then
- gpu = g
- gpuAddr = addr
- break
- end
- end
- end
- -- Fallback to primary GPU.
- if not gpu and primaryGpu then
- gpu = primaryGpu
- gpuAddr = primaryGpuAddr
- end
- end
- if not gpu then
- error("dctail: no GPU found")
- end
- local gpuCount = 0
- for _ in component.list("gpu") do gpuCount = gpuCount + 1 end
- if gpuCount < 2 and screenAddr ~= primaryScreen then
- print("DCTAIL SELF-BG LOADED")
- print("Only one GPU detected.")
- print("One GPU cannot run dcmaster on one monitor and dctail on another monitor at the same time.")
- print("Add a second GPU, or run dctail on a second OC computer.")
- print("")
- listHardware()
- return
- end
- pcall(function()
- gpu.bind(screenAddr, true)
- end)
- local mw, mh = gpu.maxResolution()
- mw = tonumber(mw) or 80
- mh = tonumber(mh) or 25
- pcall(function()
- gpu.setResolution(math.min(mw, 160), math.min(mh, 50))
- end)
- local W, H = gpu.getResolution()
- W = tonumber(W) or 80
- H = tonumber(H) or 25
- local targetKeyboards = {}
- pcall(function()
- local scr = component.proxy(screenAddr)
- if scr and scr.getKeyboards then
- local kbs = scr.getKeyboards()
- if kbs then
- for _, kb in ipairs(kbs) do
- targetKeyboards[kb] = true
- end
- end
- end
- end)
- local function isTailKeyboard(addr)
- if not next(targetKeyboards) then return true end
- return targetKeyboards[addr] == true
- end
- local C_BG = 0x000000
- local C_TEXT = 0xDDDDDD
- local C_HEAD = 0x4FC3F7
- local C_MUTED = 0x777777
- local C_RED = 0xEF5350
- local C_YELLOW = 0xFFD54F
- local C_GREEN = 0x66BB6A
- local lines = {}
- local lastSize = 0
- local dirty = true
- local maxLines = 3000
- local scrollOffset = 0
- local follow = true
- local lastStatus = ""
- local function color(fg, bg)
- gpu.setForeground(fg or C_TEXT)
- gpu.setBackground(bg or C_BG)
- end
- local function fit(s, w)
- s = tostring(s or "")
- w = tonumber(w) or 0
- if w <= 0 then return "" end
- if #s > w then
- return s:sub(1, w)
- end
- return s .. string.rep(" ", w - #s)
- end
- local function clampScroll()
- local usable = math.max(1, H - 3)
- local maxScroll = math.max(0, #lines - usable)
- if scrollOffset < 0 then scrollOffset = 0 end
- if scrollOffset > maxScroll then scrollOffset = maxScroll end
- follow = scrollOffset == 0
- end
- local function push(line)
- line = tostring(line or "")
- while #line > W do
- table.insert(lines, line:sub(1, W))
- line = line:sub(W + 1)
- end
- table.insert(lines, line)
- while #lines > maxLines do
- table.remove(lines, 1)
- if scrollOffset > 0 then scrollOffset = scrollOffset - 1 end
- end
- if follow then scrollOffset = 0 end
- clampScroll()
- dirty = true
- end
- local function append(chunk)
- if not chunk or #chunk == 0 then return end
- chunk = chunk:gsub("\r", "")
- for line in (chunk .. "\n"):gmatch("([^\n]*)\n") do
- push(line)
- end
- end
- local function draw(status)
- status = status or lastStatus
- lastStatus = status
- if not dirty then return end
- dirty = false
- clampScroll()
- color(C_TEXT, C_BG)
- gpu.fill(1, 1, W, H, " ")
- color(C_BG, C_HEAD)
- gpu.fill(1, 1, W, 1, " ")
- gpu.set(1, 1, fit(
- " dctail scroll-bg " .. LOG_FILE ..
- " GPU " .. short(gpuAddr) ..
- " SCREEN " .. short(screenAddr),
- W
- ))
- local mode = follow and "FOLLOW" or ("SCROLL -" .. tostring(scrollOffset))
- local help = " Up/Dn PgUp/PgDn Home/End F/Space=follow C=clear Q=stop"
- color(C_MUTED, C_BG)
- gpu.set(1, 2, fit((status or "") .. " [" .. mode .. "]" .. help, W))
- local usable = math.max(1, H - 3)
- local start = math.max(1, #lines - usable + 1 - scrollOffset)
- local y = 3
- for i = start, #lines do
- if y > H then break end
- local line = lines[i]
- local fg = C_TEXT
- if line:find("cutoff") or line:find("SCRAM") or line:find("crashed") or line:find("FAILED") then
- fg = C_RED
- elseif line:find("warming") or line:find("charged") or line:find("ETA") then
- fg = C_YELLOW
- elseif line:find("running") or line:find("generation") then
- fg = C_GREEN
- elseif line:find("^tick=") or line:find("^===") then
- fg = C_HEAD
- end
- color(fg, C_BG)
- gpu.set(1, y, fit(line, W))
- y = y + 1
- end
- end
- local function scrollBy(n)
- follow = false
- scrollOffset = scrollOffset + n
- clampScroll()
- dirty = true
- end
- local function followBottom()
- scrollOffset = 0
- follow = true
- dirty = true
- end
- local function clearLocal()
- lines = {}
- lastSize = 0
- scrollOffset = 0
- follow = true
- push("dctail local view cleared")
- push("Watching " .. LOG_FILE)
- push("")
- dirty = true
- end
- local function handleKey(char, code)
- char = tonumber(char) or 0
- code = tonumber(code) or 0
- local c = (char >= 32 and char < 127) and string.lower(string.char(char)) or ""
- if c == "q" then
- return false
- elseif c == "f" or c == " " then
- followBottom()
- elseif c == "c" then
- clearLocal()
- elseif code == 200 then
- scrollBy(1)
- elseif code == 208 then
- scrollBy(-1)
- elseif code == 201 then
- scrollBy(math.max(1, H - 4))
- elseif code == 209 then
- scrollBy(-math.max(1, H - 4))
- elseif code == 199 then
- follow = false
- scrollOffset = 999999
- clampScroll()
- dirty = true
- elseif code == 207 then
- followBottom()
- end
- return true
- end
- push("dctail scroll-bg initialized")
- push("Watching " .. LOG_FILE)
- push("")
- push("Controls: Up/Dn, PgUp/PgDn, Home/End, F or Space to follow, C clear, Q stop")
- push("")
- while true do
- local status = ""
- if not fs.exists(LOG_FILE) then
- status = "Waiting for log file... uptime=" .. string.format("%.1f", computer.uptime())
- dirty = true
- else
- local size = fs.size(LOG_FILE) or 0
- if size < lastSize then
- append("")
- append("--- log reset ---")
- lastSize = 0
- end
- if size > lastSize then
- local f = io.open(LOG_FILE, "rb")
- if f then
- f:seek("set", lastSize)
- local chunk = f:read("*a")
- f:close()
- append(chunk)
- lastSize = size
- end
- end
- status = "Watching. size=" .. tostring(size) .. " bytes"
- dirty = true
- end
- draw(status)
- -- Cooperative yield. OpenOS threads need this to let other scripts run.
- local ev = {event.pull(0.25)}
- if ev[1] == "interrupted" then
- -- In foreground mode this lets Ctrl+C stop it.
- -- In detached mode, ignore parent-shell interrupts so dcmaster Ctrl+C does not kill the tail.
- if opts.foreground then break end
- elseif ev[1] == "screen_resized" then
- W, H = gpu.getResolution()
- W = tonumber(W) or 80
- H = tonumber(H) or 25
- clampScroll()
- dirty = true
- elseif ev[1] == "key_down" then
- if isTailKeyboard(ev[2]) then
- if not handleKey(ev[3], ev[4]) then break end
- end
- elseif ev[1] == "touch" or ev[1] == "scroll" then
- if ev[2] == screenAddr then
- -- Some OC setups send mouse wheel as "scroll"; direction is usually ev[5].
- local dir = tonumber(ev[5]) or 0
- if dir > 0 then scrollBy(3)
- elseif dir < 0 then scrollBy(-3) end
- end
- end
- end
- color(C_TEXT, C_BG)
- gpu.set(1, H, fit("dctail stopped.", W))
- end
- --------------------------------------------------------------------------------
- -- SELF-BACKGROUND LAUNCHER
- --------------------------------------------------------------------------------
- local rawArgs = {...}
- local opts = parseArgs(rawArgs)
- if opts.listOnly then
- listHardware()
- return
- end
- if opts.foreground then
- runTail(rawArgs)
- return
- end
- local okThread, thread = pcall(require, "thread")
- if not okThread or not thread then
- print("thread library unavailable; running dctail in foreground.")
- print("Use Ctrl+C to stop.")
- runTail(rawArgs)
- return
- end
- local errLog = "/home/dctail_bg_error.txt"
- local argsCopy = {}
- for i = 1, #rawArgs do
- argsCopy[i] = rawArgs[i]
- end
- local function logError(msg)
- local f = io.open(errLog, "a")
- if f then
- f:write("\n--- dctail background error ---\n")
- f:write(tostring(msg))
- f:write("\n")
- f:close()
- end
- end
- local t = thread.create(function()
- local okRun, runtimeErr = xpcall(function()
- runTail(argsCopy)
- end, debug.traceback)
- if not okRun then
- logError(runtimeErr)
- end
- end)
- t:detach()
- print("Started dctail.lua in background.")
- print("Rear monitor should update independently.")
- print("Do not use dctail.lua & on OpenOS.")
- print("Errors, if any: " .. errLog)
Advertisement