improvement(debug): better logs

This commit is contained in:
Kazhnuz 2021-05-05 13:15:50 +02:00
parent 4d3687f627
commit 4054bbe443
5 changed files with 43 additions and 20 deletions

View file

@ -7,6 +7,7 @@
"core",
"scenes",
"utils",
"birb"
"birb",
"enum"
]
}

View file

@ -25,9 +25,18 @@ local DebugSystem = Object:extend()
local lovebird = require "birb.libs.lovebird"
function DebugSystem:new(controller, active)
local Levels = enum {
"ERROR",
"WARNING",
"INFO",
"DEBUG"
}
function DebugSystem:new(controller)
self.controller = controller
self.active = active or false
self.debugLevel = self.controller.conf.debugLevel or Levels.INFO
self.active = (self.debugLevel == Levels.DEBUG)
if (self.active) then
lovebird.update()
end
@ -42,27 +51,39 @@ end
-- PRINT FUNCTIONS
-- Print and log debug string
function DebugSystem:print(context, string)
if (self.active) then
print(self:getLogLine("DEBUG", context, string))
function DebugSystem:debug(context, string)
self:log(Levels.DEBUG, context, string)
end
function DebugSystem:print(context, string)
self:log(Levels.INFO, context, string)
end
function DebugSystem:warning(context, string)
if (self.active) then
print(self:getLogLine("WARNING", context, string))
end
self:log(Levels.WARNING, context, string)
end
function DebugSystem:error(context, string)
error(self:getLogLine("ERROR", context, string))
self:log(Levels.ERROR, context, string)
error(self:getMessage(": ", context, string))
end
function DebugSystem:getLogLine(type, string1, string2)
function DebugSystem:log(level, context, string)
if (self.debugLevel >= level) then
print(self:getLogLine(Levels[level], context, string))
end
end
function DebugSystem:getLogLine(type, context, string)
local head = self.controller:getIdentity(false) .. "|" .. os.date("%x-%X") .. "|" .. type .. "|"
return head .. self:getMessage("|", context, string)
end
function DebugSystem:getMessage(separator, string1, string2)
if (string2 == nil) then
return "[" .. type .. "] " .. string1
return string1
else
return "[" .. type .. "] " .. string1 .. ": " .. string2
return string1 .. separator .. string2
end
end

View file

@ -38,10 +38,10 @@ local SceneManager = require(cwd .. "scenemanager")
-- INIT FUNCTIONS
-- Initialize and configure the core object
function CoreSystem:new(isDebug)
function CoreSystem:new()
self:setDefaultConf()
self.debug = DebugSystem(self, isDebug)
self.debug = DebugSystem(self)
self.options = Options(self)
self.input = Input(self)
self.screen = Screen(self)

View file

@ -29,18 +29,19 @@ birb = {}
Object = require("birb.libs.classic")
utils = require("birb.utils")
enum = require("birb.utils.enum")
birb.Core = require("birb.core")
function birb.start(gamemodule, isDebug)
birb.startCore((isDebug == true))
function birb.start(gamemodule)
birb.startCore()
if (gamemodule ~= nil) then
birb.startGame(gamemodule)
end
end
function birb.startCore(isDebug)
core = birb.Core(isDebug)
function birb.startCore()
core = birb.Core()
end
function birb.startGame(gamemodule)

View file

@ -26,6 +26,6 @@ require "birb"
scenes = require "scenes"
function love.load()
birb.start("game", false)
birb.start("game")
scenes.title()
end