Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --############################
- -- logging API
- -- version 0.4c
- -- http://hevohevo.hatenablog.com/
- -- ### how to use
- --[[
- os.loadAPI("logging")
- -- default logfile name is "mylog"
- logging.forward() -- turtle.fowrard() and logging
- logging.back()
- logging.up()
- logging.down()
- logging.turnRight()
- logging.turnLeft()
- -- change logfile
- logging.changeLogfile("mylog2")
- -- write a log forcibly
- logging.write("-- my message!!")
- -- createLoggedFunc(function, succeeded_log, *failed_log)
- -- return a function which writes succeeded/failed msg to a logfile
- -- if you omit "failed_log" argument, succeeded_log is written in true/false cases
- myRefuel = logging.createLoggedFunc(turtle.refuel, "turtle.refuel()")
- myRefuel()
- -- make a reverted log file, and return start position by running REV_FILE
- logging.forward()
- logging.turnRight()
- logging.up()
- local rev_filename = logging.makeRevFile()
- shell.run(rev_filename)
- --]]
- -- ###########################
- -- config
- LOG_FILE = "mylog"
- REV_FILE = "myrev"
- TRANS_TBL = {}
- TRANS_TBL["turtle.forward()"] = "turtle.back()"
- TRANS_TBL["turtle.back()"] = "turtle.forward()"
- TRANS_TBL["turtle.up()"] = "turtle.down()"
- TRANS_TBL["turtle.down()"] = "turtle.up()"
- TRANS_TBL["turtle.turnRight()"] = "turtle.turnLeft()"
- TRANS_TBL["turtle.turnLeft()"] = "turtle.turnRight()"
- -- ###########################
- -- functions
- function write(message)
- local fh = fs.open(LOG_FILE, "a")
- fh.writeLine(message)
- fh.close()
- end
- function backupFile(filename, tail_str)
- local tail_str = tail_str or "-bak"
- if fs.exists(filename) then
- fs.delete(filename..tail_str)
- fs.move(filename, filename..tail_str)
- return true
- else
- return false, "File doesn't exist"
- end
- end
- local function readTrans(filename, trans_tbl)
- local fh = fs.open(filename, 'r')
- local tmp_tbl = {}
- repeat
- local line = fh.readLine()
- local tLine = TRANS_TBL[line]
- if tLine then
- table.insert(tmp_tbl, tLine)
- end
- until not line
- fh.close()
- return tmp_tbl
- end
- local function reverseWrite(my_array, filename)
- local fh = fs.open(filename, 'a')
- for i=#my_array,1,-1 do
- if my_array[i] then fh.writeLine(my_array[i]) end
- end
- fh.close()
- end
- function makeRevFile(log_filename, rev_filename)
- local log_filename = log_filename or LOG_FILE
- local rev_filename = rev_filename or REV_FILE
- reverseWrite(readTrans(log_filename, TRANS_TBL), rev_filename)
- return rev_filename
- end
- -- return a function which writes succeeded/failed msg to a logfile
- function createLoggedFunc(func, succeeded_log, failed_log)
- failed_log = failed_log or succeeded_log
- return function(...)
- local status, error_msg = func(...)
- if status then
- write(succeeded_log)
- else
- write(failed_log)
- end
- return status, error_msg
- end
- end
- -- redefine six movement functions in Turtle API
- forward = createLoggedFunc(turtle.forward, "turtle.forward()", "-- turtle.forward()")
- back = createLoggedFunc(turtle.back, "turtle.back()", "-- turtle.back()")
- up = createLoggedFunc(turtle.up, "turtle.up()", "-- turtle.up()")
- down = createLoggedFunc(turtle.down, "turtle.down()", "-- turtle.down()")
- turnRight = createLoggedFunc(turtle.turnRight, "turtle.turnRight()")
- turnLeft = createLoggedFunc(turtle.turnLeft, "turtle.turnLeft()")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement