document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. --############################
  2. -- logging API
  3. -- version 0.5a
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- ### how to use
  7. --[[
  8. os.loadAPI("logging")
  9.  
  10. -- backup files
  11. logging.backupFile(LOG_FILE)
  12. logging.backupFile(REV_FILE)
  13.  
  14. -- default logfile name is "mylog"
  15. logging.forward() -- turtle.fowrard() and logging
  16. logging.back()
  17. logging.up()
  18. logging.down()
  19. logging.turnRight()
  20. logging.turnLeft()
  21.  
  22. -- write a log forcibly
  23. logging.write("-- my message!!")
  24.  
  25. -- write a timestamp
  26. logging.writeTimeStamp(LOG_FILE)
  27.  
  28. -- createLoggedFunc(function, succeeded_log, *failed_log)
  29. -- return a function which writes succeeded/failed msg to a logfile
  30. -- if you omit "failed_log" argument, succeeded_log is written in true/false cases
  31. myRefuel = logging.createLoggedFunc(turtle.refuel, "turtle.refuel()")
  32. myRefuel()
  33.  
  34. -- make a reverted log file, and return start position by running REV_FILE
  35. logging.forward()
  36. logging.turnRight()
  37. logging.up()
  38.  
  39. local rev_filename = logging.makeRevFile()
  40. shell.run(rev_filename)
  41. --]]
  42.  
  43. -- ###########################
  44. -- config
  45. LOG_FILE = "mylog"
  46. REV_FILE = "myrev"
  47. CURRENT_TIME = os.day()*24 + os.time()
  48.  
  49. TRANS_TBL = {}
  50. TRANS_TBL["turtle.forward()"] = "turtle.back()"
  51. TRANS_TBL["turtle.back()"] = "turtle.forward()"
  52. TRANS_TBL["turtle.up()"] = "turtle.down()"
  53. TRANS_TBL["turtle.down()"] = "turtle.up()"
  54. TRANS_TBL["turtle.turnRight()"] = "turtle.turnLeft()"
  55. TRANS_TBL["turtle.turnLeft()"] = "turtle.turnRight()"
  56.  
  57.  
  58. -- ###########################
  59. -- functions
  60.  
  61. -- backupFile("mylog")
  62. --  ==> move to "mylog-bak"
  63. function backupFile(filename, tail_str)
  64.   local tail_str = tail_str or "-bak"
  65.   if fs.exists(filename) then
  66.     fs.delete(filename..tail_str)
  67.     fs.move(filename, filename..tail_str)
  68.     return true
  69.   else
  70.     return false, "File doesn\'t exist"
  71.   end
  72. end
  73.  
  74. -- write a log message as you like
  75. function write(message, filename)
  76.   local fh = fs.open(filename or LOG_FILE, "a")
  77.   fh.writeLine(message)
  78.   fh.close()
  79. end
  80.  
  81. -- write a time stamp "-- daytime: 678.441"
  82. function writeTimeStamp(filename)
  83.   local fh = fs.open(filename or LOG_FILE, "a")
  84.   fh.writeLine("-- daytime: "..tostring(os.day()*24+os.time()))
  85.   fh.close()
  86. end
  87.  
  88. -- readTimeStamp("-- daytime: 678.441")
  89. -- => 678.441
  90. -- readTimeStamp("turtle.forward()")
  91. -- => false
  92. local function readTimeStamp(str)
  93.   if type(str) == "string" and #str > 14 then
  94. --    return tonumber(string.match(str, "-- daytime: (%d+.%d+)")) or false
  95.     return tonumber(string.sub(str, 13, -1)) or false
  96.   else
  97.     return false
  98.   end
  99. end
  100.  
  101. -- transrate from log messages to reverted functions
  102. --  with confirming timestamp
  103. local function readTrans(filename, trans_tbl)
  104.   local fh = fs.open(filename or LOG_FILE, \'r\')
  105.   local tmp_tbl = {}
  106.   repeat
  107.     local line = fh.readLine()
  108.     local time_stamp = readTimeStamp(line)
  109.     if time_stamp and time_stamp > CURRENT_TIME then
  110.       break
  111.     else
  112.       table.insert(tmp_tbl, TRANS_TBL[line])
  113.     end
  114.   until line == nil
  115.   fh.close()
  116.   return tmp_tbl
  117. end
  118.  
  119. -- reverse reverted-func table, and write to REV_FILE
  120. local function reverseWrite(my_array, filename)
  121.   local fh = fs.open(filename or REV_FILE, \'a\')
  122.   fh.writeLine("-- "..tostring(CURRENT_TIME))
  123.   for i=#my_array,1,-1 do
  124.     if my_array[i] then
  125.       fh.writeLine(my_array[i])
  126.     end
  127.   end
  128.   fh.close()
  129. end
  130.  
  131. -- transrate from log_file to rev_file with confirming timestamps
  132. function makeRevFile(log_filename, rev_filename)
  133.   local log_filename = log_filename or LOG_FILE
  134.   local rev_filename = rev_filename or REV_FILE
  135.   reverseWrite(readTrans(log_filename, TRANS_TBL), rev_filename)
  136.   return rev_filename
  137. end
  138.  
  139. -- return a function which writes succeeded/failed msg to a logfile
  140. function createLoggedFunc(func, succeeded_log, failed_log)
  141.   failed_log = failed_log or succeeded_log
  142.   return function(...)
  143.     local status, error_msg = func(...)
  144.     writeTimeStamp()
  145.     if status then
  146.       write(succeeded_log)
  147.     else
  148.       write(failed_log)
  149.     end
  150.     return status, error_msg
  151.   end
  152. end
  153.  
  154. -- redefine six movement functions in Turtle API
  155. forward = createLoggedFunc(turtle.forward, "turtle.forward()", "-- turtle.forward()")
  156. back = createLoggedFunc(turtle.back, "turtle.back()", "-- turtle.back()")
  157. up = createLoggedFunc(turtle.up, "turtle.up()", "-- turtle.up()")
  158. down = createLoggedFunc(turtle.down, "turtle.down()", "-- turtle.down()")
  159. turnRight = createLoggedFunc(turtle.turnRight, "turtle.turnRight()")
  160. turnLeft = createLoggedFunc(turtle.turnLeft, "turtle.turnLeft()")
');