【问题标题】:how to create log file for tcl script如何为 tcl 脚本创建日志文件
【发布时间】:2015-05-29 15:59:41
【问题描述】:

我正在运行用于连接 Telnet 端口的 Tcl 脚本。对于这个脚本,我想将所有 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 
}

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

提前感谢您的帮助。

【问题讨论】:

    标签: tcl


    【解决方案1】:

    您想在要开始保存输出的位置插入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"
    

    默认情况下,log_file 如果文件存在则追加,或者创建一个新文件。如果你想每次都开始一个新的日志,那么:

    log_file -noappend myfile.log
    

    更新

    根据您关于为什么 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"
    

    上面的例子引入了另一个命令:send_user,它的作用类似于putssend_log的组合。请注意,send_user 不包含新行,因此调用者应包含它。

    【讨论】:

    • @Vu - 我正在发送以下命令,然后检查 if else 条件以调用一个过程。脚本调用正在工作并打印该调用过程的 CMD 中的放置,但不在 log_myfile.log 文件中。 send "n\r" expect "&gt;" #expect * ;#this is for to get data in expect_out set passwordOption $expect_out(buffer) #puts "$passwordOption" set searchString "Not confirmed" if {[string match *$searchString* $passwordOption]} { puts "match found" }\ else { puts "match not found" xmlPasswordChange $polName }如何在日志文件中打印所有 CMD 输出?
    • 你能用这些信息更新你的主要帖子吗?我不擅长阅读这样的文字。对此感到抱歉。
    • @Vu - 我在主帖中添加了我的问题。你能调查一下吗?
    • 非常感谢您。用 send_user 替换 puts 后,它正在工作。
    【解决方案2】:

    我们还可以通过 stanted [open $file_name w] 命令创建自己的日志文件,并继续将所有内容写入该文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-06
      • 2020-10-02
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      相关资源
      最近更新 更多