Module:SimpleDebug
Contains a functions to help debug the lua modules. It allows to collect and view the values of several variables and/or points in your lua program, from a module (which is usual) or in several modules (which are required from the main module).
Uses[edit source]
One or several points to watch | ||
---|---|---|
Function abbreviations: w : where. n : names. v : variables. s : string.
| ||
Variables | ||
Name | Default | |
tab.oneline |
true |
If true, it shows the table in a line; Alternatively with a line for each element of the table and with an indent for each nested table. |
tab.allidx |
false |
If it is true then also displays the numerical indexes of a table. |
dec |
-1 |
Spaces for the decimals:
|
enabled |
true |
If it is false all calls to the below functions do nothing. |
nowiki |
false |
Adds <nowiki> and </nowiki> to strings. |
One point to watch | ||
Functions | ||
w (where) |
| |
v (...) |
| |
wv (where, ...) |
| |
nv (...) |
| |
wnv (where, ...) |
| |
Several points to watch | ||
Variables | ||
Name | Default | |
s |
The string variable that holds the returned values from the next functions. | |
maxlines.num |
100 |
The maxim number of lines (on calling the next functions). |
maxlines.doerror |
true |
If it is true and |
Functions | ||
breakline () |
Adds a break line in | |
wtos (where) |
Equal to | |
vtos (...) |
Equal to | |
wvtos (where, ...) |
Equal to | |
nvtos (...) |
Equal to | |
wnvtos (where, ...) |
Igual a |
Examples[edit source]
One point to watch[edit source]
Following the flow[edit source]
local sd = require "Module:SimpleDebug"
return sd.v ('Here is reached')
returns:
Here is reached
Number of decimal places and value of a variable[edit source]
local sd = require "Module:SimpleDebug"
sd.dec = 2
return sd.v (1/3)
returns:
0.33
Nowiki[edit source]
local sd = require "Module:SimpleDebug"
sd.nowiki = true
return sd.v ("<b>bold</b>")
returns:
<b>bold</b>
The value of several variables[edit source]
local sd = require "Module:SimpleDebug"
local a = 12
local b = 'Hello'
return sd.v (a,b)
returns:
12 • "Hello"
Non-assigned variable detection[edit source]
local sd = require "Module:SimpleDebug"
local a = true
return sd.v (a,b)
returns:
true • nil
The value of a table[edit source]
local sd = require "Module:SimpleDebug"
local a = {{1,2,3},{4,5,6},{7,8,9}}
return sd.v (a)
returns:
{ { 1, 2, 3, } , { 4, 5, 6, } , { 7, 8, 9, } , }
local sd = require "Module:SimpleDebug"
local a = {{First=1,2,3},{4,Second=5,6},{7,8,9}}
return sd.v (a)
returns:
{ { 2, 3, [First]=1, } , { 4, 6, [Second]=5, } , { 7, 8, 9, } , }
local sd = require "Module:SimpleDebug"
SD.allidx = true
local a = {{1,2,3},{4,5,6},{7,8,9}}
return sd.v (a)
returns:
{ [1]={ [1]=1, [2]=2, [3]=3, } , [2]={ [1]=4, [2]=5, [3]=6, } , [3]={ [1]=7, [2]=8, [3]=9, } , }
Usually, you implement these functions with error function:
local sd = require "Module:SimpleDebug"
local a = {{1,2,3},{4,5,6},{7,8,9}}
error (sd.v (a))
displays:
Lua error:Module:YourModule:Line:{ { 1, 2, 3, } , { 4, 5, 6, } , { 7, 8, 9, } , }
The value of a table in multiline[edit source]
local sd = require "Module:SimpleDebug"
SD.tab.oneline = false
local a = {{First=1,2,3},'Middle',{4,Second=5,6}}
return sd.v (a)
retorna:
table#1 { table#2 { 2, 3, ["First"] = 1, }, "Middle", table#3 { 4, 6, ["Second"] = 5, }, }
The value of several variables with their name in a point[edit source]
local sd = require "Module:SimpleDebug"
local a = 12
local b = 'Hello'
return sd.nv ('a',a,'b',b)
returns:
a: 12 • b: "Hello"
Several points to watch[edit source]
Following the flow[edit source]
local sd = require "Module:SimpleDebug"
local tab = {1,12,7}
function p.CheckValues ()
local function LittleNum()
sd.wtos ('little number')
end
local function BigNum(num)
sd.wtos ('big='..num)
end
for i, num in ipairs(tab) do
if num > 9 then
BigNum(num)
else
LittleNum()
end
end
error (sd.s)
end
returns:
Error de Lua:Mòdul:VostreMòdul:Línia: little number
big=12
little number.
Monitoring of several variables[edit source]
local sd = require "Module:SimpleDebug"
a = 12
b = 'Hello'
sd.vtos (1,a,b)
a = a + a
b = b..' world!'
sd.vtos ('Finally',a,b)
return sd.s
returns:
1 => 12 • "Hello"
Finally => 24 • "Hello world!"
local sd = require "Module:SimpleDebug"
sd.breakline ()
a = 12
b = 'Hello'
c = false
sd.nvtos (1,'a',a,'b',b,'c',c)
a = a + a
b = b..' world!'
sd.nvtos ('Finally','a',a,'b',b)
error (sd.s)
displays:
Lua error:Module:YourModule:Line:
1 => a: 12 • b: "Hello" • c: false
Finally => a: 24 • b: "Hello world!"
Variables and their presentation with conditions[edit source]
local sd = require "Module:SimpleDebug"
sd.breakline()
sd.enabled = false
sd.maxlines.num = 3
local a = 'AA'
for i = 1, 10 do
a = a + 'AA'
if i == 3 then
sd.enabled = true
end
sd.nvtos (i, string.len(a), a)
end
displays:
Lua error:Module:YourModule:Line:
3 => 8 • "AAAAAAAA"
4 => 10 • "AAAAAAAAAA"
5 => 12 • "AAAAAAAAAAAA".
External links[edit source]
- Module:SimpleDebug at Wikipedia, the free Terran-based encyclopedia that anyone can edit.
local p = {}
p.s = ''
p.tab = {
oneline = true,
allidx = false,
}
p.dec = -1
p.maxlines = {
num = 100,
doerror = true,
}
p.enabled = true
p.nowiki = false
local LinCount = 0
local vep = ' • '
local function arrow()
return ' => '
end
function p.breakline ()
if p.s ~= '' then
LinCount = LinCount + 1
end
p.s = p.s..'\n\n'
if (LinCount > p.maxlines.num) and p.maxlines.doerror then
error (p.s,0)
end
end --breakline
local function CheckWhere (where)
if where == nil then
return '"where" == nil'
else
return where
end
end --CheckWhere
local function var (avar)
if type(avar) == 'table' then
if p.tab.oneline then
local wds = '{ '
for k,v in pairs(avar) do
if (p.tab.allidx == true) or (type(k) ~= 'number') then
wds = wds..'['..k..']='..var(v)..', '
else
wds = wds..var(v)..', '
end
end
EndStr = wds .. '} '
else
EndStr = mw.dumpObject(avar)
end
elseif type(avar) == 'number' then
if (p.dec == -1) or (avar == math.floor(avar)) then
EndStr = tostring(avar)
else
EndStr = tostring (math.floor ((avar*10^p.dec)+0.5) / (10^p.dec))
end
elseif type(avar) == 'boolean' then
if avar == true then
EndStr = 'true'
else
EndStr = 'false'
end
elseif avar == nil then
EndStr = 'nil'
elseif p.nowiki then
EndStr = '<nowiki>'..tostring(avar)..'</nowiki>'
else
EndStr = '"'..tostring(avar)..'"'
end
return EndStr
end --var
function p.w (where)
if p.enabled then
return CheckWhere (where)
end
end --w
function p.v (...)
if p.enabled then
local str = ''
for i = 1, #arg do
if str ~= '' then
str = str..vep
end
str = str..var(arg[i])
end
return str
end
end --v
function p.wv (where, ...)
if p.enabled then
return CheckWhere(where)..arrow()..p.v(unpack(arg))
end
end --wv
function p.nv (...)
if p.enabled then
if math.mod(#arg,2) ~= 0 then
EndStr = 'Any parameter has not a name or variable'
else
local s = ''
local IsName = true
function Concat(wds)
if s ~= '' then
if IsName then
s = s..vep
else
s = s..': '
end
end
s = s..wds
end
for i = 1, #arg do
if IsName then
Concat (CheckWhere(arg[i]))
IsName = false
else
Concat (var(arg[i]))
IsName = true
end
end
EndStr = s
end
return EndStr
end
end --nv
function p.wnv (where, ...)
if p.enabled then
return CheckWhere(where)..arrow()..p.nv (unpack(arg))
end
end
----------
local function EnabAndBl ()
if p.enabled and (LinCount < p.maxlines.num) then
if p.s ~= '' then
p.breakline ()
end
return true
else
return false
end
end --EnabAndBl
function p.wtos (where)
if EnabAndBl () then
p.s = p.s..p.w (where)
end
end --wtos
function p.vtos (...)
if EnabAndBl () then
p.s = p.s..p.v (unpack(arg))
end
end --vtos
function p.wvtos (where, ...)
if EnabAndBl () then
p.s = p.s..p.wv (where,unpack(arg))
end
end --wvtos
function p.nvtos (...)
if EnabAndBl () then
p.s = p.s..p.nv (unpack(arg))
end
end --nvtos
function p.wnvtos (where, ...)
if EnabAndBl () then
p.s = p.s..p.wnv (where, unpack(arg))
end
end --wnvtos
return p