CoCalc Public Fileswww / cgi-bin / openwebmail / misc / test / profadd.plOpen with one click!
Author: William A. Stein
1
#!/usr/bin/perl
2
#
3
# this uty adds prof log line at the begining of all subroutine,
4
# so calling of each routine will be logged into /tmp/openwebmail.debug
5
#
6
7
use strict;
8
if ($#ARGV<0) {
9
print "profadd file1 file2 ...\n";
10
exit;
11
}
12
13
my $updatedfile=0;
14
my $totalinsertion=0;
15
foreach my $script (@ARGV) {
16
my $insertion=0;
17
my $require=0;
18
my $content='';
19
my $profbeginline;
20
my $profendline;
21
22
print "add profile code to $script ...";
23
open (F, $script);
24
while (<F>) {
25
my $line=$_;
26
if ($line=~/^require "/ && !$require) {
27
$require=1;
28
$content.=qq|require "misc/test/gettimeofday.pl";\n|;
29
$content.=qq|ow::tool::timeofday_init();\n|;
30
$content.=$line;
31
$insertion++;
32
} elsif ($line=~/^sub\s*([^\s\{]+)/) {
33
$content.=$line;
34
my $subname=$1; chomp($subname);
35
if ($subname ne "log_time") {
36
$profbeginline=qq|ow::tool::log_time("PROF", ow::tool::timeofday_diff("$subname"), "CALL $subname()\\t", __LINE__, __FILE__, \@_);\n|;
37
$profendline =qq|ow::tool::log_time("PROF", ow::tool::timeofday_diff("$subname"), "END $subname()\\t", __LINE__, __FILE__);\n|;
38
$content.=$profbeginline;
39
$insertion++;
40
}
41
} elsif ($line=~/^\s*return[\s\(;]?.*;/ && $line!~/ if / && $profendline) {
42
$content.=$profendline;
43
$content.=$line;
44
$insertion++;
45
} else {
46
$content.=$line;
47
}
48
}
49
close(F);
50
51
if ($insertion) {
52
if (open (F, ">$script")) {
53
print F $content;
54
close(F);
55
print "$insertion line added\n";
56
$updatedfile++;
57
$totalinsertion+=$insertion;
58
} else {
59
print "update err!\n";
60
}
61
} else {
62
print "\n";
63
}
64
}
65
66
print "$updatedfile file updated (total $totalinsertion line added)\n";
67