Permalink
Browse files

Merge pull request #1 from kazeburo/master

Added rotationtime option
  • Loading branch information...
tokuhirom committed Dec 15, 2011
2 parents 40ec40a + 935555e commit 08518a7a0bf1655993ec841ef273f98d182ea7f3
Showing with 37 additions and 1 deletion.
  1. +10 −1 lib/File/Stamped.pm
  2. +27 −0 t/02_rotationtime.t
View
@@ -17,6 +17,7 @@ sub new {
autoflush => 1,
close_after_write => 1,
iomode => '>>:utf8',
+ rotationtime => 1,
%args
);
for my $k (keys %args) {
@@ -37,7 +38,11 @@ sub PRINT { shift->print(@_) }
sub _gen_filename {
my $self = shift;
- return POSIX::strftime(*$self->{pattern}, localtime());
+ my $time = time();
+ if ( $time > 1 ) {
+ $time = $time - $time % *$self->{rotationtime};
+ }
+ return POSIX::strftime(*$self->{pattern}, localtime($time));
}
sub print {
@@ -127,6 +132,10 @@ Default value is '>>:utf8'.
This attribute changes $|.
+=item rotationtime: Int
+
+The time between log file generates in seconds. Default value is 1.
+
=back
=item $fh->print($str: Str)
View
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+use Test::More;
+use File::Temp qw/tempdir/;
+use File::Spec;
+use File::Stamped;
+use File::Basename;
+
+my $dir = tempdir(CLEANUP => 1);
+my $pattern = File::Spec->catdir($dir, 'foo.%Y%m%d%H%M%S.log');
+
+my $f = File::Stamped->new(
+ pattern => $pattern,
+ rotationtime => 3,
+);
+
+my $f1 = $f->_gen_filename();
+ok($f1);
+
+sleep 3;
+
+my $f2 = $f->_gen_filename();
+ok($f2);
+ok($f1 ne $f2);
+
+done_testing;
+

0 comments on commit 08518a7

Please sign in to comment.