File Coverage

lib/Catalyst/Plugin/ErrorCatcher/File.pm
Criterion Covered Total %
statement 34 41 82.9
branch 4 8 50.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 47 58 81.0


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::ErrorCatcher::File;
2             $Catalyst::Plugin::ErrorCatcher::File::VERSION = '0.0.8.18';
3             {
4             $Catalyst::Plugin::ErrorCatcher::File::DIST = 'Catalyst-Plugin-ErrorCatcher';
5             }
6             # ABSTRACT: a file emitter for Catalyst::Plugin::ErrorCatcher
7 2     2   939 use strict;
  2         3  
  2         139  
8 2     2   13 use warnings;
  2         2  
  2         71  
9              
10 2     2   2063 use DateTime;
  2         289275  
  2         111  
11 2     2   25 use Path::Class;
  2         4  
  2         209  
12 2     2   1685 use File::Slurp qw(write_file);
  2         11178  
  2         857  
13              
14             sub emit {
15 2     2 1 6 my ($class, $c, $output) = @_;
16 2         3 my ($config);
17              
18             # check and tidy the config
19 2         10 $config = _check_config($c, $config);
20              
21             # write the error out as a file on disk
22 2         9 _write_file($output, $config);
23              
24 2         10 return;
25             }
26              
27             sub _check_config {
28 3     3   80118 my $c = shift;
29              
30 3         18 my $config = $c->_errorcatcher_c_cfg->{"Plugin::ErrorCatcher::File"};
31              
32             # no config, no email
33             # we die so we count as a failure
34 3 50       83 if (not defined $config) {
35 0         0 die "Catalyst::Plugin::ErrorCatcher::File has no configuration\n";
36             }
37              
38             # no To:, no email
39 3 50       14 if (not defined $config->{dir}) {
40 0         0 die qq{Catalyst::Plugin::ErrorCatcher::File has no "dir" setting\n};
41             }
42              
43 3 50       13 if (not defined $config->{prefix}) {
44 0         0 $config->{prefix} = q{ecfile};
45             }
46              
47 3         7 return $config;
48             }
49              
50             sub _write_file {
51 2     2   6 my $msg = shift;
52 2         3 my $config = shift;
53              
54 2         20 my $dt = DateTime->now;
55 2         873 my $timestamp = $dt->strftime('%Y%m%d%H%M%S');
56              
57 2         258 my $filename = file(
58             $config->{dir},
59             $config->{prefix}
60             . q{_}
61             . $timestamp
62             );
63              
64             # make sure we don't overwrite existing files
65 2 50       312 if (-e $filename) {
66 0         0 my $count = 1;
67 0         0 while (-e "${filename}.${count}") {
68 0         0 $count++;
69             }
70 0         0 $filename = "${filename}.${count}";
71             }
72              
73 2         236 write_file($filename.q{}, $msg);
74 2         704 return;
75             }
76              
77             1;
78              
79             =pod
80              
81             =encoding UTF-8
82              
83             =head1 NAME
84              
85             Catalyst::Plugin::ErrorCatcher::File - a file emitter for Catalyst::Plugin::ErrorCatcher
86              
87             =head1 VERSION
88              
89             version 0.0.8.18
90              
91             =head1 SYNOPSIS
92              
93             In your application:
94              
95             use Catalyst qw/-Debug StackTrace ErrorCatcher/;
96              
97             In your application configuration:
98              
99             <Plugin::ErrorCatcher>
100             # ...
101              
102             emit_module Catalyst::Plugin::ErrorCatcher::File
103             </Plugin::ErrorCatcher>
104              
105             <Plugin::ErrorCatcher::File>
106             dir /tmp
107              
108             # defaults to ecfile
109             prefix foobar
110              
111             </Plugin::ErrorCatcher::File>
112              
113             =head2 emit($class, $c, $output)
114              
115             Emit the error report to a file.
116              
117             =head1 AUTHOR
118              
119             Chisel <chisel@chizography.net>
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             This software is copyright (c) 2015 by Chisel Wright.
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127              
128             =cut
129              
130             __END__
131              
132              
133             # vim: ts=8 sts=4 et sw=4 sr sta