File Coverage

blib/lib/Log/SelfHistory.pm
Criterion Covered Total %
statement 6 23 26.0
branch 0 4 0.0
condition n/a
subroutine 2 5 40.0
pod 0 3 0.0
total 8 35 22.8


line stmt bran cond sub pod time code
1             package Log::SelfHistory;
2              
3 1     1   36421 use strict;
  1         1  
  1         29  
4 1     1   4 use warnings;
  1         1  
  1         382  
5              
6             require Exporter;
7              
8             our @ISA = qw(Exporter);
9             our @EXPORT = qw(writeCmd);
10              
11             our $VERSION = '0.01';
12              
13             my $PWD = `pwd`;
14             my $DATE = `date`;
15             my $USER = `whoami`;
16             my $SCRIPT = $0;
17             $SCRIPT =~ s/\.?//;
18             $SCRIPT =~ s/\/?//;
19             my $sComment = '#--------------------------------------------------';
20              
21             chomp($PWD); chomp($DATE); chomp($USER);
22              
23             sub countCmds {
24 0     0 0   my $iCount = `grep -E \"Executed(.*) on\" $PWD/$SCRIPT -c`;
25              
26 0 0         if ($iCount !~ /(\d+)/sg) {
27 0           return 0;
28             } else {
29 0           return $1;
30             }
31             }
32              
33             sub earseCmds {
34 0     0 0   my ($iMaxCount, $iActualCount) = @_;
35              
36 0 0         if ($iActualCount >= $iMaxCount) {
37 0           `sed '/\^\#Executed.*/d' -i $PWD/$SCRIPT`;
38 0           `sed '/\^\#----.*/d' -i $PWD/$SCRIPT`
39             }
40             }
41              
42             sub writeCmd {
43 0     0 0   my ($TAG, $iMaxCount) = @_;
44              
45 0           my $iCount = countCmds(); chomp($iCount);
  0            
46 0           earseCmds($iMaxCount, $iCount);
47 0           my $ARGUMENTS = join(' ', @ARGV);
48 0           my $sCMD = "sed 's|\^\#$TAG.*|\#$TAG \\n$sComment\\n\#";
49 0           $sCMD .= "Executed Cmd:\" $PWD/$SCRIPT $ARGUMENTS\\n\#Executed on:";
50 0           $sCMD .= "$DATE\\n\#Executed by: $USER\\n$sComment|' -i $PWD/$SCRIPT";
51 0           `$sCMD`;
52             }
53              
54             1;
55              
56             =head1 NAME
57              
58             Log::SelfHistory - Perl extension for logging self execution history.
59              
60             =cut
61              
62             =head1 SYNOPSIS
63              
64             use Log::SelfHistory;
65             writeCmd("CMD",3);
66              
67             Where:
68             "CMD" is the tag in the caller script to place execution history.
69             we can place this tag in script were we want execution history to
70             be displayed.
71              
72             "3" is no of exectuions to log, so after 3 exectuions the log history
73             will be reset.
74              
75             Example:
76            
77             #Contents is test.pl(caller script).
78              
79             use Log::SelfHistory;
80             writeCmd("CMD",3);
81              
82             #CMD (!!!Please note here the tag is put in form of comment!!!).
83            
84              
85             =cut
86              
87             =head1 DESCRIPTION
88              
89             Log self execution history in the caller script itself.
90             Also control the number of exectuions logged.
91              
92             ****** Module Works on Unix Boxes Only *******
93              
94             =cut
95              
96              
97             =head1 AUTHOR
98              
99             Tushar, tushar@cpan.org
100             =cut
101              
102             =head1 COPYRIGHT AND LICENSE
103              
104             Copyright (C) 2010 by Tushar Murudkar
105              
106             This library is free software; you can redistribute it and/or modify
107             it under the same terms as Perl itself, either Perl version 5.8.8 or,
108             at your option, any later version of Perl 5 you may have available.
109              
110             =cut
111