【发布时间】:2021-04-24 02:44:55
【问题描述】:
对于现有流程,运行某些脚本作业时会加载一大堆命名空间。 但是,如果我想检查和跟踪某个命名空间中某些命令的使用情况,我需要找到某个命名空间的脚本路径。 有什么办法可以得到吗?特别是,我说的是黄金时段脚本。
【问题讨论】:
-
欢迎您!您是否搜索过以前的答案,例如stackoverflow.com/questions/12651549/…
标签: path namespaces tcl
对于现有流程,运行某些脚本作业时会加载一大堆命名空间。 但是,如果我想检查和跟踪某个命名空间中某些命令的使用情况,我需要找到某个命名空间的脚本路径。 有什么办法可以得到吗?特别是,我说的是黄金时段脚本。
【问题讨论】:
标签: path namespaces tcl
从技术上讲,命名空间没有脚本路径。但我们可以做一些足够接近的事情:
proc report_current_file {call code result op} {
if {$code == 0} {
# If the [proc] call was successful...
set cmd [lindex $call 1]
set qualified_cmd [uplevel 1 [list namespace which $cmd]]
set file [file normalize [info script]]
puts "Defined $qualified_cmd in $file"
}
}
trace add execution proc leave report_current_file
如果您有动态创建过程的过程,这并不完美——当前文件可能是错误的——但幸运的是,大多数代码不是这样做的。
另一个可能对您有用的选项是使用tcl::unsupported::getbytecode,它以机器可读格式(字典)生成大量信息。其中一条信息是sourcefile 键。这是一个在我的机器上以交互方式运行的示例:
% parray tcl_platform
tcl_platform(byteOrder) = littleEndian
tcl_platform(engine) = Tcl
tcl_platform(machine) = x86_64
tcl_platform(os) = Darwin
tcl_platform(osVersion) = 20.2.0
tcl_platform(pathSeparator) = :
tcl_platform(platform) = unix
tcl_platform(pointerSize) = 8
tcl_platform(threaded) = 1
tcl_platform(user) = dkf
tcl_platform(wordSize) = 8
% dict get [tcl::unsupported::getbytecode proc parray] sourcefile
/opt/local/lib/tcl8.6/parray.tcl
请注意,必须已经定义了该过程才能使其正常工作。如果 Tcl 对代码所在的文件感到困惑(因为动态编程技巧),那么该密钥就不存在了。
【讨论】:
sourcefile 会消失。这太好了!