Question

How to write add a string containing a special character to the log file with TCL/python


Badge +7

Hello,

I woul like to write the expression VAL_NON_NULwhose value equal to "TAUX !=0" using a TCL script.

I tried the following command inside a TCLCaller:

FME_LogMessage fme_inform "condition\40("&VAL;_NON_NUL")"

but only the string "condition TAUX ". The part after the exclamation! just vanished.

I also tried to to use a python caller but I didn't get the expected results

How can I fix this issue?

Thanks


5 replies

Userlevel 2
Badge +16

Could you use the Logger transformer for this?

Logger info

Userlevel 2
Badge +17

Hi @arthy, if the VAL_NON_NUL is the name of a feature attribute storing the string value "TAUX !=0" and also you intend to set a Tcl expression to the Tcl Expression parameter in the TclCaller to log the VAL_NON_NUL value, this expression should work.

FME_LogMessage fme_inform \"condition (&VAL;_NON_NUL)\"

In the Tcl Expression parameter field, double quotation marks that surround a string value containing spaces have to be escaped with a preceding backslash. Otherwise, the double quotations will be ignored and therefore the part after the first space in the string value will also be ignored.

In addition, getting an attribute value by the syntax "& + attribute name" may not be recommended. Consider using the FME_GetAttribute function instead.

FME_LogMessage fme_inform \"condition ([FME_GetAttribute VAL_NON_NUL])\"

Alternatively, you can also define a procedure providing the same functionality in the Source Code parameter, like this. You can call the procedure through the Tcl Expression parameter. 

proc logValNonNul {} {
    FME_LogMessage fme_inform "condition ([FME_GetAttribute VAL_NON_NUL])"
}

It's not necessary to escape the double quotations in the Source Code.

[Addition]

# PythonCaller Script Example
# Log a message string containing an attribute value.
import fmeobjects
class FeatureProcessor(object):
    def __init__(self):
        self.logger = fmeobjects.FMELogFile()
        
    def input(self, feature):
        m = 'condition (%s)' % feature.getAttribute('VAL_NON_NUL')
        self.logger.logMessageString(m, fmeobjects.FME_INFORM)
        self.pyoutput(feature)
Userlevel 2
Badge +17

Hi @arthy, if the VAL_NON_NUL is the name of a feature attribute storing the string value "TAUX !=0" and also you intend to set a Tcl expression to the Tcl Expression parameter in the TclCaller to log the VAL_NON_NUL value, this expression should work.

FME_LogMessage fme_inform \"condition (&VAL;_NON_NUL)\"

In the Tcl Expression parameter field, double quotation marks that surround a string value containing spaces have to be escaped with a preceding backslash. Otherwise, the double quotations will be ignored and therefore the part after the first space in the string value will also be ignored.

In addition, getting an attribute value by the syntax "& + attribute name" may not be recommended. Consider using the FME_GetAttribute function instead.

FME_LogMessage fme_inform \"condition ([FME_GetAttribute VAL_NON_NUL])\"

Alternatively, you can also define a procedure providing the same functionality in the Source Code parameter, like this. You can call the procedure through the Tcl Expression parameter. 

proc logValNonNul {} {
    FME_LogMessage fme_inform "condition ([FME_GetAttribute VAL_NON_NUL])"
}

It's not necessary to escape the double quotations in the Source Code.

[Addition]

# PythonCaller Script Example
# Log a message string containing an attribute value.
import fmeobjects
class FeatureProcessor(object):
    def __init__(self):
        self.logger = fmeobjects.FMELogFile()
        
    def input(self, feature):
        m = 'condition (%s)' % feature.getAttribute('VAL_NON_NUL')
        self.logger.logMessageString(m, fmeobjects.FME_INFORM)
        self.pyoutput(feature)
You can also use a sophisticated custom transformer called LogWriter from the FME Hub.

 

 

Badge +7

Hi @arthy, if the VAL_NON_NUL is the name of a feature attribute storing the string value "TAUX !=0" and also you intend to set a Tcl expression to the Tcl Expression parameter in the TclCaller to log the VAL_NON_NUL value, this expression should work.

FME_LogMessage fme_inform \"condition (&VAL;_NON_NUL)\"

In the Tcl Expression parameter field, double quotation marks that surround a string value containing spaces have to be escaped with a preceding backslash. Otherwise, the double quotations will be ignored and therefore the part after the first space in the string value will also be ignored.

In addition, getting an attribute value by the syntax "& + attribute name" may not be recommended. Consider using the FME_GetAttribute function instead.

FME_LogMessage fme_inform \"condition ([FME_GetAttribute VAL_NON_NUL])\"

Alternatively, you can also define a procedure providing the same functionality in the Source Code parameter, like this. You can call the procedure through the Tcl Expression parameter. 

proc logValNonNul {} {
    FME_LogMessage fme_inform "condition ([FME_GetAttribute VAL_NON_NUL])"
}

It's not necessary to escape the double quotations in the Source Code.

[Addition]

# PythonCaller Script Example
# Log a message string containing an attribute value.
import fmeobjects
class FeatureProcessor(object):
    def __init__(self):
        self.logger = fmeobjects.FMELogFile()
        
    def input(self, feature):
        m = 'condition (%s)' % feature.getAttribute('VAL_NON_NUL')
        self.logger.logMessageString(m, fmeobjects.FME_INFORM)
        self.pyoutput(feature)
@takashi,

 

This is exactly what I was looking.

 

Thanks

 

.

 

 

 

Badge +7

Could you use the Logger transformer for this?

Logger info

@erik_jan,

 

Yes I tried the logger info transformer but it will require several extra msdos commands to pull the information I need and in the format I need.

 

Thanks for your answer

 

 

Reply