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=2 returnSD.v(1/3)
returns:
0.33
Nohtml
[edit ]localSD=require"Module:SimpleDebug" SD.nohtml=true returnSD.v("<b>bold</b>")
returns:
"⪡b⪢bold⪡/b⪢"
Plaintext
[edit ]localSD=require"Module:SimpleDebug" SD.plaintext=true returnSD.v("<b>bold</b>")
returns:
"bold"
The value of several variables
[edit ]localSD=require"Module:SimpleDebug" locala=12 localb='Hello' returnSD.v(a,b)
returns:
12 • "Hello"
Non-assigned variable detection
[edit ]localSD=require"Module:SimpleDebug" locala=true returnSD.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=true locala={{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=false locala={{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=12 localb='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') end localfunctionBigNum(num) SD.wtos('big='..num) end fori,numinipairs(tab)do ifnum>9then BigNum(num) else LittleNum() end end error(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=0 fori=1,3do n=n+2 SD.vtos(n) end end SD.counter=true Increm() returnSD.s
returns:
1 • 2
2 • 4
3 • 6
Monitoring of several variables
[edit ]localSD=require"Module:SimpleDebug" a=12 b='Hello' SD.vtos(1,a,b) a=a+a b=b..' world!' SD.vtos('Finally',a,b) returnSD.s
returns:
1 => 12 • "Hello"
Finally => 24 • "Hello world!"
localSD=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 ]localSD=require"Module:SimpleDebug" SD.breakline() SD.enabled=false SD.maxlines.num=3 locala='AA' fori=1,10do a=a+'AA' ifi==3then 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".
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
--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 == nil localp={} p.s='' p.tab={ oneline=true, allidx=false, } p.dec=-1 p.maxlines={ num=100, doerror=true, } p.enabled=true p.nowiki=false p.nohtml=false p._plaintext=false p.counter=false localLinCount=0 localvep=' • ' localfunctionMessRaised(n) return'\n\nIt has been reached to '..n..', you can change this limit with "maxlines.num".' end localfunctionarrow() return' => ' end functionp.breakline() LinCount=LinCount+1 p.s=p.s..'\n\n' ifp.counterthen p.s=p.s..LinCount..vep end if(LinCount>p.maxlines.num)andp.maxlines.doerrorthen p.pa=p.s..MessRaised(p.maxlines.num) error(p.s,0) end end--breakline localfunctionCheckWhereName(wn,what) ifwn==nilthen return'"'..what..'" == nil' elseif(type(wn)=="table")then return'Table as "'..what..'"!' else returnwn end end--CheckWhereName functionp._plain(text)--Modified from "Module:Plain text" ifnottextthenreturnend text=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 spaces returntext end--plain functionp._plain_len(text) returnmw.ustring.len(p._plain(text)) end functionp.plain(frame) returnp._plain(frame.args[1]) end functionp.plain_len(frame) returnp._plain_len(frame.args[1]) end localfunctiontotext(text) ifp._plaintextthen returnp._plain(text) else returntext end end--totext localfunctionNumToStr(N) if(p.dec==-1)or(N==math.floor(N))then returntostring(N) else returntostring(math.floor((N*10^p.dec)+0.5)/(10^p.dec)) end end--NumToStr localiniTab1Line=true functionp.containsTab(avar) localresult=false fork,vinpairs(avar)do iftype(v)=='table'then result=true break end end returnresult end--containsTab localvar localfunctionDumTab(tbl,indent) ifnotindentthenindent=1end localtoprint=" {\r\n" indent=indent+2 fork,vinpairs(tbl)do toprint=toprint..string.rep(" ",indent) localid=k if(type(k)=="string")then k='"'..k..'"' end toprint=toprint.."["..k.."] = " if(type(v)=="number")then toprint=toprint..NumToStr(v)..",\r\n" elseif(type(v)=="string")then toprint=toprint.."\""..totext(v).."\",\r\n" elseif(type(v)=="table")then ifiniTab1Lineand(notp.containsTab(v))then localwds='{' forkk,vvinpairs(v)do if(p.tab.allidx==true)or(type(kk)~='number')then wds=wds..'['..kk..']='..var(vv)..', ' else wds=wds..var(vv)..', ' end end toprint=toprint..wds.."},\r\n" else toprint=toprint..DumTab(v,indent+2)..",\r\n" end else toprint=toprint.."\""..tostring(v).."\",\r\n" end end toprint=toprint..string.rep(" ",indent-2).."}" returntoprint end--DumTab functionvar(avar) localEndStr='' ifavar==nilthen EndStr='nil' elseiftype(avar)=='table'then if#avar>0then p.s=p.s..'\r\n' end ifp.tab.onelinethen localwds='{ ' fork,vinpairs(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=DumTab(avar) end elseiftype(avar)=='number'then EndStr=NumToStr(avar) elseiftype(avar)=='boolean'then ifavar==truethen EndStr='true' else EndStr='false' end elseiftype(avar)=='function'then EndStr='function' else avar=totext(tostring(avar)) ifp.nohtmlthen avar=string.gsub(avar,"<","⪡") avar=string.gsub(avar,">","⪢") end EndStr='"'..avar..'"' end returnEndStr end--var functionp.w(where) ifp.enabledthen returnCheckWhereName(where,'w') end end--w localfunctionvarx(avar) iniTab1Line=p.tab.oneline ifp.tab.onelineand(type(avar)=='table')then p.tab.oneline=notp.containsTab(avar) end localss=var(avar) p.tab.oneline=iniTab1Line returnss end--varx functionp.v(...) ifp.enabledthen localstr='' if#arg==0then str='nil' else localc=0 fork,iinipairs(arg)do c=k end --error (c) fori=1,#argdo ifstr~=''then str=str..vep end str=str..varx(arg[i]) end end returnstr end end--v functionp.wv(where,...) ifp.enabledthen returnCheckWhereName(where,'w')..arrow()..p.v(unpack(arg)) end end--wv functionp.nv(...) ifp.enabledthen ifmath.mod(#arg,2)~=0then EndStr='Any parameter has not a name or variable' else locals='' localIsName=true functionConcat(wds) ifs~=''then ifIsNamethen s=s..vep else s=s..': ' end end s=s..wds end fori=1,#argdo ifIsNamethen Concat(CheckWhereName(arg[i],'n')) IsName=false else Concat(varx(arg[i])) IsName=true end end EndStr=s end returnEndStr end end--nv functionp.wnv(where,...) ifp.enabledthen returnCheckWhereName(where,'w')..arrow()..p.nv(unpack(arg)) end end ---------- localfunctionEnabAndBl() ifp.enabledthen ifLinCount<p.maxlines.numthen p.breakline() returntrue else p.s=p.s..MessRaised(p.maxlines.num) error(p.s) returnfalse end else returnfalse end end--EnabAndBl functionp.wtos(where) ifEnabAndBl()then p.s=p.s..p.w(where) end end--wtos functionp.vtos(...) ifEnabAndBl()then localend_nil_count=arg["n"]-#arg p.s=p.s..p.v(unpack(arg)) if#arg==0then end_nil_count=end_nil_count-1 end fori=1,end_nil_countdo p.s=p.s..vep..'nil' end end end--vtos functionp.wvtos(where,...) ifEnabAndBl()then p.s=p.s..p.wv(where,unpack(arg)) end end--wvtos functionp.nvtos(...) ifEnabAndBl()then localend_nil_count=arg["n"]-#arg ifend_nil_count>0then fori=1,arg["n"]do ifmath.mod(i,2)~=0then p.s=p.s..arg[i]..': ' else p.s=p.s..p.v(arg[i]) ifi<arg["n"]then p.s=p.s..vep end end end else p.s=p.s..p.nv(unpack(arg)) end end end--nvtos functionp.wnvtos(where,...) ifEnabAndBl()then localend_nil_count=arg["n"]-#arg ifend_nil_count>0then p.s=p.s..where..arrow() fori=1,arg["n"]do ifmath.mod(i,2)~=0then p.s=p.s..arg[i]..': ' else p.s=p.s..p.v(arg[i]) ifi<arg["n"]then p.s=p.s..vep end end end else p.s=p.s..p.wnv(where,unpack(arg)) end end end--wnvtos returnp