Advertisement
Guest User

log

a guest
Apr 9th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. --The Log API 1.1
  2. runningProgram = "test"
  3.  
  4. -- open: opens the file
  5. function openLog(name)
  6.     h = fs.open("logs/"..name..".log", "a")
  7. end
  8.  
  9. function closeLog()
  10.     h.close()
  11. end
  12.  
  13. --function setRunningProgram(name)
  14. --  runningProgram = name
  15. --end
  16.  
  17. function generateLog() --will create the Dir "log" in the root and create a .log file based on the running program (if if is not already there)
  18.  
  19.     if not fs.exists("logs") then
  20.         fs.mkDir("logs")
  21.     end
  22.    
  23.     if runningProgram == nil then
  24.         error("program not specified")
  25.     end
  26.  
  27.         h = fs.open("logs/"..runningProgram..".log", "a")
  28.         h.writeLine("--- The "..runningProgram.." event log ---")
  29.         h.writeLine(" ")
  30.         h.close()
  31. end
  32.    
  33.  
  34. function addEntry(type, str)
  35.     local time = round(os.time(), 2)
  36.     local day = os.day()
  37.     h.writeLine("["..day.."]".."["..time.."]".."["..type.."] | #"..str)
  38. end
  39.  
  40. -- start/end add start/end messages
  41. function startLog()
  42.     open = true
  43.     local time = os.time()
  44.     local day = os.day()
  45.     h.writeLine("["..day.."]".."["..time.."] | #STARTING LOG")
  46. end
  47.  
  48. function endLog()
  49.     open = false
  50.     local time = os.time()
  51.     local day = os.day()
  52.     h.writeLine("["..day.."]".."["..time.."] | #ENDING LOG")
  53.    
  54. end
  55.  
  56. -- a function you cant access ;P
  57. local function round(num, idp)
  58.   local mult = 10 ^ (idp or 0)
  59.   return (math.floor(num * mult + 0.5) / mult)
  60. end
  61.  
  62. --will print the desired log
  63. function printLog(log, side, fromLn, toLn)
  64.  
  65.      if open == true then
  66.           error("Log must be ended before printing")
  67.      end
  68.  
  69.   if not fs.exists( "logs/"..log..".log") then
  70.     error("Log does not exist")
  71.   end
  72.  
  73.   h = fs.open("logs/"..log..".log", "r")
  74.   page = {}
  75.  
  76.   while h.readLine() ~= nil do
  77.     local ln = h.readLine()
  78.     table.insert(page, ln)
  79.   end
  80.  
  81.   p = peripheral.wrap(side)
  82.   p.newPage()
  83.   local paper = p.getPaperLevel()
  84.   local ink = p.getInkLevel()
  85.   local w, h = p.getPageSize()
  86.  
  87.   local doc = {}
  88.   for i = fromLn, toLn do
  89.     table.insert(doc, page[i])
  90.   end
  91.  
  92.   local length = 0
  93.   for i = 1, #doc do
  94.     length = length + #doc[i]
  95.   end
  96.  
  97.   local size = w*h
  98.   local pages = round(length/size, 0)
  99.   if pages > paper then
  100.     error("not enough paper")
  101.   end
  102.   if pages > ink then
  103.     error("not enough ink")
  104.   end
  105.   for i = 1, pages do
  106.   p.newPage()
  107.   p.setPageTitle(log.." page:"..i)
  108.     for indx = 1, size do
  109.       p.write(doc[indx])
  110.     end
  111.   p.endPage()
  112.   end
  113.  
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement