tcl 中的exec运行第三方,怎么同时打印第三方信息到stdout

部署运行你感兴趣的模型镜像

运行第三方程序:

 

exec ?switches? arg ?arg ...? 

 

同时打印出信息到stdout呢?

exec ?switches? arg ?arg ...?  >@ stdout

 

这个方法还试验的不少次数,没有发现,因为tkcon中不支持这种,奇怪了。

tclsh中就支持。

 

当然,也可以用open |some program得到pipe后,在用fileevent $pipe readable [list Reader $pipe];来读pipe,这种方法可以过滤处理每一行的信息了。不过麻烦点儿,麻烦有麻烦的好处,在用【pid  $pipe】可以得到运行程序的pid,用[catch {close $pipe} err]还可以得到后台程序退出时的状态。

 

发现:

写道
exec ?switches? arg ?arg ...? &

 

其实只要在后台运行标准输入输出就自动重定向到前台程序上了。同样tclsh中这样行,tkcon不行,恨死tkcon,常常在这个上面做一些tcl的测试,有可以它做的GUI,这样可以改了一些东西。其实这种方法和第一种方法基本相似了。

 

 

比较好的介绍open和exec的文章:

http://www.tcl.tk/man/tcl/tutorial/Tcl26.html

 

例子:

set tempFileName invert_[pid].tcl
puts "1= $tempFileName"

# Open the output file, and
# write the program to it

set outfl [open $tempFileName w]

puts $outfl {
    set len [gets stdin line]
    if {$len < 5} {exit -1}

    for {set i [expr {$len-1}]} {$i >= 0} {incr i -1} {
        append l2 [string range $line $i $i]
    }
    puts " 2= $l2"
    exit 0
}

# Flush and close the file
flush $outfl
close $outfl

#
# Run the new Tcl script:
#
# Open a pipe to the program (for both reading and writing: r+)
#
set io [open "|[info nameofexecutable] $tempFileName" r+]

#
# send a string to the new program
#     *MUST FLUSH*
puts $io "This will come back backwards."
flush $io

# Get the reply, and display it.
set len [gets $io line]

puts "1= To reverse: 'This will come back backwards.'"
puts "1= Reversed is: $line"
puts "1= The line is $len characters long"

# Run the program with input defined in an exec call

set invert [exec [info nameofexecutable] $tempFileName << \
       "ABLE WAS I ERE I SAW ELBA"]

# display the results
puts "1= The inversion of 'ABLE WAS I ERE I SAW ELBA' is \n $invert"

# Clean up
file delete $tempFileName

 

例子就是建立了一个临时文件,运行这个文件用了两种方法,一个exec,一个open,运行的程序从stdin中得到字符,后把这个字符,反转后输出。

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

在 Android 系统中查看第三方输出的 `printf` 信息通常较为复杂,因为标准的 `printf` 输出不会直接显示在 Android 的日志系统中。Android 使用的是 `__android_log_print` 和 `__android_log_vprint` 等函数来记录日志信息,而第三方库可能并未使用这些函数进行日志输出。 一种解决方案是将标准输出stdout)和标准错误(stderr)重定向到 Android 的日志系统。可以通过以下方式实现: ```c #include <stdio.h> #include <stdlib.h> #include <android/log.h> static void redirect_stdout_to_logcat() { // 将 stdout 重定向到 Android 日志系统 setvbuf(stdout, NULL, _IOLBF, 0); freopen("/dev/null", "w", stderr); stdout = __stdout; // 重定向 stdout stderr = __stderr; // 重定向 stderr // 自定义日志写入函数 // 这里可以使用 __android_log_print 来记录日志 } ``` 此外,还可以通过 `adb logcat` 命令来查看日志信息。确保在设备连接的情况下执行以下命令: ```bash adb logcat ``` 如果第三方库使用了 `AV_LOG` 系列宏进行日志输出,可以通过自定义日志回调函数来捕获这些日志并将其输出到 Android 的日志系统中,如下所示: ```c #include <libavutil/log.h> #include <android/log.h> #define SYS_LOG_TAG "third_party_lib" static void custom_log_callback(void *ptr, int level, const char *fmt, va_list vl) { switch (level) { case AV_LOG_DEBUG: __android_log_vprint(ANDROID_LOG_VERBOSE, SYS_LOG_TAG, fmt, vl); break; case AV_LOG_VERBOSE: __android_log_vprint(ANDROID_LOG_DEBUG, SYS_LOG_TAG, fmt, vl); break; case AV_LOG_INFO: __android_log_vprint(ANDROID_LOG_INFO, SYS_LOG_TAG, fmt, vl); break; case AV_LOG_WARNING: __android_log_vprint(ANDROID_LOG_WARN, SYS_LOG_TAG, fmt, vl); break; case AV_LOG_ERROR: __android_log_vprint(ANDROID_LOG_ERROR, SYS_LOG_TAG, fmt, vl); break; } } static void setup_custom_logging() { av_log_set_callback(custom_log_callback); } ``` 通过上述方法,可以将第三方库的日志信息输出到 Android 的日志系统中,并通过 `adb logcat` 命令查看这些信息[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值