Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --############################
- -- logging API
- -- version 0.1
- -- 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()
- -- change logfile
- logging.renameLogfile("mylog2")
- logging.turnRight()
- logging.turnLeft()
- -- compulsory logging message
- logging.write("-- my message!!")
- --]]
- -- ###########################
- -- config
- local LOGFILE = "mylog"
- -- ###########################
- -- functions
- function renameLogfile(name)
- LOGFILE = name
- end
- function write(message)
- local fh = fs.open(LOGFILE, "a")
- fh.writeLine(message)
- fh.close()
- end
- -- forward()
- function forward()
- local status, error_msg = turtle.forward()
- if status then -- succeeded
- write("turtle.forward()")
- else -- failed
- write("-- turtle.forward()")
- end
- return status, error_msg
- end
- function back()
- local status, error_msg = turtle.back()
- if status then -- succeeded
- write("turtle.back()")
- else -- failed
- write("-- turtle.back()")
- end
- return status, error_msg
- end
- function up()
- local status, error_msg = turtle.up()
- if status then -- succeeded
- write("turtle.up()")
- else -- failed
- write("-- turtle.up()")
- end
- return status, error_msg
- end
- function down()
- local status, error_msg = turtle.down()
- if status then -- succeeded
- write("turtle.down()")
- else -- failed
- write("-- turtle.down()")
- end
- return status, error_msg
- end
- function turnRight()
- local status, error_msg = turtle.turnRight()
- if status then -- succeeded
- write("turtle.turnRight()")
- else -- failed
- write("-- turtle.turnRight()")
- end
- return status, error_msg
- end
- function turnLeft()
- local status, error_msg = turtle.turnLeft()
- if status then -- succeeded
- write("turtle.turnLeft()")
- else -- failed
- write("-- turtle.turnLeft()")
- end
- return status, error_msg
- end
- -- ###########################
- -- main
- -- backup LOGFILE, if same name file exists
- if fs.exists(LOGFILE) then
- -- backup file is "XXXX.bak"
- local bak_file = LOGFILE..".bak"
- if fs.exists(bak_file) then fs.delete(bak_file) end
- fs.move(LOGFILE, bak_file)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement