Debug Utilities
Print Tables
-- Print table structure
cLib.Debug.PrintTable(myTable)
-- Example output:
-- name = John
-- level = 5
-- inventory = {
-- item1 = 10
-- item2 = 5
-- }
Assertions
-- Assert with custom message
cLib.Debug.Assert(player ~= nil, "Player cannot be nil!")
cLib.Debug.Assert(amount > 0, "Amount must be positive: %d", amount)
-- On failure: logs error and throws error
Timing Functions
-- Time a function execution
local result = cLib.Debug.Time("LoadData", function()
return LoadAllData()
end)
-- Output: [INFO] LoadData took 123.456 ms
Scoped Debugging
local scope = cLib.Debug.Scope("PlayerInit")
scope.Log("Starting initialization")
scope.Set("playerCount", #player.GetAll())
-- Do work...
LoadPlayerData()
InitializeInventory()
scope.End()
-- Output:
-- [DEBUG] [PlayerInit] Starting initialization
-- [INFO] [PlayerInit] Completed in 45.123 ms
-- [DEBUG] [PlayerInit] Data: {playerCount: 5}
History Management
-- Get log history
local history = cLib.Debug.GetHistory()
for _, entry in ipairs(history) do
print(entry.time, entry.levelName, entry.message)
end
-- Clear history
cLib.Debug.ClearHistory()
Console Commands
| Command | Description |
|---|
clib_debug on | Enable debug output |
clib_debug off | Disable debug output |
clib_debug level info | Set minimum level |
clib_debug history | Show log history |
clib_debug clear | Clear history |
clib_debug file on | Enable file logging |
clib_debug file off | Disable file logging |
clib_debug export | Export logs to file |
Practical Example
function MyAddon.ProcessPurchase(ply, itemID)
local scope = cLib.Debug.Scope("Purchase")
scope.Log("Player:", ply:Nick(), "Item:", itemID)
-- Validate
cLib.Debug.Assert(IsValid(ply), "Invalid player")
cLib.Debug.Assert(itemID, "No item ID provided")
local item = GetItem(itemID)
if not item then
cLib.Debug.Warn("Item not found:", itemID)
scope.End()
return false
end
scope.Set("itemPrice", item.price)
scope.Set("playerMoney", ply:GetMoney())
if ply:GetMoney() < item.price then
cLib.Debug.Info("Insufficient funds for", ply:Nick())
scope.End()
return false
end
-- Process
ply:SetMoney(ply:GetMoney() - item.price)
GiveItem(ply, item)
cLib.Debug.Info("Purchase successful:", ply:Nick(), "bought", item.name)
scope.End()
return true
end