Page 1 of 1

How obtain the name of the current event? [Solved]

Posted: Tuesday 14 January 2025 21:16
by OldPensionado
Hi guys,
It is not a very disrupting question but I would like to have an answer. Oh yes, I searched this forum and the rest of internet but no luck so far.
I would like to know the name of the event which is running. This offers the opportunity to pass debug information in a very simple manner to my own DebugPrintEvent without typing the event name as a parameter.
I hope I've made myself clear enough.

Thanks in advance!

Re: How obtain the name of the current event?

Posted: Tuesday 14 January 2025 21:24
by waltervl
Do you want to create a special DebugPrintEvent script that is debugging running dzvents events?
What do you want to debug? Is the debug functionality where you can set the debug level per script not enough?

Re: How obtain the name of the current event?

Posted: Tuesday 14 January 2025 21:47
by OldPensionado
Gosh, you're fast!
I have already created a helper (named "MyDebugPrint") which prints the text which is passed in a parameter. And this helper only prints info into the logfile if a global variable (named "FlagDebugPrint") is true.
But I would like to pass the name of the event which is running too.
It could be something like:
MyDebugPrint(<RunningEvent.name>, 'Updating counters now...').
In this case I would have to define only the specific text ('Updating counters now').

This would be nice, though?

Re: How obtain the name of the current event?

Posted: Tuesday 14 January 2025 21:56
by FlyingDomotic
If I correctly understand, idea may be getting current script name, in order to have a generic print/log function using the script name without getting it as parameter.

Something like "scriptName = debug.getinfo(1,'S')" or equivalent, but working only under certain conditions

Re: How obtain the name of the current event?

Posted: Tuesday 14 January 2025 22:09
by OldPensionado
Thanks a lot, I will try this tomorrow!

Re: How obtain the name of the current event?

Posted: Tuesday 14 January 2025 23:37
by waltervl
What is wrong with default Dzvents debug logging with global and script level log level options?
https://wiki.domoticz.com/DzVents:_next ... (optional)

See also the demo template in the event editor and added here as code:

Code: Select all

-- This example shows you how to work with various log levels
--
-- The script is triggered by a dummy switch called 'test'
--
-- when no log level is specified, the level is taken from the default application settings (dzVents)
--
-- when you specify LOG_STATUS as minimum log level, no logs will be displayed when using LOG_INFO or LOG_DEBUG
--
-- Order is: LOG_DEBUG, LOG_INFO, LOG_STATUS, LOG_ERROR
--
-- LOG_DEBUG will also cause Execution Info to be displayed (start/finished)
--
return {
	on = {
		devices = {
			'test'
		}
	},
	logging = {
		marker = 'log_script',
		level = domoticz.LOG_DEBUG
	},
	execute = function(domoticz, device)
		domoticz.log('INFO log line!', domoticz.LOG_INFO)
		domoticz.log('STATUS log line!', domoticz.LOG_STATUS)
		domoticz.log('ERROR log line!', domoticz.LOG_ERROR)
		domoticz.log('DEBUG log line!', domoticz.LOG_DEBUG)
	end
}

Re: How obtain the name of the current event?

Posted: Wednesday 15 January 2025 7:15
by OldPensionado
FlyingDomotic wrote: Tuesday 14 January 2025 21:56 If I correctly understand, idea may be getting current script name, in order to have a generic print/log function using the script name without getting it as parameter.

Something like "scriptName = debug.getinfo(1,'S')" or equivalent, but working only under certain conditions
Yes, that's what I would like to have.

Re: How obtain the name of the current event?

Posted: Wednesday 15 January 2025 7:23
by OldPensionado
waltervl wrote: Tuesday 14 January 2025 23:37 What is wrong with default Dzvents debug logging with global and script level log level options?
https://wiki.domoticz.com/DzVents:_next ... (optional)
The default debug logging options aren't wrong but in my view too abundant. And possible caused by my own history as programmer in Basic, various assembler languages and Pascal, I very much like to control the logging by myself.
That is why I am looking for a generic printlog option. Also I use a global variable FlagDebugPrint - controlled by a virtual switch - by which debug info is printed or not. This works for me faster and smoother than go to Setup > Settings > Other and finally press Apply Settings.

