summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2018-01-13 22:26:56 +0000
committerKarl Berry <karl@freefriends.org>2018-01-13 22:26:56 +0000
commit8321808ed1278e2b4fc5e6d58fcb1acecc4155c0 (patch)
tree3a919f39d8a823b139635052cb15b83bdbdfc8ef /Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm
parent4cc1370e31979d579fabb2561b6de643adde68d9 (diff)
latexindent (13jan18)
git-svn-id: svn://tug.org/texlive/trunk@46303 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm')
-rw-r--r--Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm226
1 files changed, 142 insertions, 84 deletions
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm
index 128034a2881..ff118352762 100644
--- a/Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm
+++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm
@@ -16,39 +16,25 @@ package LatexIndent::LogFile;
# For all communication, please visit: https://github.com/cmhughes/latexindent.pl
use strict;
use warnings;
-use LatexIndent::GetYamlSettings qw/%masterSettings/;
-use LatexIndent::Switches qw/%switches/;
-use LatexIndent::Version qw/$versionNumber $versionDate/;
use FindBin;
use File::Basename; # to get the filename and directory path
use Exporter qw/import/;
-our @EXPORT_OK = qw/logger output_logfile processSwitches/;
+use Log::Log4perl qw(get_logger :levels);
+use Log::Log4perl::Appender::Screen;
+use Log::Dispatch::File;
+use LatexIndent::GetYamlSettings qw/%masterSettings/;
+use LatexIndent::Switches qw/%switches/;
+use LatexIndent::Version qw/$versionNumber $versionDate/;
+our @EXPORT_OK = qw/processSwitches $logger/;
our @logFileNotes;
+our $logger = get_logger("Document");
-# log file methods
-# log file methods
-# log file methods
-# reference: http://stackoverflow.com/questions/6736998/help-calling-a-sub-routine-from-a-perl-module-and-printing-to-logfile
-
-sub logger{
- shift;
- my $line = shift;
- my $infoLevel = shift;
- push(@logFileNotes,{line=>$line,level=>$infoLevel?$infoLevel:'default'});
- return
-}
+# log file methods, using log4perl; references
+# pattern layout: http://search.cpan.org/~mschilli/Log-Log4perl-1.32/lib/Log/Log4perl/Layout/PatternLayout.pm
+# multiple appenders: https://stackoverflow.com/questions/8620347/perl-log4perl-printing-and-logging-at-the-same-time-in-a-line?rq=1
+# getting started: https://www.perl.com/pub/2002/09/11/log4perl.html
sub processSwitches{
- my $self = shift;
-
- # details of the script to log file
- $self->logger("$FindBin::Script version $versionNumber, $versionDate, a script to indent .tex files",'heading');
- $self->logger("$FindBin::Script lives here: $FindBin::RealBin/");
-
- # time the script is used
- my $time = localtime();
- $self->logger("$time");
-
# -v switch is just to show the version number
if($switches{version}) {
print $versionNumber,", ",$versionDate,"\n";
@@ -63,6 +49,8 @@ usage: latexindent.pl [options] [file][.tex|.sty|.cls|.bib|...]
displays the version number and date of release
-h, --help
help (see the documentation for detailed instructions and examples)
+ -sl, --screenlog
+ log file will also be output to the screen
-o, --outputfile=<name-of-output-file>
output to another file; sample usage:
latexindent.pl -o outputfile.tex myfile.tex
@@ -97,84 +85,154 @@ ENDQUOTE
exit(2);
}
+ # if we've made it this far, the processing of switches and logging begins
+ my $self = shift;
+
+ ($switches{trace}||$switches{ttrace}) ? $logger->level($TRACE): $logger->level($INFO);
+
+ # cruft directory
+ ${$self}{cruftDirectory} = $switches{cruftDirectory}||(dirname ${$self}{fileName});
+ die "Could not find directory ${$self}{cruftDirectory}\nExiting, no indentation done." if(!(-d ${$self}{cruftDirectory}));
+
+ my $logfileName = ($switches{cruftDirectory} ? ${$self}{cruftDirectory} : '').($switches{logFileName}||"indent.log");
+
+ # layout of the logfile information, for example
+ #
+ # 2017/09/16 11:59:09 INFO: message
+ # 2017/09/16 12:23:53 INFO: LogFile.pm:156 LatexIndent::LogFile::processSwitches - message
+ # reference: https://stackoverflow.com/questions/46262844/log4perl-grouping-messages/46309392#46309392
+ Log::Log4perl::Layout::PatternLayout::add_global_cspec(
+ 'A', sub {
+ if($_[1] =~ /^\*/){
+ $_[1]=~s/^\*//;
+ if($_[1]=~m/\R/s){
+ my $indentation = ' '.(' ' x length $_[3]);
+ $_[1] =~ s/\R/\n$indentation/gs;
+ }
+ return "$_[3]: ".$_[1];
+ } else {
+ my $indentation = ' '.(' ' x length $_[3]);
+ $_[1] =~ s/\R/\n$indentation/gs if($_[1]=~m/\R/s);
+ return $indentation.$_[1];
+ }
+ });
+
+ my $layout = Log::Log4perl::Layout::PatternLayout->new("%A%n");
+
+ # details for the Log4perl logging
+ my $appender = Log::Log4perl::Appender->new(
+ "Log::Dispatch::File",
+ filename => $logfileName,
+ mode => "write",
+ utf8 => 1,
+ );
+
+ # add the layout
+ $appender->layout($layout);
+
+ # adjust the logger object
+ $logger->add_appender($appender);
+
+ # appender object for output to screen
+ my $appender_screen = q();
+
+ # output to screen, if appropriate
+ if($switches{screenlog}){
+ $appender_screen = Log::Log4perl::Appender->new(
+ "Log::Log4perl::Appender::Screen",
+ stderr => 0,
+ utf8 => 1,
+ );
+
+ $appender_screen->layout($layout);
+ $logger->add_appender($appender_screen);
+ }
+
+ # details of the script to log file
+ $logger->info("*$FindBin::Script version $versionNumber, $versionDate, a script to indent .tex files");
+ $logger->info("$FindBin::Script lives here: $FindBin::RealBin/");
+
+ my $time = localtime();
+ $logger->info($time);
+
+ if (${$self}{fileName} ne "-"){
+ $logger->info("Filename: ${$self}{fileName}");
+ } else {
+ $logger->info("Reading input from STDIN");
+ }
+
# log the switches from the user
- $self->logger('Processing switches','heading');
+ $logger->info("*Processing switches:");
# check on the trace mode switch (should be turned on if ttrace mode active)
$switches{trace} = $switches{ttrace} ? 1 : $switches{trace};
-
+
# output details of switches
- $self->logger('-t|--trace: Trace mode active (you have used either -t or --trace)') if($switches{trace} and !$switches{ttrace});
- $self->logger('-tt|--ttrace: TTrace mode active (you have used either -tt or --ttrace)') if($switches{tracingModeVeryDetailed});
- $self->logger('-s|--silent: Silent mode active (you have used either -s or --silent)') if($switches{silentMode});
- $self->logger('-d|--onlydefault: Only defaultSettings.yaml will be used (you have used either -d or --onlydefault)') if($switches{onlyDefault});
- $self->logger("-w|--overwrite: Overwrite mode active, will make a back up of ${$self}{fileName} first") if($switches{overwrite});
- $self->logger("-l|--localSettings: Read localSettings YAML file") if($switches{readLocalSettings});
- $self->logger("-y|--yaml: YAML settings specified via command line") if($switches{yaml});
- $self->logger("-o|--outputfile: output to file") if($switches{outputToFile});
- $self->logger("-m|--modifylinebreaks: modify line breaks") if($switches{modifyLineBreaks});
- $self->logger("-g|--logfile: logfile name") if($switches{logFileName});
- $self->logger("-c|--cruft: cruft directory") if($switches{cruftDirectory});
+ $logger->info("-sl|--screenlog: log file will also be output to the screen") if($switches{screenlog});
+ $logger->info("-t|--trace: Trace mode active (you have used either -t or --trace)") if($switches{trace} and !$switches{ttrace});
+ $logger->info("-tt|--ttrace: TTrace mode active (you have used either -tt or --ttrace)") if($switches{ttrace});
+ $logger->info("-s|--silent: Silent mode active (you have used either -s or --silent)") if($switches{silentMode});
+ $logger->info("-d|--onlydefault: Only defaultSettings.yaml will be used (you have used either -d or --onlydefault)") if($switches{onlyDefault});
+ $logger->info("-w|--overwrite: Overwrite mode active, will make a back up of ${$self}{fileName} first") if($switches{overwrite});
+ $logger->info("-l|--localSettings: Read localSettings YAML file") if($switches{readLocalSettings});
+ $logger->info("-y|--yaml: YAML settings specified via command line") if($switches{yaml});
+ $logger->info("-o|--outputfile: output to file") if($switches{outputToFile});
+ $logger->info("-m|--modifylinebreaks: modify line breaks") if($switches{modifyLineBreaks});
+ $logger->info("-g|--logfile: logfile name") if($switches{logFileName});
+ $logger->info("-c|--cruft: cruft directory") if($switches{cruftDirectory});
# check if overwrite and outputfile are active similtaneously
if($switches{overwrite} and $switches{outputToFile}){
- $self->logger("Options check",'heading');
- $self->logger("You have called latexindent.pl with both -o and -w");
- $self->logger("-o (output to file) will take priority, and -w (over write) will be ignored");
+ $logger->info("Options check, -w and -o specified\nYou have called latexindent.pl with both -o and -w\noutput to file) will take priority, and -w (over write) will be ignored");
$switches{overwrite}=0;
}
- # cruft directory
- ${$self}{cruftDirectory} = $switches{cruftDirectory}||(dirname ${$self}{fileName});
- die "Could not find directory ${$self}{cruftDirectory}\nExiting, no indentation done." if(!(-d ${$self}{cruftDirectory}));
- my $logfileName = $switches{logFileName}||"indent.log";
- $self->logger("Directory for backup files and $logfileName: ${$self}{cruftDirectory}",'heading');
+ $logger->info("*Directory for backup files and $logfileName: ${$self}{cruftDirectory}");
# output location of modules
if($FindBin::Script eq 'latexindent.pl' or ($FindBin::Script eq 'latexindent.exe' and $switches{trace} )) {
- my @listOfModules = ('FindBin', 'YAML::Tiny', 'File::Copy', 'File::Basename', 'Getopt::Long','File::HomeDir','Unicode::GCString');
- $self->logger("Perl modules are being loaded from the following directories:",'heading');
+ my @listOfModules = ('FindBin', 'YAML::Tiny', 'File::Copy', 'File::Basename', 'Getopt::Long','File::HomeDir','Unicode::GCString','Log::Log4perl');
+ $logger->info("*Perl modules are being loaded from the following directories:");
foreach my $moduleName (@listOfModules) {
(my $file = $moduleName) =~ s|::|/|g;
- require $file . '.pm';
- $self->logger($INC{$file .'.pm'});
+ $logger->info($INC{$file .'.pm'});
}
- $self->logger("Latex Indent perl modules are being loaded from, for example:",'heading');
+ $logger->info("*Latex Indent perl modules are being loaded from, for example:");
(my $file = 'LatexIndent::Document') =~ s|::|/|g;
- require $file . '.pm';
- $self->logger($INC{$file .'.pm'});
+ $logger->info($INC{$file .'.pm'});
}
- return;
-}
-sub output_logfile{
- my $self = shift;
- my $logfile;
- my $logfileName = $switches{logFileName}||"indent.log";
-
- open($logfile,">","${$self}{cruftDirectory}/$logfileName") or die "Can't open $logfileName";
-
- # put the final line in the logfile
- $self->logger("${$masterSettings{logFilePreferences}}{endLogFileWith}",'heading');
-
- # github info line
- $self->logger("Please direct all communication/issues to: ",'heading') if ${$masterSettings{logFilePreferences}}{showGitHubInfoFooter};
- $self->logger("https://github.com/cmhughes/latexindent.pl") if ${$masterSettings{logFilePreferences}}{showGitHubInfoFooter};
-
- # output the logfile
- foreach my $line (@logFileNotes){
- if(${$line}{level} eq 'heading'){
- print $logfile ${$line}{line},"\n";
- } elsif(${$line}{level} eq 'default') {
- # add tabs to the beginning of lines
- # for default logfile lines
- ${$line}{line} =~ s/^/\t/mg;
- print $logfile ${$line}{line},"\n";
- }
- }
-
- close($logfile);
-}
+ # read the YAML settings
+ $self->readSettings;
+
+ # the user may have specified their own settings for the rest of the log file,
+ # for example:
+ #
+ # logFilePreferences:
+ # PatternLayout:
+ # default: "%p: %m{indent}%n"
+ # trace: "%p: %m{indent}%n"
+ # ttrace: "%d %p: %F{1}:%L %M - %m{indent}%n"
+ #
+ # e.g, default mode:
+ # 2017/09/16 11:59:09 INFO: message
+ # or trace mode:
+ # 2017/09/16 11:59:09 INFO: message
+ # or in trace mode:
+ # 2017/09/16 12:23:53 INFO: LogFile.pm:156 LatexIndent::LogFile::processSwitches - message
+ my $pattern = q();
+ if($switches{ttrace}){
+ $pattern = ${${$masterSettings{logFilePreferences}}{PatternLayout}}{ttrace};
+ } elsif($switches{trace}){
+ $pattern = ${${$masterSettings{logFilePreferences}}{PatternLayout}}{trace};
+ } else {
+ $pattern = ${${$masterSettings{logFilePreferences}}{PatternLayout}}{default};
+ }
+ $layout = Log::Log4perl::Layout::PatternLayout->new($pattern);
+ $appender->layout($layout);
+ $appender_screen->layout($layout) if $switches{screenlog};
+ return;
+}
1;