![](/img/trans.png)
[英]How to print a datagridview where each row is printed in a new page?
[英]TCL - How to print on the screen th messages that where printed during execution of exec command?
假設我想通過打印運行時執行輸出來執行腳本或可執行文件。
當我做:
set log [exec ./executable_file]
puts $log
然后等待很長時間然后立即打印所有內容。 但我想要運行時打印。 我怎樣才能做到這一點?
不完美(因為它需要寫入外部文件):
set log [exec executable_file | tee log.txt >@stdout]
輸出將立即顯示,同時保存為“log.txt”。 如果您不關心保存輸出:
set log [exec executable_file >@stdout]
從返回的描述符中使用open "| ..."
和異步行讀取,如下所示:
proc ReadLine fd {
if {[gets $fd line] < 0} {
if {[chan eof $fd]} {
chan close $fd
set ::forever now
return
}
}
puts $line
}
set fd [open "| ./executable_file"]
chan configure $fd -blocking no
chan event $fd readable [list ReadLine $fd]
vwait forever
有關更多參與示例,請參閱此Wiki頁面 。
在實際程序中,您可能已經運行了一個事件循環,因此不需要特定於讀取一個命令輸出的vwait
。
此外,如果您需要收集輸出,而不僅僅是[puts]
讀取后的每一行,您可能需要創建一個全局(通常是命名空間的)變量,將其初始化為“”,將其名稱作為另一個參數傳遞給回調過程(此處為ReadLine
)並將該行追加到該變量的值。
也許你可以在可執行文件之前在后台啟動另一個進程,比如tail -f logfile.txt,並在這個logfile.txt中輸出你的可執行文件的結果?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.