如何为tcl脚本创建日志文件

Piy*_*ush 1 tcl

我正在运行Tcl脚本来连接Telnet端口.对于此脚本,我想将所有CMD输出存储在日志文件中.如何在Tcl脚本中执行此操作?脚本如下:

#!/usr/bin/expect -f
#!usr/bin/expect
package require Expect
spawn telnet $serverName $portNum
expect "TradeAggregator>"
send "Clients\r"
expect "Client:"
send "1\r"
expect "1-Client>"
send "Pollers\r"
expect "Client Pollers"
send "2\r"
send "n\r"
expect ">"
set passwordOption $expect_out(buffer)
set searchString "Not confirmed" 
if {[string match *$searchString* $passwordOption]} { 
puts "match found" }\ 
else { 
puts "match not found" 
xmlPasswordChange $polName 
}
Run Code Online (Sandbox Code Playgroud)

所有puts输出和xmlPasswordChange过程输出都不在日志文件中打印.你能指出我做错了吗?

感谢您的帮助.

Hai*_* Vu 11

您想插入要在log_file哪里开始保存输出的命令.例如,如果要保存所有输出,请将其粘贴到开头:

#!/usr/bin/expect -f

log_file myfile.log ;# <<< === append output to a file

package require Expect
spawn telnet $serverName $portNum
expect "TradeAggregator>"
send "Clients\r"
expect "Client:"
send "1\r"
expect "1-Client>"
send "Pollers\r"
expect "Client Pollers"
send "2\r"
Run Code Online (Sandbox Code Playgroud)

默认情况下,log_file如果文件存在则附加,或创建新文件.如果您想每次都启动一个新日志,那么:

log_file -noappend myfile.log
Run Code Online (Sandbox Code Playgroud)

更新

根据您的问题,puts输出转到控制台而不是日志文件.我理解的方式是puts进入控制台.如果要登录日志文件(使用该log_file命令打开的日志文件),请改用send_log命令:

log_file -noappend myfile.log
send_log "This line goes into the log file"
send_user "This line goes into both the log file and console\n"
puts "This line goes to the console"
Run Code Online (Sandbox Code Playgroud)

上面的例子引入了另一个命令:send_user,其作用类似于puts并且send_log组合在一起 请注意,send_user不包含新行,因此调用者应包含它.