summaryrefslogtreecommitdiff
path: root/Build/source/texk/texlive/linked_scripts/texlogfilter/texlogfilter
blob: f960c9a3773c0cbfd26690ef5c55b359700063d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env perl
################################################################################
# texlogfilter - filter latex engines output or log file
# Julien Labbé, 2021
#
# This work may be distributed and/or modified under the conditions of the LaTeX
# Project Public License, either version 1.3 of this license or (at your option)
# any later version. The latest version of this license is in
#   http://www.latex-project.org/lppl.txt and version 1.3
# or later is part of all distributions of LaTeX version 2005/12/01 or later.
################################################################################

use strict;
use warnings;
use Getopt::Long;
use Term::ANSIColor;

my $name = "texlogfilter";
my $version = "1.1";

# options
my @userfilters;
my $showhelp = 0;
my $showversion = 0;
my $showrefmsg = 1;
my $showboxmsg = 1;
my $showinfomsg = 0;
my $showskips = 0;
my $skipmarker = "...";
my $extpattern = "tex|sty|cls|bib";
my $fulllog = 0;
my $printfilename = -1; # -1: value not modified by options
my $errcolor = "red";
my $wrncolor = "yellow";
my $infcolor = "blue";
my $skpcolor = "bright_black";
my $usrcolor = "reset";

GetOptions ("box!"              => \$showboxmsg,    # show box warnings
            "ref!"              => \$showrefmsg,    # show reference and citation warnings
            "info!"             => \$showinfomsg,   # show latex info messages
            "skips!"            => \$showskips,     # show that lines have been skipped
            "skip-marker=s"     => \$skipmarker,    # how to indicate skipped lines
            "files-ext=s"       => \$extpattern,    # file extension regex pattern
            "add-filter=s"      => \@userfilters,   # user filters list
            "errors-color=s"    => \$errcolor,      # color for errors
            "warnings-color=s"  => \$wrncolor,      # color for warnings
            "infos-color=s"     => \$infcolor,      # color for informations
            "skips-color=s"     => \$skpcolor,      # color for skipped lines marker
            "user-color=s"      => \$usrcolor,      # color for content filtered by pattern added with --add-filter
            "filename!"         => \$printfilename, # print current file name
            "full-log!"         => \$fulllog,       # show full log (don't filter - only colorize latex engine output)
            "help"              => \$showhelp,
            "version"           => \$showversion);

if ($printfilename == -1 && $fulllog) { $printfilename = 0; } # don't show filename for full log, if not explicitly required

# counters
my $nerr = 0;
my $nwrn = 0;

# iteration state
my $nextlines = 0;
my $currentpackage = "";
my $currentfile = "";
my $lastfile = "";
my $samecontext = -1;   # expected values: 0 (continuing previous line) / 1 (lines skipped) / -1 (initial value)

# functions

