Changeset 31
- Timestamp:
- 11/05/05 17:47:24 (19 years ago)
- 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 10 10 our @ISA = qw(ObsData); 11 11 12 my @loglevel = ( 13 'DEBUG', 14 'INFO', 15 'RESULT', 16 'WARNING', 17 'ERROR', 18 'FATAL', 19 ); 20 21 =head1 METHODS 22 23 =head2 new 24 25 Parameters list: 26 27 =item configfile 28 29 The configuration file to use 30 31 =item sublog 32 33 Coderef to external loging function 34 35 =item subloglevel 36 37 Minimum log level to call sublog, default is 1 38 39 =item minloglevel 40 41 Minimum loglevel for internal logging 42 43 =item dir 44 45 The directory where internal data are put 46 47 =cut 48 12 49 sub new { 13 50 my ($class, %config) = @_; … … 15 52 my $odr = $class->SUPER::new($config{configfile}) or return undef; 16 53 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; 17 63 64 bless($odr, $class); 65 } 18 66 19 bless($odr, $class); 67 =head2 logmsg 68 69 Add a message to log stack 70 71 Parameters list: 72 73 =item loglevel 74 75 The log level of message (num or text) 76 77 =item info 78 79 hash ref to additionnal info 80 81 =item message ... 82 83 printf like args 84 85 =cut 86 87 sub 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 114 sub logleveln { 115 my ($t) = @_; 116 my $i = 0; 117 foreach (@loglevel) { 118 return $i if ($t eq $_); 119 $i++ 120 } 121 return undef; 20 122 } 21 123 … … 35 137 ) or return 0; 36 138 139 my $odr = ObsData::Repository->new(dir => $dir); 140 $odr->logmsg('INFO', undef, "Repository succefully created in %s", $dir); 141 37 142 1; 38 143 } -
trunk/soft/ObsData/t/OR-02.t
- Property cvs2svn:cvs-rev changed from 1.1 to 1.2
r28 r31 2 2 # $Id$ 3 3 4 use Test::More tests => 4;4 use Test::More tests => 6; 5 5 use File::Temp qw(tempdir); 6 6 use ObsData::Repository; … … 13 13 14 14 ok(ObsData::Repository->new(configfile => 'testdata/obsdata-conftest', dir => $td), "Can create object"); 15 system("rm -fvr $td"); 15 16 my $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 29 system("rm -fr $td"); 16 30 }
Note: See TracChangeset
for help on using the changeset viewer.