]> git.ipfire.org Git - thirdparty/squid.git/blob - scripts/trace-kid.pl
Source Format Enforcement (#532)
[thirdparty/squid.git] / scripts / trace-kid.pl
1 #!/usr/bin/perl -w
2 #
3 ## Copyright (C) 1996-2020 The Squid Software Foundation and contributors
4 ##
5 ## Squid software is distributed under GPLv2+ license and includes
6 ## contributions from numerous individuals and organizations.
7 ## Please see the COPYING and CONTRIBUTORS files for details.
8 ##
9
10 # Reads cache.log and displays lines that correspond to a given kid.
11 #
12 # Cache log format and logging bugs make accurate kid attribution impossible,
13 # but this script is much better than running "grep kidN cache.log" and missing
14 # all "kidless" lines that do not contain kidN ID, such as HTTP header dumps.
15
16 use strict;
17 use warnings;
18 use Getopt::Long;
19
20 my $IncludePrefix = 0; # include initial kidless lines
21 my $IncludeMentions = 0; # include other kid references to the targeted kid
22 GetOptions(
23 "prefix!" => \$IncludePrefix,
24 "mentions!" => \$IncludeMentions,
25 ) or die(usage());
26
27 my $Kid = shift or die(usage());
28 die("$0: error: expecting an integer kid ID but got $Kid\n")
29 unless $Kid =~ /^\d+$/;
30
31 my $lastKid;
32 while (<>) {
33 my ($currentKid) = (/^\d[^a-z]+? kid(\d+)[|]/);
34 $lastKid = $currentKid if defined $currentKid;
35
36 if (!defined($currentKid) && !defined($lastKid)) { # kidless prefix
37 print $_ if $IncludePrefix;
38 next;
39 }
40
41 # targeted kid output or kidless output by, hopefully, the targeted kid
42 if (defined $lastKid && $lastKid == $Kid) {
43 print $_;
44 next;
45 }
46
47 if (defined $currentKid) { # wrong kid output
48 # print lines mentioning our kid if requested, isolating each such line
49 print "\n$_\n" if $IncludeMentions && /\bkid(:\s*)?$Kid\b/o;
50 next;
51 }
52
53 # ignore kidless output produced by, hopefully, wrong kids
54 }
55
56 exit(0);
57
58 sub usage() {
59 return <<"USAGE";
60 usage: $0 [option...] <kid ID> [log file...]
61 options:
62 --prefix include initial kidless lines
63 --mentions include other kid references to the targeted kid
64 USAGE
65 }