It is currently April 19th, 2024, 12:03 pm

Creating a log file

Get help with creating, editing & fixing problems with skins
ritwyk
Posts: 4
Joined: August 10th, 2011, 5:12 pm

Creating a log file

Post by ritwyk »

I'm working on a skin that uses the WebParser to grab a certain value off a page every five minutes and display it in a String meter.
I now need to dump the values into a text files every five minutes too. I understand it probably involves Lua, but I can't figure out how to accomplish that.
As it stands now, the code has the following structure:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[MeasureValue]
Measure=Plugin
Plugin=WebParser
URL=http://www.xyz.com/home.html
RegExp=(?siU)searchstring(.*)\)
StringIndex=1

[MeterCurrentValue]
Meter=String
MeasureName=MeasureValue
DynamicVariables=1

TLDR: All I need to know is how to write the value of a measure to the end of a text file (not a .inc file). Any pointers?
FlyingHyrax
Posts: 232
Joined: July 1st, 2011, 1:32 am
Location: US

Re: Creating a log file

Post by FlyingHyrax »

The Lua function should be fairly easy - this isn't tested but it should give you the right idea:

Code: Select all

function appendToLog()
	-- this gets a handle to your WebParse measure
	local parserMeasure = SKIN:GetMeasure('MeasureValue')
	-- this gets the value of your WebParser measure
	local text = parserMeasure:GetStringValue()
	-- converts '.\log.txt' into an absolute path
	local logPath = SKIN:MakePathAbsolute('log.txt')
	-- open the log file in append-update mode
	local logFile = io.open(logPath, 'a+')
	-- make sure the file was opened successfully
	if not logFile then
		print ('could not open ' .. logPath)
	else
		-- write the line
		logFile:write(text)
		-- close the file
		logFile:close()
	end
end
Then whenever you'd like you can call that function from your skin by having a script measure and using !CommandMeasure (for example, in the FinishAction on your WebParser measure)

Code: Select all

[measureScript]
Measure=Script
ScriptFile=append.lua
UpdateDivider=-1
...
(somewhere else entirely)
!CommandMeasure "measureScript" "appendToLog()"
...
References:
http://www.lua.org/manual/5.1/manual.html#5.7
http://lua-users.org/wiki/IoLibraryTutorial
http://docs.rainmeter.net/manual/lua-scripting
Last edited by FlyingHyrax on January 8th, 2014, 4:18 pm, edited 1 time in total.
Flying Hyrax on DeviantArt
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Creating a log file

Post by jsmorley »

Using Lua, you could do something like:

Skin:

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1

[MeasureInput]
Measure=Plugin
Plugin=WebParser
URL=file://#CURRENTPATH#Test.html
RegExp=(?siU)<item>(.*)</item>
StringIndex=1
FinishAction=[!CommandMeasure MeasureOutput """UpdateLog("[MeasureInput]")"""]

[MeasureOutput]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
OutputFile=#CURRENTPATH#Output.log
UpdateDivider=-1

[MeterInput]
Meter=String
MeasureName=MeasureInput
FontSize=13
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Test.lua

Code: Select all

function Initialize()

	filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))
	
end

function Update()

end

function UpdateLog(stringArg)

	local fileHandle = io.open(filePath, 'a+')
	
	fileHandle:write(os.date()..' | '..stringArg..'\n')
	
	fileHandle:close()
	
end
I threw the date / time on the entry, you can certainly remove that from the output if you don't want or need it.

P.S. My real question about this is how you know that an item returned by WebParser is really "new" and should be written. Maybe not important to what you are trying to do, but I'd hate to have a log that is adding a line 200+ times a day if things haven't really changed.

Edit: FlyingHyrax beat me to it...
FlyingHyrax
Posts: 232
Joined: July 1st, 2011, 1:32 am
Location: US

Re: Creating a log file

Post by FlyingHyrax »

Sorry Jeff, didn't see you there. :)
Edit: JSMorley's is better anyhow - much more complete.
Flying Hyrax on DeviantArt
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Creating a log file

Post by jsmorley »

The only real difference is that I passed the text to write on the !CommandMeasure to the script, rather than having the script "come back" and get the WebParser measure value.

It might not be bad for completeness sake to do some kind of "assert" on the file open, so if in fact it fails you are told so. However, 'a+' will create the file if it doesn't already exist, so the only real possible error is if the file can't be created due to an invalid folder structure or a write permission issue in the folder. If this is something the author will distribute, then I would make that part of it a bit more robust. For personal use, it's probably not as important.
ritwyk
Posts: 4
Joined: August 10th, 2011, 5:12 pm

Re: Creating a log file

Post by ritwyk »

That is a perfect solution. Thanks a lot, Jeff!
FlyingHyrax, thanks for the explanations.
MaestroSky
Posts: 33
Joined: April 20th, 2021, 4:15 pm

Re: Creating a log file

Post by MaestroSky »

jsmorley wrote: January 8th, 2014, 4:18 pm Using Lua, you could do something like:

Skin:

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1

[MeasureInput]
Measure=Plugin
Plugin=WebParser
URL=file://#CURRENTPATH#Test.html
RegExp=(?siU)<item>(.*)</item>
StringIndex=1
FinishAction=[!CommandMeasure MeasureOutput """UpdateLog("[MeasureInput]")"""]

[MeasureOutput]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
OutputFile=#CURRENTPATH#Output.log
UpdateDivider=-1

[MeterInput]
Meter=String
MeasureName=MeasureInput
FontSize=13
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Test.lua

Code: Select all

function Initialize()

	filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))
	
end

function Update()

end

function UpdateLog(stringArg)

	local fileHandle = io.open(filePath, 'a+')
	
	fileHandle:write(os.date()..' | '..stringArg..'\n')
	
	fileHandle:close()
	
end
I threw the date / time on the entry, you can certainly remove that from the output if you don't want or need it.

P.S. My real question about this is how you know that an item returned by WebParser is really "new" and should be written. Maybe not important to what you are trying to do, but I'd hate to have a log that is adding a line 200+ times a day if things haven't really changed.

Edit: FlyingHyrax beat me to it...
And how to delete a date / time from lua?
That's it, I figured it out