Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1 from kazeburo/master

Added rotationtime option
  • Loading branch information...
commit 08518a7a0bf1655993ec841ef273f98d182ea7f3 2 parents 40ec40a + 935555e
Tokuhiro Matsuno authored
11  lib/File/Stamped.pm
@@ -17,6 +17,7 @@ sub new {
17 17
         autoflush         => 1,
18 18
         close_after_write => 1,
19 19
         iomode            => '>>:utf8',
  20
+        rotationtime      => 1,
20 21
         %args
21 22
     );
22 23
     for my $k (keys %args) {
@@ -37,7 +38,11 @@ sub PRINT     { shift->print(@_) }
37 38
 
38 39
 sub _gen_filename {
39 40
     my $self = shift;
40  
-    return POSIX::strftime(*$self->{pattern}, localtime());
  41
+    my $time = time();
  42
+    if ( $time > 1 ) {
  43
+        $time = $time - $time % *$self->{rotationtime};
  44
+    }
  45
+    return POSIX::strftime(*$self->{pattern}, localtime($time));
41 46
 }
42 47
 
43 48
 sub print {
@@ -127,6 +132,10 @@ Default value is '>>:utf8'.
127 132
 
128 133
 This attribute changes $|.
129 134
 
  135
+=item rotationtime: Int
  136
+
  137
+The time between log file generates in seconds. Default value is 1.
  138
+
130 139
 =back
131 140
 
132 141
 =item $fh->print($str: Str)
27  t/02_rotationtime.t
... ...
@@ -0,0 +1,27 @@
  1
+use strict;
  2
+use warnings;
  3
+use Test::More;
  4
+use File::Temp qw/tempdir/;
  5
+use File::Spec;
  6
+use File::Stamped;
  7
+use File::Basename;
  8
+
  9
+my $dir = tempdir(CLEANUP => 1);
  10
+my $pattern = File::Spec->catdir($dir, 'foo.%Y%m%d%H%M%S.log');
  11
+
  12
+my $f = File::Stamped->new(
  13
+    pattern => $pattern,
  14
+    rotationtime => 3,
  15
+);
  16
+
  17
+my $f1 = $f->_gen_filename();
  18
+ok($f1);
  19
+
  20
+sleep 3;
  21
+
  22
+my $f2 = $f->_gen_filename();
  23
+ok($f2);
  24
+ok($f1 ne $f2);
  25
+
  26
+done_testing;
  27
+

0 notes on commit 08518a7

Please sign in to comment.
Something went wrong with that request. Please try again.