Redirect the stdout when loading package in Tcl

一曲冷凌霜 提交于 2021-01-29 02:52:54

问题


I would like to redirect the stdout to null when loading package in Windows Tcl. (Redirect the wording of "Quality Windows Audio/Video Experience (qWAVE) support is available." to null)

Is their any way to solve this or any idea for this ?? Thank you so much.

C:\Users\Tester>tclsh
%       set ixchariot_installation_dir "C:/Program Files x86)/Ixia/IxChariot"
C:/Program Files (x86)/Ixia/IxChariot
%       cd $ixchariot_installation_dir
%       load ChariotExt
Quality Windows Audio/Video Experience (qWAVE) support is available.

回答1:


If the library is using Tcl channels to write its message, and you're using Tcl 8.6, it's pretty easy. You just push a transform on the stdout channel that swallows all bytes.

# Most of this is boiler-plate stuff...
namespace eval swallow {
    proc initialize {handle mode} {
        return {initialize clear finalize write flush}
    }
    proc clear {handle} {}
    proc finalize {handle} {}
    proc flush {handle} {}
    # The important one; do nothing to throw away bytes
    proc write {handle buffer} {}
    # Export as an ensemble
    namespace export *
    namespace ensemble create
}

# Start dropping output
chan push stdout swallow
load ChariotExt
# Stop dropping output
chan pop stdout

That only works if the library is using Tcl channels to write it's message. If it is using a direct write (the more likely case) it won't. You can instead try a full redirect, but these are not easily undone.

close stdout
open NUL
load ChariotExt

I know that'd work on POSIX systems (except with /dev/null instead of NUL). Not sure on Windows. And it can't be easily undone; the old standard output stream is gone.

And in any case, it's possible that the library is using a direct write to the console; those aren't blockable, and you might just have to live with that irritating message.



来源:https://stackoverflow.com/questions/40425550/redirect-the-stdout-when-loading-package-in-tcl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!