Advertisement
antonsavov

WIP Logger

Jan 4th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  1. -------------------------------
  2. -- /lib/Logger
  3. -------------------------------
  4. --- This packages handles the debug log
  5.  
  6. --os.loadAPI('lib/utils')
  7.  
  8. debug = {} -- the public API to hold the logger functions
  9.  
  10. local logfilename = "log.txt"
  11.  
  12. -- store the log in the global table _G so that if multiple scripts do the os.loadAPI on this one it still adds to the same log
  13. --_G.debug_log = _G.debug_log or {}
  14. --local debug_log = _G.debug_log -- keep all messages of debug log here
  15.  
  16. debug_log = {} -- keep all messages of debug log here
  17.  
  18. --- Adds a message to the debug log file
  19. function debug.log(message)
  20.  
  21.     local fullmsg = message
  22.     local timestamp = os.clock()
  23.     --add to the log
  24.     table.insert(debug_log, {time=timestamp,message=fullmsg})
  25.     local log_msg = timestamp..": "..fullmsg.."\n"
  26.     --save message to logfile
  27.     if #debug_log == 1 then
  28.         --if it is the first message then reset the log file   
  29.         file_write(log_msg, logfilename)
  30.     else
  31.         --append it to the log file
  32.         file_append(log_msg, logfilename)
  33.     end
  34. end
  35.  
  36.  
  37. local symbols = {
  38.     "(X) ",
  39.     "(-) "
  40.     }
  41. local spin = 1
  42.  
  43. --- Displays the game information in the terminal
  44. -- if detailed is true it diplays also the latest log events and the buildzone stats
  45. -- by default detailed is false
  46. function debug.printState(game, detailed)
  47.     detailed = detailed or false
  48.     --print("starting debug display")
  49.     term.clear()
  50.     term.setTextColor(colors.green)
  51.     print("20.000 BLOCKS v."..game.version_number.." - (HOLD CTRL+T TO QUIT) "..symbols[spin])
  52.     spin = spin +1
  53.     if spin > #symbols then spin = 1 end
  54.     if detailed then
  55.         --[[ disabled for now until we deal with the TP glitches
  56.         if number_of_players_adventure and number_of_players_creative then
  57.             print("TOTAL PLAYERS: "..(number_of_players_adventure + number_of_players_creative).." - ADVENTURE [ "..number_of_players_adventure.." ], CREATIVE [ "..number_of_players_creative.." ]")
  58.         end
  59.         --]]
  60.         --if number_of_players_adventure  then
  61.         print("TOTAL ADVENTURE MODE PLAYERS: "..(number_of_players_adventure or 0))
  62.         --end
  63.         term.setTextColor(colors.white)
  64.    
  65.         --local line = 2
  66.         for i,kew in ipairs(game.queues) do
  67.             local minutes = string.format("%02d",math.floor(kew.timer/60))
  68.             local seconds = string.format("%02d",math.floor(kew.timer - (minutes*60)))
  69.             print("BUILDZONE "..i.." | PHASE: "..kew.phase.." | TIME: "..math.floor(kew.timer).." | PLAYERS: "..tableLength(kew.playerlist))
  70.             print("-LIST: "..tostring(kew.playerlist))
  71.             --print(json.encode(kew.playerlist))
  72.         end
  73.    
  74.         --use the remaining terminal lines to show the log
  75.         -- the terminal has 19 lines, 18 available for printing, last one stays clear at all times
  76.         term.setTextColor(colors.red)
  77.         print("===== LATEST LOG:")
  78.         for i=13-(2*#game.queues), 0, -1 do
  79.             if #debug_log > i then
  80.                 print(debug_log[#debug_log-i].message:sub (1,51)) --trim the logged message to fit into a single line in the terminal
  81.             end
  82.         end
  83.     end
  84.     term.setTextColor(colors.white)
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement