[BACK]Return to update_trango.pl CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / scripts

Diff for /trango/Net-Telnet-Trango/scripts/update_trango.pl between version 1.10 and 1.31

version 1.10, 2005/12/21 01:17:06 version 1.31, 2007/02/06 23:15:53
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.9 2005/11/18 19:17:12 andrew Exp $  # $RedRiver: update_trango.pl,v 1.30 2007/02/06 23:00:31 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango hosts with a new firmware
 #  #
 # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>  # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>
 ########################################################################  ########################################################################
 use strict;  use strict;
 use warnings;  use warnings;
   
 use Net::TFTP;  use YAML qw/ LoadFile /;
   use Net::TFTP;
 my $config_file = shift || 'update_trango.conf';  use Net::Telnet::Trango;
 my $max_tries = 3;  
   my $config_file = shift || 'update_trango.yaml';
   my $max_tries = 3;
   
 my $l = Mylogger->new( { log_prefix => 'UT' } );  my $l = Mylogger->new( { log_prefix => 'UT' } );
   
   $l->sp("Reading config file '$config_file'");
 $l->sp("Reading config file '$config_file'");  my $conf = LoadFile($config_file);
 my $conf = read_conf($config_file);  
   my $hosts;
 $l->sp("  Hardware Type: $conf->{'type'}");  if (@ARGV) {
 $l->sp("  File Name:     $conf->{'file_name'}");      @{ $hosts } = map { { name => $_, group => 'Trango-Client' } } @ARGV
 $l->sp("  File Size:     $conf->{'file_size'}");  } else {
 $l->sp("  File Checksum: $conf->{'file_cksum'}");      $hosts = parse_hosts($conf->{hosts});
 $l->sp("  FW Version:    $conf->{'ver'}");  }
 $l->sp("  FW Checksum:   $conf->{'cksum'}");  
 $l->sp("");  #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
   
   my $global_tries = $max_tries * 2;
   while ($global_tries > 0) {
       $global_tries--;
       my $processed = 0;
   
 foreach my $fox (@{ $conf->{'ips'} }) {      foreach my $host (@{ $hosts }) {
   $l->sp("Updating: $fox");  
           if (! exists $host->{retry}) {
   ## Connect and login.              $host->{tries} = 0;
   my $t = new Trango::Telnet ({              $host->{retry} = 1;
     Host    => $fox,          }
     Timeout => 5,  
   });          if ($host->{tries} >= $max_tries) {
               $host->{retry} = 0;
   $l->p("Connecting to $fox");          }
   my ($type, $version) = $t->connect();  
           if ($host->{retry} <= 0) {
   unless (defined $type && defined $version) {              next;
     $l->sp("Error connecting!");          }
     next;  
   }          $host->{tries}++;
           $processed++;
   if ($type ne $conf->{'type'}) {  
     $l->sp("Wrong type of unit ('$type' should be '$conf->{'type'}')");          $l->sp("");
     $t->close;          $l->sp("Checking: $host->{name} (try $host->{tries})");
     next;          my $needs_reboot = 0;
   }  
           ## Connect and login.
   if ($version eq $conf->{'ver'}) {          my $t = new Net::Telnet::Trango (
     $l->sp("Already up to date with firmware version '$version'");              Timeout => 5,
     $t->close;              Errmode => 'return',
     next;          ) or die "Couldn't make new connection: $!";
   }          $l->p("Connecting to $host->{name}");
           unless ( $t->open($host->{name}) ) {
   $l->p("Logging in");              $l->sp("Error connecting: $!");
   $t->login($conf->{'password'}) || die "Couldn't login: $!";              next;
           }
   $l->p("Sending commands");  
   ## Send commands          my $password = $host->{Telnet_Password} || $conf->{general}->{password};
   if ( upload($t, $conf->{'file_name'}) ) {  
     $l->p("Rebooting");          $l->p("Logging in");
     $t->reboot;          $t->login($password);
   } else {          unless ($t->logged_in) {
     $l->p("Exiting");              $l->p('Failed!');
     $t->exit;              $t->close;
   }              next;
   $t->close;          }
   $l->sp("");  
 }          $l->sp("Getting sudb");
           my $sudb = $t->sudb_view;
 sub upload          if ($sudb) {
 {              foreach my $su (@{ $sudb }) {
   my $t    = shift;                  $l->p("Getting su info $su->{suid}");
   my $file = shift;                  my $su_info = $t->su_info( $su->{suid} );
                   if ($su_info->{ip}) {
   $l->p("Getting current version");                      if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
   my $ver = $t->ver;                          $l->p("Already have $su_info->{ip}");
                           next;
   if (                      }
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&                      $l->sp("Adding host $su_info->{ip}");
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}                      my $new_host = {
   ) {                          password => $host->{password},
     $l->sp("Already updated!");                          name     => $su_info->{ip},
     return 1;                          remarks  => $su_info->{remarks},
   }                      };
                       push @{ $hosts }, $new_host;
   my $try = 0;                  } else {
   while (1) {                      $l->sp("Couldn't get su info for $su->{suid}");
     if ($try >= $max_tries) {                      $l->sp("ERR: " . $t->last_error);
       $l->sp("Couldn't update in $max_tries tries!");                  }
       return undef;              }
     }          }
     $try++;  
           foreach my $firmware_type ('Firmware', 'FPGA') {
     #sysinfo($self->{'_host'});  
                   if (! exists $conf->{$firmware_type}) {
     $l->p("Enabling TFTPd");                  $l->s("No configs for '$firmware_type'");
     $t->enable_tftpd || die "Couldn't enable tftpd";                  next;
               }
     $l->p("Uploading file ($file)");  
     # use tftp to push the file up              my $host_type = $t->host_type;
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');              if ($firmware_type eq 'FPGA') {
                   $host_type =~ s/\s.*$//;
     $tftp->put($file, $file)              }
       or die "Error uploading: " . $tftp->error;  
               if (! exists $conf->{$firmware_type}->{$host_type}) {
     # waitfor some sort of output                  $l->sp("No '$firmware_type' config for type $host_type");
     # make sure it says 'Success.' otherwise error                  next;
     #print "LAST: " . $self->lastline;              }
     #my @lines = $self->getlines;  
     #print Dump \@lines;              if ($firmware_type eq 'Firmware' &&
                       $t->firmware_version eq
     $l->p("Checking upload ($conf->{'file_cksum'})");                  $conf->{$firmware_type}->{$host_type}->{ver}
     my $results = $t->tftpd;              ) {
     # check the 'File Length' against ???                  $l->sp("Firmware already up to date");
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {                  next;
       $l->sp(              }
         "File checksum '" . $results->{'File Checksum'} .  
         "does not match config file '" . $conf->{'file_cksum'} . "'!"              if (! $t->logged_in) {
       );                  $l->p("Logging in");
       next;                  $t->login($password);
     }                  unless ($t->logged_in) {
     $l->p("File checksum matches . . . ");                      $l->p('Failed!');
                       $t->close;
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {                      last;
       $l->sp(                  }
         "File length '" . $results->{'File Length'} .              }
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"  
       );              foreach my $k (keys %{ $conf->{general} }) {
       next;                  $conf->{$firmware_type}->{$host_type}->{$k}
     }                    ||= $conf->{general}->{$k};
     $l->p("File length matches . . . ");              }
               $conf->{$firmware_type}->{$host_type}->{firmware_type}
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {                ||= $firmware_type;
       $l->sp(              $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
         "File name '" . $results->{'File Name'} .  
         "' does not match config file '" . $conf->{'file_name'} . "'!"              $l->sp("$host_type $firmware_type");
       );              ## Send commands
       next;              my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
     }              if ($rc) {
     $l->p("File name  matches . . . ");                  $l->sp("Successfull!");
                   $host->{retry}--;
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");                  $needs_reboot = 1;
     unless ($results = $t->updateflash(              } elsif (defined $rc) {
       $ver->{'Firmware Checksum'}, $conf->{'cksum'}                  $l->sp("Already up to date");
     ) ) {                  $host->{retry}--;
       $l->sp("Couldn't update flash: $!");              } else {
       next;                  $l->sp("Failed");
     }                  $t->bye;
                   next;
     unless (              }
       defined $results->{'Checksum'} &&  
       $results->{'Checksum'} eq $conf->{'cksum'}          }
     ) {  
       $l->sp("Saved checksum does not match config file!");          if ($needs_reboot) {
       next;              $l->sp("Rebooting $host->{name}");
     }              $t->reboot;
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .          } else {
           "matches ($conf->{'cksum'})");              $l->sp("Bye $host->{name}");
                   $t->bye();
     $l->sp("Successfully updated!");          }
     return 1;      }
   }  
 }      if (! $processed) {
           $l->sp("");
 sub read_conf          $l->sp("Finished.  No more hosts.");
 {          last;
   my $file = shift;      }
   my %conf;  }
   my $in_ip_list = 0;  
   open my $fh, $file or die "Couldn't open file $file: $!";  sub upload
   while (<$fh>) {  {
     chomp;      my $t    = shift;
     next if /^#/;      my $conf = shift;
     next if /^$/;  
     if ($in_ip_list) {      my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
       s/\s+//g; # Whitespace is a no no  
       my $fw_type = $conf->{firmware_type};
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {  
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);      my $ver = $t->ver;
       } else {  
         push @{ $conf{'ips'} }, $_;      if (! (
       }              $ver->{$fw_type . ' Version'}  &&
     } else {              $ver->{$fw_type . ' Checksum'}
       my ($key, $val) = split /\s+/, $_, 2;          )) {
           $l->sp("Error getting current version numbers");
       if (lc($key) eq 'ips') {          return;
         $in_ip_list = 1;      }
         next;  
       }      if (
           $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
       $key =~ s/^\s+//;          $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
       $key =~ s/\s+$//;      ) {
       $val =~ s/^\s+//;          return 0;
       $val =~ s/\s+$//;      }
   
       $conf{ lc($key) } = $val;      $l->sp("Updating $fw_type");
     }      $l->sp("Config information:");
   }      $l->sp("  Hardware Type: $conf->{'type'}");
   close $fh;      $l->sp("  File Name:     $conf->{'file_name'}");
       $l->sp("  File Size:     $conf->{'file_size'}");
   #print Dump \%conf;      $l->sp("  File Checksum: $conf->{'file_cksum'}");
   foreach (      $l->sp("  Conf Version:  $conf->{'ver'}");
     'password',      $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
     'file_name', 'file_size', 'file_cksum',      $l->sp("  Conf Checksum: $conf->{'cksum'}");
     'ver', 'cksum', 'ips',      $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
   ) {  
     die "No $_ specified in config file!"      my $try = 0;
       if (not exists $conf{$_});      while (1) {
   }          if ($try >= $max_tries) {
               $l->sp("Couldn't update in $max_tries tries!");
   #print Dump \%conf;              return;
   #exit;          }
   return \%conf;          $try++;
 }  
           $l->p("Enabling TFTPd");
 package Trango::Telnet;          unless ($t->enable_tftpd) {
 use base 'Net::Telnet';              $l->sp("Couldn't enable tftpd");
               next;
 my %PRIVATE = (          }
   connected => 0,  
   logged_in => 0,          $l->p("Uploading file ($conf->{file_name})");
 );          # use tftp to push the file up
           my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
 sub new {  
   my $class = shift;          unless ($tftp->put($file, $file)) {
   my $args = shift || {};              $l->sp("Error uploading: " . $tftp->error);
               next;
   $args->{'Timeout'} ||= 5;          }
   $args->{'Prompt'}  ||= '/#> *$/';  
           $l->p("Checking upload ($conf->{'file_cksum'})");
   foreach my $key (keys %{ $args }) {          my $results = $t->tftpd;
     $PRIVATE{$key} = $args->{$key};          # check the 'File Length' against ???
   }          if (! (
                   $results->{'File Checksum'} &&
   my $self = $class->SUPER::new(%{ $args });                  $results->{'File Length'}   &&
   bless $self;                  $results->{'File Name'}
               )) {
   #bless $self, $package;              $l->sp("Unable to get results of upload");
   return $self;              next;
 }          }
           if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
 sub connect              $l->sp(
 {                  "File checksum (" . $results->{'File Checksum'} .
   my $self = shift;                  ") does not match config file (" . $conf->{'file_cksum'} . ")!"
               );
   unless ( $self->open(              next;
       Host => $PRIVATE{'Host'},          }
       Errmode => 'return',          $l->p("File checksum matches . . . ");
   ) ) {  
     $! = "Couldn't connect to $self->{'Host'}.  Connection timed out.";          if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
     return undef, undef;              $l->sp(
   }                  "File length (" . $results->{'File Length'} .
   #$self->dump_log('dump.log');                  ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
               );
   ## Login to remote host.              next;
   unless ($self->waitfor(          }
     -match => '/password: ?$/i',          $l->p("File length matches . . . ");
     -errmode => "return",  
   ) ) {          if ( uc($results->{'File Name'}) ne uc($file) ) {
     $! = "problem connecting to host ($self->{'Host'}): " . $self->lastline;              $l->sp(
     return undef;                  "File name (" . $results->{'File Name'} .
   }                  ") does not match config file (" . $file . ")!"
               );
   $self->login_banner($self->lastline);              next;
           }
   $PRIVATE{'connected'} = 1;          $l->p("File name matches . . . ");
   
   return ($self->host_type, $self->firmware_version);          my $image_type = 'mainimage';
 }          if ($fw_type eq 'FPGA') {
               $image_type = 'fpgaimage';
 sub login          }
 {          $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   my $self = shift;          unless ($results = $t->updateflash(
                   args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
   my $password = shift;                  ' '          . $conf->{'cksum'},
                   Timeout => 90,
   $self->print($password);              ) ) {
   unless ($self->waitfor(              $l->sp("Couldn't update flash: $!");
     -match => $self->prompt,              next;
     -errmode => "return",          }
   ) ) {  
     $! = "login ($self->{'Host'}) failed: " . $self->lastline;          unless (
     return undef;              defined $results->{'Checksum'} &&
   }              $results->{'Checksum'} eq $conf->{'cksum'}
           ) {
   $PRIVATE{'logged_in'} = 1;              $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
               next;
   return 1;          }
 }  
           $l->p("$fw_type saved checksum matches . . . ");
 sub login_banner  
 {          return 1;
   my $self = shift;      }
   }
   my $banner = shift || $PRIVATE{'login_banner'};  
   sub parse_hosts
   my ($type, $ver) = $banner =~  {
     /Welcome to Trango Broadband Wireless (.+)[\s-](\w+)$/i;      my $src = shift;
   
   $self->host_type($type);      my @hosts;
   $self->firmware_version($ver);      foreach my $h (@{ $src }) {
           if ($h->{name} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
   return $banner;              for ($2..$3) {
 }                  my %cur_host;
                   foreach my $k (keys %{ $h }) {
 sub host_type                      $cur_host{$k} = $h->{$k};
 {                  }
   my $self = shift;                  $cur_host{name} = $1 . $_;
                   if (! grep { $cur_host{name} eq $h->{name} } @hosts) {
   my $type = shift || $PRIVATE{'host_type'};                      push @hosts, \%cur_host;
   $PRIVATE{'host_type'} = $type;                  }
               }
   return $type;          } else {
 }              push @hosts, $h;
           }
 sub firmware_version      }
 {  
   my $self = shift;      return \@hosts;
   }
   my $ver = shift || $PRIVATE{'firmware_version'};  
   $PRIVATE{'firmware_version'} = $ver;  package Mylogger;
     
   return $ver  use Fcntl ':flock'; # import LOCK_* constants
 }  #use YAML;
   use constant LOG_PRINT => 128;
 sub Host  use constant LOG_SAVE  =>  64;
 {  
   my $self = shift;  DESTROY {
         my $self = shift;
   my $host = shift || $PRIVATE{'Host'};      if ($self->{'MYLOG'}) {
   $PRIVATE{'HOST'} = $host;          $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
           close $self->{'MYLOG'};
   return $host;      }
 }  }
   
   sub new {
 sub reboot      my $package = shift;
 {      my $self = shift || {};
   my $self = shift;  
       $self->{'base_path'}  ||= '.';
   $self->print("reboot\n");      $self->{'log_path'}   ||= $self->{'base_path'};
   $self->getline;      $self->{'log_prefix'} ||= 'LOG';
       $self->{'log_file'}   ||= GetLogName(
   return 1;          $self->{'log_prefix'},
 }          $self->{'log_path'}
       );
 sub exit      bless $self, $package;
 {  }
   my $self = shift;  
   sub s
   $self->print("exit\n");  {
   $self->getline;      my $self = shift;
       my $m = shift;
   return 1;      return $self->mylog($m, LOG_SAVE);
 }  }
   
 sub ver  sub p
 {  {
   my $self = shift;      my $self = shift;
       my $m = shift;
   return $self->cmd('ver');      return $self->mylog($m, LOG_PRINT);
 }  }
   
 sub tftpd  sub sp
 {  {
   my $self = shift;      my $self = shift;
       my $m = shift;
   return $self->cmd('tftpd', 'Success.');      return $self->mylog($m, LOG_SAVE | LOG_PRINT);
 }  }
   
 sub sysinfo  sub mylog
 {  {
   my $self = shift;      my $self = shift;
   
   return $self->cmd('sysinfo', 'Success.');      my $thing = shift;
 }      chomp $thing;
   
 sub enable_tftpd      my $which = shift;
 {  
   my $self = shift;      my $MYLOG;
       if ($which & LOG_PRINT) {
   my $vals = $self->cmd('tftpd on', 'Success.');          print $thing, "\n";
       }
   if ($vals->{'Tftpd'} eq 'listen') {  
     return $vals;      if ($which & LOG_SAVE) {
   } else {          if ($self->{'MYLOG'}) {
     return undef;              $MYLOG = $self->{'MYLOG'};
   }          } else {
 }              unless ($MYLOG) {
                   open ($MYLOG, '>>', $self->{'log_path'} . '/' .
 sub updateflash                      $self->{'log_file'}) or die "Couldn't open logfile!\n";
 {                  my $ofh = select $MYLOG;
   my $self = shift;                  $|=1;
                   select $ofh;
   my $old = shift;                  $self->{'MYLOG'} = $MYLOG;
   my $new = shift;  
                   $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
   return undef unless $new;              }
           }
   return $self->cmd("updateflash mainimage $old $new", 'Success.', 90);          flock($MYLOG, LOCK_EX);
 }          print $MYLOG (scalar gmtime), "\t", $thing, "\n"
               or die "Couldn't print to MYLOG: $!";
 sub cmd          flock($MYLOG, LOCK_UN);
 {      }
   my $self = shift;  }
   
   my $string = shift;  sub GetLogName
   my $expect_last = shift;  {
   my $timeout = shift || $PRIVATE{'Timeout'};      my $prefix  = shift || die "Invalid prefix passed for log";
   
   unless (defined $string) {      my $logdate = GetLogDate();
     $! = "No command passed";      my $logver  = 0;
     return undef;      my $logname;
   }  
       do {
   unless ($PRIVATE{'connected'}) {          $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
     $! = "Not connected";          $logver++;
     return undef;      } until (not -e $logname);
   }  
       return $logname;
   unless ($PRIVATE{'logged_in'}) {  }
     $! = "Not logged in";  
     return undef;  sub GetLogDate
   }  {
       my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   my @lines = $self->SUPER::cmd(String => $string, Timeout => $timeout);  
       $mon++;
   my $vals = _decode_lines(@lines);      $year += 1900;
   
   unless ($expect_last) {      if ($min  < 10) { $min  = "0$min"  }
     return $vals;      if ($sec  < 10) { $sec  = "0$sec"  }
   }      if ($hour < 10) { $hour = "0$hour" }
       if ($mday < 10) { $mday = "0$mday" }
   my $last = $self->lastline;      if ($mon  < 10) { $mon  = "0$mon"  }
   
   if ($last =~ /$expect_last$/) {      my $time = $year . $mon . $mday;
     return $vals;  
   } else {      return $time;
     warn "Error with command ($string): $last";  }
     return undef;  
   }  
 }  
   
 sub _decode_lines  
 {  
   my @lines = @_;  
   
   my %conf;  
   
   my $key = '';  
   my $val = '';  
   my $in_key = 0;  
   my $in_val = 0;  
   
   foreach my $line (@lines) {  
     my @chars = split //, $line;  
   
     my $last_key = '';  
     foreach my $c (@chars) {  
   
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {  
         if ($c eq '[') {  
           $in_key = 1;  
           $in_val = 0;  
         } else {  
           $in_key = 0;  
           $in_val = 0;  
         }  
   
         if ($key) {  
           $key =~ s/^\s+//;  
           $key =~ s/\s+$//;  
   
           $val =~ s/^\s+//;  
           $val =~ s/\s+$//;  
   
           if ($key eq 'Checksum' && $last_key) {  
             # Special case for these bastids.  
             my $new = $last_key;  
             $new =~ s/\s+\S+$//;  
             $key = $new . " " . $key;  
           }  
   
           $last_key = $key;  
           $conf{$key} = $val;  
           $key = '';  
           $val = '';  
         }  
   
       } elsif ($c eq ']') {  
         $in_val = 1;  
         $in_key = 0;  
         $c = shift @chars;  
   
       } elsif ($in_key) {  
         $key .= $c;  
   
       } elsif ($in_val) {  
         $val .= $c;  
       }  
     }  
   }  
   #print Dump \%conf;  
   
   if (%conf) {  
     return \%conf;  
   } else {  
     return \@lines;  
   }  
 }  
   
   
   
 package Mylogger;  
   
 use Fcntl ':flock'; # import LOCK_* constants  
 #use YAML;  
 use constant LOG_PRINT => 128;  
 use constant LOG_SAVE  =>  64;  
   
 DESTROY {  
   my $self = shift;  
   if ($self->{'MYLOG'}) {  
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");  
     close $self->{'MYLOG'};  
   }  
 }  
   
 sub new {  
   my $package = shift;  
   my $self = shift || {};  
   
   $self->{'base_path'}  ||= '.';  
   $self->{'log_path'}   ||= $self->{'base_path'};  
   $self->{'log_prefix'} ||= 'LOG';  
   $self->{'log_file'}   ||= GetLogName(  
     $self->{'log_prefix'},  
     $self->{'log_path'}  
   );  
   bless $self, $package;  
 }  
   
 sub s  
 {  
   my $self = shift;  
   my $m = shift;  
   return $self->mylog($m, LOG_SAVE);  
 }  
   
 sub p  
 {  
   my $self = shift;  
   my $m = shift;  
   return $self->mylog($m, LOG_PRINT);  
 }  
   
 sub sp  
 {  
   my $self = shift;  
   my $m = shift;  
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);  
 }  
   
 sub mylog  
 {  
   my $self = shift;  
   
   my $thing = shift;  
   chomp $thing;  
   
   my $which = shift;  
   
   my $MYLOG;  
   if ($which & LOG_PRINT) {  
     print $thing, "\n";  
   }  
   
   if ($which & LOG_SAVE) {  
     if ($self->{'MYLOG'}) {  
       $MYLOG = $self->{'MYLOG'};  
     } else {  
       unless ($MYLOG) {  
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})  
           or die "Couldn't open logfile!\n";  
         my $ofh = select $MYLOG;  
         $|=1;  
         select $ofh;  
         $self->{'MYLOG'} = $MYLOG;  
   
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");  
       }  
     }  
     flock($MYLOG, LOCK_EX);  
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"  
       or die "Couldn't print to MYLOG: $!";  
     flock($MYLOG, LOCK_UN);  
   }  
 }  
   
 sub GetLogName  
 {  
   my $prefix  = shift || die "Invalid prefix passed for log";  
   
   my $logdate = GetLogDate();  
   my $logver  = 0;  
   my $logname;  
   
   do {  
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';  
     $logver++;  
   } until (not -e $logname);  
   
   return $logname;  
 }  
   
 sub GetLogDate  
 {  
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();  
   
   $mon++;  
   $year += 1900;  
   
   if ($min  < 10) { $min  = "0$min"  }  
   if ($sec  < 10) { $sec  = "0$sec"  }  
   if ($hour < 10) { $hour = "0$hour" }  
   if ($mday < 10) { $mday = "0$mday" }  
   if ($mon  < 10) { $mon  = "0$mon"  }  
   
   my $time = $year . $mon . $mday;  
   
   return $time;  
 }  

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.31

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>