| 1 | package XAS::Lib::Modules::Log::File; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use Params::Validate 'HASHREF'; | ||
| 6 | |||
| 7 | use XAS::Class | ||
| 8 | debug => 0, | ||
| 9 | version => $VERSION, | ||
| 10 | base => 'XAS::Base', | ||
| 11 | utils => 'dotid', | ||
| 12 | mixins => 'init_log output destroy', | ||
| 13 | messages => { | ||
| 14 | invperms => "unable to change file permissions on %s", | ||
| 15 | creatfile => "unable to create file %s" | ||
| 16 | } | ||
| 17 | ; | ||
| 18 | |||
| 19 | # ---------------------------------------------------------------------- | ||
| 20 | # Public Methods | ||
| 21 | # ---------------------------------------------------------------------- | ||
| 22 | |||
| 23 | sub output { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | $self = $self->prototype() unless ref $self; | ||
| 27 | |||
| 28 | my ($args) = $self->validate_params(\@_, [ | ||
| 29 | { type => HASHREF } | ||
| 30 | ]); | ||
| 31 | |||
| 32 | $self->filename->append( | ||
| 33 | sprintf("[%s] %-5s - %s\n", | ||
| 34 | $args->{datetime}->strftime('%Y-%m-%d %H:%M:%S'), | ||
| 35 | uc($args->{priority}), | ||
| 36 | $args->{message} | ||
| 37 | )); | ||
| 38 | |||
| 39 | } | ||
| 40 | |||
| 41 | sub destroy { | ||
| 42 | my $self = shift; | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | # ---------------------------------------------------------------------- | ||
| 47 | # Private Methods | ||
| 48 | # ---------------------------------------------------------------------- | ||
| 49 | |||
| 50 | sub init_log { | ||
| 51 | my $self = shift; | ||
| 52 | |||
| 53 | # check to see if the file exists, otherwise create it | ||
| 54 | |||
| 55 | unless ($self->filename->exists) { | ||
| 56 | |||
| 57 | if (my $fh = $self->filename->open('>')) { | ||
| 58 | |||
| 59 | $fh->close; | ||
| 60 | |||
| 61 | } else { | ||
| 62 | |||
| 63 | $self->throw_msg( | ||
| 64 | dotid($self->class) . '.init_log.creatfile', | ||
| 65 | 'creatfile', | ||
| 66 | $self->filename->path | ||
| 67 | ); | ||
| 68 | |||
| 69 | } | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | # Change the file permissions to rw-rw-r, skip this on Windows | ||
| 74 | # as this will create a read only file. | ||
| 75 | |||
| 76 | if ($^O ne "MSWin32") { | ||
| 77 | |||
| 78 | my ($cnt, $mode, $permissions); | ||
| 79 | |||
| 80 | # set file permissions | ||
| 81 | |||
| 82 | $mode = ($self->filename->stat)[2]; | ||
| 83 | $permissions = sprintf("%04o", $mode & 07777); | ||
| 84 | |||
| 85 | if ($permissions ne "0664") { | ||
| 86 | |||
| 87 | $cnt = chmod(0664, $self->filename->path); | ||
| 88 | $self->throw_msg( | ||
| 89 | dotid($self->class) . '.init_log.invperms', | ||
| 90 | 'invperms', | ||
| 91 | $self->filename->path) if ($cnt < 1); | ||
| 92 | |||
| 93 | } | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ | ||
| 102 | |||
| 103 | =head1 NAME | ||
| 104 | |||
| 105 | XAS::Lib::Modules::Log::File - A mixin class for logging | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | This module is a mixin for logging. It logs to a file. | ||
| 110 | |||
| 111 | =head1 METHODS | ||
| 112 | |||
| 113 | =head2 init_log | ||
| 114 | |||
| 115 | This method initializes the module. It uses the file specified with the | ||
| 116 | -filename parameter. It checks to make sure it exists, if it doesn't it | ||
| 117 | creates the file. On a Unix like system, it will change the file permissions | ||
| 118 | to rx-rx-r. | ||
| 119 | |||
| 120 | =head2 output($hashref) | ||
| 121 | |||
| 122 | The method formats the hashref and writes out the results. | ||
| 123 | |||
| 124 | =head2 destroy | ||
| 125 | |||
| 126 | This methods deinitializes the module. | ||
| 127 | |||
| 128 | =head1 SEE ALSO | ||
| 129 | |||
| 130 | =over 4 | ||
| 131 | |||
| 132 | =item L<XAS|XAS> | ||
| 133 | |||
| 134 | =back | ||
| 135 | |||
| 136 | =head1 AUTHOR | ||
| 137 | |||
| 138 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 139 | |||
| 140 | =head1 COPYRIGHT AND LICENSE | ||
| 141 | |||
| 142 | Copyright (C) 2014 Kevin L. Esteb | ||
| 143 | |||
| 144 | This library is free software; you can redistribute it and/or modify | ||
| 145 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 146 | at your option, any later version of Perl 5 you may have available. | ||
| 147 | |||
| 148 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 149 | |||
| 150 | =cut |