Module:SimpleDebug/doc
![]() | This is a documentation subpage for Module:SimpleDebug. It may contain usage information, categories and other content that is not part of the original module page. |
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
One or several points to watch | ||
---|---|---|
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. |
One point to watch | ||
Functions | ||
function var (avar, where) |
| |
array (anarray, where) |
| |
namearray (anarray, 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 | |
wheretos (where) |
where: a explained point to watch stored in | |
vartos (avar, where) |
Equal to | |
arraytos (anarray, where) |
Equal to | |
namearraytos (anarray, where) |
Equal to |
Examples
One point to watch
Following the flow
local sd = require "Module:SimpleDebug"
return sd.var('Here is reached')
returns:
Here is reached
Number of decimal places and value of a variable
local sd = require "Module:SimpleDebug"
sd.dec = 2
return sd.var(1/3)
returns:
0.33
The value of a table
local sd = require "Module:SimpleDebug"
local a = {{1,2,3},{4,5,6},{7,8,9}}
return sd.var(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.var(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.var(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.var(a))
displays:
Lua error:Module:YourModule:Line:{ { 1, 2, 3, } , { 4, 5, 6, } , { 7, 8, 9, } , }
The value of a table in multiline
local sd = require "Module:SimpleDebug"
SD.tab.oneline = false
local a = {{First=1,2,3},'Middle',{4,Second=5,6}}
return sd.var(a)
retorna:
table#1 { table#2 { 2, 3, ["First"] = 1, }, "Middle", table#3 { 4, 6, ["Second"] = 5, }, }
The value of several variables in a point
local sd = require "Module:SimpleDebug"
local a = 12
local b = 'Hello'
return sd.array ({a,b})
returns:
12 • "Hello"
The value of several variables with their name in a point
local sd = require "Module:SimpleDebug"
local a = 12
local b = 'Hello'
return sd.namearray ({'a',a,'b',b})
returns:
a: 12 • b: "Hello"
Several points to watch
Following the flow
local sd = require "Module:SimpleDebug"
local tab = {1,12,7}
function p.CheckValues ()
local function LittleNum()
sd.wheretos ('little number')
end
local function BigNum(num)
sd.wheretos ('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
local sd = require "Module:SimpleDebug"
a = 12
b = 'Hello'
sd.arraytos ({a,b},1)
a = a + a
b = b..' world!'
sd.arraytos ({'a',a,'b',b},'Finally')
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.namearraytos ({'a',a,'b',b,'c',c},1)
a = a + a
b = b..' world!'
sd.namearraytos ({'a',a,'b',b},'Finally')
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
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.arraytos ({string.len(a), a}, i)
end
displays:
Lua error:Module:YourModule:Line:
3 => 8 • "AAAAAAAA"
4 => 10 • "AAAAAAAAAA"
5 => 12 • "AAAAAAAAAAAA".