Modul:Debug/class
This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
This is a Class Module. It implements a class in Lua using Module:Middleclass. This class provides methods for creating and printing a debug log.
Usage
local classDebug = require('Module:Debug/class')
local debugObject = classDebug:new('handlername')
debugObject:methodcall()
classDebug:staticMethodcall()
To use the debug log, create an object and provide a (unique) handler. Then log things via log(level, text) or classDebug:log(level, text, handler). At any time, you can print the log. When doing so, you can either
- print the whole debug log (over all available handlers)
- or only your specific handler
This class uses a static property to create the debug log, meaning all instances of class debug (all objects) write in one log (ie. a table). Later - depending whether you use the public or the static method - you print only 'your' entries (the one using your handler) or the whole log.
Besides a handler, all entries have a timestamp and a loglevel. On printout, all entries with a loglevel higher than a given number will be ommited.
Constructor
new(handler)
Creates a new Object for class debug.
- handler
- string, mandatory
- identifies your entries. should be unique over all used handlers/objects
- return
- object, of class debug
Methods
Public methods
__tostring()
Returns a string representation of your debug log (all entries identified by your handler). Uses default debug level and default outputtype.
- return
- string, representation of itself
log(level, text)
Creates a log entry.
- level
- integer, mandatory
- defines the log-level of the entry. the lower, the more important.
- text
- string, mandatory
- the actual log entry
- return
- void
printLog(level, outputtype)
Prints all log entries having the same handler as this specific object. Not provided parameters will be defaulted. See static properties.
- level
- integer, suggested
- specifies which log entries will be printed in the log. all entries with a level higher than this number will be omitted.
- outputtype
- string, suggested
- formats the log. supported types are 'plain', 'ol', 'ul', 'pre'
- return
- string, formatted and filtered debug log
Static methods
classDebug:log(level, text, handler)
Creates a log entry. Can be called by other (static) methods without having a debug object at hand.
- level
- integer, mandatory
- defines the log-level of the entry. the lower, the more important.
- text
- string, mandatory
- the actual log entry
- handler
- string, suggested
- helps to identify the log entry. works just like the handler you create when instanciating an object of this class
- return
- void
classDebug:printLog(level, outputtype)
Prints all log entries (effectively doing the same as the public method printLog(level, outputtype) only without the handler restriction). Not provided parameters will be defaulted. See static properties
- level
- integer, suggested
- specifies which log entries will be printed in the log. all entries with a level higher than this number will be omitted.
- outputtype
- string, suggested
- formats the log. supported types are 'plain', 'ol', 'ul', 'pre'
- return
- string, formatted and filtered debug log
Private methods
none
Properties
static
- _debugLog
- table, holds all debug entries
- _defaultDebugLevel
- integer, default log level for printLog methods if none is provided
- _defaultOutputType
- string, default output format for printLog methods if none is provided
- _LANG
- object, mw.language.new( 'de' ) used for timestamp format in debug log
private
Note: all private properties are stored in table _private[self]. _private is a static array that is indexed by self, so the table _private[self] holds all properties for instance self.
- handler
- 'my' log entry handler with which this object marks all entries it loggs.
The above documentation is transcluded from Modul:Debug/class/doc. (edit | history) Subpages of this module. |
local class = require('Module:Middleclass').class
local classDebug = class('Debug')
-- ****************************************************************
-- * properties *
-- ****************************************************************
-- **************** initialization of table for private properties
local _private = setmetatable({}, {__mode = "k"}) -- weak table storing all private attributes
-- **************** declaration of private static properties
-- local _PROPERTY = require( 'Module:' )
local _debugLog = {}
local _numberingByChannel = {}
local _defaultDebugLevel = 1
local _defaultOutputType = 'pre'
local _LANG = mw.language.new( 'de' )
-- ***************************************************************
-- * methods *
-- ***************************************************************
-- **************** declaration of static methods
-- local _wrapLog -- declaration ahead, so this private method can be used in the constructor and in other private methods
function classDebug:initialize(handler) -- constructor
-- initialize all private properties
_private[self] = {
handler = handler or 'NIL',
}
end
function classDebug.static:log(level, text, handler)
local handler = handler or 'NIL'
_numberingByChannel[handler] = _numberingByChannel[handler] and (_numberingByChannel[handler] + 1) or 1
table.insert(_debugLog, {level = level,
text = text,
handler = handler,
line = _numberingByChannel[handler],
ts = _LANG:formatDate('H:i:s', nil, true)}
)
return true
end
function classDebug.static:printLog(level, outputtype)
local str = ''
local level = level or _defaultDebugLevel
local outputtype = outputtype and mw.ustring.lower(outputtype) or _defaultOutputType
for _, entry in pairs(_debugLog) do
if level >= entry.level then
local line = entry.ts .. ' [' .. entry.handler .. '].' .. entry.line .. ' <l:' .. entry.level .. '> - ' .. entry.text
if outputtype == 'ol' or outputtype == 'ul' then
line = '<li>' .. line .. '</li>'
end
str = str .. line .. '\n'
end
end
if #str > 0 then
if outputtype == 'pre' then
str = '<pre>\n' .. str .. '</pre>\n'
elseif outputtype == 'ul' then
str = '<ul>\n' .. str .. '</ul>\n'
elseif outputtype == 'ol' then
str = '<ol>\n' .. str .. '</ol>\n'
end
end
return str
end
-- **************** declaration of private methods
-- none
-- **************** declaration of public methods
function classDebug:__tostring()
return tostring(self:getOutput())
end
function classDebug:log(level, text)
return classDebug:log(level, text, _private[self].handler)
end
function classDebug:printLog(level, outputtype)
local str = ''
local level = level or _defaultDebugLevel
local outputtype = outputtype and mw.ustring.lower(outputtype) or _defaultOutputType
local handler = _private[self].handler
for _, entry in pairs(_debugLog) do
if handler == entry.handler then
if level >= entry.level then
local line = entry.ts .. ' [' .. entry.handler .. '].' .. entry.line .. ' <l:' .. entry.level .. '> - ' .. entry.text
if outputtype == 'ol' or outputtype == 'ul' then
line = '<li>' .. line .. '</li>'
end
str = str .. line .. '\n'
end
end
end
if #str > 0 then
if outputtype == 'pre' then
str = '<pre>\n' .. str .. '</pre>\n'
elseif outputtype == 'ul' then
str = '<ul>\n' .. str .. '</ul>\n'
elseif outputtype == 'ol' then
str = '<ol>\n' .. str .. '</ol>\n'
end
end
return str
end
return classDebug