Module:log set and get
Appearance
- The following documentation is located at Module:log set and get/documentation. [edit]
- Useful links: subpage list • links • transclusions • testcases • sandbox
Creates a new version of a table that prints setting (__newindex
) and getting (__index
) operations to the log, that is table[key] = value
and value = table[key]
. This can help you identify why a value in your table has vanished or appeared. May not work if the table already has a metatable.
-- unique keys
local inner_key = function() end
local name_key = function() end
local function write_key(self, k)
return (rawget(self, name_key) or "") .. "[" .. tostring(k) .. "]"
end
local mt = {
__newindex = function(self, k, v)
mw.logObject(v, "setting " .. write_key(self, k))
rawset(rawget(self, inner_key), k, v)
end,
__index = function(self, k)
local v = rawget(rawget(self, inner_key), k)
mw.logObject(v, "getting " .. write_key(self, k))
return v
end,
__pairs = function(self)
return pairs(self[inner_key])
end,
__ipairs = function(self)
return ipairs(self[inner_key])
end,
}
return function(t, name)
return setmetatable({ [inner_key] = t, [name_key] = name }, mt)
end