我正在使用 tcl 中的一个程序,我无法控制该程序。它在输出窗口上输出很多详细信息,例如:
Response:<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server</faultcode><faultstring>Item not valid: The specified Standard SIP1 Profile was not found</faultstring><detail><axlError><axlcode>5007</axlcode><axlmessage>Item not valid: The specified Standard SIP1 Profile was not found</axlmessage><request>updatePhone</request></axlError></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>
有什么办法可以将这个标准输出重定向到变量吗?我是 tcl 的新手,不知道如何做到这一点。
如果您使用 Tcl 8.6,您可以将所有输出捕获到stdout
通过添加合适的变换chan push http://www.tcl.tk/man/tcl8.6/TclCmd/chan.htm#M32:
# Use a class to simplify the capture code
oo::class create CapturingTransform {
variable var
constructor {varName} {
# Make an alias from the instance variable to the global variable
my eval [list upvar \#0 $varName var]
}
method initialize {handle mode} {
if {$mode ne "write"} {error "can't handle reading"}
return {finalize initialize write}
}
method finalize {handle} {
# Do nothing, but mandatory that it exists
}
method write {handle bytes} {
append var $bytes
# Return the empty string, as we are swallowing the bytes
return ""
}
}
# Attach an instance of the capturing transform
set myBuffer ""
chan push stdout [CapturingTransform new myBuffer]
# ... call the problem code as normal ...
# Detach to return things to normal
chan pop stdout
注意事项:这捕获了all通道上的输出,无论如何产生(它甚至可以跨线程工作或在 C 级别生成输出),这使得bytes into myBuffer
因为捕获是在转换为通道配置的编码之后应用的。并且需要8.6;相关的 API 并未暴露于早期版本中的脚本(尽管它的 C 等效项被某些扩展使用,例如 SSL 支持)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)