]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/logger.cc
add OpenSSL exception to PowerDNS, Netherlabs, van Dijk and Hubert copyrights
[thirdparty/pdns.git] / pdns / logger.cc
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 #include "logger.hh"
23 #include "config.h"
24
25 #ifndef RECURSOR
26 #include "statbag.hh"
27 extern StatBag S;
28 #endif
29
30 #include "namespaces.hh"
31
32 Logger &theL(const string &pname)
33 {
34 static Logger l("", LOG_DAEMON);
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 }
53 if( u <= d_loglevel ) {
54 #ifndef RECURSOR
55 S.ringAccount("logmessages",msg);
56 #endif
57 syslog(u,"%s",msg.c_str());
58 }
59 }
60
61 void Logger::setLoglevel( Urgency u )
62 {
63 d_loglevel = u;
64 }
65
66
67 void Logger::toConsole(Urgency u)
68 {
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
132 Logger& Logger::operator<<(double i)
133 {
134 ostringstream tmp;
135 tmp<<i;
136 *this<<tmp.str();
137 return *this;
138 }
139
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
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
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
171 Logger& Logger::operator<<(long i)
172 {
173 ostringstream tmp;
174 tmp<<i;
175
176 *this<<tmp.str();
177
178 return *this;
179 }
180
181 Logger& Logger::operator<<(ostream & (&)(ostream &))
182 {
183 // *this<<" ("<<(int)d_outputurgencies[pthread_self()]<<", "<<(int)consoleUrgency<<")";
184 pthread_mutex_lock(&lock);
185
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 }