diff options
author | Clement Farabet <clement.farabet@gmail.com> | 2014-07-05 20:32:39 +0400 |
---|---|---|
committer | Clement Farabet <clement.farabet@gmail.com> | 2014-07-05 20:32:39 +0400 |
commit | 734c159c01c9f214ffd65a54d6d227ea71b073b0 (patch) | |
tree | 9015d9befed7f087dab905b40c7325b25e899be9 | |
parent | 8b35f2146a42a89013e9f0f27f0f605288995b51 (diff) |
More advanced globals tracker.
-rw-r--r-- | init.lua | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -310,24 +310,46 @@ end -- Monitor Globals function monitor_G(cb) + -- Force load of penlight packages: + stringx = require 'pl.stringx' + tablex = require 'pl.tablex' + path = require 'pl.path' + dir = require 'pl.dir' + + -- Store current globals: local evercreated = {} for k in pairs(_G) do evercreated[k] = true end + + -- Overwrite global namespace meta tables to monitor it: setmetatable(_G, { __newindex = function(G,key,val) if not evercreated[key] then if cb then cb(key) else - print('created a global variable: ' .. key) + local file = debug.getinfo(2).source:gsub('^@','') + local line = debug.getinfo(2).currentline + if line > 0 then + print(colors.red .. 'created global variable: ' + .. colors.blue .. key .. colors.none + .. ' @ ' .. colors.magenta .. file .. colors.none + .. ':' .. colors.green .. line .. colors.none + ) + else + print(colors.red .. 'created global variable: ' + .. colors.blue .. key .. colors.none + .. ' @ ' .. colors.yellow .. '[C-module]' .. colors.none + ) + end end end evercreated[key] = true rawset(G,key,val) end, __index = function (table, key) - error("attempt to read undeclared variable "..key, 2) + error(colors.red .. "attempt to read undeclared variable " .. colors.blue .. key .. colors.none, 2) end, }) end |