]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of PowerDNS or dnsdist. | |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * In addition, for the avoidance of any doubt, permission is granted to | |
10 | * link this program with OpenSSL and to (re)distribute the binaries | |
11 | * produced as the result of such linking. | |
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 Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | #pragma once | |
23 | ||
24 | #include <string> | |
25 | #include <ctime> | |
26 | #include <iostream> | |
27 | #include <sstream> | |
28 | #include <syslog.h> | |
29 | #include <pthread.h> | |
30 | ||
31 | #include "namespaces.hh" | |
32 | #include "dnsname.hh" | |
33 | #include "iputils.hh" | |
34 | ||
35 | //! The Logger class can be used to log messages in various ways. | |
36 | class Logger | |
37 | { | |
38 | public: | |
39 | Logger(const string &, int facility=LOG_DAEMON); //!< pass the identification you wish to appear in the log | |
40 | ||
41 | //! The urgency of a log message | |
42 | enum Urgency {All=32767,Alert=LOG_ALERT, Critical=LOG_CRIT, Error=LOG_ERR, Warning=LOG_WARNING, | |
43 | Notice=LOG_NOTICE,Info=LOG_INFO, Debug=LOG_DEBUG, None=-1}; | |
44 | ||
45 | /** Log a message. | |
46 | \param msg Message you wish to log | |
47 | \param u Urgency of the message you wish to log | |
48 | */ | |
49 | void log(const string &msg, Urgency u=Notice); | |
50 | ||
51 | void setFacility(int f){d_facility=f;open();} //!< Choose logging facility | |
52 | void setFlag(int f){flags|=f;open();} //!< set a syslog flag | |
53 | void setName(const string &); | |
54 | ||
55 | //! set lower limit of urgency needed for console display. Messages of this urgency, and higher, will be displayed | |
56 | void toConsole(Urgency); | |
57 | void setLoglevel( Urgency ); | |
58 | ||
59 | void disableSyslog(bool d) { | |
60 | d_disableSyslog = d; | |
61 | } | |
62 | ||
63 | void setTimestamps(bool t) { | |
64 | d_timestamps = t; | |
65 | } | |
66 | ||
67 | //! Log to a file. | |
68 | void toFile( const string & filename ); | |
69 | ||
70 | void resetFlags(){flags=0;open();} //!< zero the flags | |
71 | /** Use this to stream to your log, like this: | |
72 | \code | |
73 | L<<"This is an informational message"<<endl; // logged at default loglevel (Info) | |
74 | L<<Logger::Warning<<"Out of diskspace"<<endl; // Logged as a warning | |
75 | L<<"This is an informational message"<<endl; // logged AGAIN at default loglevel (Info) | |
76 | \endcode | |
77 | */ | |
78 | Logger& operator<<(const char *s); | |
79 | Logger& operator<<(const string &s); //!< log a string | |
80 | Logger& operator<<(int); //!< log an int | |
81 | Logger& operator<<(double); //!< log a double | |
82 | Logger& operator<<(unsigned int); //!< log an unsigned int | |
83 | Logger& operator<<(long); //!< log an unsigned int | |
84 | Logger& operator<<(unsigned long); //!< log an unsigned int | |
85 | Logger& operator<<(unsigned long long); //!< log an unsigned 64 bit int | |
86 | Logger& operator<<(const DNSName&); | |
87 | Logger& operator<<(const ComboAddress&); //!< log an address | |
88 | Logger& operator<<(Urgency); //!< set the urgency, << style | |
89 | ||
90 | Logger& operator<<(std::ostream & (&)(std::ostream &)); //!< this is to recognise the endl, and to commit the log | |
91 | ||
92 | private: | |
93 | struct PerThread | |
94 | { | |
95 | PerThread() | |
96 | { | |
97 | d_urgency=Info; | |
98 | } | |
99 | string d_output; | |
100 | Urgency d_urgency; | |
101 | }; | |
102 | static void initKey(); | |
103 | static void perThreadDestructor(void *); | |
104 | PerThread* getPerThread(); | |
105 | void open(); | |
106 | ||
107 | string name; | |
108 | int flags; | |
109 | int d_facility; | |
110 | Urgency d_loglevel; | |
111 | Urgency consoleUrgency; | |
112 | bool opened; | |
113 | bool d_disableSyslog; | |
114 | bool d_timestamps{true}; | |
115 | static pthread_once_t s_once; | |
116 | static pthread_key_t s_loggerKey; | |
117 | }; | |
118 | ||
119 | extern Logger &theL(const string &pname=""); | |
120 | ||
121 | #ifdef VERBOSELOG | |
122 | #define DLOG(x) x | |
123 | #else | |
124 | #define DLOG(x) ((void)0) | |
125 | #endif |