Re: How obtain the name of the current event?

Posted: Wednesday 15 January 2025 7:31
by mgugu
I use that for years :

Code: Select all

local scriptName = string.gsub(string.match(debug.getinfo(1, 'S').short_src, "[^/]+$"), '.lua', '')

Re: How obtain the name of the current event?

Posted: Wednesday 15 January 2025 7:43
by OldPensionado
Wow, fast response and briljant. It works like a charm.
Thank you @mgugu very much!

Re: How obtain the name of the current event?

Posted: Wednesday 15 January 2025 8:38
by waltervl
OldPensionado wrote: Wednesday 15 January 2025 7:23
waltervl wrote: Tuesday 14 January 2025 23:37 What is wrong with default Dzvents debug logging with global and script level log level options?
https://wiki.domoticz.com/DzVents:_next ... (optional)
The default debug logging options aren't wrong but in my view too abundant. And possible caused by my own history as programmer in Basic, various assembler languages and Pascal, I very much like to control the logging by myself.
That is why I am looking for a generic printlog option. Also I use a global variable FlagDebugPrint - controlled by a virtual switch - by which debug info is printed or not. This works for me faster and smoother than go to Setup > Settings > Other and finally press Apply Settings.
But then you get debug for all running scripts....
I prefer to set global debug level to INFO (and never change it) and when I need debug in a script I change the debug level in that script....

Re: How obtain the name of the current event?

Posted: Wednesday 15 January 2025 13:32
by FlyingDomotic
For those wanting, in addition to the script name, getting caller(s) line number, there's a small function returning the information:

Code: Select all

-- Returns source name(s) and line(s) of caller(s)
function getTraceback()
	local level = 2														-- Start with our caller
	local source = ""													-- Source name
	local trace = ""													-- Trace message
	while true do														-- Loop forever
		local info = debug.getinfo(level, "Sl")							-- Get level info
		if not info or info.currentline <= 0 then						-- No more level or Domoticz root level reached
			break														-- Exit loop
		end
		if source ~= info.short_src then								-- Any change in source name?
			source = info.short_src										-- Save it
			trace = trace .. ":" .. 									-- Add name to traceback
				string.gsub(string.match(source, "[^/]+$"), '.lua', '')
		end    
		trace = trace .. ":" .. info.currentline						-- Add line number
		level = level + 1												-- Increase level
	end
	return string.sub(trace,2)											-- Return trace except first character

Re: How obtain the name of the current event?

Posted: Wednesday 15 January 2025 15:47
by FlyingDomotic
In addition, for those not using dzVents, but "classical" Domoticz LUA, here's a small function extracting comment of first line of script.

Code: Select all

-- Returns Domoticz (non dzVent) LUA first line
function luaSourceName(level)
    if level == nil then                                                -- You can specify stack level 
        level = 1                                                       -- Default to 1 if not given
    end
    index = 0
    source = debug.getinfo(level, "Sl").short_src                       -- Get source name (content for Domoticz)
    source = string.sub(source, 1, string.find(source.."\n", "\n") -1)  -- Keep only first line
    for str in string.gmatch(source, '([^"]+)') do                      -- Split source using double quote
        index = index + 1                                               -- Increment index
        if index == 2 then                                              -- Consider data at index 2
            if string.sub(str, 1, 3) == "-- " then                      -- Does string start with "-- "?
                source = string.sub(str, 4)                             -- Keep comment
                if string.sub(source, -3) == "..." then                 -- If source ends with "..."
                    source = string.sub(source, 1, -4)                  -- Remove them
                end
                return source                                           -- Return comment part
            end
        end
    end        
    return source                                                       -- Return first line (no comment found)
end
You just have to begin your script with

Code: Select all

-- script name
for the routine to return "script name"