Module:SimpleDebug/doc explained
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).
It is designed so that its functions are called from within the module that is to be debugged, calls that will have to be part of the code (of the module that you have designed, or that you want to improve or adapt) until you decide to delete them (when you already have determined the bug). Thus, you do not have to call any of its functions from an invoke.
Uses
One or several points to watch |
---|
Function abbreviations: w : where. n : names. v : variables. s : string. |
Variables |
Name | Default | |
tab.oneline | true | - If it is false or it is true and contains nested tables, it will show a line for each item in the table and with an indent for each nested table.
- If it is true and it does not contain nested tables, it shows the table in a line.
|
tab.allidx | false | If it is true then also displays the numerical indexes of a table. |
dec | -1 | Spaces for the decimals:- -1: It displays all required decimals.
- 0: No decimals.
- n: 1: one decimal, 2: two decimals, etc.
|
enabled | true | If it is false all calls to the below functions do nothing. |
nohtml | false | In strings, it replaces < for ⪡ and > for ⪢. |
plaintext | false | Deletes html format. |
One point to watch |
---|
Functions |
w (where) |
|
v (...) | - ...: a number of variables = var1, var2...
|
wv (where, ...) | - where: point label.
- ...: a number of variables = var1, var2...
|
nv (...) | - ...: a number of pairs of name-variable = name1, var1, name2, var2....
|
wnv (where, ...) | - where: point label.
- ...: a number of pairs of name-variable = name1, var1, name2, var2....
|
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 maxlines.num is reached, error(s) is called. |
counter | false | Adds an autoincremental number at the beginning of each call of a function. |
Functions |
breakline | Adds a break line in s . |
wtos (where) | Equal to w , but the return string is stored in s . |
vtos (...) | Equal to v , but the return string is stored in s . |
wvtos (where, ...) | Equal to wv , but the return string is stored in s . |
nvtos (...) | Equal to nv , but the return string is stored in s . |
wnvtos (where, ...) | Igual a wnv , but the return string is stored in s . | |
Examples
One point to watch
Following the flow
local SD = require "Module:SimpleDebug"return SD.v ('Here is reached')returns:
Here is reached
Number of decimal places and value of a variable
local SD = require "Module:SimpleDebug"SD.dec = 2return SD.v (1/3)returns:
0.33
Nohtml
local SD = require "Module:SimpleDebug"SD.nohtml = truereturn SD.v ("bold")returns:
"⪡b⪢bold⪡/b⪢"
Plaintext
local SD = require "Module:SimpleDebug"SD.plaintext = truereturn SD.v ("bold")returns:
"bold"
The value of several variables
local SD = require "Module:SimpleDebug"local a = 12local b = 'Hello'return SD.v (a,b)returns:
12 • "Hello"
Non-assigned variable detection
local SD = require "Module:SimpleDebug"local a = truereturn SD.v (a,b)returns:
true • nil
The value of a table
local SD = require "Module:SimpleDebug"local a = return SD.v (a)
returns:
local SD = require "Module:SimpleDebug"local a = return SD.v (a)returns:
local SD = require "Module:SimpleDebug"local a = return SD.v (a)returns:
local SD = require "Module:SimpleDebug"SD.tab.allidx = truelocal a = return SD.v (a)returns:
Usually, you implement these functions with error function:local SD = require "Module:SimpleDebug"local a = error (SD.v (a))displays:
All values of a table in multiline
local SD = require "Module:SimpleDebug"SD.tab.oneline = falselocal a = return SD.v (a)retorna:
{
[1] = {
[1] = 2,
[2] = 3,
["First"] = 1,
},
[2] = "Middle",
[3] = {
[1] = 4,
[2] = 6,
["Second"] = 5,
},
}
The value of several variables with their name in a point
local SD = require "Module:SimpleDebug"local a = 12local b = 'Hello'return SD.nv ('a',a,'b',b)returns:
a: 12 • b: "Hello"
Several points to watch
Following the flow
local SD = require "Module:SimpleDebug" local tab = 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)endreturns:
With counter
local SD = require "Module:SimpleDebug" function Increm local n = 0 for i = 1, 3 do n = n + 2 SD.vtos (n) endendSD.counter = trueIncremreturn SD.sreturns:
1 • 2
2 • 4
3 • 6
Monitoring of several variables
local SD = require "Module:SimpleDebug"a = 12b = 'Hello'SD.vtos (1,a,b)a = a + ab = b..' world!'SD.vtos ('Finally',a,b)return SD.sreturns:
1 => 12 • "Hello"
Finally => 24 • "Hello world!"
local SD = require "Module:SimpleDebug"SD.breakline a = 12b = 'Hello'c = falseSD.nvtos (1,'a',a,'b',b,'c',c)a = a + ab = b..' world!'SD.nvtos ('Finally','a',a,'b',b)error (SD.s)displays:
Variables and their presentation with conditions
local SD = require "Module:SimpleDebug"SD.breaklineSD.enabled = falseSD.maxlines.num = 3local 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)enddisplays: