Changeset 31


Ignore:
Timestamp:
11/05/05 17:47:24 (19 years ago)
Author:
thauvin
Message:
  • adding loging functions
Location:
trunk/soft/ObsData
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/soft/ObsData/ObsData/Repository.pm

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.2
    r27 r31  
    1010our @ISA = qw(ObsData); 
    1111 
     12my @loglevel = ( 
     13    'DEBUG', 
     14    'INFO', 
     15    'RESULT', 
     16    'WARNING', 
     17    'ERROR', 
     18    'FATAL', 
     19); 
     20 
     21=head1 METHODS 
     22 
     23=head2 new 
     24 
     25Parameters list: 
     26 
     27=item configfile 
     28 
     29The configuration file to use 
     30 
     31=item sublog 
     32 
     33Coderef to external loging function 
     34 
     35=item subloglevel 
     36 
     37Minimum log level to call sublog, default is 1 
     38 
     39=item minloglevel 
     40 
     41Minimum loglevel for internal logging 
     42 
     43=item dir 
     44 
     45The directory where internal data are put 
     46 
     47=cut 
     48 
    1249sub new { 
    1350    my ($class, %config) = @_; 
     
    1552    my $odr = $class->SUPER::new($config{configfile}) or return undef; 
    1653 
     54    $odr->{sublog} = $config{sublog}; 
     55    $odr->{subloglevel} = defined($config{subloglevel}) ? $config{subloglevel} : 1; 
     56    $odr->{minloglevel} = defined($config{minloglevel}) ? $config{minloglevel} : 1; 
     57 
     58    if(!($config{dir} && -d $config{dir})) { 
     59        return undef; 
     60    } 
     61 
     62    open($odr->{loghandle}, "> $config{dir}/logs/obsdata.log") or return undef;  
    1763     
     64    bless($odr, $class); 
     65} 
    1866 
    19     bless($odr, $class); 
     67=head2 logmsg 
     68 
     69Add a message to log stack 
     70 
     71Parameters list: 
     72 
     73=item loglevel  
     74 
     75The log level of message (num or text) 
     76 
     77=item info 
     78 
     79hash ref to additionnal info 
     80 
     81=item message ... 
     82 
     83printf like args 
     84 
     85=cut 
     86 
     87sub logmsg { 
     88    my ($self, $level, $info, $pf, @pfa) = @_; 
     89    my $textlevel = $level =~ /^\d+$/ ? $loglevel[$level] : $level; 
     90    my $nlevel = logleveln($textlevel); 
     91     
     92    my @call = caller; 
     93     
     94    $textlevel ||= ""; 
     95    if (!defined($nlevel)) { 
     96        $textlevel = 'INFO'; 
     97        $nlevel = logleveln($textlevel); 
     98        $self->logmsg('ERROR', undef, 
     99           'Invalid use of loglevel "%s" at %s line %d, unsing %s"', 
     100           $level || "", $call[1], $call[2], $textlevel); 
     101    } 
     102 
     103    if ($self->{minloglevel} <= $nlevel) { 
     104       my $h = $self->{loghandle}; 
     105       print $h "[$textlevel]: " . sprintf($pf, @pfa) . "\n"; 
     106    } 
     107 
     108    if ($self->{sublog} && $self->{subloglevel} <= $nlevel) { 
     109       $self->{sublog}->($textlevel, $info, sprintf($pf, @pfa)); 
     110    } 
     111 
     112} 
     113 
     114sub logleveln { 
     115    my ($t) = @_; 
     116    my $i = 0; 
     117    foreach (@loglevel) { 
     118        return $i if ($t eq $_); 
     119        $i++ 
     120    } 
     121    return undef; 
    20122} 
    21123 
     
    35137    ) or return 0; 
    36138 
     139    my $odr = ObsData::Repository->new(dir => $dir); 
     140    $odr->logmsg('INFO', undef, "Repository succefully created in %s", $dir); 
     141 
    37142    1; 
    38143} 
  • trunk/soft/ObsData/t/OR-02.t

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.2
    r28 r31  
    22# $Id$ 
    33 
    4 use Test::More tests => 4; 
     4use Test::More tests => 6; 
    55use File::Temp qw(tempdir); 
    66use ObsData::Repository; 
     
    1313 
    1414ok(ObsData::Repository->new(configfile => 'testdata/obsdata-conftest', dir => $td), "Can create object"); 
    15 system("rm -fvr $td"); 
     15 
     16my $odr = ObsData::Repository->new( 
     17    configfile => 'testdata/obsdata-conftest', dir => $td, 
     18    sublog => sub {  
     19        ok($_[0] eq "INFO", "Log Callback give good loglevel"); 
     20        ok($_[2] eq "LOG MSG", "Log Callback give good message"); 
     21    }, 
     22); 
     23 
     24$odr->logmsg('INFO', undef, "LOG %s", 'MSG'); 
     25# DEBUG msg are filtrered, so if more test are run, there is a pb here 
     26$odr->logmsg('DEBUG', undef, "LOG %s", 'MSG'); 
     27$odr = undef; 
     28 
     29system("rm -fr $td"); 
    1630} 
Note: See TracChangeset for help on using the changeset viewer.