]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/logger.cc
turns out that for each signature, we consulted the database because we ignored the...
[thirdparty/pdns.git] / pdns / logger.cc
CommitLineData
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
23extern StatBag S;
24#endif
12c86877
BH
25
26using namespace std;
27
28Logger &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
36void 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
57void Logger::setLoglevel( Urgency u )
58{
fe1ce82e 59 d_loglevel = u;
eefd15f9
BH
60}
61
62
12c86877
BH
63void Logger::toConsole(Urgency u)
64{
12c86877
BH
65 consoleUrgency=u;
66}
67
68void Logger::open()
69{
70 if(opened)
71 closelog();
72 openlog(name.c_str(),flags,d_facility);
73 opened=true;
74}
75
76void Logger::setName(const string &_name)
77{
78 name=_name;
79 open();
80}
81
82Logger::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
94Logger& 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
104Logger& 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
118Logger& Logger::operator<<(int i)
119{
120 ostringstream tmp;
121 tmp<<i;
122
123 *this<<tmp.str();
124
125 return *this;
126}
127
ff181ffa
BH
128Logger& Logger::operator<<(unsigned int i)
129{
130 ostringstream tmp;
131 tmp<<i;
132
133 *this<<tmp.str();
134
135 return *this;
136}
137
96879e7d
BH
138Logger& Logger::operator<<(unsigned long i)
139{
140 ostringstream tmp;
141 tmp<<i;
142
143 *this<<tmp.str();
144
145 return *this;
146}
147
1d5b3ce6
BH
148Logger& Logger::operator<<(unsigned long long i)
149{
150 ostringstream tmp;
151 tmp<<i;
152
153 *this<<tmp.str();
154
155 return *this;
156}
157
158
ac8a2021
BH
159Logger& Logger::operator<<(long i)
160{
161 ostringstream tmp;
162 tmp<<i;
163
164 *this<<tmp.str();
165
166 return *this;
167}
ff181ffa 168
12c86877
BH
169Logger& Logger::operator<<(ostream & (&)(ostream &))
170{
171 // *this<<" ("<<(int)d_outputurgencies[pthread_self()]<<", "<<(int)consoleUrgency<<")";
172 pthread_mutex_lock(&lock);
173
12c86877
BH
174 log(d_strings[pthread_self()], d_outputurgencies[pthread_self()]);
175 d_strings.erase(pthread_self());
176 d_outputurgencies.erase(pthread_self());
177
178 pthread_mutex_unlock(&lock);
179 return *this;
180}