feat(debug): add basic loggin framework

This commit is contained in:
Kazhnuz 2020-05-10 14:06:20 +02:00
parent 92cbda69a1
commit 9a0db953ba
21 changed files with 136 additions and 57 deletions

View file

@ -15,9 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add a gamesystem module - Add a gamesystem module
- **core/debug:** add log functions - **core/debug:** new logging framework
- **core:** add a way to activate easily debug mode directly
- **camera+maps:** add a way to add padding to map limits - **camera+maps:** add a way to add padding to map limits

View file

@ -1,4 +1,4 @@
-- core/debug.lua :: Debug functions for the core system. -- core/debug.lua :: A basic framework for debug.
--[[ --[[
Copyright © 2019 Kazhnuz Copyright © 2019 Kazhnuz
@ -25,36 +25,115 @@ local DebugSystem = Object:extend()
local lovebird = require("birb.libs.lovebird") local lovebird = require("birb.libs.lovebird")
function DebugSystem:new(controller, active) local ERROR = 0
self.controller = controller local WARN = 1
lovebird.update() local INFO = 2
self.active = active or false local DEBUG = 3
--- Create a new DebugSystem.
-- It allow you to determinate the debug level of the game.
-- @param controller the birb core
-- @param debugLevel the debug mode level
function DebugSystem:new(controller, debugLevel)
self.core = controller
self.debugLevel = debugLevel or 0
self:updateUI()
end end
--- Update the DebugSystem.
-- @param dt the delta time
function DebugSystem:update(dt) function DebugSystem:update(dt)
lovebird.update(dt) self:updateUI(dt)
end
--- Update the lovebird UI if the debug level is debug.
-- @param dt the delta time
function DebugSystem:updateUI(dt)
if (self:isDebug()) then
lovebird.update(dt)
end
end
-- TEST FUNCTIONS
-- Simple warper to get easily the debug level
--- Test if debug level is "DEBUG"
-- @return true if the debug level is "DEBUG"
function DebugSystem:isDebug()
return (self.debugLevel >= DEBUG)
end
--- Test if debug level is "INFO"
-- @return true if the debug level is "INFO"
function DebugSystem:isInfo()
return (self.debugLevel >= INFO)
end
--- Test if debug level is "WARN"
-- @return true if the debug level is "WARN"
function DebugSystem:isWarn()
return (self.debugLevel >= WARN)
end
--- Test if debug level is "ERROR"
-- @return true if the debug level is "ERROR"
function DebugSystem:isError()
return (self.debugLevel >= ERROR)
end end
-- PRINT FUNCTIONS -- PRINT FUNCTIONS
-- Print and log debug string -- Print and log debug string
function DebugSystem:print(context, string) --- Log a debug line
if (self.active) then --
print("[DEBUG] ".. context .. ": " .. string) -- @param context the context of the log
-- @param string the logged string
function DebugSystem:logDebug(context, string)
if (self:isDebug()) then
print(self:createLogLine("DEBUG", context, string))
end end
end end
function DebugSystem:warning(context, string) --- Log an info
if (self.active) then --
print("[WARNING] " .. context .. ": " .. string) -- @param context the context of the log
-- @param string the logged string
function DebugSystem:logInfo(context, string)
if (self:isInfo()) then
print(self:createLogLine("INFO", context, string))
end end
end end
function DebugSystem:error(context, string) --- Log a warning
if (self.active) then --
error("[ERROR] " .. context .. ": " .. string) -- @param context the context of the log
-- @param string the logged string
function DebugSystem:logWarn(context, string)
if (self:isWarn()) then
print(self:createLogLine("WARNING", context, string))
end end
end end
--- Log an error
--
-- @param context the context of the log
-- @param string the logged string
function DebugSystem:logError(context, string)
if (self:isError()) then
print(self:createLogLine("ERROR", context, string))
end
end
--- Create a formatted debug line
--
-- @param level the level of the log
-- @param context the context of the log
-- @param string the logged string
-- @return the debug line
function DebugSystem:createLogLine(level, context, string)
local head = "BIRB" .. "|" .. os.date("%x-%X") .. "|"
return head .. level .. "|" .. context .. ": " .. string;
end
return DebugSystem return DebugSystem

View file

@ -37,15 +37,17 @@ local SceneManager = require(cwd .. "scenemanager")
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialize and configure the core object -- Initialize and configure the core object
function CoreSystem:new(DEBUGMODE) function CoreSystem:new(debugLevel)
self.modules = birb.modules self.modules = birb.modules
self.debug = DebugSystem(self, DEBUGMODE) self.debug = DebugSystem(self, debugLevel)
self.options = Options(self) self.options = Options(self)
self.input = Input(self) self.input = Input(self)
self.screen = Screen(self) self.screen = Screen(self)
self.scenemanager = SceneManager(self) self.scenemanager = SceneManager(self)
self.lang = Lang(self) self.lang = Lang(self)
self.debug:logDebug("birbcore","Birb initialized")
end end
function CoreSystem:registerGameSystem(gamesystem) function CoreSystem:registerGameSystem(gamesystem)

View file

@ -142,7 +142,7 @@ function VirtualPad:isDown(key)
isdown = love.keyboard.isDown(self.data.keys[key]) isdown = love.keyboard.isDown(self.data.keys[key])
else else
local warnstring = "unsupported input device " .. self.type .. " for source " .. self.id local warnstring = "unsupported input device " .. self.type .. " for source " .. self.id
core.debug:warning("core/input", warnstring) core.debug:logWarn("core/input", warnstring)
end end
return isdown return isdown
@ -158,13 +158,13 @@ function VirtualPad:checkKey(key)
local isDown = self:isDown(key) local isDown = self:isDown(key)
if (isDown) then if (isDown) then
if not (self.keys[key].isDown) then if not (self.keys[key].isDown) then
core.debug:print("virtualpad", "key " .. key .. " is Pressed") core.debug:logDebug("virtualpad", "key " .. key .. " is Pressed")
self.keys[key].isDown = true self.keys[key].isDown = true
self.keys[key].isPressed = true self.keys[key].isPressed = true
self.keys[key].isReleased = false self.keys[key].isReleased = false
else else
if (self.keys[key].isPressed) then if (self.keys[key].isPressed) then
core.debug:print("virtualpad", "key " .. key .. " is Down") core.debug:logDebug("virtualpad", "key " .. key .. " is Down")
self.keys[key].isPressed = false self.keys[key].isPressed = false
end end
end end
@ -175,7 +175,7 @@ function VirtualPad:checkKey(key)
self.keys[key].isReleased = true self.keys[key].isReleased = true
else else
if (self.keys[key].isReleased) then if (self.keys[key].isReleased) then
core.debug:print("virtualpad", "key " .. key .. " is Released") core.debug:logDebug("virtualpad", "key " .. key .. " is Released")
self.keys[key].isReleased = false self.keys[key].isReleased = false
end end
end end

View file

@ -95,7 +95,7 @@ function LanguageManager:getTranslationStringList(lang, library)
if fileinfo ~= nil then if fileinfo ~= nil then
list = require(_path) list = require(_path)
else else
core.debug:warning("core/lang","file " .. _path .. " do not exists") core.debug:logWarn("core/lang","file " .. _path .. " do not exists")
end end
return list return list
@ -120,7 +120,7 @@ function LanguageManager:translate(library, string)
if (translation == nil) then if (translation == nil) then
translation = string translation = string
core.debug:warning("core/lang", "no translation path found for " .. string .. " in " .. library) core.debug:logWarn("core/lang", "no translation path found for " .. string .. " in " .. library)
end end
return translation return translation

View file

@ -151,11 +151,11 @@ function OptionsManager:read()
filepath = self:getFile(true) filepath = self:getFile(true)
if utils.filesystem.exists("options.data") then if utils.filesystem.exists("options.data") then
local loadedDatas = binser.readFile(filepath) local loadedDatas = binser.readFile(filepath)
self.controller.debug:print("core/options", "data file found, loading it") self.controller.debug:logInfo("core/options", "data file found, loading it")
self:setData(loadedDatas[1]) self:setData(loadedDatas[1])
else else
self:reset() self:reset()
self.controller.debug:print("core/options", "no data file found, reseting data") self.controller.debug:logInfo("core/options", "no data file found, reseting data")
end end
end end

View file

@ -34,8 +34,8 @@ utils = require("birb.utils")
birb.modules = require("birb.modules") birb.modules = require("birb.modules")
birb.Core = require("birb.core") birb.Core = require("birb.core")
function birb.startCore() function birb.startCore(debugLevel)
core = birb.Core(true) core = birb.Core(debugLevel)
end end
require("birb.callbacks") require("birb.callbacks")

View file

@ -48,7 +48,7 @@ end
function Animator:update(dt) function Animator:update(dt)
if (self.currentAnimation == "") then if (self.currentAnimation == "") then
core.debug:warning("animator", "no current animation data") core.debug:logWarn("animator", "no current animation data")
return 0 return 0
end end

View file

@ -90,7 +90,7 @@ function Assets:batchImport(datafile)
elseif (asset_type == "sfx") then elseif (asset_type == "sfx") then
self:importSFX(assets) self:importSFX(assets)
else else
core.debug:warning("assets/importer", "unkown asset type " .. asset_type) core.debug:logWarn("assets/importer", "unkown asset type " .. asset_type)
end end
end end
end end

View file

@ -151,12 +151,12 @@ end
function Scene:getKeys(sourceid) function Scene:getKeys(sourceid)
if sourceid == nil then if sourceid == nil then
core.debug:warning("scene", "no sourceid detected, will default to 1") core.debug:logWarn("scene", "no sourceid detected, will default to 1")
end end
local sourceid = sourceid or 1 local sourceid = sourceid or 1
if (self.inputLocked) or (not self.isActive) then if (self.inputLocked) or (not self.isActive) then
core.debug:print("scene", "inputs are currently locked") core.debug:logDebug("scene", "inputs are currently locked")
return core.input.fakekeys return core.input.fakekeys
else else
return self.sources[sourceid].keys return self.sources[sourceid].keys

View file

@ -60,7 +60,7 @@ end
function TimersManager:timerResponse(timername) function TimersManager:timerResponse(timername)
if self.subject.timerResponse == nil then if self.subject.timerResponse == nil then
core.debug:warning("TimersManager", "the subject have no timerResponse function") core.debug:logWarn("TimersManager", "the subject have no timerResponse function")
return 0 return 0
end end
self.subject:timerResponse(timername) self.subject:timerResponse(timername)

View file

@ -163,7 +163,7 @@ end
function Actor2D:addHitbox(name, type, ox, oy, w, h, isSolid) function Actor2D:addHitbox(name, type, ox, oy, w, h, isSolid)
if (self.hitboxes[name] ~= nil) then if (self.hitboxes[name] ~= nil) then
core.debug:warning("actor2D", "the hitbox " .. name .. " already exists") core.debug:logWarn("actor2D", "the hitbox " .. name .. " already exists")
else else
local hitbox = Hitbox(self, type, ox, oy, w, h, isSolid) local hitbox = Hitbox(self, type, ox, oy, w, h, isSolid)
self.hitboxes[name] = hitbox self.hitboxes[name] = hitbox

View file

@ -187,7 +187,7 @@ end
function Actor3D:addHitbox(name, type, ox, oy, oz, w, h, d, isSolid) function Actor3D:addHitbox(name, type, ox, oy, oz, w, h, d, isSolid)
if (self.hitboxes[name] ~= nil) then if (self.hitboxes[name] ~= nil) then
core.debug:warning("actor3D", "the hitbox " .. name .. " already exists") core.debug:logWarn("actor3D", "the hitbox " .. name .. " already exists")
else else
local hitbox = Hitbox(self, type, ox, oy, oz, w, h, d, isSolid) local hitbox = Hitbox(self, type, ox, oy, oz, w, h, d, isSolid)
self.hitboxes[name] = hitbox self.hitboxes[name] = hitbox

View file

@ -234,7 +234,7 @@ function BaseActor:initTimers()
end end
function BaseActor:addTimer(name, t) function BaseActor:addTimer(name, t)
core.debug:warning("actor", "function actor:addTimer is deprecated, prefer actor.timers:newTimer") core.debug:logWarn("actor", "function actor:addTimer is deprecated, prefer actor.timers:newTimer")
self.timers:newTimer(t, name) self.timers:newTimer(t, name)
end end
@ -373,7 +373,7 @@ end
function BaseActor:changeAnimation(animation, restart) function BaseActor:changeAnimation(animation, restart)
if (self.sprite ~= nil) then if (self.sprite ~= nil) then
core.debug:warning("actor", "the function BaseActor:changeAnimation is deprecated, prefer BaseActor.sprite:changeAnimation()") core.debug:logWarn("actor", "the function BaseActor:changeAnimation is deprecated, prefer BaseActor.sprite:changeAnimation()")
self.sprite:changeAnimation(animation, restart) self.sprite:changeAnimation(animation, restart)
end end
end end
@ -384,7 +384,7 @@ end
function BaseActor:setCustomSpeed(customSpeed) function BaseActor:setCustomSpeed(customSpeed)
if (self.sprite ~= nil) then if (self.sprite ~= nil) then
core.debug:warning("actor", "the function BaseActor:setCustomSpeed is deprecated, prefer BaseActor.sprite:setCustomSpeed()") core.debug:logWarn("actor", "the function BaseActor:setCustomSpeed is deprecated, prefer BaseActor.sprite:setCustomSpeed()")
self.sprite:setCustomSpeed(customSpeed) self.sprite:setCustomSpeed(customSpeed)
end end
end end
@ -397,33 +397,33 @@ end
function BaseActor:getCurrentAnimation() function BaseActor:getCurrentAnimation()
if (self.sprite ~= nil) then if (self.sprite ~= nil) then
core.debug:warning("actor", "the function BaseActor:getCurrentAnimation is deprecated, prefer BaseActor.sprite:getCurrentAnimation()") core.debug:logWarn("actor", "the function BaseActor:getCurrentAnimation is deprecated, prefer BaseActor.sprite:getCurrentAnimation()")
return self.sprite:getCurrentAnimation() return self.sprite:getCurrentAnimation()
end end
end end
function BaseActor:getSpriteScalling() function BaseActor:getSpriteScalling()
core.debug:warning("actor", "the function BaseActor:getSpriteScalling is deprecated, prefer BaseActor.sprite:getScalling()") core.debug:logWarn("actor", "the function BaseActor:getSpriteScalling is deprecated, prefer BaseActor.sprite:getScalling()")
return self.sprite:getScalling() return self.sprite:getScalling()
end end
function BaseActor:getFrame() function BaseActor:getFrame()
if (self.sprite ~= nil) then if (self.sprite ~= nil) then
core.debug:warning("actor", "the function BaseActor:getFrame is deprecated, prefer BaseActor.sprite:getFrame()") core.debug:logWarn("actor", "the function BaseActor:getFrame is deprecated, prefer BaseActor.sprite:getFrame()")
return self.sprite:getFrame() return self.sprite:getFrame()
end end
end end
function BaseActor:getRelativeFrame() function BaseActor:getRelativeFrame()
if (self.sprite ~= nil) then if (self.sprite ~= nil) then
core.debug:warning("actor", "the function BaseActor:getRelativeFrame is deprecated, prefer BaseActor.sprite:getRelativeFrame()") core.debug:logWarn("actor", "the function BaseActor:getRelativeFrame is deprecated, prefer BaseActor.sprite:getRelativeFrame()")
return self.sprite:getRelativeFrame() return self.sprite:getRelativeFrame()
end end
end end
function BaseActor:getAnimationDuration() function BaseActor:getAnimationDuration()
if (self.sprite ~= nil) then if (self.sprite ~= nil) then
core.debug:warning("actor", "the function BaseActor:getAnimationDuration is deprecated, prefer BaseActor.sprite:getAnimationDuration()") core.debug:logWarn("actor", "the function BaseActor:getAnimationDuration is deprecated, prefer BaseActor.sprite:getAnimationDuration()")
return self.sprite:getAnimationDuration() return self.sprite:getAnimationDuration()
end end
end end

View file

@ -33,7 +33,7 @@ function GFX:new(world, x, y, spritename)
end end
function GFX:animationEnded(animation) function GFX:animationEnded(animation)
core.debug:print("gfx2D", 'Current animation "' .. animation .. '" have ended, destroying gfx') core.debug:logDebug("gfx2D", 'Current animation "' .. animation .. '" have ended, destroying gfx')
self:destroy() self:destroy()
end end

View file

@ -33,7 +33,7 @@ function GFX:new(world, x, y, z, spritename)
end end
function GFX:animationEnded(animation) function GFX:animationEnded(animation)
core.debug:print("gfx2D", 'Current animation "' .. animation .. '" have ended, destroying gfx') core.debug:logDebug("gfx3D", 'Current animation "' .. animation .. '" have ended, destroying gfx')
self:destroy() self:destroy()
end end

View file

@ -37,7 +37,7 @@ end
function MappedBox:drawTextureContent() function MappedBox:drawTextureContent()
local tx, ty = self.x, self.y - (self.z + self.d) local tx, ty = self.x, self.y - (self.z + self.d)
core.debug:print("mappedbox", "getting map layers at position " .. tx .. ";" .. ty) core.debug:logInfo("mappedbox", "getting map layers at position " .. tx .. ";" .. ty)
love.graphics.push() love.graphics.push()
love.graphics.origin() love.graphics.origin()
love.graphics.translate(math.floor(-tx), math.floor(-ty)) love.graphics.translate(math.floor(-tx), math.floor(-ty))

View file

@ -104,7 +104,7 @@ end
function BaseWorld:newActor(name, x, y, z) function BaseWorld:newActor(name, x, y, z)
local debugstring = " at (" .. x .. ";" .. y .. ")." local debugstring = " at (" .. x .. ";" .. y .. ")."
core.debug:print("world2D", "adding actor " .. name .. debugstring) core.debug:logInfo("world2D", "adding actor " .. name .. debugstring)
self.obj.index[name](self, x, y) self.obj.index[name](self, x, y)
end end
@ -112,7 +112,7 @@ function BaseWorld:newCollision(name, x, y, z, w, h, d)
local debugstringpos = "at (" .. x .. ";" .. y .. ")" local debugstringpos = "at (" .. x .. ";" .. y .. ")"
local debugstringsize = "size is (" .. w .. ";" .. h .. ")" local debugstringsize = "size is (" .. w .. ";" .. h .. ")"
local debugstring = " " .. debugstringpos .. ". " .. debugstringsize .. "." local debugstring = " " .. debugstringpos .. ". " .. debugstringsize .. "."
core.debug:print("world2D", "creating collision " .. name .. debugstring) core.debug:logInfo("world2D", "creating collision " .. name .. debugstring)
self.obj.collisions[name](self, x, y, w, h) self.obj.collisions[name](self, x, y, w, h)
end end

View file

@ -50,7 +50,7 @@ end
function CameraSystem:setMode(mode) function CameraSystem:setMode(mode)
self.mode = mode self.mode = mode
core.debug:print("camera", "mode is now set to " .. mode) core.debug:logInfo("camera", "mode is now set to " .. mode)
return mode return mode
end end

View file

@ -34,7 +34,7 @@ function StiMap:loadCollisions()
for k, objectlayer in pairs(self.sti.layers) do for k, objectlayer in pairs(self.sti.layers) do
if self.world:isCollisionIndexed(objectlayer.name) then if self.world:isCollisionIndexed(objectlayer.name) then
local debugstring = "loading " .. #objectlayer.objects .. " objects in " .. objectlayer.name .. " collision layer" local debugstring = "loading " .. #objectlayer.objects .. " objects in " .. objectlayer.name .. " collision layer"
core.debug:print("map/sti", debugstring) core.debug:logDebug("map/sti", debugstring)
for k, object in pairs(objectlayer.objects) do for k, object in pairs(objectlayer.objects) do
self:newCollision(objectlayer, object) self:newCollision(objectlayer, object)
end end
@ -47,7 +47,7 @@ function StiMap:loadActors()
for k, objectlayer in pairs(self.sti.layers) do for k, objectlayer in pairs(self.sti.layers) do
if self.world:isActorIndexed(objectlayer.name) then if self.world:isActorIndexed(objectlayer.name) then
local debugstring = "loading " .. #objectlayer.objects .. " objects in " .. objectlayer.name .. " actor layer" local debugstring = "loading " .. #objectlayer.objects .. " objects in " .. objectlayer.name .. " actor layer"
core.debug:print("map/sti", debugstring) core.debug:logDebug("map/sti", debugstring)
for k, object in pairs(objectlayer.objects) do for k, object in pairs(objectlayer.objects) do
if (object.properties.batchActor) then if (object.properties.batchActor) then
self:batchActor(objectlayer, object) self:batchActor(objectlayer, object)
@ -64,7 +64,7 @@ function StiMap:loadPlayers()
for k, objectlayer in pairs(self.sti.layers) do for k, objectlayer in pairs(self.sti.layers) do
if (objectlayer.name == "player") then if (objectlayer.name == "player") then
local debugstring = "loading at most " .. #objectlayer.objects .. " actors in " .. objectlayer.name .. " actor layer" local debugstring = "loading at most " .. #objectlayer.objects .. " actors in " .. objectlayer.name .. " actor layer"
core.debug:print("map/sti", debugstring) core.debug:logDebug("map/sti", debugstring)
local i = 1 local i = 1
for k, object in pairs(objectlayer.objects) do for k, object in pairs(objectlayer.objects) do
self:newPlayer(object, i) self:newPlayer(object, i)
@ -115,7 +115,7 @@ function StiMap:newCollision(objectlayer, object)
local y = object.y local y = object.y
if (fromTop) then if (fromTop) then
local poschange = z .. ";" .. y .. " => " .. z-d .. ";" .. y+z local poschange = z .. ";" .. y .. " => " .. z-d .. ";" .. y+z
core.debug:print("map/sti", "create from top, set z and y: " .. poschange) core.debug:logDebug("map/sti", "create from top, set z and y: " .. poschange)
y = y + z y = y + z
z = z - d z = z - d
end end
@ -129,7 +129,7 @@ function StiMap:newPlayer(object, i)
local y = object.y local y = object.y
if (adaptPosition) then if (adaptPosition) then
core.debug:print("map/sti", "adapting position, set y: " .. y .. " => ", y+z) core.debug:logDebug("map/sti", "adapting position, set y: " .. y .. " => ", y+z)
y = y + z y = y + z
end end

View file

@ -26,7 +26,7 @@ Game = require "game"
scenes = require "scenes" scenes = require "scenes"
function love.load() function love.load()
birb.startCore() birb.startCore(3)
game = Game() game = Game()
game:read(1) game:read(1)