2020-05-10 14:06:20 +02:00
|
|
|
-- core/debug.lua :: A basic framework for debug.
|
2019-03-16 12:27:38 +01:00
|
|
|
|
|
|
|
--[[
|
|
|
|
Copyright © 2019 Kazhnuz
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
]]
|
|
|
|
|
|
|
|
local DebugSystem = Object:extend()
|
|
|
|
|
2020-04-05 18:39:08 +02:00
|
|
|
local lovebird = require("birb.libs.lovebird")
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2020-05-10 14:06:20 +02:00
|
|
|
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()
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-05-10 14:06:20 +02:00
|
|
|
--- Update the DebugSystem.
|
|
|
|
-- @param dt the delta time
|
2019-03-16 12:27:38 +01:00
|
|
|
function DebugSystem:update(dt)
|
2020-05-10 14:06:20 +02:00
|
|
|
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)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2019-07-24 11:19:10 +02:00
|
|
|
-- PRINT FUNCTIONS
|
|
|
|
-- Print and log debug string
|
|
|
|
|
2020-05-10 14:06:20 +02:00
|
|
|
--- 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))
|
2019-07-24 11:19:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-10 14:06:20 +02:00
|
|
|
--- 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))
|
2019-07-24 11:19:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-10 14:06:20 +02:00
|
|
|
--- 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))
|
2019-07-24 11:19:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-10 14:06:20 +02:00
|
|
|
--- 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)
|
2020-05-10 14:21:45 +02:00
|
|
|
local head = self.core:getName() .. "|" .. os.date("%x-%X") .. "|"
|
2020-05-10 14:06:20 +02:00
|
|
|
return head .. level .. "|" .. context .. ": " .. string;
|
|
|
|
end
|
2019-07-24 11:19:10 +02:00
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
return DebugSystem
|