]> git.ipfire.org Git - thirdparty/squid.git/blame - scripts/trace-kid.pl
Source Format Enforcement (#532)
[thirdparty/squid.git] / scripts / trace-kid.pl
CommitLineData
00afd565
AR
1#!/usr/bin/perl -w
2#
77b1029d 3## Copyright (C) 1996-2020 The Squid Software Foundation and contributors
00afd565
AR
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
16use strict;
17use warnings;
18use Getopt::Long;
19
20my $IncludePrefix = 0; # include initial kidless lines
21my $IncludeMentions = 0; # include other kid references to the targeted kid
22GetOptions(
23 "prefix!" => \$IncludePrefix,
24 "mentions!" => \$IncludeMentions,
25) or die(usage());
26
27my $Kid = shift or die(usage());
28die("$0: error: expecting an integer kid ID but got $Kid\n")
29 unless $Kid =~ /^\d+$/;
30
31my $lastKid;
32while (<>) {
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
56exit(0);
57
58sub usage() {
59 return <<"USAGE";
60usage: $0 [option...] <kid ID> [log file...]
61options:
62 --prefix include initial kidless lines
63 --mentions include other kid references to the targeted kid
64USAGE
65}