It is currently April 20th, 2024, 6:57 am

LUA Logging

Tips and Tricks from the Rainmeter Community
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

LUA Logging

Post by raiguard »

Code: Select all

-- writes the given value or table to the rainmeter log
function RmLog(...)

    if debug == nil then debug = true end
    if printIndent == nil then printIndent = '' end
      
    if type(arg[1]) == 'table' then
        if arg[3] == nil then arg[3] = 'Debug' end
        if arg[3] == 'Debug' and debug == false then return end

        RmLog(printIndent .. arg[2] .. ' = {')
        local pI = printIndent
        printIndent = printIndent .. '    '
        for k,v in pairs(arg[1]) do
            if type(v) == 'table' then
                RmLog(v, k, arg[3])
            else
                RmLog(printIndent .. tostring(k) .. ' = ' .. tostring(v), arg[3])
            end
        end
        printIndent = pI
        RmLog(printIndent .. '}', arg[3])
    else
        if arg[2] == nil then arg[2] = 'Debug' end
        if arg[2] == 'Debug' and debug == false then return end
        SKIN:Bang("!Log", tostring(arg[1]), arg[2])
    end
      
end
The RmLog() function will print the given value or LUA table to the Rainmeter log. The function will optionally skip printing messages of type Debug if the global variable debug is set to false.

Table printing is recursive, so printing a table will also print any subtables.

Usage

Syntax: RmLog(message, type)
message (string): The message to be printed.
type (string): One of type Notice, Warning, Error, or Debug.

Syntax: RmLog(table, tableName, type)
table (table): The table to be printed.
tableName (string): The name of the table.
type (string): One of type Notice, Warning, Error, or Debug.

Example

LUA:

Code: Select all

debug = true

function Initialize()
	local i = 0
	local s = 'bar'
	local b = false
	local t = {}
	table.insert(t, 'foo')
	table.insert(t, 42)
	table.insert(t, true)
	table.insert(t, { a = 'lorem', b = 'ipsum', c = 'dolor', d = 'sit', e = 'amet' })

	RmLog(i, 'Notice')
	RmLog(s, 'Warning')
	RmLog(b, 'Error')
	RmLog(t, 't', 'Debug')
end

function Update() end

-- writes the given value or table to the rainmeter log
function RmLog(...)

    if debug == nil then debug = true end
    if printIndent == nil then printIndent = '' end
      
    if type(arg[1]) == 'table' then
        if arg[3] == nil then arg[3] = 'Debug' end
        if arg[3] == 'Debug' and debug == false then return end

        RmLog(printIndent .. arg[2] .. ' = {')
        local pI = printIndent
        printIndent = printIndent .. '    '
        for k,v in pairs(arg[1]) do
            if type(v) == 'table' then
                RmLog(v, k, arg[3])
            else
                RmLog(printIndent .. tostring(k) .. ' = ' .. tostring(v), arg[3])
            end
        end
        printIndent = pI
        RmLog(printIndent .. '}', arg[3])
    else
        if arg[2] == nil then arg[2] = 'Debug' end
        if arg[2] == 'Debug' and debug == false then return end
        SKIN:Bang("!Log", tostring(arg[1]), arg[2])
    end
      
end
Log Output:

Code: Select all

NOTE (17:21:54.350) RainmeterSettings\RainmeterSettings.ini: Refreshing skin
NOTE (17:21:54.365) RainmeterSettings\RainmeterSettings.ini: 0
WARN (17:21:54.366) RainmeterSettings\RainmeterSettings.ini: bar
ERRO (17:21:54.367) RainmeterSettings\RainmeterSettings.ini: false
DBUG (17:21:54.368) RainmeterSettings\RainmeterSettings.ini: t = {
DBUG (17:21:54.369) RainmeterSettings\RainmeterSettings.ini:     1 = foo
DBUG (17:21:54.370) RainmeterSettings\RainmeterSettings.ini:     2 = 42
DBUG (17:21:54.371) RainmeterSettings\RainmeterSettings.ini:     3 = true
DBUG (17:21:54.372) RainmeterSettings\RainmeterSettings.ini:     4 = {
DBUG (17:21:54.374) RainmeterSettings\RainmeterSettings.ini:         a = lorem
DBUG (17:21:54.375) RainmeterSettings\RainmeterSettings.ini:         c = dolor
DBUG (17:21:54.376) RainmeterSettings\RainmeterSettings.ini:         b = ipsum
DBUG (17:21:54.377) RainmeterSettings\RainmeterSettings.ini:         e = amet
DBUG (17:21:54.379) RainmeterSettings\RainmeterSettings.ini:         d = sit
DBUG (17:21:54.382) RainmeterSettings\RainmeterSettings.ini:     }
DBUG (17:21:54.383) RainmeterSettings\RainmeterSettings.ini: }
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017