Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- --[[
- Backup for old functions
- ]]--
- local fOld = {
- term = {
- write = term.write,
- clear = term.clear,
- setCursorPos = term.setCursorPos,
- clearLine = term.clearLine,
- },
- print = print,
- write = write,
- read = read,
- }
- local function addlog(t, i)
- if i == false then fLog.write(t)
- else fLog.write(t.."\n") end
- fLog.flush()
- end
- local function openlog()
- fLog = fs.open(debug_path, "w") -- Global...
- end
- local function closelog()
- fLog.close()
- fLog = nil
- end
- local function restore()
- term.write = fOld.term.write
- term.clear = fOld.term.clear
- term.setCursorPos = fOld.term.setCursorPos
- term.clearLine = fOld.term.clearLine
- print = fOld.print
- write = fOld.write
- closelog()
- debug_path = nil
- fs.delete("debug_files/.launch")
- print("\n\n\nDebbuging finished!")
- end
- local function mkfunc()
- -- Open log
- openlog()
- --[[
- All global override functions go here.
- ]]--
- function term.writeNew(t)
- addlog(t)
- return fOld.term.write(t)
- end
- term.write = term.writeNew
- function read(t)
- term.write = fOld.term.write
- local r
- if t == nil then
- r = fOld.read()
- else
- r = fOld.read(t)
- end
- term.write = term.writeNew
- addlog("[read("..tostring(t)..") = "..tostring(r).."]")
- return r
- end
- function print(t)
- local r = fOld.print(t)
- addlog("[print("..tostring(t)..") = "..tostring(r).."]")
- return r
- end
- function write(t)
- local r = fOld.write(t)
- addlog("[write("..tostring(t)..") = "..tostring(r).."]")
- return r
- end
- function term.clear()
- local r = fOld.term.clear()
- addlog("[term.clear() = "..tostring(r).."]")
- return r
- end
- function term.setCursorPos(a, b)
- local r = fOld.term.setCursorPos(a, b)
- addlog("[term.setCursorPos("..tostring(a)..", "..tostring(b)..") = "..tostring(r).."]")
- return r
- end
- function term.clearLine()
- local r = fOld.term.clearLine()
- addlog("[term.clearLine() = "..tostring(r).."]")
- return r
- end
- end
- if args[1] == "run" then
- debug_path = "debug_files/"..args[2]..".log" --Globalize args[2]
- term.clear()
- term.setCursorPos(1, 1)
- print("Debugging "..args[2]..".\n\nDo not terminate program, if you do, run \"debug restore\" to restore functions.\n\nPress any key to continue")
- local e = nil
- while(e ~= "char") do e = os.pullEvent("char") end --Wait for key
- term.clear()
- term.setCursorPos(1, 1)
- if(not fs.exists("debug_files")) then fs.makeDir("debug_files") end -- Check & make "debug" dir
- mkfunc()
- local l = fs.open("debug_files/.launch", "w") -- Open temp launch file
- local s = "shell.run(\""..args[2].."\""-- Temp string
- for i = 3, #args do
- s = s..", \""..args[i].."\""
- end
- s = s..")"
- l.write(s)
- l.close()
- shell.run("debug_files/.launch") --Run app
- restore() -- Restore old functions
- return
- end
- if args[1] == "log" then
- if(not fs.exists("debug_files/"..args[2]..".log")) then print("No such file!") return end
- local f = fs.open("debug_files/"..args[2]..".log", "r") -- Open and print
- print(f.readAll())
- f.close()
- return
- end
- if args[1] == "restore" then
- restore() -- Restore
- return
- end
- print([[
- Usage:
- - debug run <program full path> [args]
- Logs are saves in debug_files/<program name>.log
- - debug log <program name>
- Prints a log
- - debug restore
- Use if program was terminated or to manually restore write & print functions
- Debug by jesysthekiller, idea from urielsalis
- Version 1.1
- ]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement