zoukankan      html  css  js  c++  java
  • perl的logwrapper

    来源:

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

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

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

    代码:

     1 #!/usr/bin/perl
     2 use warnings;
     3 use strict;
     4 no strict "refs";
     5 
     6 sub testLogToStd{
     7 print "Test stdout : 
    ";
     8 open LOG,"> 2.txt";
     9 select LOG;
    10 print "just a test
    ";
    11 #recover STDOUT
    12 select STDOUT;
    13 print "just a test2
    ";
    14 close LOG;
    15 }
    16 
    17 sub testFun{
    18   print "From testFun
    ";
    19   print STDERR "From TestFun Error
    ";
    20 }
    21 sub testFun2{
    22   my $arg1 = shift;
    23   my $arg2 = shift;
    24   print "From testFun2
    ";
    25   print $arg1."
    ";
    26   print $arg2."
    ";
    27 }
    28 
    29 my $log_root = "log" if(! $3 ||$3 == "");
    30 my $ret = system("mkdir $log_root") if(! -e $log_root);
    31 my $report_log = "$log_root/report.log";
    32 open my $REPORTLOG,">",$report_log or die "cannot not open log file report.log
    ";
    33 
    34 sub logWrapper{
    35   my $log_root = shift;
    36   my $REPORTLOG  = shift;
    37   my $fun = shift;
    38   my @parameters = @_;
    39   *old_stdout = *STDOUT;
    40   *old_stderr = *STDERR;
    41   open LOG, ">","$log_root/$fun.log" or die "annot open log file $fun.
    ";
    42   *STDOUT = *LOG;
    43   *STDERR = *LOG;
    44   my $start = time;
    45   my $ret = &$fun(@parameters);
    46   my $end = time;
    47   *STDOUT = *old_stdout;
    48   *STDERR = *old_stderr;
    49   close LOG;
    50 
    51   my $duration = $end - $start;
    52   print $REPORTLOG "$fun
    ";
    53   print $REPORTLOG "start:".localtime($start)."
    ";
    54   print $REPORTLOG "end:".localtime($end)."
    ";
    55   print $REPORTLOG "duration:".formatTimeDuration($duration)."
    ";
    56   print $REPORTLOG "result:$ret
    ";
    57   print $REPORTLOG "
    ";
    58   print $REPORTLOG "
    ";
    59 }
    60 
    61 sub formatTimeDuration($){
    62   my $t = shift;
    63   my $hrs = int($t/3600);
    64   my $mins = int($t%3600/60);
    65   my $secs = int($t%3600%60);
    66   return "$hrs:$mins:$secs";
    67 }
    68 
    69 
    70 &logWrapper($log_root,$REPORTLOG,"testFun");
    71 &logWrapper($log_root,$REPORTLOG,"testFun2","arg1","arg2");

    print "thanks "; 

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

     1 use strict;
     2 use warnings;
     3 
     4 # run external commands
     5 # redirect stdout and stderr
     6 sub run_cmd{
     7   my $cmd = shift;
     8   my $pid = open(PH, "$cmd 2>&1 |");
     9   while (<PH>) {print $_; }
    10 }
    11 
    12 open(FH, ">", "perl-test.log");
    13 *old_stdout = *STDOUT;
    14 *old_stderr = *STDERR;
    15 *STDOUT = *FH;
    16 *STDERR = *FH;
    17 my $ret = undef;
    18 $ret = readpipe("cp a b ");
    19 $ret = system("cp a b");
    20 $ret = `cp a b`;
    21 run_cmd("cp a b");
    22 print "AA";
    23 print STDERR "BB";
    24 *STDOUT = *old_stdout;
    25 *STDERR = *old_stderr;
  • 相关阅读:
    HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】
    POJ 3177——Redundant Paths——————【加边形成边双连通图】
    亲历:IT 从业者避免猝死攻略 v1.0
    Linux如何统计进程的CPU利用率
    gcp – 源于CP的高级命令行文件拷贝工具
    丰田栽了的原因,嵌入式软件工程师都该看看
    四件在我步入职业软件开发生涯那天起就该知道的事情
    浅谈自底向上的Shell脚本编程及效率优化
    实用硬件产品集锦
    [置顶] openHAB 体系结构与编程模型 (1) --- 术语
  • 原文地址:https://www.cnblogs.com/spriteflk/p/5741099.html
Copyright © 2011-2022 走看看