我想在程序完成后将STDOUT,STDERR重定向到temp_log,然后重定向到logfile.txt.这个过程运行了整整 20分钟,因此我想在时间进程运行中涌出temp_log.
mob*_*mob 15
STDOUT
并且STDERR
只是初始化为程序的标准输出和错误的文件句柄,但它们可以随时因任何原因重新分配.为此,您需要保存原始设置,以便还原它们.
sub process_that_writes_to_temp_log {
# save original settings. You could also use lexical typeglobs.
*OLD_STDOUT = *STDOUT;
*OLD_STDERR = *STDERR;
# reassign STDOUT, STDERR
open my $log_fh, '>>', '/logdirectory/the-log-file';
*STDOUT = $log_fh;
*STDERR = $log_fh;
# ...
# run some other code. STDOUT/STDERR are now redirected to log file
# ...
# done, restore STDOUT/STDERR
*STDOUT = *OLD_STDOUT;
*STDERR = *OLD_STDERR;
}
Run Code Online (Sandbox Code Playgroud)
根据程序的布局方式,可以使用自动保存和恢复旧的STDOUT/STDERR设置local
.
sub routine_that_writes_to_log {
open my $log_fh, '>>', 'log-file';
local *STDOUT = $log_fh;
local *STDERR = $log_fh;
# now STDOUT,STDERR write to log file until this subroutine ends.
# ...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17754 次 |
最近记录: |