sonic-radiance/sonic-radiance.love/birb/core/debug.lua

92 lines
2.6 KiB
Lua
Raw Normal View History

-- core/debug.lua :: The basic internal debug framework of the birb engine.
--[[
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"
2021-05-05 13:15:50 +02:00
local Levels = enum {
"ERROR",
"WARNING",
"INFO",
"DEBUG"
}
function DebugSystem:new(controller)
self.controller = controller
2021-05-05 13:15:50 +02:00
self.debugLevel = self.controller.conf.debugLevel or Levels.INFO
self.active = (self.debugLevel == Levels.DEBUG)
2021-04-03 19:59:41 +02:00
if (self.active) then
lovebird.update()
end
end
function DebugSystem:update(dt)
2021-04-03 19:59:41 +02:00
if (self.active) then
lovebird.update(dt)
end
end
2019-07-26 16:46:43 +02:00
-- PRINT FUNCTIONS
-- Print and log debug string
2021-05-05 13:15:50 +02:00
function DebugSystem:debug(context, string)
self:log(Levels.DEBUG, context, string)
end
2019-07-26 16:46:43 +02:00
function DebugSystem:print(context, string)
2021-05-05 13:15:50 +02:00
self:log(Levels.INFO, context, string)
2019-07-26 16:46:43 +02:00
end
function DebugSystem:warning(context, string)
2021-05-05 13:15:50 +02:00
self:log(Levels.WARNING, context, string)
2019-07-26 16:46:43 +02:00
end
function DebugSystem:error(context, string)
2021-05-05 13:15:50 +02:00
self:log(Levels.ERROR, context, string)
error(self:getMessage(": ", context, string))
end
function DebugSystem:log(level, context, string)
if (self.debugLevel >= level) then
print(self:getLogLine(Levels[level], context, string))
end
end
function DebugSystem:getLogLine(type, context, string)
local head = self.controller:getIdentity(false) .. "|" .. os.date("%x-%X") .. "|" .. type .. "|"
return head .. self:getMessage("|", context, string)
2021-04-18 14:56:16 +02:00
end
2021-05-05 13:15:50 +02:00
function DebugSystem:getMessage(separator, string1, string2)
2021-04-18 14:56:16 +02:00
if (string2 == nil) then
2021-05-05 13:15:50 +02:00
return string1
2021-04-18 14:56:16 +02:00
else
2021-05-05 13:15:50 +02:00
return string1 .. separator .. string2
2019-07-26 16:46:43 +02:00
end
end
return DebugSystem