]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/logger.hh
3c7b2ccbb82de52b222048edd841d5d9a7736f8c
[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 #include <syslog.h>
29 #include <pthread.h>
30
31 #include "namespaces.hh"
32
33 //! The Logger class can be used to log messages in various ways.
34 class Logger
35 {
36 public:
37 Logger(const string &, int facility=LOG_DAEMON); //!< pass the identification you wish to appear in the log
38
39 //! The urgency of a log message
40 enum Urgency {All=99999,NTLog=12345,Alert=LOG_ALERT, Critical=LOG_CRIT, Error=LOG_ERR, Warning=LOG_WARNING,
41 Notice=LOG_NOTICE,Info=LOG_INFO, Debug=LOG_DEBUG, None=-1};
42
43 /** Log a message.
44 \param msg Message you wish to log
45 \param Urgency Urgency of the message you wish to log
46 */
47 void log(const string &msg, Urgency u=Notice);
48
49 void setFacility(int f){d_facility=f;open();} //!< Choose logging facility
50 void setFlag(int f){flags|=f;open();} //!< set a syslog flag
51 void setName(const string &);
52
53 //! set lower limit of urgency needed for console display. Messages of this urgency, and higher, will be displayed
54 void toConsole(Urgency);
55 void setLoglevel( Urgency );
56
57 //! Log to a file.
58 void toFile( const string & filename );
59
60 void resetFlags(){flags=0;open();} //!< zero the flags
61 /** Use this to stream to your log, like this:
62 \code
63 L<<"This is an informational message"<<endl; // logged at default loglevel (Info)
64 L<<Logger::Warning<<"Out of diskspace"<<endl; // Logged as a warning
65 L<<"This is an informational message"<<endl; // logged AGAIN at default loglevel (Info)
66 \endcode
67 */
68 Logger& operator<<(const string &s); //!< log a string
69 Logger& operator<<(int); //!< log an int
70 Logger& operator<<(double); //!< log a double
71 Logger& operator<<(unsigned int); //!< log an unsigned int
72 Logger& operator<<(long); //!< log an unsigned int
73 Logger& operator<<(unsigned long); //!< log an unsigned int
74 Logger& operator<<(unsigned long long); //!< log an unsigned 64 bit int
75 Logger& operator<<(Urgency); //!< set the urgency, << style
76
77 Logger& operator<<(std::ostream & (&)(std::ostream &)); //!< this is to recognise the endl, and to commit the log
78
79 private:
80 map<pthread_t,string>d_strings;
81 map<pthread_t,Urgency> d_outputurgencies;
82 void open();
83 string name;
84 int flags;
85 int d_facility;
86 bool opened;
87 Urgency d_loglevel;
88 Urgency consoleUrgency;
89 pthread_mutex_t lock;
90 };
91
92 extern Logger &theL(const string &pname="");
93
94 #ifdef VERBOSELOG
95 #define DLOG(x) x
96 #else
97 #define DLOG(x) ((void)0)
98 #endif
99
100
101 #endif