/[pdpsoft]/nl.nikhef.pdp.fetchcrl/trunk/FCLog.pm
ViewVC logotype

Annotation of /nl.nikhef.pdp.fetchcrl/trunk/FCLog.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2690 - (hide annotations) (download) (as text)
Tue Mar 4 16:26:06 2014 UTC (7 years, 7 months ago) by davidg
File MIME type: application/x-perl
File size: 5142 byte(s)
Add rcmode option and no re-run of boot script (first version)

1 davidg 1758 #
2     # @(#)$Id$
3     #
4     # ###########################################################################
5     #
6     # Fetch-CRL3 logging support
7     package FCLog;
8     use Sys::Syslog;
9    
10     # Syntax:
11     # $log = CL->new( [outputmode=qualified,cache,direct,syslog] )
12     # $log->destadd( destination [,facility] )
13     # $log->destremove ( destination )
14     # $log->setverbose( level )
15     # $log->setdebug( level )
16     # $log->setwarnings( 0|1 )
17     # $log->debug( level, message ...)
18     # $log->verb( level, message ...)
19     # $log->warn( level, message ...)
20     # $log->err( level, message ...)
21     # $log->clear( )
22     # $log->flush( )
23     # $log->exitstatus( )
24     #
25     sub new {
26     my $self = shift;
27     my $obref = {}; bless $obref;
28     $obref->{"debug"} = 0;
29     $obref->{"verbose"} = 0;
30     $obref->{"messagecache"} = ();
31     $obref->{"warnings"} = 1;
32     $obref->{"errors"} = 1;
33 davidg 2690 $obref->{"rcmode"} = 1;
34 davidg 1758 $obref->{"warncount"} = 0;
35     $obref->{"errorcount"} = 0;
36     $obref->{"syslogfacility"} = "daemon";
37    
38     while ( my $mode = shift ) {
39     $obref->destadd($mode);
40     }
41    
42     return $obref;
43     }
44    
45     sub destadd {
46     my $self = shift;
47     my $mode = shift;
48     my $facility = (shift or $self->{"syslogfacility"});
49    
50     return 0 unless defined $mode;
51    
52     $self->{"logmode"}{$mode} = 1;
53     if ( $mode eq "syslog" ) {
54     my $progname = $0;
55     $progname =~ s/^.*\///;
56     $self->{"syslogfacility"} = $facility;
57     openlog($progname,"nowait,pid", $facility);
58     }
59     return 1;
60     }
61    
62     sub destremove {
63     my $self = shift;
64     my $ok = 1;
65    
66     my $mode = shift;
67     $self->{"logmode"} = {} and return 1 if (defined $mode and $mode eq "all");
68     unshift @_,$mode;
69    
70     while ( my $mode = shift ) {
71     if ( defined $self->{"logmode"}{$mode} ) {
72     closelog() if $mode eq "syslog";
73     delete $self->{"logmode"}{$mode};
74     } else {
75     $ok=0;
76     }
77     }
78     return $ok;
79     }
80    
81     sub setverbose {
82     my ($self,$level) = @_;
83     my $oldlevel = $self->{"verbose"};
84     $self->{"verbose"} = 0+$level;
85     return $oldlevel;
86     }
87    
88     sub getverbose {
89     my ($self) = @_;
90     return $self->{"verbose"};
91     }
92    
93     sub setdebug {
94     my ($self,$level) = @_;
95     my $oldlevel = $self->{"debug"};
96     $self->{"debug"} = $level;
97     return $oldlevel;
98     }
99    
100     sub getdebug {
101     my ($self) = @_;
102     return $self->{"debug"};
103     }
104    
105     sub setwarnings {
106     my ($self,$level) = @_;
107     my $oldlevel = $self->{"warnings"};
108     $self->{"warnings"} = $level;
109     return $oldlevel;
110     }
111    
112     sub getwarnings {
113     my ($self) = @_;
114     return $self->{"warnings"};
115     }
116    
117     sub geterrors {
118     my ($self) = @_;
119     return $self->{"errors"};
120     }
121    
122     sub seterrors {
123     my ($self,$level) = @_;
124     my $oldlevel = $self->{"errors"};
125     $self->{"errors"} = $level;
126     return $oldlevel;
127     }
128    
129 davidg 2690 sub getrcmode {
130     my ($self) = @_;
131     return $self->{"rcmode"};
132     }
133    
134     sub setrcmode {
135     my ($self,$level) = @_;
136     my $oldlevel = $self->{"rcmode"};
137     $self->{"rcmode"} = $level;
138     return $oldlevel;
139     }
140    
141 davidg 1758 sub verb($$$) {
142     my $self = shift;
143     my $level = shift;
144     return 1 unless ( $level <= $self->{"verbose"} );
145     my $message = "@_";
146     $self->output("VERBOSE($level)",$message);
147     return 1;
148     }
149    
150     sub debug($$$) {
151     my $self = shift;
152     my $level = shift;
153     return 1 unless ( $level <= $self->{"debug"} );
154     my $message = "@_";
155     $self->output("DEBUG($level)",$message);
156     return 1;
157     }
158    
159     sub warn($@) {
160     my $self = shift;
161     return 1 unless ( $self->{"warnings"} );
162     $self->{"warningcount"}++;
163     my $message = "@_";
164     $self->output("WARN",$message);
165     return 1;
166     }
167    
168     sub err($@) {
169     my $self = shift;
170     my $message = "@_";
171     return 1 unless ( $self->{"errors"} );
172     $self->output("ERROR",$message);
173     $self->{"errorcount"}++;
174     return 1;
175     }
176    
177 davidg 2690 sub retr_err($@) {
178     my $self = shift;
179     my $message = "@_";
180     return 1 unless ( $self->{"errors"} );
181     $self->output("ERROR",$message);
182     return 1 unless ( $self->{"rcmode"} );
183     $self->{"errorcount"}++;
184     return 1;
185     }
186    
187 davidg 1758 sub output($$@) {
188     my ($self,$label,@message) = @_;
189     return 0 unless defined $label and @message;
190    
191     my $message = join " ",@message;
192    
193     print "" . ($label?"$label ":"") . "$message\n"
194     if ( defined $self->{"logmode"}{"qualified"} );
195     push @{$self->{"messagecache"}},"" . ($label?"$label ":"") . "$message\n"
196     if ( defined $self->{"logmode"}{"cache"} );
197     print "$message\n"
198     if ( defined $self->{"logmode"}{"direct"} );
199    
200     if ( defined $self->{"logmode"}{"syslog"} ) {
201     my $severity = "LOG_INFO";
202     $severity = "LOG_NOTICE" if $label eq "WARN";
203     $severity = "LOG_ERR" if $label eq "ERROR";
204     $severity = "LOG_DEBUG" if $label =~ /^VERBOSE/;
205     $severity = "LOG_DEBUG" if $label =~ /^DEBUG/;
206     syslog($severity, "%s", $message);
207     }
208    
209     return 1;
210     }
211    
212     sub clear($) {
213     my $self = shift;
214    
215     $self->{"messagecache"} = ();
216     return 1;
217     }
218    
219     sub flush($) {
220     my $self = shift;
221    
222     foreach my $s ( @{$self->{"messagecache"}} ) {
223     print $s;
224     }
225     $self->{"messagecache"} = ();
226    
227     $self->{"errorcount"} and $self->{"errors"} and return 0;
228     $self->{"warningcount"} and $self->{"warnings"} and return 1;
229     return 1;
230     }
231    
232     sub cleanse($) {
233     my $self = shift;
234     $self->{"messagecache"} = ();
235     $self->{"errorcount"} = 0;
236     $self->{"warningcount"} = 0;
237     $self->{"logmode"} = {};
238     return 1;
239     }
240    
241    
242     sub exitstatus($) {
243     my $self = shift;
244    
245     $self->{"errorcount"} and $self->{"errors"} and return 1;
246     return 0;
247     }
248    
249     1;

Properties

Name Value
svn:keywords Id

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28