Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --############################
- -- logging API
- -- version 0.2b
- -- 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()
- --]]
- -- ###########################
- -- config
- local logfile_name = "mylog"
- -- ###########################
- -- functions
- function changeLogfile(name)
- logfile_name = name
- end
- function write(message)
- local fh = fs.open(logfile_name, "a")
- fh.writeLine(message)
- fh.close()
- 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()")
- -- ###########################
- -- main
- -- backup "logfile_name", if same name file exists
- if fs.exists(logfile_name) then
- -- backup file is "XXXX.bak"
- local bak_file = logfile_name..".bak"
- if fs.exists(bak_file) then fs.delete(bak_file) end
- fs.move(logfile_name, bak_file)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement