Changeset 69

Show
Ignore:
Timestamp:
12/11/09 22:53:07 (4 years ago)
Author:
depesz
Message:

work in progress. do not touch. committed as safety precaution before travel

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/omnipitr/bin/sanity-check.sh

    r67 r69  
    7070done 
    7171 
     72for MODULE in Time::HiRes 
     73do 
     74    if ! perl -I"$LIB_DIR" -e "use $MODULE" &>/dev/null 
     75    then 
     76        WARNINGS_COUNT=$(( 1 + $WARNINGS_COUNT )) 
     77        WARNINGS[$WARNINGS_COUNT]="you don't have $MODULE Perl library - it's optional, but it could help" 
     78    fi 
     79done 
     80 
    7281# Report of status 
    7382 
  • trunk/omnipitr/lib/OmniPITR/Archive.pm

    r68 r69  
    22use strict; 
    33use warnings; 
     4use Carp; 
     5use OmniPITR::Log; 
     6use Getopt::Long; 
     7use Data::Dumper; 
     8 
     9sub new { 
     10    my $class = shift; 
     11    my $self = bless {}, $class; 
     12    $self->read_args(); 
     13    $self->validate_args(); 
     14    return $self; 
     15} 
     16 
     17sub run { 
     18    my $self = shift; 
     19    print "YAY, it worked!\n"; 
     20} 
     21 
     22sub read_args { 
     23    my $self = shift; 
     24 
     25    my %args = (); 
     26    croak('Error while reading command line arguments. Please check documentation in doc/omnipitr-archive.pod') 
     27        unless GetOptions( 
     28            \%args, 
     29            'data-dir|D=s', 
     30            'dst-local|dl=s@', 
     31            'dst-remote|dr=s@', 
     32            'temp-dir|t=s', 
     33            'log|l=s', 
     34            'state-dir|s=s', 
     35            'pid-file=s', 
     36            'verbose|v', 
     37            'help|?', 
     38        ); 
     39    croak('--log was not provided - cannot continue.') unless $args{'log'}; 
     40 
     41    # We do it here so it will actually work for reporing problems in validation 
     42    $self->{'log'} = OmniPITR::Log->new( $args{'log'} ); 
     43     
     44} 
    445 
    5461; 
  • trunk/omnipitr/lib/OmniPITR/Log.pm

    r68 r69  
    22use strict; 
    33use warnings; 
     4use English qw( -no_match_vars ); 
     5use Carp; 
     6use File::Basename; 
     7use File::Path; 
     8use POSIX qw(strftime floor); 
     9use IO::File; 
     10 
     11BEGIN { 
     12    eval { use Time::HiRes qw( time ); }; 
     13} 
     14 
     15sub new { 
     16    my $class                 = shift; 
     17    my ( $filename_template ) = @_; 
     18    my $self                  = bless {}, $class; 
     19 
     20    $self->{ 'template' }       = $filename_template; 
     21    $self->{ 'program' }        = basename( $PROGRAM_NAME ); 
     22    $self->{ 'current_log_ts' } = 0; 
     23    $self->{ 'current_log_fn' } = ''; 
     24 
     25    return $self; 
     26} 
     27 
     28sub _log { 
     29    my $self = shift; 
     30    my ( $level, $format, @args ) = @_; 
     31 
     32    my $log_line_prefix = $self->get_log_line_prefix(); 
     33    my $fh              = $self->get_log_fh(); 
     34 
     35    my $message = sprintf $format, @args; 
     36    $message =~ s/\s*\z//; 
     37 
     38    for my $line ( split /\r?\n/, $message ) { 
     39        printf $fh '%s : %s : %s%s', $log_line_prefix, $level, $line, "\n"; 
     40    } 
     41 
     42    $fh->flush(); 
     43    $fh->sync(); 
     44 
     45    return; 
     46} 
     47 
     48sub log { 
     49    my $self = shift; 
     50    return $self->_log( 'LOG', @_ ); 
     51} 
     52 
     53sub error { 
     54    my $self = shift; 
     55    return $self->_log( 'ERROR', @_ ); 
     56} 
     57 
     58sub fatal { 
     59    my $self = shift; 
     60    $self->_log( 'FATAL', @_ ); 
     61    exit(1); 
     62} 
     63 
     64sub get_log_line_prefix { 
     65    my $self         = shift; 
     66    my $time         = time(); 
     67    my $date_time    = strftime( '%Y-%m-%d %H:%M:%S', localtime $time ); 
     68    my $microseconds = ( $time * 1_000_000 ) % 1_000_000; 
     69    my $time_zone    = strftime( '%z', localtime $time ); 
     70 
     71    my $time_stamp = sprintf "%s.%06u %s", $date_time, $microseconds, $time_zone; 
     72    return sprintf "%s : %u : %s", $time_stamp, $PROCESS_ID, $self->{ 'program' }; 
     73} 
     74 
     75sub get_log_fh { 
     76    my $self = shift; 
     77 
     78    my $time = floor( time() ); 
     79    return $self->{ 'log_fh' } if $self->{ 'current_log_ts' } == $time; 
     80 
     81    $self->{ 'current_log_ts' } = $time; 
     82    my $filename = strftime( $self->{ 'template' }, localtime $time ); 
     83    return $self->{ 'log_fh' } if $self->{ 'current_log_fn' } eq $filename; 
     84 
     85    $self->{ 'current_log_fn' } = $filename; 
     86    close delete $self->{ 'log_fh' } if exists $self->{ 'log_fh' }; 
     87 
     88    my $dirname = dirname $filename; 
     89    mkpath( $dirname ) unless -e $dirname; 
     90    open my $fh, '>>', $filename or croak( "Cannot open $filename for writing: $OS_ERROR" ); 
     91 
     92    $self->{ 'log_fh' } = $fh; 
     93    return $fh; 
     94} 
    495 
    5961;