How can I suppress the output messages of TCL procedures?

后端 未结 1 1153
遥遥无期
遥遥无期 2021-01-16 04:59

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

相关标签:
1条回答
  • 2021-01-16 05:12

    Try altering puts in your code:

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

    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
    

    Demo:

    $ 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}
    
    0 讨论(0)
提交回复
热议问题