I've quickly wrote this app after seeing this thread - I've thought this idea can be developed a bit more :)/>
What is does:
It basically logs apps and saves it to "debug_files/<app name>.log".
How to use:
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
Where to get it:
- pastebin get 9YSN2Mht debug
- Code:
Spoiler
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
]])
Spoiler
- 1.0: Initial release
- 1.1: Logging part of term.*, read, write, print
License:
Spoiler
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. Copy of this license.