epervier-old/birb/core/debug.lua

151 lines
4.1 KiB
Lua
Raw Normal View History

-- core/debug.lua :: A basic framework for debug.
--[[
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()
local lovebird = require("birb.libs.lovebird")
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)
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
2019-07-24 11:19:10 +02:00
-- PRINT FUNCTIONS
-- Print and log debug 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))
2019-07-24 11:19:10 +02:00
end
end
--- 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
--- 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
--- 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
2021-01-17 12:44:25 +01:00
--- Log an error and fail
--
-- @param context the context of the log
-- @param string the logged string
function DebugSystem:fail(context, string)
if (self:isError()) then
print(self:createLogLine("ERROR", context, string))
error(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 = self.core:getName() .. "|" .. os.date("%x-%X") .. "|"
return head .. level .. "|" .. context .. ": " .. string;
end
2019-07-24 11:19:10 +02:00
return DebugSystem