#!/usr/bin/perl -w

# Trida pro logovani do souboru

package tlogger;

use Time::Local;

$logdir="/usr/local/netadmin/dohled/";

sub new {
  my ($type,$soubor)=@_;
  my ($self)={};
  bless($self,$type);
  open(FW,">>$logdir$soubor") or die("Nelze otevrit $soubor");;
  return($self);
}

sub tolog {
  my ($self, $text) = @_;
  my ($timestamp);
  @cas=localtime(time);
  $cas[5]+=1900;
  $cas[4]+=1;
  $cas[2]=$self->zerofill($cas[2]);
  $cas[1]=$self->zerofill($cas[1]);
  $timestamp="$cas[3].$cas[4].$cas[5] $cas[2]:$cas[1]";
  flock(FW,2) or die("Nelze uzamknout $soubor");
  print FW "$timestamp $text\n";
  flock(FW,8);
}


# doplneni numerickeho reteze o nulu, pokud je <=9
sub zerofill {
  my ($self, $vstup) = @_;
  if($vstup =~ /^\d{1}$/) { return("0$vstup"); } else { return($vstup); }
}

sub DESTROY {
  close(FW);
}

return(1);