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
- **core/debug:** add log functions
- **core:** add a way to activate easily debug mode directly
- **core/debug:** new logging framework
- **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
@ -25,36 +25,115 @@ local DebugSystem = Object:extend()
local lovebird = require("birb.libs.lovebird")
function DebugSystem:new(controller, active)
self.controller = controller
lovebird.update()
self.active = active or false
local ERROR = 0
local WARN = 1
local INFO = 2
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
--- Update the DebugSystem.
-- @param dt the delta time
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
-- PRINT FUNCTIONS
-- Print and log debug string
function DebugSystem:print(context, string)
if (self.active) then
print("[DEBUG] ".. context .. ": " .. string)
--- Log a debug line
--
-- @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
function DebugSystem:warning(context, string)
if (self.active) then
print("[WARNING] " .. context .. ": " .. string)
--- Log an info
--
-- @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
function DebugSystem:error(context, string)
if (self.active) then
error("[ERROR] " .. context .. ": " .. string)
--- Log a warning
--
-- @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
--- 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

View file

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

View file

@ -142,7 +142,7 @@ function VirtualPad:isDown(key)
isdown = love.keyboard.isDown(self.data.keys[key])
else
local warnstring = "unsupported input device " .. self.type .. " for source " .. self.id
core.debug:warning("core/input", warnstring)
core.debug:logWarn("core/input", warnstring)
end
return isdown
@ -158,13 +158,13 @@ function VirtualPad:checkKey(key)
local isDown = self:isDown(key)
if (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].isPressed = true
self.keys[key].isReleased = false
else
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
end
end
@ -175,7 +175,7 @@ function VirtualPad:checkKey(key)
self.keys[key].isReleased = true
else
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
end
end

View file

@ -95,7 +95,7 @@ function LanguageManager:getTranslationStringList(lang, library)
if fileinfo ~= nil then
list = require(_path)
else
core.debug:warning("core/lang","file " .. _path .. " do not exists")
core.debug:logWarn("core/lang","file " .. _path .. " do not exists")
end
return list
@ -120,7 +120,7 @@ function LanguageManager:translate(library, string)
if (translation == nil) then
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
return translation

View file

@ -151,11 +151,11 @@ function OptionsManager:read()
filepath = self:getFile(true)
if utils.filesystem.exists("options.data") then
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])
else
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

View file

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

View file

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

View file

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

View file

@ -151,12 +151,12 @@ end
function Scene:getKeys(sourceid)
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
local sourceid = sourceid or 1
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
else
return self.sources[sourceid].keys

View file

@ -60,7 +60,7 @@ end
function TimersManager:timerResponse(timername)
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
end
self.subject:timerResponse(timername)

View file

@ -163,7 +163,7 @@ end
function Actor2D:addHitbox(name, type, ox, oy, w, h, isSolid)
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
local hitbox = Hitbox(self, type, ox, oy, w, h, isSolid)
self.hitboxes[name] = hitbox

View file

@ -187,7 +187,7 @@ end
function Actor3D:addHitbox(name, type, ox, oy, oz, w, h, d, isSolid)
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
local hitbox = Hitbox(self, type, ox, oy, oz, w, h, d, isSolid)
self.hitboxes[name] = hitbox

View file

@ -234,7 +234,7 @@ function BaseActor:initTimers()
end
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)
end
@ -373,7 +373,7 @@ end
function BaseActor:changeAnimation(animation, restart)
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)
end
end
@ -384,7 +384,7 @@ end
function BaseActor:setCustomSpeed(customSpeed)
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)
end
end
@ -397,33 +397,33 @@ end
function BaseActor:getCurrentAnimation()
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()
end
end
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()
end
function BaseActor:getFrame()
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()
end
end
function BaseActor:getRelativeFrame()
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()
end
end
function BaseActor:getAnimationDuration()
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()
end
end

View file

@ -33,7 +33,7 @@ function GFX:new(world, x, y, spritename)
end
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()
end

View file

@ -33,7 +33,7 @@ function GFX:new(world, x, y, z, spritename)
end
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()
end

View file

@ -37,7 +37,7 @@ end
function MappedBox:drawTextureContent()
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.origin()
love.graphics.translate(math.floor(-tx), math.floor(-ty))

View file

@ -104,7 +104,7 @@ end
function BaseWorld:newActor(name, x, y, z)
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)
end
@ -112,7 +112,7 @@ function BaseWorld:newCollision(name, x, y, z, w, h, d)
local debugstringpos = "at (" .. x .. ";" .. y .. ")"
local debugstringsize = "size is (" .. w .. ";" .. h .. ")"
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)
end

View file

@ -50,7 +50,7 @@ end
function CameraSystem:setMode(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
end

View file

@ -34,7 +34,7 @@ function StiMap:loadCollisions()
for k, objectlayer in pairs(self.sti.layers) do
if self.world:isCollisionIndexed(objectlayer.name) then
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
self:newCollision(objectlayer, object)
end
@ -47,7 +47,7 @@ function StiMap:loadActors()
for k, objectlayer in pairs(self.sti.layers) do
if self.world:isActorIndexed(objectlayer.name) then
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
if (object.properties.batchActor) then
self:batchActor(objectlayer, object)
@ -64,7 +64,7 @@ function StiMap:loadPlayers()
for k, objectlayer in pairs(self.sti.layers) do
if (objectlayer.name == "player") then
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
for k, object in pairs(objectlayer.objects) do
self:newPlayer(object, i)
@ -115,7 +115,7 @@ function StiMap:newCollision(objectlayer, object)
local y = object.y
if (fromTop) then
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
z = z - d
end
@ -129,7 +129,7 @@ function StiMap:newPlayer(object, i)
local y = object.y
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
end

View file

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