如何抑制 TCL 程序的输出消息? [英] How can I suppress the output messages of TCL procedures?

查看:36
本文介绍了如何抑制 TCL 程序的输出消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 TCL 脚本中,我使用了几个我没有源代码的过程.所有这些过程都执行一些任务并输出大量消息.但我只想完成任务,我想抑制消息.有没有办法做到这一点.

In my TCL script I'm using several procedures that I don't have the source for. All these procedures do some tasks and output a lot of messages. But I just want the tasks to be done and I want to suppress the messages. Is there a way to do this.

例如,我想运行这样的程序:

So for example I would like to run a procedure like so:

my_proc $arg1 $arg2 $arg3

并抑制所有消息.任何解决方法/智能替代方案都值得赞赏.

and suppress all it's messages. Any workarounds/ smart alternatives are appreciated.

更多信息:我正在使用一个自定义 shell,它将一个 TCL 文件作为参数并运行它.在这个自定义 shell 中,我可以访问一些我没有代码的 TCL 过程.

More info: I'm using a custom shell that takes a TCL file as an argument and runs it. Inside this custom shell I have access to some TCL procedures for which I don't have the code.

或者甚至有什么方法可以让脚本的输出转到文件而不是命令提示符(stdout)?

Or even is there any way I can have the output of the script go to a file instead of the command prompt (stdout)?

推荐答案

尝试更改代码中的 puts:

rename ::puts ::tcl_puts
proc puts args {}        ;# do nothing

然后,如果你想打印一些东西,使用 tcl_puts

Then, if you want to print something, use tcl_puts

这有点核选项.你可以变得更微妙:

This is a bit of a nuclear option. You can get subtler:

proc puts args {
    if {[llength $args] == 1} {
        set msg [lindex $args 0]
        # here you can filter based on the content, or just ignore it
        # ...
    } else {
        # in the 2a\-args case, it's file io, let that go
        # otherwise, it's an error "too many args"
        # let Tcl handle it
        tcl_puts {*}$args

        # should probably to stuff there so that errors look like
        # they're coming from "puts", not "tcl_puts"
    }
}

<小时>

另一个想法:在您调用的命令期间执行此操作:


Another thought: just do it for the duration of the command you're calling:

proc noputs {args} {
    rename ::puts ::tcl_puts
    proc ::puts args {}

    uplevel 1 $args

    rename ::puts ""
    rename ::tcl_puts ::puts
}

noputs my_proc $arg1 $arg2 $arg3

演示:

$ tclsh
% proc noputs {args} {
    rename ::puts ::tcl_puts
    proc ::puts args {}

    uplevel 1 $args

    rename ::puts ""
    rename ::tcl_puts ::puts
}
% proc my_proc {foo bar baz} {
    lappend ::my_proc_invocations [list $foo $bar $baz]
    puts "in myproc with: $foo $bar $baz"
}
% my_proc 1 2 3
in myproc with: 1 2 3
% noputs my_proc a b c
% my_proc x y z
in myproc with: x y z
% set my_proc_invocations
{1 2 3} {a b c} {x y z}

这篇关于如何抑制 TCL 程序的输出消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