将TCL PROC的输出重定向到文件和输出(如TEE)第2部分
我正在使用从我的过程中重定向文件输出。我需要将Stdout和STDERR重定向到文件。
使用重定向tcl proc的输入文件和输出(如Tee)我到达了以下操作:
set LogFile [open ${LogFileName} w]
tee channel stderr $LogFile
tee channel stdout $LogFile
set BuildErrorCode [catch {LocalBuild $BuildName $Path_Or_File} BuildErrMsg]
set BuildErrorInfo $::errorInfo
# Restore stdout and stderr
chan pop stdout
chan pop stderr
# Handle errors from Build ...
我在三种不同的EDA工具上对此进行测试,并且有三个不同的问题。
- 当我从TCLSH(在Windows 10上运行的MSYS2上)运行并运行开源模拟器GHDL,Modelsim或QuestAsim时,所有偶数字符均为
nul
字符。 - 如果我从GUI中运行Modelsim或QuestAsim,我会错过每个命令的输出。那不应该去stdout还是stderr?
- 在Riviera-Pro中,我得到了以前印刷的无关角色。它们通常是单词的后半部分。
我做错了吗?我使用上述代码测试了:
set LogFile [open test_tee.log w]
tee channel stderr $LogFile
tee channel stdout $LogFile
puts "Hello, World!"
puts stderr "Error Channel"
puts stdout "Output Channel"
chan pop stdout
chan pop stderr
这很好。
我希望找到所有工具中一般情况下有效的东西,而不必为每个工具编写其他处理程序。
===========================
对于上面的#1,有了 @Shawn的建议,我尝试了以下内容,但它不起作用。
set LogFile [open ${LogFileName} w]
chan configure $LogFile -encoding ascii
. . .
我还尝试了以下内容,但它没有起作用。
set LogFile [open ${LogFileName} w]
fconfigure $LogFile -encoding ascii
. . .
然后,我尝试将T恤中的写入更新为以下内容,但它没有可行:
proc tee::write {fd handle buffer} {
puts -nonewline $fd [encoding convertto ascii $buffer]
return $buffer
}
任何其他提示解决方案都赞赏
===============================
我已经成功地删除了NUL角色,除了现在我有了一个额外的新线。仍然不是解决方案。
proc tee::write {fd handle buffer} {
puts -nonewline $fd [regsub -all \x00 $buffer ""]
return $buffer
}
I am using tee from https://wiki.tcl-lang.org/page/Tee to redirect file output from my procedures. I need to redirect both stdout and stderr to the file.
Using the input from Redirecting output of tcl proc to file and output (like tee) I arrived at doing the following:
set LogFile [open ${LogFileName} w]
tee channel stderr $LogFile
tee channel stdout $LogFile
set BuildErrorCode [catch {LocalBuild $BuildName $Path_Or_File} BuildErrMsg]
set BuildErrorInfo $::errorInfo
# Restore stdout and stderr
chan pop stdout
chan pop stderr
# Handle errors from Build ...
I am testing this on three different EDA tools and I have three different issues.
- When I run from tclsh (on MSYS2 running on Windows 10) and run either the open source simulator GHDL, ModelSim, or QuestaSim, all the even characters are the
NUL
character. - If I run ModelSim or QuestaSim from the GUI, I miss the output of each command. Shouldn't that be going to either stdout or stderr?
- In Riviera-PRO, I am getting extraneous characters that were previously printed. They are generally the second half of a word.
Am I doing something wrong? I tested out the above code using:
set LogFile [open test_tee.log w]
tee channel stderr $LogFile
tee channel stdout $LogFile
puts "Hello, World!"
puts stderr "Error Channel"
puts stdout "Output Channel"
chan pop stdout
chan pop stderr
And this works well.
I am hoping to find something that works in the general case for all tools rather than having to write a different handler for each tool.
============ Update =============
For #1 above, with @Shawn's suggestion, I tried the following and it did not work.
set LogFile [open ${LogFileName} w]
chan configure $LogFile -encoding ascii
. . .
I also tried the following and it did not work.
set LogFile [open ${LogFileName} w]
fconfigure $LogFile -encoding ascii
. . .
Then I tried updating the write in tee to the following and it did not work:
proc tee::write {fd handle buffer} {
puts -nonewline $fd [encoding convertto ascii $buffer]
return $buffer
}
Any other hints solutions appreciated
============ Update2 =============
I have successfully removed the nul characters by doing the following, except now I have an extra newline. Still not a solution.
proc tee::write {fd handle buffer} {
puts -nonewline $fd [regsub -all \x00 $buffer ""]
return $buffer
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
额外的nul字节可能是因为在UTF-16中编写了Stdout AHD转向通道(该编码的主要用途是Windows上的控制台)。
TEE
您正在使用的拦截器编码后,您正在使用的拦截器。有几种修复它的方法,但最简单的方法是在阅读时用正确的编码打开文件。命令的输出不一定写入这些频道。用C或C ++编写的代码完全可以直接免费编写,而TCL代码看不到这一点;这一切都发生在我们背后。命令结果可以使用执行跟踪拦截,但这看不到任何内部打印的命令以某种方式未通过TCL库路由。 (由于操作系统处理I/O的不同方式,UNIX还有更多选择。)
不知道额外的字符发生了什么。我可以告诉您,您正在获得通过频道的情况,但是在这方面有太多技巧(尤其是在交互式使用中!)。
The extra NUL bytes are probably because the stdout ahd steer channels are being written in UTF-16 (the main use for that encoding is the console on Windows). The
tee
interceptors you are using come after the data being written is encoded. There's a few ways to fix it, but the easiest is to open the file with the right encoding when reading it.The output of the commands is not necessarily written to those channels. Code written in C or C++ is entirely free to write directly, and Tcl code cannot see that; it's all happening behind our back. Command results can be intercepted using execution traces, but that cannot see anything that the commands internally print that aren't routed via the Tcl library somehow. (There are a few more options on Unix due to the different ways that the OS handles I/O.)
Don't know what's happening with the extra characters. I can tell you that you are getting what goes through the channel, but there are too many tricks (especially in interactive use!) for a useful guess on that front.