Module:Log globals: Difference between revisions
Appearance
hp>Erutuon m return rawget and rawset so as not to interfere with operation of module |
Humanipedia (talk | contribs) m 1 revision imported |
(No difference)
|
Latest revision as of 00:36, 28 January 2025
Lua error in package.lua at line 80: module 'Module:Yesno' not found. Template:Transclusionless
This module finds nil global variable use and adds the to the Lua log along with where they were read/written. This module is different from Template:Ml-lua as you can see all nil global variables that are being read/written to rather than only getting an error for the first problematic global variable. The arg variable is excluded.
See also[edit source]
- Template:Ml-lua - 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)