]>
Commit | Line | Data |
---|---|---|
12c86877 | 1 | /* |
12471842 PL |
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 | */ | |
7abbc40f | 22 | #pragma once |
12c86877 BH |
23 | |
24 | #include <string> | |
12c86877 BH |
25 | #include <ctime> |
26 | #include <iostream> | |
27 | #include <sstream> | |
76473b92 | 28 | #include <syslog.h> |
12c86877 | 29 | |
10f4eea8 | 30 | #include "namespaces.hh" |
7abbc40f | 31 | #include "dnsname.hh" |
ded6b08d | 32 | #include "iputils.hh" |
12c86877 BH |
33 | |
34 | //! The Logger class can be used to log messages in various ways. | |
35 | class Logger | |
36 | { | |
37 | public: | |
12c86877 BH |
38 | Logger(const string &, int facility=LOG_DAEMON); //!< pass the identification you wish to appear in the log |
39 | ||
40 | //! The urgency of a log message | |
b34510a3 CH |
41 | enum Urgency {All=32767,Alert=LOG_ALERT, Critical=LOG_CRIT, Error=LOG_ERR, Warning=LOG_WARNING, |
42 | Notice=LOG_NOTICE,Info=LOG_INFO, Debug=LOG_DEBUG, None=-1}; | |
12c86877 | 43 | |
16276aa8 RK |
44 | /** Log a message. |
45 | \param msg Message you wish to log | |
46 | \param u Urgency of the message you wish to log | |
12c86877 | 47 | */ |
16276aa8 | 48 | void log(const string &msg, Urgency u=Notice); |
12c86877 BH |
49 | |
50 | void setFacility(int f){d_facility=f;open();} //!< Choose logging facility | |
51 | void setFlag(int f){flags|=f;open();} //!< set a syslog flag | |
52 | void setName(const string &); | |
53 | ||
54 | //! set lower limit of urgency needed for console display. Messages of this urgency, and higher, will be displayed | |
55 | void toConsole(Urgency); | |
eefd15f9 | 56 | void setLoglevel( Urgency ); |
12c86877 | 57 | |
b6cfa948 PL |
58 | void disableSyslog(bool d) { |
59 | d_disableSyslog = d; | |
60 | } | |
61 | ||
b18fa400 PL |
62 | void setTimestamps(bool t) { |
63 | d_timestamps = t; | |
64 | } | |
65 | ||
e908ad87 PL |
66 | void setPrefixed(bool p) { |
67 | d_prefixed = p; | |
68 | } | |
69 | ||
12c86877 BH |
70 | //! Log to a file. |
71 | void toFile( const string & filename ); | |
72 | ||
73 | void resetFlags(){flags=0;open();} //!< zero the flags | |
74 | /** Use this to stream to your log, like this: | |
75 | \code | |
e6a9dde5 PL |
76 | g_log<<"This is an informational message"<<endl; // logged at default loglevel (Info) |
77 | g_log<<Logger::Warning<<"Out of diskspace"<<endl; // Logged as a warning | |
78 | g_log<<"This is an informational message"<<endl; // logged AGAIN at default loglevel (Info) | |
12c86877 BH |
79 | \endcode |
80 | */ | |
7abbc40f | 81 | Logger& operator<<(const char *s); |
12c86877 | 82 | Logger& operator<<(const string &s); //!< log a string |
7abbc40f | 83 | Logger& operator<<(const DNSName&); |
ded6b08d | 84 | Logger& operator<<(const ComboAddress&); //!< log an address |
12c86877 BH |
85 | Logger& operator<<(Urgency); //!< set the urgency, << style |
86 | ||
3a0387f7 OM |
87 | // Using const & since otherwise SyncRes:: values induce (illegal) copies |
88 | template<typename T> Logger & operator<<(const T & i) { | |
89 | ostringstream tmp; | |
90 | tmp<<i; | |
91 | *this<<tmp.str(); | |
92 | return *this; | |
93 | } | |
94 | ||
10f4eea8 | 95 | Logger& operator<<(std::ostream & (&)(std::ostream &)); //!< this is to recognise the endl, and to commit the log |
12c86877 BH |
96 | |
97 | private: | |
d1449e4d | 98 | struct PerThread |
99 | { | |
c613b06f CHB |
100 | PerThread() : d_urgency(Info) |
101 | {} | |
d1449e4d | 102 | string d_output; |
103 | Urgency d_urgency; | |
104 | }; | |
ca07bd2c | 105 | PerThread& getPerThread(); |
12c86877 | 106 | void open(); |
c0eb8971 | 107 | |
ca07bd2c | 108 | static thread_local PerThread t_perThread; |
12c86877 BH |
109 | string name; |
110 | int flags; | |
111 | int d_facility; | |
eefd15f9 | 112 | Urgency d_loglevel; |
12c86877 | 113 | Urgency consoleUrgency; |
c0eb8971 | 114 | bool opened; |
b6cfa948 | 115 | bool d_disableSyslog; |
b18fa400 | 116 | bool d_timestamps{true}; |
e908ad87 | 117 | bool d_prefixed{false}; |
12c86877 BH |
118 | }; |
119 | ||
772dd39c RG |
120 | Logger& getLogger(); |
121 | ||
122 | #define g_log getLogger() | |
12c86877 BH |
123 | |
124 | #ifdef VERBOSELOG | |
125 | #define DLOG(x) x | |
126 | #else | |
cea46e92 | 127 | #define DLOG(x) ((void)0) |
12c86877 | 128 | #endif |