来源:

http://www.cnblogs.com/itech/archive/2012/09/22/2698385.html

对任何的函数将标准输出和错误输出重定向到对应的log文件。

对任何的函数记录函数运行的时间。

代码:

 #!/usr/bin/perl
use warnings;
use strict;
no strict "refs"; sub testLogToStd{
print "Test stdout : \n";
open LOG,"> 2.txt";
select LOG;
print "just a test\n";
#recover STDOUT
select STDOUT;
print "just a test2\n";
close LOG;
} sub testFun{
print "From testFun\n";
print STDERR "From TestFun Error\n";
}
sub testFun2{
my $arg1 = shift;
my $arg2 = shift;
print "From testFun2\n";
print $arg1."\n";
print $arg2."\n";
} my $log_root = "log" if(! $ ||$ == "");
my $ret = system("mkdir $log_root") if(! -e $log_root);
my $report_log = "$log_root/report.log";
open my $REPORTLOG,">",$report_log or die "cannot not open log file report.log\n"; sub logWrapper{
my $log_root = shift;
my $REPORTLOG = shift;
my $fun = shift;
my @parameters = @_;
*old_stdout = *STDOUT;
*old_stderr = *STDERR;
open LOG, ">","$log_root/$fun.log" or die "annot open log file $fun.\n";
*STDOUT = *LOG;
*STDERR = *LOG;
my $start = time;
my $ret = &$fun(@parameters);
my $end = time;
*STDOUT = *old_stdout;
*STDERR = *old_stderr;
close LOG; my $duration = $end - $start;
print $REPORTLOG "$fun\n";
print $REPORTLOG "start:".localtime($start)."\n";
print $REPORTLOG "end:".localtime($end)."\n";
print $REPORTLOG "duration:".formatTimeDuration($duration)."\n";
print $REPORTLOG "result:$ret\n";
print $REPORTLOG "\n";
print $REPORTLOG "\n";
} sub formatTimeDuration($){
my $t = shift;
my $hrs = int($t/);
my $mins = int($t%/);
my $secs = int($t%%);
return "$hrs:$mins:$secs";
} &logWrapper($log_root,$REPORTLOG,"testFun");
&logWrapper($log_root,$REPORTLOG,"testFun2","arg1","arg2");

print "thanks\n";

如果需要调用外部命令需要如下:

 use strict;
use warnings; # run external commands
# redirect stdout and stderr
sub run_cmd{
my $cmd = shift;
my $pid = open(PH, "$cmd 2>&1 |");
while (<PH>) {print $_; }
} open(FH, ">", "perl-test.log");
*old_stdout = *STDOUT;
*old_stderr = *STDERR;
*STDOUT = *FH;
*STDERR = *FH;
my $ret = undef;
$ret = readpipe("cp a b ");
$ret = system("cp a b");
$ret = `cp a b`;
run_cmd("cp a b");
print "AA";
print STDERR "BB";
*STDOUT = *old_stdout;
*STDERR = *old_stderr;

perl的logwrapper的更多相关文章

  1. 精通Perl(第2版)

    精通Perl(第2版)(通往Perl大师之路必读经典书籍,体现了一种编程思维,能够帮你解决很多实际的问题) [美]brian d foy(布瑞恩·D·福瓦)著   王兴宇 刘宸宇 译 ISBN 978 ...

  2. perl

    introduction: http://www.yiibai.com/perl/perl_introduction.html functions: http://www.yiibai.com/per ...

  3. perl学习之路3

    Perl编程之路3 标签: perl 列表与数组   Perl里面代表复数的就是列表和数组 列表(list)指的是标量的有序集合, 而数组(array)则是存储列表的变量. 在Perl这两个属于尝尝混 ...

  4. perl学习之路2

    这些主要是从 "小骆驼" 书上粘贴或者摘抄出来的, 个人认为需要记的语法知识 "在某些情况下, 你可能需要在一台机器上写程序, 再传送到另一台机器上运行.这时候, 请使用 ...

  5. perl学习之路1

    一切要从Hollo world开始 公司要用perl....啊, 不会只能自学了, 毕竟是公司啊, 不是学校...公司不学习就滚蛋了...惨惨惨 因为是学习嘛, 感觉开虚拟机比较麻烦所以直接用了个 瘟 ...

  6. perl 切换 dnspod 域名记录

    提供域名,dnspod 账户密码(毕竟dns密码比较重要 不能谁 cat一下都可以看到 需要base64加密),原IP,切换目标IP, #!/bin/perl use warnings; use MI ...

  7. perl 删除过期文件

    #!/usr/bin/perl `find /bak/ >list.txt`; open LIST,"/root/list.txt"; while (<LIST> ...

  8. 通过远程 http API 来控制 lnmp 环境的重启perl脚本

    #!/usr/bin/perl use DBD::mysql; use strict; use warnings; use DBI; use utf8; binmode(STDOUT, ':encod ...

  9. 使用Python和Perl绘制北京跑步地图

    当你在一个城市,穿越大街小巷,跑步跑了几千公里之后,一个显而易见的想法是,如果能把在这个城市的所有路线全部画出来,会是怎样的景象呢? 文章代码比较多,为了不吊人胃口,先看看最终效果,上到北七家,下到南 ...

随机推荐

  1. 触发器实现对插入数据的字段更改 Oracle+SQL Server

    最近有个使用触发器实现对插入数据的某个列做更改的需求,因此整理了Oracle和SQL Server对于此类需求的触发器写法,本文仅提到了Insert触发器. 首先我们创建一张表: --创建Test表 ...

  2. MySQL导入乱码解决

    导入时出现乱码,需要在语句中添加指定导入数据的编码格式: mysql -uroot -p database_name < database_backup.sql --default-charac ...

  3. NYIST OJ 题目42 一笔画问题

    水题.无向图欧拉通路的判定.用并查集判定是不是连通图! #include<cstdio> #include<cstring> #include<cmath> #in ...

  4. Redis断线重连编码注意事项

    应用在Redis重启.网络闪断并恢复正常后,应用必须能够自恢复,下面以Java语言的jedis客户端为例说明: 1.作为发布者 Jedis对象不能作为单例,网络闪断后该Jedis对象无法自恢复.应该每 ...

  5. 为什么PHP(CLI)同一个错误信息会打印两次?

    第一个信息是display_errors输出的,在fpm环境下输出到浏览器那里,而在CLI环境下会打印到屏幕上. display_errors = On 第二个信息是log_errors输出的. lo ...

  6. 浙大pat1019题解

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  7. MATLAB制作符合IEEE标准的图插入Latex

    1.MATLAB最好保存为eps格式,虽然IEEE也支持png等其他格式,但是MATLAB在保存为png格式时,很容易在后期插图时,出现分辨率不足等问题. 2. MATLAB在save as图片的时候 ...

  8. OpenCV2.x自学笔记——最大类间方差法OTSU

    推荐用法:(参数勿动) threshold(gray,binary,0,255,CV_THRESH_OTSU+CV_THRESH_BINARY);

  9. HDU 2102 A计划(DFS)

    题目链接 Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主 ...

  10. MySQL5.5.源码安装

    MySQL5.5.34安装需要用到cmke  ncurses-devel yum install -y ncurses-devel cmake gcc gcc-c++ bison 下载http://m ...