]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/logger.hh
and the final bit of whitespace/tab cleanup
[thirdparty/pdns.git] / pdns / logger.hh
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2005 PowerDNS.COM BV
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #ifndef LOGGER_HH
19 #define LOGGER_HH
20 /* (C) 2002 POWERDNS.COM BV */
21
22 #include <string>
23 #include <map>
24 #include <ctime>
25 #include <iostream>
26 #include <sstream>
27 #include "config.h"
28 #ifndef WIN32
29 # include <syslog.h>
30 #include <pthread.h>
31
32 #else
33 # define WINDOWS_LEAN_AND_MEAN
34 # include <windows.h>
35 typedef int pthread_mutex_t;
36 typedef int pthread_t;
37 #endif // WIN32
38
39 using namespace std;
40
41 //! The Logger class can be used to log messages in various ways.
42 class Logger
43 {
44 public:
45 #ifndef WIN32
46 Logger(const string &, int facility=LOG_DAEMON); //!< pass the identification you wish to appear in the log
47
48 //! The urgency of a log message
49 enum Urgency {All=99999,NTLog=12345,Alert=LOG_ALERT, Critical=LOG_CRIT, Error=LOG_ERR, Warning=LOG_WARNING,
50 Notice=LOG_NOTICE,Info=LOG_INFO, Debug=LOG_DEBUG, None=-1};
51
52 #else
53 Logger( const string &, int facility = 0 ); //!< pass the identification you wish to appear in the log
54
55 //! The urgency of a log message
56 enum Urgency
57 {
58 All = 99999,
59 NTLog = 12345,
60 Alert = EVENTLOG_ERROR_TYPE,
61 Critical= EVENTLOG_ERROR_TYPE,
62 Error = EVENTLOG_ERROR_TYPE,
63 Warning = EVENTLOG_WARNING_TYPE,
64 Notice = EVENTLOG_INFORMATION_TYPE,
65 Info = EVENTLOG_INFORMATION_TYPE,
66 Debug = EVENTLOG_INFORMATION_TYPE,
67 None = -1
68 };
69
70 void toNTLog( void );
71
72 private:
73 //! Handle used to communicate with the event log.
74 HANDLE m_eventLogHandle;
75
76 //! Log file handle.
77 FILE *m_pLogFile;
78
79 //! Log current message to the NT log?
80 map< pthread_t, bool > m_toNTLog;
81
82 public:
83
84 #endif // WIN32
85
86 /** Log a message.
87 \param msg Message you wish to log
88 \param Urgency Urgency of the message you wish to log
89 */
90 void log(const string &msg, Urgency u=Notice);
91
92 void setFacility(int f){d_facility=f;open();} //!< Choose logging facility
93 void setFlag(int f){flags|=f;open();} //!< set a syslog flag
94 void setName(const string &);
95
96 //! set lower limit of urgency needed for console display. Messages of this urgency, and higher, will be displayed
97 void toConsole(Urgency);
98 void setLoglevel( Urgency );
99
100 //! Log to a file.
101 void toFile( const string & filename );
102
103 void resetFlags(){flags=0;open();} //!< zero the flags
104 /** Use this to stream to your log, like this:
105 \code
106 L<<"This is an informational message"<<endl; // logged at default loglevel (Info)
107 L<<Logger::Warning<<"Out of diskspace"<<endl; // Logged as a warning
108 L<<"This is an informational message"<<endl; // logged AGAIN at default loglevel (Info)
109 \endcode
110 */
111 Logger& operator<<(const string &s); //!< log a string
112 Logger& operator<<(int); //!< log an int
113 Logger& operator<<(unsigned int); //!< log an unsigned int
114 Logger& operator<<(long); //!< log an unsigned int
115 Logger& operator<<(unsigned long); //!< log an unsigned int
116 Logger& operator<<(unsigned long long); //!< log an unsigned 64 bit int
117 Logger& operator<<(Urgency); //!< set the urgency, << style
118
119 #ifndef WIN32
120 Logger& operator<<(ostream & (&)(ostream &)); //!< this is to recognise the endl, and to commit the log
121 #else
122 // This is a hack to keep MSVC from generating a internal compiler error.
123 Logger& operator<<(ostream & (hack)(ostream &)); //!< this is to recognise the endl, and to commit the log
124 #endif // WIN32
125
126 private:
127 map<pthread_t,string>d_strings;
128 map<pthread_t,Urgency> d_outputurgencies;
129 void open();
130 string name;
131 int flags;
132 int d_facility;
133 bool opened;
134 Urgency d_loglevel;
135 Urgency consoleUrgency;
136 pthread_mutex_t lock;
137 };
138
139 extern Logger &theL(const string &pname="");
140
141 #ifdef VERBOSELOG
142 #define DLOG(x) x
143 #else
144 #define DLOG(x)
145 #endif
146
147
148 #endif