diff --git a/CHANGELOG.md b/CHANGELOG.md index fb82064..0a9482c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/birb/core/debug.lua b/birb/core/debug.lua index 53d62f2..7259a24 100644 --- a/birb/core/debug.lua +++ b/birb/core/debug.lua @@ -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 diff --git a/birb/core/init.lua b/birb/core/init.lua index 21dd59e..9572ba3 100644 --- a/birb/core/init.lua +++ b/birb/core/init.lua @@ -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) diff --git a/birb/core/input.lua b/birb/core/input.lua index 4653aa1..b8d0c70 100644 --- a/birb/core/input.lua +++ b/birb/core/input.lua @@ -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 diff --git a/birb/core/lang.lua b/birb/core/lang.lua index 260c82d..1d8fb50 100644 --- a/birb/core/lang.lua +++ b/birb/core/lang.lua @@ -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 diff --git a/birb/core/options.lua b/birb/core/options.lua index 0067e62..a137df0 100644 --- a/birb/core/options.lua +++ b/birb/core/options.lua @@ -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 diff --git a/birb/init.lua b/birb/init.lua index a43f79f..f0e7f8e 100644 --- a/birb/init.lua +++ b/birb/init.lua @@ -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") diff --git a/birb/modules/assets/animator.lua b/birb/modules/assets/animator.lua index 9a42ba8..c5e322f 100644 --- a/birb/modules/assets/animator.lua +++ b/birb/modules/assets/animator.lua @@ -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 diff --git a/birb/modules/assets/init.lua b/birb/modules/assets/init.lua index 8dd7fb4..84b0e86 100644 --- a/birb/modules/assets/init.lua +++ b/birb/modules/assets/init.lua @@ -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 diff --git a/birb/modules/scenes.lua b/birb/modules/scenes.lua index c615f6f..4cd67f5 100644 --- a/birb/modules/scenes.lua +++ b/birb/modules/scenes.lua @@ -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 diff --git a/birb/modules/timers/init.lua b/birb/modules/timers/init.lua index 50206f4..4ea7e20 100644 --- a/birb/modules/timers/init.lua +++ b/birb/modules/timers/init.lua @@ -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) diff --git a/birb/modules/world/actors/actor2D.lua b/birb/modules/world/actors/actor2D.lua index 5038eb7..0dedb5e 100644 --- a/birb/modules/world/actors/actor2D.lua +++ b/birb/modules/world/actors/actor2D.lua @@ -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 diff --git a/birb/modules/world/actors/actor3D.lua b/birb/modules/world/actors/actor3D.lua index e00786e..c7eab0f 100644 --- a/birb/modules/world/actors/actor3D.lua +++ b/birb/modules/world/actors/actor3D.lua @@ -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 diff --git a/birb/modules/world/actors/baseactor.lua b/birb/modules/world/actors/baseactor.lua index a639b40..a119c5d 100644 --- a/birb/modules/world/actors/baseactor.lua +++ b/birb/modules/world/actors/baseactor.lua @@ -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 diff --git a/birb/modules/world/actors/gfx2D.lua b/birb/modules/world/actors/gfx2D.lua index 955170a..666369f 100644 --- a/birb/modules/world/actors/gfx2D.lua +++ b/birb/modules/world/actors/gfx2D.lua @@ -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 diff --git a/birb/modules/world/actors/gfx3D.lua b/birb/modules/world/actors/gfx3D.lua index 5bc465b..868cdc4 100644 --- a/birb/modules/world/actors/gfx3D.lua +++ b/birb/modules/world/actors/gfx3D.lua @@ -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 diff --git a/birb/modules/world/actors/utils/boxes/mapped.lua b/birb/modules/world/actors/utils/boxes/mapped.lua index e733cab..dc0cc04 100644 --- a/birb/modules/world/actors/utils/boxes/mapped.lua +++ b/birb/modules/world/actors/utils/boxes/mapped.lua @@ -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)) diff --git a/birb/modules/world/baseworld.lua b/birb/modules/world/baseworld.lua index 3804867..35b0cdb 100644 --- a/birb/modules/world/baseworld.lua +++ b/birb/modules/world/baseworld.lua @@ -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 diff --git a/birb/modules/world/camera/init.lua b/birb/modules/world/camera/init.lua index 9a136c4..b5e4943 100644 --- a/birb/modules/world/camera/init.lua +++ b/birb/modules/world/camera/init.lua @@ -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 diff --git a/birb/modules/world/maps/sti.lua b/birb/modules/world/maps/sti.lua index ca10745..7de6837 100644 --- a/birb/modules/world/maps/sti.lua +++ b/birb/modules/world/maps/sti.lua @@ -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 diff --git a/examples/main.lua b/examples/main.lua index b0f6ba0..622cf65 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -26,7 +26,7 @@ Game = require "game" scenes = require "scenes" function love.load() - birb.startCore() + birb.startCore(3) game = Game() game:read(1)