SHARE
TWEET

ComputerCraft Tutorial: logging_API_0_4

hevohevo Jun 19th, 2014 290 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --############################
  2. -- logging API
  3. -- version 0.4c
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- ### how to use
  7. --[[
  8. os.loadAPI("logging")
  9.  
  10. -- default logfile name is "mylog"
  11. logging.forward() -- turtle.fowrard() and logging
  12. logging.back()
  13. logging.up()
  14. logging.down()
  15. logging.turnRight()
  16. logging.turnLeft()
  17.  
  18. -- change logfile
  19. logging.changeLogfile("mylog2")
  20.  
  21. -- write a log forcibly
  22. logging.write("-- my message!!")
  23.  
  24. -- createLoggedFunc(function, succeeded_log, *failed_log)
  25. -- return a function which writes succeeded/failed msg to a logfile
  26. -- if you omit "failed_log" argument, succeeded_log is written in true/false cases
  27. myRefuel = logging.createLoggedFunc(turtle.refuel, "turtle.refuel()")
  28. myRefuel()
  29.  
  30. -- make a reverted log file, and return start position by running REV_FILE
  31. logging.forward()
  32. logging.turnRight()
  33. logging.up()
  34.  
  35. local rev_filename = logging.makeRevFile()
  36. shell.run(rev_filename)
  37. --]]
  38.  
  39. -- ###########################
  40. -- config
  41. LOG_FILE = "mylog"
  42. REV_FILE = "myrev"
  43.  
  44. TRANS_TBL = {}
  45. TRANS_TBL["turtle.forward()"] = "turtle.back()"
  46. TRANS_TBL["turtle.back()"] = "turtle.forward()"
  47. TRANS_TBL["turtle.up()"] = "turtle.down()"
  48. TRANS_TBL["turtle.down()"] = "turtle.up()"
  49. TRANS_TBL["turtle.turnRight()"] = "turtle.turnLeft()"
  50. TRANS_TBL["turtle.turnLeft()"] = "turtle.turnRight()"
  51.  
  52.  
  53. -- ###########################
  54. -- functions
  55. function write(message)
  56.   local fh = fs.open(LOG_FILE, "a")
  57.   fh.writeLine(message)
  58.   fh.close()
  59. end
  60.  
  61. function backupFile(filename, tail_str)
  62.   local tail_str = tail_str or "-bak"
  63.   if fs.exists(filename) then
  64.     fs.delete(filename..tail_str)
  65.     fs.move(filename, filename..tail_str)
  66.     return true
  67.   else
  68.     return false, "File doesn't exist"
  69.   end
  70. end
  71.  
  72. local function readTrans(filename, trans_tbl)
  73.   local fh = fs.open(filename, 'r')
  74.   local tmp_tbl = {}
  75.   repeat
  76.     local line = fh.readLine()
  77.     local tLine = TRANS_TBL[line]
  78.     if tLine then
  79.       table.insert(tmp_tbl, tLine)
  80.     end
  81.   until not line
  82.   fh.close()
  83.   return tmp_tbl
  84. end
  85.  
  86. local function reverseWrite(my_array, filename)
  87.   local fh = fs.open(filename, 'a')
  88.   for i=#my_array,1,-1 do
  89.     if my_array[i] then fh.writeLine(my_array[i]) end
  90.   end
  91.   fh.close()
  92. end
  93.  
  94. function makeRevFile(log_filename, rev_filename)
  95.   local log_filename = log_filename or LOG_FILE
  96.   local rev_filename = rev_filename or REV_FILE
  97.   reverseWrite(readTrans(log_filename, TRANS_TBL), rev_filename)
  98.   return rev_filename
  99. end
  100.  
  101. -- return a function which writes succeeded/failed msg to a logfile
  102. function createLoggedFunc(func, succeeded_log, failed_log)
  103.   failed_log = failed_log or succeeded_log
  104.   return function(...)
  105.     local status, error_msg = func(...)
  106.     if status then
  107.       write(succeeded_log)
  108.     else
  109.       write(failed_log)
  110.     end
  111.     return status, error_msg
  112.   end
  113. end
  114.  
  115. -- redefine six movement functions in Turtle API
  116. forward = createLoggedFunc(turtle.forward, "turtle.forward()", "-- turtle.forward()")
  117. back = createLoggedFunc(turtle.back, "turtle.back()", "-- turtle.back()")
  118. up = createLoggedFunc(turtle.up, "turtle.up()", "-- turtle.up()")
  119. down = createLoggedFunc(turtle.down, "turtle.down()", "-- turtle.down()")
  120. turnRight = createLoggedFunc(turtle.turnRight, "turtle.turnRight()")
  121. turnLeft = createLoggedFunc(turtle.turnLeft, "turtle.turnLeft()")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top