Advertisement
jesusthekiller

debug v1.1

Apr 14th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1. local args = {...}
  2.  
  3. --[[
  4.     Backup for old functions
  5. ]]--
  6.  
  7. local fOld = {
  8.     term = {
  9.         write = term.write,
  10.         clear = term.clear,
  11.         setCursorPos = term.setCursorPos,
  12.         clearLine = term.clearLine,
  13.     },
  14.     print = print,
  15.     write = write,
  16.     read = read,
  17. }
  18.  
  19.  
  20. local function addlog(t, i)
  21.     if i == false then fLog.write(t)
  22.     else fLog.write(t.."\n") end
  23.     fLog.flush()
  24. end
  25.  
  26. local function openlog()
  27.     fLog = fs.open(debug_path, "w") -- Global...
  28. end
  29.  
  30. local function closelog()
  31.     fLog.close()
  32.     fLog = nil
  33. end
  34.  
  35. local function restore()
  36.     term.write = fOld.term.write
  37.     term.clear = fOld.term.clear
  38.     term.setCursorPos = fOld.term.setCursorPos
  39.     term.clearLine = fOld.term.clearLine
  40.     print = fOld.print
  41.     write = fOld.write
  42.     closelog()
  43.     debug_path = nil
  44.     fs.delete("debug_files/.launch")
  45.     print("\n\n\nDebbuging finished!")
  46. end
  47.  
  48. local function mkfunc()
  49.     -- Open log
  50.     openlog()
  51.    
  52.     --[[
  53.         All global override functions go here.
  54.     ]]--
  55.     function term.writeNew(t)
  56.         addlog(t)
  57.         return fOld.term.write(t)
  58.     end
  59.    
  60.     term.write = term.writeNew
  61.    
  62.     function read(t)
  63.         term.write = fOld.term.write
  64.        
  65.         local r
  66.        
  67.         if t == nil then
  68.             r = fOld.read()
  69.         else
  70.             r = fOld.read(t)
  71.         end
  72.        
  73.         term.write = term.writeNew
  74.         addlog("[read("..tostring(t)..") = "..tostring(r).."]")
  75.         return r
  76.     end
  77.    
  78.     function print(t)
  79.         local r = fOld.print(t)
  80.         addlog("[print("..tostring(t)..") = "..tostring(r).."]")
  81.         return r
  82.     end
  83.    
  84.     function write(t)
  85.         local r = fOld.write(t)
  86.         addlog("[write("..tostring(t)..") = "..tostring(r).."]")
  87.         return r
  88.     end
  89.    
  90.     function term.clear()
  91.         local r = fOld.term.clear()
  92.         addlog("[term.clear() = "..tostring(r).."]")
  93.         return r
  94.     end
  95.    
  96.     function term.setCursorPos(a, b)
  97.         local r = fOld.term.setCursorPos(a, b)
  98.         addlog("[term.setCursorPos("..tostring(a)..", "..tostring(b)..") = "..tostring(r).."]")
  99.         return r
  100.     end
  101.    
  102.     function term.clearLine()
  103.         local r = fOld.term.clearLine()
  104.         addlog("[term.clearLine() = "..tostring(r).."]")
  105.         return r
  106.     end
  107. end
  108.  
  109.  
  110. if args[1] == "run" then
  111.     debug_path = "debug_files/"..args[2]..".log" --Globalize args[2]
  112.    
  113.     term.clear()
  114.     term.setCursorPos(1, 1)
  115.     print("Debugging "..args[2]..".\n\nDo not terminate program, if you do, run \"debug restore\" to restore functions.\n\nPress any key to continue")
  116.    
  117.     local e = nil
  118.     while(e ~= "char") do e = os.pullEvent("char") end --Wait for key
  119.    
  120.     term.clear()
  121.     term.setCursorPos(1, 1)
  122.    
  123.     if(not fs.exists("debug_files")) then fs.makeDir("debug_files") end -- Check & make "debug" dir
  124.    
  125.     mkfunc()
  126.    
  127.     local l = fs.open("debug_files/.launch", "w") -- Open temp launch file
  128.     local s = "shell.run(\""..args[2].."\""-- Temp string
  129.    
  130.     for i = 3, #args do
  131.         s = s..", \""..args[i].."\""
  132.     end
  133.    
  134.     s = s..")"
  135.    
  136.     l.write(s)
  137.     l.close()
  138.    
  139.     shell.run("debug_files/.launch") --Run app
  140.    
  141.     restore() -- Restore old functions
  142.     return
  143. end
  144.  
  145. if args[1] == "log" then
  146.     if(not fs.exists("debug_files/"..args[2]..".log")) then print("No such file!") return end
  147.     local f = fs.open("debug_files/"..args[2]..".log", "r") -- Open and print
  148.     print(f.readAll())
  149.     f.close()
  150.     return
  151. end
  152.  
  153. if args[1] == "restore" then
  154.     restore() -- Restore
  155.     return
  156. end
  157.  
  158. print([[
  159.     Usage:
  160.     - debug run <program full path> [args]
  161.       Logs are saves in debug_files/<program name>.log
  162.     - debug log <program name>
  163.       Prints a log
  164.     - debug restore
  165.       Use if program was terminated or to manually restore write & print functions
  166.      
  167.     Debug by jesysthekiller, idea from urielsalis
  168.     Version 1.1
  169. ]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement