]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/logger.hh
Merge pull request #1026 from mind04/dominfo
[thirdparty/pdns.git] / pdns / logger.hh
CommitLineData
12c86877
BH
1/*
2 PowerDNS Versatile Database Driven Nameserver
96879e7d 3 Copyright (C) 2005 PowerDNS.COM BV
12c86877
BH
4
5 This program is free software; you can redistribute it and/or modify
96879e7d
BH
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation
12c86877
BH
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
06bd9ccf 16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12c86877
BH
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>
06748762 27#include "config.h"
12c86877
BH
28#ifndef WIN32
29# include <syslog.h>
f14ebffb 30#include <pthread.h>
12c86877
BH
31
32#else
33# define WINDOWS_LEAN_AND_MEAN
34# include <windows.h>
f14ebffb
BH
35typedef int pthread_mutex_t;
36typedef int pthread_t;
12c86877
BH
37#endif // WIN32
38
10f4eea8 39#include "namespaces.hh"
12c86877
BH
40
41//! The Logger class can be used to log messages in various ways.
42class Logger
43{
44public:
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,
4957a608 50 Notice=LOG_NOTICE,Info=LOG_INFO, Debug=LOG_DEBUG, None=-1};
12c86877
BH
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,
4957a608 64 Notice = EVENTLOG_INFORMATION_TYPE,
12c86877
BH
65 Info = EVENTLOG_INFORMATION_TYPE,
66 Debug = EVENTLOG_INFORMATION_TYPE,
67 None = -1
68 };
69
70 void toNTLog( void );
71
72private:
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
82public:
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);
eefd15f9 98 void setLoglevel( Urgency );
12c86877
BH
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
f1f85f12 113 Logger& operator<<(double); //!< log a double
ff181ffa 114 Logger& operator<<(unsigned int); //!< log an unsigned int
ac8a2021 115 Logger& operator<<(long); //!< log an unsigned int
96879e7d 116 Logger& operator<<(unsigned long); //!< log an unsigned int
1d5b3ce6 117 Logger& operator<<(unsigned long long); //!< log an unsigned 64 bit int
12c86877
BH
118 Logger& operator<<(Urgency); //!< set the urgency, << style
119
120#ifndef WIN32
10f4eea8 121 Logger& operator<<(std::ostream & (&)(std::ostream &)); //!< this is to recognise the endl, and to commit the log
12c86877
BH
122#else
123 // This is a hack to keep MSVC from generating a internal compiler error.
124 Logger& operator<<(ostream & (hack)(ostream &)); //!< this is to recognise the endl, and to commit the log
125#endif // WIN32
126
127private:
128 map<pthread_t,string>d_strings;
129 map<pthread_t,Urgency> d_outputurgencies;
130 void open();
131 string name;
132 int flags;
133 int d_facility;
134 bool opened;
eefd15f9 135 Urgency d_loglevel;
12c86877
BH
136 Urgency consoleUrgency;
137 pthread_mutex_t lock;
138};
139
140extern Logger &theL(const string &pname="");
141
142#ifdef VERBOSELOG
143#define DLOG(x) x
144#else
145#define DLOG(x)
146#endif
147
148
149#endif