8 ------------------------------------------------------------------------------------------------------------
9 -- function tracing asked in config file ?
10 -- if traceFunctions == true then
11 -- local function traceFunction(what)
13 -- local di = debug.getinfo(2)
15 -- if di.name ~= nil and di.name ~= 'getmetatable' then
16 -- debugInfo(what .. " : " .. tostring(di.name) .. " : " .. tostring(di.short_src) .. " : " .. tostring(di.currentline))
18 -- debug.sethook(traceFunction, "cr")
20 -- debugInfo("turning debug hook on")
21 -- debug.sethook(traceFunction, "c")
23 -- --debugInfo("turning debug hook off")
27 ------------------------------------------------------------------------------------------------------------
28 -- dump objects cycles for the given object
29 function dumpCycles(base
, visitedSet
, infos
)
30 local function fullName(infos
)
33 result
= infos
.Name
.. "." .. result
38 if visitedSet
== nil then
42 infos
= { Name
= "root", Parent
= nil }
44 for k
, v
in pairs(base
) do
46 if type(v
) == "table" then
47 local newInfos
= { Name
= tostring(k
), Parent
= infos
}
49 debugInfo(fullName(visitedSet
[v
]) .. "is referenced by " .. fullName(newInfos
))
51 visitedSet
[v
] = newInfos
-- mark as visited
52 dumpCycles(v
, visitedSet
, newInfos
)
53 visitedSet
[v
] = nil -- only intersted in cycles
60 ------------------------------------------------------------------------------------------------------------
61 -- display time taken to execute a function
62 function profileFunction(func
, name
)
63 assert(type(func
) == "function")
65 name
= debug
.getinfo(func
).name
67 local startTime
= nltime
.getPreciseLocalTime()
69 local endTime
= nltime
.getPreciseLocalTime()
70 --debugInfo(string.format("time for %s is %d", tostring(name), endTime - startTime))
73 -- display time taken to execute a function
74 function profileMethod(table, funcName
, name
)
76 assert(type(funcName
) == "string")
77 assert(type(table[funcName
]) == "function")
79 name
= select(debug
.getinfo(table[funcName
]).name
, funcName
)
81 local startTime
= nltime
.getLocalTime()
82 table[funcName
](table)
83 local endTime
= nltime
.getLocalTime()
84 debugInfo(string.format("time for %s is %f", tostring(name
), (endTime
- startTime
) / 1000))
87 ------------------------------------------------------------------------------------------------------------
88 -- add a break that is triggered when a value in a table has been changed
89 function addBreakOnChange(table, watchedKey
)
90 assert(type(table) == "table")
91 local value
= table[watchedKey
]
93 table[watchedKey
] = nil
94 debugInfo("Adding break on change of key " .. tostring(watchedKey
))
95 local mt
= getmetatable(table)
98 oldNewIndex
= mt
.__newindex
101 if mt
then newMT
= clone(mt
) else newMT
= {} end
103 newMT
.__newindex
= function(table, key
, newValue
)
105 if key
== watchedKey
then
107 debugInfo(debug
.traceback())
108 debugWarning("<addBreakOnChange> Key " .. tostring(watchedKey
) .. " changed to " .. tostring(value
))
110 elseif mt
and mt
.__newindex
then
111 mt
.__newindex(table, key
, newValue
)
113 rawset(table, key
, newValue
)
117 newMT
.__index
= function(table, key
)
119 if key
== watchedKey
then
121 elseif mt
and mt
.__index
then
122 return mt
.__index(table, key
, value
)
124 return rawget(table, key
)
128 setmetatable(table, newMT
)
136 -- replace the assert function with a more verbose one
137 if oldAssert
== nil then
141 function assert(cond
)
143 -- rawDebugInfo(colorTag(255, 0, 255) .. "ASSERTION FAILED !! ")
144 rawDebugInfo("@{FOFF}ASSERTION FAILED !! ")
153 -- special nico stuff : modified lua version calls "__notify_debug" when a garbage collection cycle occurs
154 function __notify_gc()
155 gcStartTime
= nltime
.getLocalTime()
157 function __notify_post_gc()
158 local deltaTime
= nltime
.getLocalTime() - gcStartTime
159 local used
, threshold
= gcinfo()
160 debugInfo(colorTag(255, 0, 0) .. string.format("** GC ** (%d ms) (used = %d kb, threshold = %d kb", deltaTime
, used
, threshold
))
164 --testTable = { tata = 1, toto = 2 }
166 --addBreakOnChange(testTable, "tata")
169 --testTable.tata = testTable.tata + 20