sub print_help {
    # print usage
    print <<EOT;
Usage: $name [options] file.log

Filter latex engines output or log file. log is latex, pdflatex, lualatex or
xelatex output or log file.

Without input file, standard input is used. Use on latex engine output with:
    latex file.tex < /dev/null | $name

Options:
 --help                 : print this help and exit
 --version              : print version and exit

 --box                  : show box warnings
 --no-box               : mask box warnings

 --ref                  : show reference/citation warnings
 --no-ref               : mask reference/citation warnings

 --info                 : show latex info messages
 --no-info              : mask latex info messages

 --filename             : print current file name
 --no-filename          : do not print current file name
 --files-ext=string     : regex pattern used to match files extension (default: $extpattern)

 --skips                : indicate skipped lines
 --no-skips             : mask skipped lines
 --skip-marker=string   : marker used to indicate skipped lines (defaut: $skipmarker)

 --add-filter=string    : add user filter pattern (as perl regular expression)

 --full-log             : show full log (don't filter - only colorize latex engine output)

 --errors-color=string
 --warnings-color=string
 --infos-color=string
 --skips-color=string
 --user-color=string    : colors used for errors, warnings, informations and skipped lines
EOT
}

sub print_version {
    # print version
    print $version,"\n";
}

sub print_filename {
    # print current file name if new
    if ($printfilename && $currentfile ne $lastfile) {
        print color("bold"), color($infcolor), "\nFile: ", $currentfile, "\n", color("reset");
        $lastfile = $currentfile;
    }
}

sub print_skips {
    # show if some lines have benn skipped
    if ($showskips && !$samecontext) { print color("reset"), color($skpcolor), $skipmarker, "\n", color("reset") ; }
    $samecontext = 1;
}

sub handle_error {
    # handle error
    #  nl (optional)         : nextline value
    #  nofilename (optional) : if set, don't print filename
    my ( $nl, $nofilename ) = @_;
    $nerr++;
    if (defined $nl) { $nextlines = $nl ; }
    print_skips();
    if (! defined $nofilename) {
        print_filename();
    }
    print color($errcolor), $_, ;
}

sub handle_warning {
    # handle warning
    #  nl (optional)         : nextline value
    #  nofilename (optional) : if set, don't print filename
    my ( $nl, $nofilename ) = @_;
    $nwrn++;
    if (defined $nl) { $nextlines = $nl ; }
    print_skips();
    if (! defined $nofilename) {
        print_filename();
    }
    print color($wrncolor), $_, ;
}

sub handle_info {
    # handle info
    #  nl (optional)         : nextline value
    #  colorattr             : additional color attribute
    my ( $nl, $colorattr ) = @_;
    if (defined $nl) { $nextlines = $nl ; }
    print_skips();
    if (defined $colorattr) {print color($infcolor, $colorattr), $_, ; }
    else { print color($infcolor), $_, ; }
}

# print help
if ( $showhelp ) {
  print_help();
  exit(0);
}

# print version
if ( $showversion ) {
  print_version();
  exit(0);
}

# main loop
while (<>) {

    # assume currentfile is given in the last open parenthesis
    # (works badly for packages)
    if (/\(([^)]+$extpattern)$/) {$currentfile = $1;}

    # try to show usefull lines following some warnings or errors (starting with
    # whitespaces or package name)
    if ($currentpackage ne ""){
        if (/(s+|\($currentpackage\))/){print $_;}
        else {$currentpackage = ""; }
    }

    # print above lines, if required
    if ($nextlines > 0) { $nextlines--; print $_; }

    # find errors
    elsif (/^(!\s+|.*?:\d+:\s+)?(Class|Package) (\w+ )?Error/i)         { if ($3){$currentpackage=$3}  handle_error(0, "nofilename"); }
    elsif (/^(!\s+)?LaTeX (?:Error)/i)                                  { handle_error(); }
    elsif (/^(!|.*?:\d+:) Undefined control sequence\./i)               { chomp; handle_error(4); }
    elsif (/^(!|.*?:\d+:) Use of (.*) doesn't match its definition\./i) { handle_error(3); }
    elsif (/^(!|.*?:\d+:) Missing/i)                                    { handle_error(3); }
    elsif (/^(!|.*?:\d+:)/i)                                            { handle_error(); }
    elsif (/^No pages of output/i)                                      { handle_error(); }

    # find warning
    elsif (/^(!\s+)?(Class|Package) (\w+ )?Warning/i)                   { if ($3){$currentpackage=$3}  handle_warning(0, "nofilename"); }
    elsif (/^(!\s+)?(LaTeX|\* LaTeX) Warning: (Citation|Reference)/i)   { if ($showrefmsg) {handle_warning();} }
    elsif (/^(!\s+)?(LaTeX|\* LaTeX) Warning/i)                         { handle_warning(); }
    elsif (/Runaway argument\?/i)                                       { handle_warning(1); }
    elsif (/(overfull|underfull|badbox)/i)                              { if ($showboxmsg){handle_warning(1);} }

    # find infos
    elsif (/^(LaTeX) (\w+ )?Info/i)                                     { if ($showinfomsg){handle_info();} }
    elsif (/^(LaTeX)/i)                                                 { handle_info(); }
    elsif (/^Document Class/i)                                          { handle_info(); }
    elsif (/^\\input/)                                                  { handle_info(); }
    elsif (/^Output written/i)                                          { print "\n"; handle_info(0, "bold"); }

    else {
        # user added filters
        my $line=$_;
        my $usrmatch=0;
        for my $filter (@userfilters){
            if ($line =~ m/$filter/ && ! $usrmatch) {
                $usrmatch = 1;
                print_skips();
                print color($usrcolor), $line;
            }
        }
        # if not, skip line
        if (! $usrmatch){
            $samecontext=0;
            if ($fulllog ){ print color("reset"), $line, ; }
        }
    }
}

# end message
print "\n";
if ($nerr > 0) {print color("bold"), color($errcolor), "Found ", $nerr, " error(s)\n", color("reset") ;}
if ($nwrn > 0) {print color("bold"), color($wrncolor), "Found ", $nwrn, " warning(s)\n", color("reset") ;}

__END__

=pod

=encoding utf8

=head1 texlogfilter

=head2 NAME

texlogfilter - filter latex engines output or log file

=head2 SYNOPSIS

    texlogfilter [options] file.log

=head2 DESCRIPTION

Filter latex engines output or log file. For latex, pdflatex, lualatex or
xelatex output or log file.

Without input file, standard input is used.  Use on latex engine output with:

    latex file.tex < /dev/null | texlogfilter

=head2 OPTIONS

    --help                 : print this help and exit
    --version              : print version and exit

    --box                  : show box warnings
    --no-box               : mask box warnings

    --ref                  : show reference/citation warnings
    --no-ref               : mask reference/citation warnings

    --info                 : show latex info messages
    --no-info              : mask latex info messages

    --filename             : print current file name
    --no-filename          : do not print current file name
    --files-ext=string     : regex pattern used to match files extension (default: tex|sty|cls|bib)

    --skips                : indicate skipped lines
    --no-skips             : mask skipped lines
    --skip-marker=string   : marker used to indicate skipped lines (defaut: ...)

    --add-filter=string    : add user filter pattern (as perl regular expression)

    --full-log             : show full log (don't filter - only colorize latex engine output)

    --errors-color=string
    --warnings-color=string
    --infos-color=string
    --skips-color=string
    --user-color=string    : colors used for errors, warnings, informations and skipped lines

=head2 ALTERNATIVES

=over

=item * B<texlog-extract> : L<https://www.ctan.org/pkg/texlog-extract>

=item * B<texloganalyser> : L<https://www.ctan.org/pkg/texloganalyser>

=item * B<texlogsieve> : L<https://ctan.org/pkg/texlogsieve>

=back

=head2 AUTHOR

Written by Julien Labbé.

Inspired from a script given by Martin Scharrer on Stack Exchange network (see
L<https://tex.stackexchange.com/a/10564>).

=head2 LICENCE

This work may be distributed and/or modified under the conditions of the LaTeX
Project Public License, either version 1.3 of this license or (at your option)
any later version. The latest version of this license is in
L<http://www.latex-project.org/lppl.txt> and version 1.3 or later is part of all
distributions of LaTeX version 2005/12/01 or later.

=head2 VERSION

1.1

=head2 HISTORY

=over

=item * 2022, march, version 1.1 : add --info and --no-info options.

=item * 2021, november, version 1.0 : initial version.

=back

=cut

=head2 DOCUMENTATION

The documentation is integrated, writtent in Plain Old Documentation (POD)
format. Exported versions are available, in the following files:

=over

=item * F<README>: created with perldoc.

=item * F<texlogfilter.html>: created with pod2html.

=back

=cut