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).
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
[edit]One or several points to watch | ||
---|---|---|
Function abbreviations: w : where. n : names. v : variables. s : string. | ||
Variables | ||
Name | Default | |
tab.oneline | true |
|
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. |
nohtml | false | In strings, it replaces < for ⪡ and > for ⪢. |
plaintext | false | Deletes html format. |
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 |
counter | false | Adds an autoincremental number at the beginning of each call of a function. |
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]One point to watch
[edit]Following the flow
[edit]localSD=require"Module:SimpleDebug"returnSD.v('Here is reached')
returns:
Here is reached
Number of decimal places and value of a variable
[edit]localSD=require"Module:SimpleDebug"SD.dec=2returnSD.v(1/3)
returns:
0.33
Nohtml
[edit]localSD=require"Module:SimpleDebug"SD.nohtml=truereturnSD.v("<b>bold</b>")
returns:
"⪡b⪢bold⪡/b⪢"
Plaintext
[edit]localSD=require"Module:SimpleDebug"SD.plaintext=truereturnSD.v("<b>bold</b>")
returns:
"bold"
The value of several variables
[edit]localSD=require"Module:SimpleDebug"locala=12localb='Hello'returnSD.v(a,b)
returns:
12 • "Hello"
Non-assigned variable detection
[edit]localSD=require"Module:SimpleDebug"locala=truereturnSD.v(a,b)
returns:
true • nil
The value of a table
[edit]localSD=require"Module:SimpleDebug"locala={1,tab='a','b'}returnSD.v(a)
returns: { 1, "b", [tab]="a", }
localSD=require"Module:SimpleDebug"locala={{1,2,3},{4,5,6},{7,8,9}}returnSD.v(a)
returns:
{ [1] = {1, 2, 3, }, [2] = {4, 5, 6, }, [3] = {7, 8, 9, }, }
localSD=require"Module:SimpleDebug"locala={{First=1,2,3},{4,Second=5,6},{7,8,9}}returnSD.v(a)
returns:
{ [1] = {2, 3, [First]=1, }, [2] = {4, 6, [Second]=5, }, [3] = {7, 8, 9, }, }
localSD=require"Module:SimpleDebug"SD.tab.allidx=truelocala={{1,2,3},{4,nil,6},{7,8,9}}returnSD.v(a)
returns:
{ [1]={[1]=1, [2]=2, [3]=3, }, [2]={[1]=4, [3]=6, }, [3]={[1]=7, [2]=8, [3]=9, }, }
Usually, you implement these functions with error function:
localSD=require"Module:SimpleDebug"locala={{1,2,3},{4,5,6},{7,8,9}}error(SD.v(a))
displays:
Lua error:Module:YourModule:Line:{
[1] = {1, 2, 3, },
[2] = {4, 5, 6, },
[3] = {7, 8, 9, },
}
All values of a table in multiline
[edit]localSD=require"Module:SimpleDebug"SD.tab.oneline=falselocala={{First=1,2,3},'Middle',{4,Second=5,6}}returnSD.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
[edit]localSD=require"Module:SimpleDebug"locala=12localb='Hello'returnSD.nv('a',a,'b',b)
returns:
a: 12 • b: "Hello"
Several points to watch
[edit]Following the flow
[edit]localSD=require"Module:SimpleDebug"localtab={1,12,7}functionp.CheckValues()localfunctionLittleNum()SD.wtos('little number')endlocalfunctionBigNum(num)SD.wtos('big='..num)endfori,numinipairs(tab)doifnum>9thenBigNum(num)elseLittleNum()endenderror(SD.s)end
returns:
Lua Error:Module:Your module:Line:
little number
big=12
little number.
With counter
[edit]localSD=require"Module:SimpleDebug"functionIncrem()localn=0fori=1,3don=n+2SD.vtos(n)endendSD.counter=trueIncrem()returnSD.s
returns:
1 • 2
2 • 4
3 • 6
Monitoring of several variables
[edit]localSD=require"Module:SimpleDebug"a=12b='Hello'SD.vtos(1,a,b)a=a+ab=b..' world!'SD.vtos('Finally',a,b)returnSD.s
returns:
1 => 12 • "Hello"
Finally => 24 • "Hello world!"
localSD=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:
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]localSD=require"Module:SimpleDebug"SD.breakline()SD.enabled=falseSD.maxlines.num=3locala='AA'fori=1,10doa=a+'AA'ifi==3thenSD.enabled=trueendSD.nvtos(i,string.len(a),a)end
displays:
Lua error:Module:YourModule:Line:
3 => 8 • "AAAAAAAA"
4 => 10 • "AAAAAAAAAA"
5 => 12 • "AAAAAAAAAAAA".
--2020-06-16 fix error when vtos(nil), then it showed two nil--2020-06-08 if a variable is a function now is displayed as function (before "function")--2020-06-06 fix error which occasionally happens when a value == nillocalp={}p.s=''p.tab={oneline=true,allidx=false,}p.dec=-1p.maxlines={num=100,doerror=true,}p.enabled=truep.nowiki=falsep.nohtml=falsep._plaintext=falsep.counter=falselocalLinCount=0localvep=' • 'localfunctionMessRaised(n)return'\n\nIt has been reached to '..n..', you can change this limit with "maxlines.num".'endlocalfunctionarrow()return' => 'endfunctionp.breakline()LinCount=LinCount+1p.s=p.s..'\n\n'ifp.counterthenp.s=p.s..LinCount..vependif(LinCount>p.maxlines.num)andp.maxlines.doerrorthenp.pa=p.s..MessRaised(p.maxlines.num)error(p.s,0)endend--breaklinelocalfunctionCheckWhereName(wn,what)ifwn==nilthenreturn'"'..what..'" == nil'elseif(type(wn)=="table")thenreturn'Table as "'..what..'"!'elsereturnwnendend--CheckWhereNamefunctionp._plain(text)--Modified from "Module:Plain text"ifnottextthenreturnendtext=mw.text.killMarkers(text):gsub(' ',' ')--replace nbsp spaces with regular spaces:gsub('<br ?/?>',', ')--replace br with commas:gsub('<span.->(.-)</span>','%1')--remove spans while keeping text inside:gsub('<b>(.-)</b>','%1')--remove bold while keeping text inside:gsub('<i>(.-)</i>','%1')--remove italic while keeping text inside:gsub('<sub>(.-)</sub>','%1')--remove bold while keeping text inside:gsub('<sup>(.-)</sup>','%1')--remove bold while keeping text inside:gsub('<.->.-<.->','')--strip out remaining tags and the text inside:gsub('<.->','')--remove any other tag markup:gsub('%[%[%s*[Ff]ile%s*:.-%]%]','')--strip out files:gsub('%[%[%s*[Ii]mage%s*:.-%]%]','')--strip out use of image::gsub('%[%[%s*[Cc]ategory%s*:.-%]%]','')--strip out categories:gsub('%[%[[^%]]-|','')--strip out piped link text:gsub('[%[%]]','')--then strip out remaining [ and ]:gsub("'''''","")--strip out bold italic markup:gsub("'''?","")--not stripping out '''' gives correct output for bolded text in quotes:gsub('----','')--remove ---- lines:gsub("^%s+","")--strip leading:gsub("%s+$","")--and trailing spaces:gsub("%s+"," ")--strip redundant spacesreturntextend--plainfunctionp._plain_len(text)returnmw.ustring.len(p._plain(text))endfunctionp.plain(frame)returnp._plain(frame.args[1])endfunctionp.plain_len(frame)returnp._plain_len(frame.args[1])endlocalfunctiontotext(text)ifp._plaintextthenreturnp._plain(text)elsereturntextendend--totextlocalfunctionNumToStr(N)if(p.dec==-1)or(N==math.floor(N))thenreturntostring(N)elsereturntostring(math.floor((N*10^p.dec)+0.5)/(10^p.dec))endend--NumToStrlocaliniTab1Line=truefunctionp.containsTab(avar)localresult=falsefork,vinpairs(avar)doiftype(v)=='table'thenresult=truebreakendendreturnresultend--containsTablocalvarlocalfunctionDumTab(tbl,indent)ifnotindentthenindent=1endlocaltoprint=" {\r\n"indent=indent+2fork,vinpairs(tbl)dotoprint=toprint..string.rep(" ",indent)localid=kif(type(k)=="string")thenk='"'..k..'"'endtoprint=toprint.."["..k.."] = "if(type(v)=="number")thentoprint=toprint..NumToStr(v)..",\r\n"elseif(type(v)=="string")thentoprint=toprint.."\""..totext(v).."\",\r\n"elseif(type(v)=="table")thenifiniTab1Lineand(notp.containsTab(v))thenlocalwds='{'forkk,vvinpairs(v)doif(p.tab.allidx==true)or(type(kk)~='number')thenwds=wds..'['..kk..']='..var(vv)..', 'elsewds=wds..var(vv)..', 'endendtoprint=toprint..wds.."},\r\n"elsetoprint=toprint..DumTab(v,indent+2)..",\r\n"endelsetoprint=toprint.."\""..tostring(v).."\",\r\n"endendtoprint=toprint..string.rep(" ",indent-2).."}"returntoprintend--DumTabfunctionvar(avar)localEndStr=''ifavar==nilthenEndStr='nil'elseiftype(avar)=='table'thenif#avar>0thenp.s=p.s..'\r\n'endifp.tab.onelinethenlocalwds='{ 'fork,vinpairs(avar)doif(p.tab.allidx==true)or(type(k)~='number')thenwds=wds..'['..k..']='..var(v)..', 'elsewds=wds..var(v)..', 'endendEndStr=wds..'} 'elseEndStr=DumTab(avar)endelseiftype(avar)=='number'thenEndStr=NumToStr(avar)elseiftype(avar)=='boolean'thenifavar==truethenEndStr='true'elseEndStr='false'endelseiftype(avar)=='function'thenEndStr='function'elseavar=totext(tostring(avar))ifp.nohtmlthenavar=string.gsub(avar,"<","⪡")avar=string.gsub(avar,">","⪢")endEndStr='"'..avar..'"'endreturnEndStrend--varfunctionp.w(where)ifp.enabledthenreturnCheckWhereName(where,'w')endend--wlocalfunctionvarx(avar)iniTab1Line=p.tab.onelineifp.tab.onelineand(type(avar)=='table')thenp.tab.oneline=notp.containsTab(avar)endlocalss=var(avar)p.tab.oneline=iniTab1Linereturnssend--varxfunctionp.v(...)ifp.enabledthenlocalstr=''if#arg==0thenstr='nil'elselocalc=0fork,iinipairs(arg)doc=kend--error (c)fori=1,#argdoifstr~=''thenstr=str..vependstr=str..varx(arg[i])endendreturnstrendend--vfunctionp.wv(where,...)ifp.enabledthenreturnCheckWhereName(where,'w')..arrow()..p.v(unpack(arg))endend--wvfunctionp.nv(...)ifp.enabledthenifmath.mod(#arg,2)~=0thenEndStr='Any parameter has not a name or variable'elselocals=''localIsName=truefunctionConcat(wds)ifs~=''thenifIsNamethens=s..vepelses=s..': 'endends=s..wdsendfori=1,#argdoifIsNamethenConcat(CheckWhereName(arg[i],'n'))IsName=falseelseConcat(varx(arg[i]))IsName=trueendendEndStr=sendreturnEndStrendend--nvfunctionp.wnv(where,...)ifp.enabledthenreturnCheckWhereName(where,'w')..arrow()..p.nv(unpack(arg))endend----------localfunctionEnabAndBl()ifp.enabledthenifLinCount<p.maxlines.numthenp.breakline()returntrueelsep.s=p.s..MessRaised(p.maxlines.num)error(p.s)returnfalseendelsereturnfalseendend--EnabAndBlfunctionp.wtos(where)ifEnabAndBl()thenp.s=p.s..p.w(where)endend--wtosfunctionp.vtos(...)ifEnabAndBl()thenlocalend_nil_count=arg["n"]-#argp.s=p.s..p.v(unpack(arg))if#arg==0thenend_nil_count=end_nil_count-1endfori=1,end_nil_countdop.s=p.s..vep..'nil'endendend--vtosfunctionp.wvtos(where,...)ifEnabAndBl()thenp.s=p.s..p.wv(where,unpack(arg))endend--wvtosfunctionp.nvtos(...)ifEnabAndBl()thenlocalend_nil_count=arg["n"]-#argifend_nil_count>0thenfori=1,arg["n"]doifmath.mod(i,2)~=0thenp.s=p.s..arg[i]..': 'elsep.s=p.s..p.v(arg[i])ifi<arg["n"]thenp.s=p.s..vependendendelsep.s=p.s..p.nv(unpack(arg))endendend--nvtosfunctionp.wnvtos(where,...)ifEnabAndBl()thenlocalend_nil_count=arg["n"]-#argifend_nil_count>0thenp.s=p.s..where..arrow()fori=1,arg["n"]doifmath.mod(i,2)~=0thenp.s=p.s..arg[i]..': 'elsep.s=p.s..p.v(arg[i])ifi<arg["n"]thenp.s=p.s..vependendendelsep.s=p.s..p.wnv(where,unpack(arg))endendend--wnvtosreturnp