]>
Commit | Line | Data |
---|---|---|
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 | 8 | |
f782fe38 MH |
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 | ||
12c86877 BH |
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 | |
06bd9ccf | 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
12c86877 BH |
21 | */ |
22 | #include "logger.hh" | |
a2bfc3ff BH |
23 | #include "config.h" |
24 | ||
25 | #ifndef RECURSOR | |
12c86877 | 26 | #include "statbag.hh" |
a2bfc3ff BH |
27 | extern StatBag S; |
28 | #endif | |
12c86877 | 29 | |
10f4eea8 | 30 | #include "namespaces.hh" |
12c86877 BH |
31 | |
32 | Logger &theL(const string &pname) | |
33 | { | |
eefd15f9 | 34 | static Logger l("", LOG_DAEMON); |
12c86877 BH |
35 | if(!pname.empty()) |
36 | l.setName(pname); | |
37 | return l; | |
38 | } | |
39 | ||
40 | void Logger::log(const string &msg, Urgency u) | |
41 | { | |
42 | struct tm tm; | |
43 | time_t t; | |
44 | time(&t); | |
45 | tm=*localtime(&t); | |
46 | ||
47 | if(u<=consoleUrgency) {// Sep 14 06:52:09 | |
48 | char buffer[50]; | |
49 | strftime(buffer,sizeof(buffer),"%b %d %H:%M:%S ", &tm); | |
50 | clog<<buffer; | |
51 | clog <<msg <<endl; | |
52 | } | |
fe1ce82e | 53 | if( u <= d_loglevel ) { |
a2bfc3ff | 54 | #ifndef RECURSOR |
fe1ce82e | 55 | S.ringAccount("logmessages",msg); |
a2bfc3ff | 56 | #endif |
fe1ce82e BH |
57 | syslog(u,"%s",msg.c_str()); |
58 | } | |
12c86877 BH |
59 | } |
60 | ||
eefd15f9 BH |
61 | void Logger::setLoglevel( Urgency u ) |
62 | { | |
fe1ce82e | 63 | d_loglevel = u; |
eefd15f9 BH |
64 | } |
65 | ||
66 | ||
12c86877 BH |
67 | void Logger::toConsole(Urgency u) |
68 | { | |
12c86877 BH |
69 | consoleUrgency=u; |
70 | } | |
71 | ||
72 | void Logger::open() | |
73 | { | |
74 | if(opened) | |
75 | closelog(); | |
76 | openlog(name.c_str(),flags,d_facility); | |
77 | opened=true; | |
78 | } | |
79 | ||
80 | void Logger::setName(const string &_name) | |
81 | { | |
82 | name=_name; | |
83 | open(); | |
84 | } | |
85 | ||
86 | Logger::Logger(const string &n, int facility) | |
87 | { | |
88 | opened=false; | |
89 | flags=LOG_PID|LOG_NDELAY; | |
90 | d_facility=facility; | |
91 | consoleUrgency=Error; | |
92 | name=n; | |
93 | pthread_mutex_init(&lock,0); | |
94 | open(); | |
95 | ||
96 | } | |
97 | ||
98 | Logger& Logger::operator<<(Urgency u) | |
99 | { | |
100 | pthread_mutex_lock(&lock); | |
101 | ||
102 | d_outputurgencies[pthread_self()]=u; | |
103 | ||
104 | pthread_mutex_unlock(&lock); | |
105 | return *this; | |
106 | } | |
107 | ||
108 | Logger& Logger::operator<<(const string &s) | |
109 | { | |
110 | pthread_mutex_lock(&lock); | |
111 | ||
112 | if(!d_outputurgencies.count(pthread_self())) // default urgency | |
113 | d_outputurgencies[pthread_self()]=Info; | |
114 | ||
115 | // if(d_outputurgencies[pthread_self()]<=(unsigned int)consoleUrgency) // prevent building strings we won't ever print | |
116 | d_strings[pthread_self()].append(s); | |
117 | ||
118 | pthread_mutex_unlock(&lock); | |
119 | return *this; | |
120 | } | |
121 | ||
122 | Logger& Logger::operator<<(int i) | |
123 | { | |
124 | ostringstream tmp; | |
125 | tmp<<i; | |
126 | ||
127 | *this<<tmp.str(); | |
128 | ||
129 | return *this; | |
130 | } | |
131 | ||
f1f85f12 BH |
132 | Logger& Logger::operator<<(double i) |
133 | { | |
134 | ostringstream tmp; | |
135 | tmp<<i; | |
136 | *this<<tmp.str(); | |
137 | return *this; | |
138 | } | |
139 | ||
ff181ffa BH |
140 | Logger& Logger::operator<<(unsigned int i) |
141 | { | |
142 | ostringstream tmp; | |
143 | tmp<<i; | |
144 | ||
145 | *this<<tmp.str(); | |
146 | ||
147 | return *this; | |
148 | } | |
149 | ||
96879e7d BH |
150 | Logger& Logger::operator<<(unsigned long i) |
151 | { | |
152 | ostringstream tmp; | |
153 | tmp<<i; | |
154 | ||
155 | *this<<tmp.str(); | |
156 | ||
157 | return *this; | |
158 | } | |
159 | ||
1d5b3ce6 BH |
160 | Logger& Logger::operator<<(unsigned long long i) |
161 | { | |
162 | ostringstream tmp; | |
163 | tmp<<i; | |
164 | ||
165 | *this<<tmp.str(); | |
166 | ||
167 | return *this; | |
168 | } | |
169 | ||
170 | ||
ac8a2021 BH |
171 | Logger& Logger::operator<<(long i) |
172 | { | |
173 | ostringstream tmp; | |
174 | tmp<<i; | |
175 | ||
176 | *this<<tmp.str(); | |
177 | ||
178 | return *this; | |
179 | } | |
ff181ffa | 180 | |
12c86877 BH |
181 | Logger& Logger::operator<<(ostream & (&)(ostream &)) |
182 | { | |
183 | // *this<<" ("<<(int)d_outputurgencies[pthread_self()]<<", "<<(int)consoleUrgency<<")"; | |
184 | pthread_mutex_lock(&lock); | |
185 | ||
12c86877 BH |
186 | log(d_strings[pthread_self()], d_outputurgencies[pthread_self()]); |
187 | d_strings.erase(pthread_self()); | |
188 | d_outputurgencies.erase(pthread_self()); | |
189 | ||
190 | pthread_mutex_unlock(&lock); | |
191 | return *this; | |
192 | } |