SHARE
TWEET

ComputerCraft Tutorial: logging_API_0_1

hevohevo Jun 7th, 2014 909 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --############################
  2. -- logging API
  3. -- version 0.1
  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.  
  16. -- change logfile
  17. logging.renameLogfile("mylog2")
  18. logging.turnRight()
  19. logging.turnLeft()
  20.  
  21. -- compulsory logging message
  22. logging.write("-- my message!!")
  23.  
  24. --]]
  25.  
  26. -- ###########################
  27. -- config
  28. local LOGFILE = "mylog"
  29.  
  30. -- ###########################
  31. -- functions
  32. function renameLogfile(name)
  33.   LOGFILE = name
  34. end
  35.  
  36. function write(message)
  37.   local fh = fs.open(LOGFILE, "a")
  38.   fh.writeLine(message)
  39.   fh.close()
  40. end
  41.  
  42. -- forward()
  43. function forward()
  44.   local status, error_msg = turtle.forward()
  45.   if status then -- succeeded
  46.     write("turtle.forward()")
  47.   else -- failed
  48.     write("-- turtle.forward()")
  49.   end
  50.   return status, error_msg
  51. end
  52.  
  53. function back()
  54.   local status, error_msg = turtle.back()
  55.   if status then -- succeeded
  56.     write("turtle.back()")
  57.   else -- failed
  58.     write("-- turtle.back()")
  59.   end
  60.   return status, error_msg
  61. end
  62.  
  63. function up()
  64.   local status, error_msg = turtle.up()
  65.   if status then -- succeeded
  66.     write("turtle.up()")
  67.   else -- failed
  68.     write("-- turtle.up()")
  69.   end
  70.   return status, error_msg
  71. end
  72.  
  73. function down()
  74.   local status, error_msg = turtle.down()
  75.   if status then -- succeeded
  76.     write("turtle.down()")
  77.   else -- failed
  78.     write("-- turtle.down()")
  79.   end
  80.   return status, error_msg
  81. end
  82.  
  83. function turnRight()
  84.   local status, error_msg = turtle.turnRight()
  85.   if status then -- succeeded
  86.     write("turtle.turnRight()")
  87.   else -- failed
  88.     write("-- turtle.turnRight()")
  89.   end
  90.   return status, error_msg
  91. end
  92.  
  93. function turnLeft()
  94.   local status, error_msg = turtle.turnLeft()
  95.   if status then -- succeeded
  96.     write("turtle.turnLeft()")
  97.   else -- failed
  98.     write("-- turtle.turnLeft()")
  99.   end
  100.   return status, error_msg
  101. end
  102.  
  103. -- ###########################
  104. -- main
  105.  
  106. -- backup LOGFILE, if same name file exists
  107. if fs.exists(LOGFILE) then
  108.   -- backup file is "XXXX.bak"
  109.   local bak_file = LOGFILE..".bak"
  110.   if fs.exists(bak_file) then fs.delete(bak_file) end
  111.   fs.move(LOGFILE, bak_file)
  112. end
RAW Paste Data
Pastebin PRO Autumn Special!
Get 40% OFF on Pastebin PRO accounts!
Top