]> git.donarmstrong.com Git - perltidy.git/blob - lib/Perl/Tidy/Diagnostics.pm
New upstream version 20181120
[perltidy.git] / lib / Perl / Tidy / Diagnostics.pm
1 #####################################################################
2 #
3 # The Perl::Tidy::Diagnostics class writes the DIAGNOSTICS file, which is
4 # useful for program development.
5 #
6 # Only one such file is created regardless of the number of input
7 # files processed.  This allows the results of processing many files
8 # to be summarized in a single file.
9
10 # Output messages go to a file named DIAGNOSTICS, where
11 # they are labeled by file and line.  This allows many files to be
12 # scanned at once for some particular condition of interest.  It was
13 # particularly useful for developing guessing strategies.
14 #
15 # NOTE: This feature is deactivated in final releases but can be
16 # reactivated for debugging by un-commenting the 'I' options flag
17 #
18 #####################################################################
19
20 package Perl::Tidy::Diagnostics;
21 use strict;
22 use warnings;
23 our $VERSION = '20181120';
24
25 sub new {
26
27     my $class = shift;
28     return bless {
29         _write_diagnostics_count => 0,
30         _last_diagnostic_file    => "",
31         _input_file              => "",
32         _fh                      => undef,
33     }, $class;
34 }
35
36 sub set_input_file {
37     my ( $self, $input_file ) = @_;
38     $self->{_input_file} = $input_file;
39     return;
40 }
41
42 sub write_diagnostics {
43     my ( $self, $msg ) = @_;
44
45     unless ( $self->{_write_diagnostics_count} ) {
46         open( $self->{_fh}, ">", "DIAGNOSTICS" )
47           or Perl::Tidy::Die("couldn't open DIAGNOSTICS: $!\n");
48     }
49
50     my $fh                   = $self->{_fh};
51     my $last_diagnostic_file = $self->{_last_diagnostic_file};
52     my $input_file           = $self->{_input_file};
53     if ( $last_diagnostic_file ne $input_file ) {
54         $fh->print("\nFILE:$input_file\n");
55     }
56     $self->{_last_diagnostic_file} = $input_file;
57     my $input_line_number = Perl::Tidy::Tokenizer::get_input_line_number();
58     $fh->print("$input_line_number:\t$msg");
59     $self->{_write_diagnostics_count}++;
60     return;
61 }
62
63 1;
64