epervier-old/framework/core/debug.lua

93 lines
2.6 KiB
Lua
Raw Normal View History

2021-12-04 13:18:54 +01:00
-- 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()
2021-12-04 13:18:54 +01:00
local lovebird
2021-12-04 13:18:54 +01:00
local Levels = enum {
"ERROR",
"WARNING",
"INFO",
"DEBUG"
}
function DebugSystem:new(controller, debugLevel)
2021-12-04 13:18:54 +01:00
self.controller = controller
2021-12-04 13:18:54 +01:00
self.debugLevel = debugLevel or Levels.WARNING
self.active = (self.debugLevel == Levels.DEBUG)
if (self.active) then
lovebird = require "framework.libs.lovebird"
2021-12-04 13:18:54 +01:00
lovebird.update()
end
end
function DebugSystem:update(dt)
2021-12-04 13:18:54 +01:00
if (self.active and lovebird ~= nil) then
lovebird.update(dt)
end
end
2021-12-04 13:18:54 +01:00
-- PRINT FUNCTIONS
-- Print and log debug string
2021-12-04 13:18:54 +01:00
function DebugSystem:debug(context, string)
self:log(Levels.DEBUG, context, string)
end
2021-12-04 13:18:54 +01:00
function DebugSystem:print(context, string)
self:log(Levels.INFO, context, string)
end
2021-12-04 13:18:54 +01:00
function DebugSystem:warning(context, string)
self:log(Levels.WARNING, context, string)
2019-07-24 11:19:10 +02:00
end
2021-12-04 13:18:54 +01:00
function DebugSystem:error(context, string)
self:log(Levels.ERROR, context, string)
error(self:getMessage(": ", context, string))
2019-07-24 11:19:10 +02:00
end
2021-12-04 13:18:54 +01:00
function DebugSystem:log(level, context, string)
if (self.debugLevel >= level) then
print(self:getLogLine(Levels[level], context, string))
2019-07-24 11:19:10 +02:00
end
end
2021-12-04 13:18:54 +01:00
function DebugSystem:getLogLine(type, context, string)
local head = self.controller:getIdentity(false) .. "|" .. os.date("%x-%X") .. "|" .. type .. "|"
return head .. self:getMessage("|", context, string)
end
2021-12-04 13:18:54 +01:00
function DebugSystem:getMessage(separator, string1, string2)
if (string2 == nil) then
return string1
else
return string1 .. separator .. string2
2021-01-17 12:44:25 +01:00
end
end
2019-07-24 11:19:10 +02:00
return DebugSystem