来源:

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. openstack私有云布署实践【4.1 上层代理haproxy配置 (科兴环境)】

    官方文档上的高可用配置,它推荐的是使用haproxy的上层代理来实现服务组件的主备访问.或者负载均衡访问   一开始我也是使用haproxy来做的,但后来方式改了   测试环境:haproxy + n ...

  2. android使用shape做selector按钮按下和弹起的动画

    平时效果:   按下效果: selector代码: <?xml version="1.0" encoding="utf-8"?> <selec ...

  3. Linux用户相关命令

    1.建用户: adduser snailz //新建用户 snailz passwd snailz //给用户 snailz 设置密码 2.建工作组 groupadd test //新建test工作组 ...

  4. c# delegate的invoke和bejinInvoke的区别

    先看下面实实例代码 private delegate void testdg(); private void button1_Click(object sender, EventArgs e)     ...

  5. map 理解

    键值对 map会将同名的值覆盖掉 public static void main(String[] args) { Map<String,String> maptest=new HashM ...

  6. UltraEdit 中的常用正则表达式

    正则表达式 (UltraEdit Syntax):   % 匹配行首 - 表明要搜索的字符串一定在行首.   $ 匹配行尾 - 表明要搜索的字符串一定在行尾   ? 匹配除换行符外的任一单个字符.   ...

  7. 安装 sublime package control

    import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_p ...

  8. Chapter 15_3 使用环境

    创建模块的基本方法的缺点在于,忘记使用local,很容易就污染全局空间. “函数环境”是一种有趣的技术,它能够解决上面的问题.就是让模块的主程序块独占一个环境. 这样不仅它的所有函数可以共享这个tab ...

  9. Foundations of Computer Science

    1, Iteration, Induction and Recursion 2, the running time of program 3, combinatorics and probabilit ...

  10. java 判断大小写、数字出现的次数

    //定义一个字符串 String s = "Hello123World"; //定义三个统计变量 int bigCount = 0; int smallCount = 0; int ...