File Coverage

lib/Bio/VertRes/Config/CommandLine/LogParameters.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Bio::VertRes::Config::CommandLine::LogParameters;
2              
3             # ABSTRACT: A class to represent multiple top level files. It splits out mixed config files into the correct top level files
4              
5              
6 1     1   144174 use Moose;
  0            
  0            
7             use Bio::VertRes::Config::Exceptions;
8             use File::Basename;
9             use File::Path qw(make_path);
10              
11             has 'args' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
12             has 'script_name' => ( is => 'ro', isa => 'Str', required => 1 );
13             has 'log_file' => ( is => 'rw', isa => 'Str', default => '/tmp/command_line.log' );
14             has '_output_string' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build__output_string' );
15             has '_user_name' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build__user_name' );
16              
17             sub BUILD {
18             my ($self) = @_;
19              
20             # Build the variable just after object construction because the array ref gets modified by GetOpts
21             $self->_output_string;
22             }
23              
24              
25             sub _build__user_name
26             {
27             my ($self) = @_;
28             getpwuid( $< );
29             }
30              
31             sub _build__output_string {
32             my ($self) = @_;
33             my $output_str = time()." ";
34            
35             if ( defined( $self->_user_name))
36             {
37             $output_str .= $self->_user_name . " ";
38             }
39            
40             if ( defined( $self->script_name ) ) {
41             $output_str .= $self->script_name . " ";
42             }
43              
44             if ( defined( $self->args ) && @{ $self->args } > 0 ) {
45             $output_str .= join( ' ', @{ $self->args } );
46             }
47             $output_str .= "\n";
48              
49             return $output_str;
50             }
51              
52             sub create {
53             my ($self) = @_;
54              
55             my $mode = 0777;
56             if ( !( -e $self->log_file ) ) {
57             my ( $config_filename, $directories, $suffix ) = fileparse( $self->log_file );
58             make_path($directories, {mode => $mode});
59             }
60              
61             open( my $fh, '+>>', $self->log_file )
62             or Bio::VertRes::Config::Exceptions::FileCantBeModified->throw(
63             error => 'Couldnt open file for writing ' . $self->log_file );
64             print {$fh} $self->_output_string;
65             close($fh);
66             chmod $mode, $self->log_file;
67              
68             return 1;
69             }
70              
71             __PACKAGE__->meta->make_immutable;
72             no Moose;
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =head1 NAME
80              
81             Bio::VertRes::Config::CommandLine::LogParameters - A class to represent multiple top level files. It splits out mixed config files into the correct top level files
82              
83             =head1 VERSION
84              
85             version 1.133090
86              
87             =head1 SYNOPSIS
88              
89             A class to represent multiple top level files. It splits out mixed config files into the correct top level files
90             use Bio::VertRes::Config::CommandLine::LogParameters;
91              
92             Bio::VertRes::Config::CommandLine::LogParameters->new( args => \@ARGV, log_file => '/path/to/log/file')->create;
93              
94             =head1 AUTHOR
95              
96             Andrew J. Page <ap13@sanger.ac.uk>
97              
98             =head1 COPYRIGHT AND LICENSE
99              
100             This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
101              
102             This is free software, licensed under:
103              
104             The GNU General Public License, Version 3, June 2007
105              
106             =cut