Mòideal:Log globals
Coltas
(deasbaireachd⧼tpt-languages-separator⧽ ⧼tpt-languages-separator⧽eachdraidh⧼tpt-languages-separator⧽ceanglaichean⧼tpt-languages-separator⧽doc⧼tpt-languages-separator⧽bogsa-gainmhich⧼tpt-languages-separator⧽cùisean deuchainn)
This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
This module finds nil globals and adds the to the lua log along with where they were read/written. This module is different from [[Mòideal::No globals]] as you can see all nil global variables that are being read/written to rather then only getting an error for the first problematic global variable. The arg variable is excluded.
Faic cuideachd - Also see
- [[Mòideal::No globals]] - Creates error message for first encountered nil global read/write
local mt = getmetatable(_G) or {}
local function print(val)
if type(val) == "table" then
local printout = {}
local i = 1
for k, v in pairs(val) do
table.insert(printout, ("[%s] = %s"):format(tostring(k), tostring(v)) )
i = i + 1
if i > 5 then
table.insert(printout, "...")
break
end
end
printout = { table.concat(printout, ", ") }
table.insert(printout, 1, "{")
table.insert(printout, "}")
return table.concat(printout)
elseif type(val) == "string" then
return '"' .. val .. '"'
else
return tostring(val)
end
end
mt.__newindex = function (self, key, value)
if key ~= "arg" then
mw.log("Global variable " .. print(key) .. " was set to "
.. print(value) .. " somewhere:",
debug.traceback("", 2))
end
return rawset(self, key, value)
end
mt.__index = function (self, key)
if key ~= "arg" then
mw.log("Nil global variable " .. print(key) .. " was read somewhere:",
debug.traceback("", 2))
end
return rawget(self, key)
end
setmetatable(_G, mt)