adium: Adding fix per bug #196 comments
[siplcs.git] / contrib / debug / parse_log.pl
blob9724c10f7d852f82ae802deee9c87c12b68e4ed4
1 #!/usr/bin/perl -w
2 use 5.010;
3 use strict;
4 use warnings;
6 use File::Spec;
7 use Getopt::Long;
8 use Pod::Usage;
10 # Command line option
11 my %Options = (
12 directory => ".",
14 GetOptions(\%Options,
15 "directory=s",
16 "callid",
17 "from",
18 "method",
19 "filter",
20 "help|h|?")
21 or pod2usage(2);
22 pod2usage(-verbose => 2) if $Options{help};
24 ###############################################################################
26 # Message parsing
28 ###############################################################################
29 my %callid;
30 my %from;
31 my %method;
32 sub AddMessage($$$@)
34 my($direction, $type, $time, @message) = @_;
36 # Only handle SIP for now...
37 return unless $type eq "SIP";
39 my($index, $callid, $from, $method);
40 foreach my $line (@message) {
41 next if $index++ < 1;
42 last if $line =~ /^\s+$/;
44 next unless my($keyword, $value) = $line =~ /^([^:]+):\s+(.+)/;
45 $callid = $value if $keyword =~ /^call-id$/i;
46 ($from) = $value =~ /^<sip:([^;>]+)/ if $keyword =~ /^from$/i;
47 ($method) = $value =~ /^\d+\s+(\S+)/ if $keyword =~ /^cseq$/i;
49 push(@{$callid{$callid}}, \@message) if $Options{callid} && defined $callid;
50 push(@{$from{lc($from)}}, \@message) if $Options{from} && defined $from;
51 push(@{$method{uc($method)}}, \@message) if $Options{method} && defined $method;
54 sub DumpMessages()
56 foreach my $callid (keys %callid) {
57 if (open(my $fh, ">",
58 File::Spec->catfile($Options{directory}, "callid-${callid}.txt"))) {
59 print $fh @{$_} foreach (@{$callid{$callid}});
60 close($fh);
63 foreach my $from (keys %from) {
64 if (open(my $fh, ">",
65 File::Spec->catfile($Options{directory}, "from-${from}.txt"))) {
66 print $fh @{$_} foreach (@{$from{$from}});
67 close($fh);
70 foreach my $method (keys %method) {
71 if (open(my $fh, ">",
72 File::Spec->catfile($Options{directory}, "method-${method}.txt"))) {
73 print $fh @{$_} foreach (@{$method{$method}});
74 close($fh);
79 ###############################################################################
81 # Main program
83 ###############################################################################
85 # For all lines from command line files or STDIN
86 my @message;
87 my $counter;
88 while (<>) {
90 # Start of message?
91 if (my ($direction, $type, $time) =
92 /^MESSAGE START\s+([<>]+)\s+(\S+)\s+-\s+(.+)/) {
93 push(@message,
94 "------------- NEXT MESSAGE: " .
95 (($direction =~ /^>/) ? "outgoing" : "incoming") .
96 " $type at $time\n");
98 # End of message?
99 } elsif (($direction, $type, $time) =
100 /^MESSAGE END\s+([<>]+)\s+(\S+)\s+-\s+(.+)/) {
102 if ($Options{filter}) {
103 print @message;
104 } else {
105 print STDERR "." if (++$counter % 10 == 0);
106 AddMessage($direction, $type, $time, @message);
109 # Done with the current message
110 undef @message;
112 # All other lines
113 } else {
115 # Collect message information
116 push(@message, $_) if @message;
120 unless ($Options{filter}) {
121 print STDERR "\n" unless $Options{filter};
122 DumpMessages();
125 # That's all folks...
126 exit 0;
128 __END__
130 =head1 NAME
132 parse_log.pl - parse pidgin-sipe debug log
134 =head1 SYNOPSIS
136 [perl} parse_log.pl --help|-h|-?
138 [perl} parse_log.pl --filter
139 [file ...]
141 [perl} parse_log.pl [--directory <dir>]
142 [--callid]
143 [--from]
144 [--method]
145 [file ...]
147 =head1 OPTIONS
149 =over 8
151 =item B<--callid>
153 Dump all SIP messages belonging to one Call-ID to the same file.
155 =item B<--directory>
157 Directory for output files. Default is the current directory.
159 =item B<--filter>
161 Enable filter mode. Messages are simply printed to stdout.
163 =item B<--from>
165 Dump all SIP messages sent from the same SIP URI to the same file.
167 =item B<--help>
169 =item B<--h>
171 =item B<--?>
173 Print a brief help message and exits.
175 =item B<--method>
177 Dump all SIP messages with the same method to the same file.
179 =back
181 =head1 DESCRIPTION
183 B<This program> extracts SIP/HTTP messages from pidgin-sipe debug logs. If
184 no file is specified then it reads from STDIN.
186 =cut