Centos Newbie
Centos Newbie

Reputation: 59

TCL::Proc Name From another Proc

I would like to spell out the proc name in tcl for building a stronger tcl program. I went through some questions in stackoverflow which talk about the same and I built the following out of it -

proc logInfo { {type DEBUG} {msg "This is a debug statement"} } {

set timeStampLog [clock format [clock seconds] -format "%m/%d/%y::%H:%M:%S"]

puts "${timeStampLog}::'[lindex [info level [info level]] 0]'::$type - $msg"

}


proc test { } {

logInfo Warning "This is a test proc only"

}

I expect the proc to display the following when called -

tclsh> test
08/05/15::09:41:48::'test'::Warning - This is a test proc only
tclsh>

This is what I see -

tclsh> test
08/05/15::09:41:48::'logInfo'::Warning - This is a test proc only
tclsh>

Is there a way to point to the current proc name?

Thanks

Upvotes: 0

Views: 130

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137557

“Current” is a tricky concept sometimes, since when you are running logInfo, it really is the current procedure. You want the caller of the current procedure; instead of info level [info level], use info level -1 (positive numbers count from the base of the stack, zero and negative count from the deep end of the stack).

This actually simplifies your code a bit.

proc logInfo { {type DEBUG} {msg "This is a debug statement"} } {
    set timeStampLog [clock format [clock seconds] -format "%m/%d/%y::%H:%M:%S"]
    puts "${timeStampLog}::'[lindex [info level -1] 0]'::$type - $msg"
}

Upvotes: 4

Related Questions