]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/logger.hh
add OpenSSL exception to PowerDNS, Netherlabs, van Dijk and Hubert copyrights
[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 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #ifndef LOGGER_HH
23 #define LOGGER_HH
24 /* (C) 2002 POWERDNS.COM BV */
25
26 #include <string>
27 #include <map>
28 #include <ctime>
29 #include <iostream>
30 #include <sstream>
31 #include "config.h"
32 #include <syslog.h>
33 #include <pthread.h>
34
35 #include "namespaces.hh"
36
37 //! The Logger class can be used to log messages in various ways.
38 class Logger
39 {
40 public:
41 Logger(const string &, int facility=LOG_DAEMON); //!< pass the identification you wish to appear in the log
42
43 //! The urgency of a log message
44 enum Urgency {All=99999,NTLog=12345,Alert=LOG_ALERT, Critical=LOG_CRIT, Error=LOG_ERR, Warning=LOG_WARNING,
45 Notice=LOG_NOTICE,Info=LOG_INFO, Debug=LOG_DEBUG, None=-1};
46
47 /** Log a message.
48 \param msg Message you wish to log
49 \param Urgency Urgency of the message you wish to log
50 */
51 void log(const string &msg, Urgency u=Notice);
52
53 void setFacility(int f){d_facility=f;open();} //!< Choose logging facility
54 void setFlag(int f){flags|=f;open();} //!< set a syslog flag
55 void setName(const string &);
56
57 //! set lower limit of urgency needed for console display. Messages of this urgency, and higher, will be displayed
58 void toConsole(Urgency);
59 void setLoglevel( Urgency );
60
61 //! Log to a file.
62 void toFile( const string & filename );
63
64 void resetFlags(){flags=0;open();} //!< zero the flags
65 /** Use this to stream to your log, like this:
66 \code
67 L<<"This is an informational message"<<endl; // logged at default loglevel (Info)
68 L<<Logger::Warning<<"Out of diskspace"<<endl; // Logged as a warning
69 L<<"This is an informational message"<<endl; // logged AGAIN at default loglevel (Info)
70 \endcode
71 */
72 Logger& operator<<(const string &s); //!< log a string
73 Logger& operator<<(int); //!< log an int
74 Logger& operator<<(double); //!< log a double
75 Logger& operator<<(unsigned int); //!< log an unsigned int
76 Logger& operator<<(long); //!< log an unsigned int
77 Logger& operator<<(unsigned long); //!< log an unsigned int
78 Logger& operator<<(unsigned long long); //!< log an unsigned 64 bit int
79 Logger& operator<<(Urgency); //!< set the urgency, << style
80
81 Logger& operator<<(std::ostream & (&)(std::ostream &)); //!< this is to recognise the endl, and to commit the log
82
83 private:
84 map<pthread_t,string>d_strings;
85 map<pthread_t,Urgency> d_outputurgencies;
86 void open();
87 string name;
88 int flags;
89 int d_facility;
90 bool opened;
91 Urgency d_loglevel;
92 Urgency consoleUrgency;
93 pthread_mutex_t lock;
94 };
95
96 extern Logger &theL(const string &pname="");
97
98 #ifdef VERBOSELOG
99 #define DLOG(x) x
100 #else
101 #define DLOG(x) ((void)0)
102 #endif
103
104
105 #endif