你必须使用info frame
简单地完成这件事。
info frame ?number?
此命令提供对堆栈上所有帧的访问,甚至是那些隐藏在信息级别的帧。如果未指定数字,则此命令返回一个给出命令帧级别的数字。如果在顶层调用该命令,则值为 1。如果指定了 number,则结果是一个字典,其中包含堆栈上编号级别的命令的位置信息。
如果 number 为正 (> 0),则它选择特定的堆栈级别(1 指最顶层的活动命令,即info frame
本身,2 到调用它的命令,依此类推);否则它给出相对于当前命令的级别(0 指当前命令,即信息帧本身,-1 指其调用者,依此类推)。
我们将利用返回的字典info frame
命令。键之一是“line”,它包含脚本的行号。
有一个简单的proc
as,
proc printLine {frame_info} {
# Getting value of the key 'line' from the dictionary
# returned by 'info frame'
set result [dict get [info frame $frame_info] line]
}
一般来说,生成的字典来自[info frame $frame_info]
会是这样的,
type source line 17 file /home/dinesh/stackoverflow/test cmd {printLine [info frame] } proc ::B level 1
由此,我们只是得到键值“line”dict get
只需使用该上下文的当前帧号调用此过程即可,这可以通过以下方式实现info frame
itself.
i.e.
set lineNumber [printLine [info frame]]; #Place this line in your code.
该逻辑的演示如下。
打印行号.tcl
#!/usr/bin/tclsh
proc printLine {frame_info} {
# Getting value of the key 'line' from the dictionary
# returned by 'info frame'
set result [dict get [info frame $frame_info] line]
}
proc D {} {
puts "proc D"
puts [ printLine [info frame] ]
}
proc C {} {
puts "proc C"
puts [ printLine [info frame] ]
D
}
proc B {} {
puts "proc B"
puts [ printLine [info frame] ]
C
}
proc A {} {
puts "proc A"
puts [ printLine [info frame] ]
B
}
puts "Global"
puts [ printLine [info frame] ]
A
文档:info, dict