OSDN Git Service

Fix checkinig content
[ultramonkey-l7/ultramonkey-l7-v2.git] / logger / extract_log
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6
7 @ARGV || do {
8     die "Usage: $0 SRCFILE ...\n"
9       . "Extract log message from specified source code,\n"
10       . "and output log lists in CSV format.\n"
11       . "\n"
12       . "Example:\n"
13       . "  \$ $0 ../src/*.c ../module/*/*.c*\n";
14 };
15
16 my %all_log;
17 $| = 1;
18
19 # pick out all log message!
20 for my $src (@ARGV) {
21     print STDERR "reading $src ... ";
22     if (!-f $src) {
23         print STDERR "\n$src: No such file\nPush Enter key to continue.\n";
24         <STDIN>;
25         next;
26     }
27     my $fh;
28     if (!open $fh, "<", $src) {
29         print STDERR "\n$src: Cannot open file\nPush Enter key to continue.\n";
30         <STDIN>;
31         next;
32     }
33     my $all_line;
34     map { $all_line .= $_ } <$fh>;
35     close $fh;
36
37     # remove all CRLF
38     $all_line =~ s/[\r\n]//g;
39     my $count = 0;
40
41     # log function regex
42     while ($all_line =~ /
43         (                                       # capture start
44           (?:
45             LOGGER_PUT_LOG_([^\s(]+) \s* \( \s* # LOGGER_PUT_LOG_LEVEL start
46           |                                     # or
47             PUT_LOG_([^\s(]+) \s* \( [^,]+, \s* # PUT_LOG_LEVEL start
48           )
49           ([^\s,]+) \s* , \s*                   # log category
50           (\d+) \s* , \s*                   # log number
51           ( (?: (?: "[^"]*" \s* )+ | [^,)]+) )  # log message
52           .*?                                   # any argument
53           \) \s* ;                              # log function end
54         )                                       # capture end
55     /xsg) {
56         my ($log, $category, $number, $message) = ($1, $4, $5, $6);
57         my $level = $2 || $3;
58
59         # push to hash table (push for duplicate log)
60         push @{ $all_log{$category}{$level}{$number}{file    } }, $src;
61         push @{ $all_log{$category}{$level}{$number}{log     } }, $log;
62         push @{ $all_log{$category}{$level}{$number}{message } }, $message;
63
64         $count++;
65     }
66     print STDERR "$count logs.\n";
67 }
68
69 #print Dumper \%all_log;
70 #exit;
71
72 # output log list
73 my %summary;
74 my @duplicate_list;
75 for my $category (sort keys %all_log) {
76     print "===== $category =====\n";
77     for my $level ( sort keys %{ $all_log{$category} } ) {
78         print "----- $level -----\n";
79         for my $number ( sort { $a <=> $b } keys %{ $all_log{$category}{$level} } ) {
80             my $entry = $#{ $all_log{$category}{$level}{$number}{file} };
81             for (0 .. $entry) {
82                 my $file    = $all_log{$category}{$level}{$number}{file   }[$_];
83                 my $log     = $all_log{$category}{$level}{$number}{log    }[$_];
84                 my $message = $all_log{$category}{$level}{$number}{message}[$_];
85                 # CSV
86                 print "$category,$level,$number,$file,$message,";
87                 if ($entry > 0) {
88                     # duplicate tag
89                     print "DUPLICATE";
90                     push @duplicate_list, "$file: $log\n";
91                 }
92                 print "\n";
93                 $summary{$category}{all}++;
94                 $summary{$category}{$level}++;
95                 $summary{$category}{duplicate}{all}    += $entry;
96                 $summary{$category}{duplicate}{$level} += $entry;
97             }
98         }
99     }
100     print "\n";
101 }
102
103 # print all duplicate log
104 #print "===== DUPLICATE LOG =====\n";
105 #for (@duplicate_list) {
106 #    print;
107 #}
108 #print "\n";
109
110 # print summary
111 print "===== SUMMARY =====\n";
112 for my $category (sort keys %summary) {
113     # category has duplicate log
114     if ( $summary{$category}{duplicate}{all} ) {
115         print "$category (duplicate)\n";
116         printf "ALL: %d(%d), ", $summary{$category}{all} || 0, $summary{$category}{duplicate}{all} || 0;
117         printf "FATAL: %d(%d), ", $summary{$category}{FATAL} || 0, $summary{$category}{duplicate}{FATAL} || 0;
118         printf "ERROR: %d(%d), ", $summary{$category}{ERROR} || 0, $summary{$category}{duplicate}{ERROR} || 0;
119         printf "WARN: %d(%d), ", $summary{$category}{WARN} || 0, $summary{$category}{duplicate}{WARN} || 0;
120         printf "INFO: %d(%d), ", $summary{$category}{INFO} || 0, $summary{$category}{duplicate}{INFO} || 0;
121         printf "DEBUG: %d(%d)", $summary{$category}{DEBUG} || 0, $summary{$category}{duplicate}{DEBUG} || 0;
122         print "\n";
123     }
124     # category has no duplicate log
125     else {
126         print "$category\n";
127         printf "ALL: %d, ", $summary{$category}{all} || 0;
128         printf "FATAL: %d, ", $summary{$category}{FATAL} || 0;
129         printf "ERROR: %d, ", $summary{$category}{ERROR} || 0;
130         printf "WARN: %d, ", $summary{$category}{WARN} || 0;
131         printf "INFO: %d, ", $summary{$category}{INFO} || 0;
132         printf "DEBUG: %d ", $summary{$category}{DEBUG} || 0;
133         print "\n";
134     }
135 }