PDA

View Full Version : Logging data to a file from Portal


kbanks
04-23-2008, 11:33 AM
Quoting the SNAP Reference Manual: "Since Portal runs on a PC, its script executes in a full Python environment with access to the many libraries, services, and capabilities there."

One example of a capability that you can make use of from Portal is Python's logging capabiility. The attached script shows a quick example of one way to do this. Put logSupport.py in your Portal directory, and then in any script that needs to log something to a file, do:

import logSupport

...
...
...

logSupport.logToFile(root, "MYLOG", "some text goes here")

The first parameter is some object that can be considered the "owner" of the log file (this allows having multiple active log files, just give them distint owner objects).

The second parameter is the "base name" for the log file.

The third parameter is the text to be logged. If you want to include numeric data in the log, simply format up a string containing all the component data.

formattedTemperature = "Temperature is " + str(temperature) + " degrees"
logSupport.logToFile(root, "LOGFILE", formattedTemperature)

In this example, files are rolled over at midnight, and given names that include the date. Each entry into the file is prefixed by a timestamp.

All of these formatting decisions could be changed by customizing logSupport.py

martinnossel
10-28-2008, 11:37 AM
def plotval(who,y):
logData(who,y,1024)


gives an error
name "logData" not defined ??

could you attach a sample bit of code that uses
logSupport.logToFile(root, "LOGFILE", formattedTemperature)
for portal and a rf unit

I cant get anyting to run on portal
PortalManyMeter and anything else gives errors. I set the python lib location to program files/portal as well.

rpc("\x00\x00\x01","plotval","ADC",aa) on a node is working is the chart data only graphic? Is there a way to save the data sent to the chart using the above code in text format?

I am using portal 2.1.22

Thanks
Martin

kbanks
10-28-2008, 05:02 PM
From our phone call just now (after a few wrong turns) we realized what you were running into - you were running the SNAPpy validator on your Portal scripts.

(From reading your post originally, I thought you were getting an error when you tried to LOAD the Portal scripts), which is why I was mistakenly looking into your LIB settings.

The Test SNAPpy Script button is documented at the bottom of page 119 of the SNAP Reference Manual. I've made a note to try and explain this button more clearly in a future version of the docs. We also will consider disabling the button if you are editing a script from the Portal directory.

Other points from the phone call:

If you want to use functions from a file like logSupport.py, be sure to do something like


from logSupport import *


at the top of your script.

No, the graph does not support logging to a text file (that is done by logSupport). The chart only allows saving the picture of the graph.

Keep in mind that your nodes can call a single Portal script function, and that single function can then do multiple things with the data, like logging it to a file, putting it in the Portal Event Log, and graphing it on a chart.

martinnossel
10-28-2008, 06:14 PM
I have this code in a node.
====================
rpc("\x00\x00\x01","logToFile", "test_LOG", "aa") #logToFile("test_LOG", "temp 1=",aa))
cls()
writeChars(str(aa)[:])
writeChars("t=")
cc = readAdc(6)
print "t",cc
writeChars(str(cc)[:])
rpc("\x00\x00\x01","plotval","ADC1",cc)
rpc("\x00\x00\x01","logToFile", "test_LOG",("b="))
rpc("\x00\x00\x01","logToFile", "test_LOG_2", cc)
===============================


I have this loaded in potral
======================
import logSupport

def plotval(who,y):
........logData(who,y,1024)

def logToFile(logname, logdata):
........logSupport.logToFile("c:", logname, logdata
======================


only the test_LOG file is generated not thetest_LOG_2 ????

also the data logged

is each entry repeated 172 or 173 times?

16:51:11.655 aa #repeared 172 times then
16:51:11.687 b= #repeared 172 times

How do I debug this (or any python program for portal)?

???????????????????

Ok I was trying to erase the test_LOG file and couldnot as poratal was using it.

I closed portal. renamed the file, restarted portal ... and its logging correctly. mabye this is usefull to you, once I had typed this all I though I would send it anyway.

martin

martinnossel
10-28-2008, 10:16 PM
somtimes the file is created in ...\My Documents\Portal\snappyImages
other times in C:\Program Files\Portal.

How is the desicion made? (...ok probably the root thing..)

also why wont both log file get created?
test_LOG .... is created
LOG2test ......is not

see code below
===================================
formattedTemperature = "Temperature aa " + str(aa) + " units"
rpc("\x00\x00\x01","logToFile", "test_LOG", formattedTemperature)
.
.
.
.
cc = readAdc(6)
formattedTemperature = "Temperature cc " + str(cc) + " units"
rpc("\x00\x00\x01","logToFile", "LOG2test",formattedTemperature)


Martin

kbanks
10-29-2008, 02:01 PM
Taking a quick look at logSupport.py, I don't think it was intended to support multiple simultaneous log files. The intent of the "baseName" parameter was to let you choose what the name of the ONE log file would be.

Example scripts we post on the forum (like logSupport.py) are just that - examples. If they don't do exactly what you want, feel free to change them.

kbanks
10-29-2008, 02:05 PM
I take part of that back... looking at the very first post of this thread, I see

The first parameter is some object that can be considered the "owner" of the log file (this allows having multiple active log files, just give them distint owner objects).


Are you providing a distinct owner object (some object that does exist, and is not object "root") in the function call?

(It still is possible that the example script needs further modifications to do what you want...)

kbanks
10-30-2008, 07:35 AM
Looking more closely at your posted code, I see my suspicion was correct.

You are only able to create one log file because you are only providing one root object.


def logToFile(logname, logdata):
........logSupport.logToFile("c:", logname, logdata


Judging by your use of the value "c:" for the first parameter, I think you thought the parameter was "root as in root directory" (it's not). It's "root as in root object".

In other words, the parent/containing object. If you are using logSupport.py functions from within Portal, there is a pre-existing object NAMED root that can be used (once! as in for one log file only) for this.

It's important to note that you can only use each "root" object once per run of Portal. (Once you has used "file1" with root, you cannot use "file2" - the association has already been made).

To have more than one log file, you will need to create some other global object, and pass it in to the logToFile() function.

When you do this, be sure to use the right "root/parent/container" objects with the right filenames (in other words, be consistent). A Python dictionary might be useful for this purpose (see for example "Core Python Programming" by Wesley J. Chun, page 40).

mgenti
08-26-2009, 02:14 PM
I'm not sure how your post relates to any products that Synapse Wireless makes. Do you currently own or are looking at purchasing any SNAP based products? Unfortunately we deal daily with automated programs trying to post on our forums so if you make another unrelated post we will have to ban you.

Daid
10-27-2011, 06:16 PM
I'm having problems using this method for some reason but I am unable to figure out why. I'm wondering if I placed the Logsupport.py file in the wrong place. Also if I do test the portal script it errors not allowing me to load the logToFile function. I'm using the protoboard from the eval kit to monitor a simple temperature sensor and I need it to log the data and save it to a file.