diff --git a/src/lib/Utils/TextFiles/LineCounter.pm b/src/lib/Utils/TextFiles/LineCounter.pm index 0720ee6..7e1474f 100644 --- a/src/lib/Utils/TextFiles/LineCounter.pm +++ b/src/lib/Utils/TextFiles/LineCounter.pm @@ -77,3 +77,90 @@ sub non_empty_linecount { } 1; + +__END__ + +=pod + +=head1 NAME + +C - A file parsing module for counting lines in files. + +=head1 SYNOPSIS + + use Utils::TextFiles::LineCounter qw(non_empty_linecount); + + print non_empty_linecount('dev.log'), "\n"; + +=head1 DESCRIPTION + +The C module provides utility functions for counting lines in files. + +=head1 METHODS + +=head2 linecount + +Counts the number of lines in a text file. Also counts blank lines. + +=head3 PARAMETERS + +=over 4 + +=item C<$filename> + +The name of the file whose lines to count. + +=back + +=head3 RETURN VALUE + +Returns an integer representing the line count for that file, or +C<0> if the file doesn't exist. + +=head3 ERROR HANDLING + +If the file specified by C<$filename> cannot be opened or closed correctly, +the function calls C. This behaviour is subject to change. + +=head3 EXAMPLE USAGE + + use Utils::TextFiles::LineCounter qw(linecount); + + print linecount('dev.log'), "\n"; + +=head2 non_empty_linecount + +Performs the same line-counting as L, but +doesn't count empty lines. + +=head3 PARAMETERS + +=over 4 + +=item C<$filename> + +The name of the file whose lines are to be counted. + +=back + +=head3 RETURN VALUE + +Returns an integer representing the count of non-empty lines for +C<$filename>. + +=head3 ERROR HANDLING + +If the file specified by C<$filename> cannot be opened or closed correctly, +the function calls C. This behaviour is subject to change. + +=head3 EXAMPLE USAGE + + use Utils::TextFiles::LineCounter qw(non_empty_linecount); + + print non_empty_linecount('dev.log'), "\n"; + +=head1 AUTHOR + +Ethan Kletschke + +=cut diff --git a/src/lib/Utils/TextFiles/LogFileParsing.pm b/src/lib/Utils/TextFiles/LogFileParsing.pm index 99878c7..f3742f4 100644 --- a/src/lib/Utils/TextFiles/LogFileParsing.pm +++ b/src/lib/Utils/TextFiles/LogFileParsing.pm @@ -31,8 +31,8 @@ sub count_logs_by_type { # Remove the newline character from the end of $line chomp($line); - # If the start of the line doesn't match the regex (see module manual) - unless ($line =~ /^\[?([a-zA-Z]+)\]?/) { + # If the start of the line doesn't match the regex (see module POD) + unless ($line =~ /^\[?([a-z]+)\]?:?/i) { # Abort the program (temporary code) die "No matching log line"; } @@ -99,13 +99,115 @@ C - A file parsing module for log files. =head1 DESCRIPTION -=for comment -DO THIS LATER +The C module provides utility functions for parsing, handling, +and using log files. =head1 METHODS -=for comment -Document methods later +=head2 count_logs_by_type + +Counts the number of logs of a certain type in a +provided log file. + +Each line is tested against the RegEx C, which +allows for the following log types as line prefixes: + +=over 4 + +=item * C<[LOG]> + +=item * C + +=item * C<[LOG]:> + +=item * C<[log]> + +=back + +=head3 PARAMETERS + +=over 4 + +=item C<$log_type> + +The type of log to count. + +=item C<$filename> + +The relative path to the log file, including the file extension. + +=back + +=head3 RETURN VALUE + +Returns an integer, i.e., the number of lines for the provided log type, or +C<0> if the log type was not found in the log file. + +=head3 ERROR HANDLING + +If the file specified with C<$filename> doesn't open or close correctly, +the method currently calls C with a stack trace. This behaviour is subject +to change. + +=head3 LIMITATIONS + +The log type passed to the function has to include only the text +of the log type. For example, if the file includes +C<[ERR]>, the input must be passed without the square +brackets, i.e., C. + +=head3 EXAMPLE USAGE + + use Utils::TextFiles::LogFileParsing qw( + count_logs_by_type + ); + + print count_logs_by_type("ERR", "dev.log"), "\n"; + +=head2 filter_logs + +Filters a log file by a specific log type. + +Each log line is tested against the RegEx C, +and only the lines that match will be returned in an array. + +=head3 PARAMETERS + +=over 4 + +=item C<$log_type> + +The type of log to C in the log file. + +=item C<$filename> + +The log file to C. + +=back + +=head3 RETURN VALUE + +Returns an array of the matched lines from C<$filename>. + +=head3 ERROR HANDLING + +If the file specified with C<$filename> doesn't open or close correctly, +the method currently calls C with a stack trace. This behaviour is subject +to change. + +=head3 LIMITATIONS + +Unlike with L, you can pass C<[ERR]> for the log type, +and it will work the same as passing C. This is due to the use of +L. + +=head3 EXAMPLE USAGE + + use Utils::TextFiles::LogFileParsing qw( + filter_logs + ); + + print filter_logs('ERR', 'dev.log'), "\n"; =head1 AUTHOR diff --git a/src/t/TextFiles.t b/src/t/TextFiles.t index bcb19e8..a310d99 100644 --- a/src/t/TextFiles.t +++ b/src/t/TextFiles.t @@ -28,6 +28,10 @@ subtest "LineCounter" => sub { ; close $fh; + # Write an empty test file + open $fh, '>', "empty.txt" or die "Could not create empty.txt: $!\n"; + close $fh; + # Test that the linecount subroutine correctly returns a count of 4 is( linecount("LineCounter.tmp"), @@ -48,6 +52,13 @@ subtest "LineCounter" => sub { 0, "Doesn't return 0 for an existing file" ); + + # Test that the linecount returns 0 for an empty file + is( + linecount("empty.txt"), + 0, + "Returns 0 for an empty, existing file" + ); }; subtest "non_empty_linecount()" => sub { @@ -81,6 +92,7 @@ subtest "LineCounter" => sub { # Delete the testing file unlink "LineCounter.tmp"; + unlink "empty.txt"; }; }; @@ -143,6 +155,12 @@ subtest "LogFileParsing" => sub { "Filters 2 Error logs" ); + is( + scalar filter_logs('[ERR]', 'testing.tmp'), + 2, + "Filters 2 Error logs with square brackets in the supplied log type" + ); + unlink("testing.tmp"); }; };