Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/lib/Utils/TextFiles/LineCounter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,90 @@ sub non_empty_linecount {
}

1;

__END__

=pod

=head1 NAME

C<LineCounter> - 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<LineCounter> 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<die>. 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</linecount>, 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<die>. 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
114 changes: 108 additions & 6 deletions src/lib/Utils/TextFiles/LogFileParsing.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down Expand Up @@ -99,13 +99,115 @@ C<LogFileParsing> - A file parsing module for log files.

=head1 DESCRIPTION

=for comment
DO THIS LATER
The C<LogFileParsing> 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</^\[?([a-z]+)\]?:?/i>, which
allows for the following log types as line prefixes:

=over 4

=item * C<[LOG]>

=item * C<Log:>

=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<die> 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<ERR>.

=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</^\[?$log_type\]?:?/i>,
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<grep> in the log file.

=item C<$filename>

The log file to C<grep>.

=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<die> with a stack trace. This behaviour is subject
to change.

=head3 LIMITATIONS

Unlike with L</count_logs_by_type>, you can pass C<[ERR]> for the log type,
and it will work the same as passing C<ERR>. This is due to the use of
L<perlfunc/"grep">.

=head3 EXAMPLE USAGE

use Utils::TextFiles::LogFileParsing qw(
filter_logs
);

print filter_logs('ERR', 'dev.log'), "\n";

=head1 AUTHOR

Expand Down
18 changes: 18 additions & 0 deletions src/t/TextFiles.t
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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 {
Expand Down Expand Up @@ -81,6 +92,7 @@ subtest "LineCounter" => sub {

# Delete the testing file
unlink "LineCounter.tmp";
unlink "empty.txt";
};
};

Expand Down Expand Up @@ -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");
};
};
Expand Down
Loading