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