1 // Copyright 2009 The Archiveopteryx Developers <info@aox.org>
2
3 #include "stderrlogger.h"
4
5 #include "log.h"
6
7 #include <stdio.h> // fprintf
8 #include <stdlib.h> // exit
9 #include <sysexits.h> // all the exit codes
10
11 /*! \class StderrLogger stderrlogger.h
12
13 The StderrLogger logs errors and disaster output to stderr and
14 exits the program immediately in case of a disaster. It is used by
15 some command-line programs.
16 */
17
18
19 /*! Creates a logger named \a name. The object will preface its output
20 lines with \a name. If \a verbosity is 0, info messages are
21 suppressed. If \a verbosity is 0 or 1, debug messages are
22 suppressed.
23 */
24
StderrLogger(const EString & name,uint verbosity)25 StderrLogger::StderrLogger( const EString & name, uint verbosity )
26 : Logger(), n( name ), v( verbosity )
27 {
28 Log::Severity ls;
29
30 if ( v == 0 )
31 ls = Log::Significant;
32 else if ( v == 1 )
33 ls = Log::Info;
34 else
35 ls = Log::Debug;
36
37 Log::setLogLevel( ls );
38 }
39
40
send(const EString &,Log::Severity s,const EString & m)41 void StderrLogger::send( const EString &, Log::Severity s, const EString & m )
42 {
43 // we don't need to handle Disaster, Log already has done that
44 if ( s != Log::Disaster ) {
45 fprintf( stderr, "%s: %s\n", name().cstr(), m.cstr() );
46 }
47 else {
48 fprintf( stderr, "%s: Fatal error. Exiting.\n", name().cstr() );
49 exit( EX_UNAVAILABLE );
50 }
51 }
52
53
54 /*! Returns the name of this object, as supplied to the constructor. */
55
name() const56 EString StderrLogger::name() const
57 {
58 return n;
59 }
60