TCL :: Proc名称来自另一个Proc

时间:2015-08-05 13:46:15

标签: tcl

我想在tcl中拼出proc名称来构建一个更强大的tcl程序。我在stackoverflow中讨论了一些问题并谈论了同样的问题,我构建了以下内容 -

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"

}

我希望proc在调用时显示以下内容 -

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

这就是我所看到的 -

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

有没有办法指向当前的proc名称?

由于

1 个答案:

答案 0 :(得分:4)

“当前”有时是一个棘手的概念,因为当你运行logInfo时,它确实是当前的程序。你想要当前程序的调用者;而不是info level [info level],使用info level -1(正数来自堆栈的基数,来自堆栈深端的零和负数)。

这实际上简化了你的代码。

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"
}
相关问题