Modul:Log

Aus Star Citizen Wiki
Modul Dokumentation[view][edit][history][purge]
Diese Dokumentation wird von Modul:Log/doc aus übernommen. Änderungen können auf der Diskussionsseite vorgeschlagen werden.
Function list
L 14 — methodtable.makeOutput
L 48 — methodtable.setFrame
L 56 — Log.main
L 82 — Log.info
L 91 — Log.success
L 100 — Log.warning
L 109 — Log.error
L 116 — Log.new

Dies ist ein internes Modul, welches zur Ausgabe von Informationen und Fehlermeldungen gedacht ist.


local Log = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local TNT = require( 'Module:Translate' ):new()


--- Creates the actual output, either a HTML comment if options.silent is true, or a mw-message-box
--- @param severity string
--- @param message string
function methodtable.makeOutput( self, severity, message )
	local currentDate = mw.getContentLanguage():formatDate( 'c' )
	if type( message ) ~= 'string' and type( message ) ~= 'number' then
		message = TNT.formatInLanguage( self.options.lang, 'Module:Log/i18n.json', 'msg_content_error' )
	end

	local content

	-- Output as HTML Comment
	if self.options.silent ~= nil then
		content = string.format(
			'[%s] - %s: %s',
			currentDate,
			TNT.formatInLanguage( self.options.lang, 'Module:Log/i18n.json', severity ),
			message
		)
		content = mw.html.create( 'p' )
			:css( 'display', 'none' )
			:wikitext( content )
	else -- Output as HTML
		content = mw.html.create( 'p' )
			:addClass( 'log' )
			:addClass( 'mw-message-box' )
			:addClass( 'mw-message-box-' .. severity )
			:attr( 'title', currentDate )
			:wikitext( message )
	end

	return tostring( content )
end


--- Set the frame and load args
--- @param frame table
function methodtable.setFrame( self, frame )
    self.currentFrame = frame
    self.frameArgs = require( 'Module:Arguments' ).getArgs( frame )
end


--- Template entry
--- @param frame table
function Log.main( frame )
    local instance = Log:new()
    instance:setFrame( frame )

	local severity = string.lower( instance.frameArgs[ 1 ] or instance.frameArgs[ 'Schweregrad' ] or 'info' )
	local message = instance.frameArgs[ 2 ] or instance.frameArgs[ 'Nachricht' ]
	local silent = string.lower( instance.frameArgs[ 3 ] or instance.frameArgs[ 'Versteckt' ] or '' )

	if silent == '1' or silent == 'true' or silent == 'wahr' then
		silent = true
	else
		silent = nil
	end

	instance.options = {
		silent = silent,
		lang = require( 'Module:Template translation' )._getLanguageSubpage( mw.title.getCurrentTitle() )
	}

	return instance:makeOutput( severity, message )
end


--- Output an info log
--- @param message string The message Content
--- @param options table Output options
function Log.info( message, options )
	local instance = Log:new( options )
	return instance:makeOutput( 'info', message )
end


--- Output a success log
--- @param message string The message Content
--- @param options table Output options
function Log.success( message, options )
	local instance = Log:new( options )
	return instance:makeOutput( 'success', message )
end


--- Output a warning log
--- @param message string The message Content
--- @param options table Output options
function Log.warning( message, options )
	local instance = Log:new( options )
	return instance:makeOutput( 'warning', message )
end


--- Output a error log
--- @param message string The message Content
--- @param options table Output options
function Log.error( message, options )
	local instance = Log:new( options )
	return instance:makeOutput( 'error', message )
end


--- New Instance
function Log.new( self, options )
	if options == true then
		options = {
			silent = true
		}
	else
		options = options or {}
	end
	
	if options.lang == nil or #options.lang ~= 2 then
		options.lang = 'de'
	end

    local instance = {
		options = options
	}

    setmetatable( instance, metatable )

    return instance
end


return Log
Cookies helfen uns bei der Bereitstellung dieses Wikis. Durch die Nutzung des Star Citizen Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.