Debug Utilities

Debug System Updated: Dec 10, 2025

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

CommandDescription
clib_debug onEnable debug output
clib_debug offDisable debug output
clib_debug level infoSet minimum level
clib_debug historyShow log history
clib_debug clearClear history
clib_debug file onEnable file logging
clib_debug file offDisable file logging
clib_debug exportExport 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