]>
Commit | Line | Data |
---|---|---|
12471842 PL |
1 | /* |
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 | */ | |
64e4ebb4 | 22 | #pragma once |
23 | #include <iostream> | |
24 | #include <sstream> | |
25 | #include <syslog.h> | |
26 | ||
27 | /* This file is intended not to be metronome specific, and is simple example of C++2011 | |
28 | variadic templates in action. | |
29 | ||
30 | The goal is rapid easy to use logging to console & syslog. | |
31 | ||
32 | Usage: | |
33 | string address="localhost"; | |
fb7f8ec3 | 34 | vinfolog("Got TCP connection from %s", remote); |
64e4ebb4 | 35 | infolog("Bound to %s port %d", address, port); |
36 | warnlog("Query took %d milliseconds", 1232.4); // yes, %d | |
37 | errlog("Unable to bind to %s: %s", ca.toStringWithPort(), strerr(errno)); | |
38 | ||
9c9b4998 | 39 | Will log to stdout. Will syslog in any case with LOG_INFO, |
fb7f8ec3 | 40 | LOG_WARNING, LOG_ERR respectively. If g_verbose=false, vinfolog is a noop. |
64e4ebb4 | 41 | More generically, dolog(someiostream, "Hello %s", stream) will log to someiostream |
42 | ||
43 | This will happily print a string to %d! Doesn't do further format processing. | |
44 | */ | |
45 | ||
46 | inline void dolog(std::ostream& os, const char*s) | |
47 | { | |
48 | os<<s; | |
49 | } | |
50 | ||
51 | template<typename T, typename... Args> | |
52 | void dolog(std::ostream& os, const char* s, T value, Args... args) | |
53 | { | |
54 | while (*s) { | |
55 | if (*s == '%') { | |
56 | if (*(s + 1) == '%') { | |
57 | ++s; | |
58 | } | |
59 | else { | |
60 | os << value; | |
61 | s += 2; | |
62 | dolog(os, s, args...); | |
63 | return; | |
64 | } | |
65 | } | |
66 | os << *s++; | |
67 | } | |
68 | } | |
69 | ||
64e4ebb4 | 70 | extern bool g_verbose; |
bbfaaa6f | 71 | extern bool g_syslog; |
64e4ebb4 | 72 | |
0ca6a67f RG |
73 | inline void setSyslogFacility(int facility) |
74 | { | |
75 | /* we always call openlog() right away at startup */ | |
76 | closelog(); | |
77 | openlog("dnsdist", LOG_PID|LOG_NDELAY, facility); | |
78 | } | |
79 | ||
64e4ebb4 | 80 | template<typename... Args> |
81 | void genlog(int level, const char* s, Args... args) | |
82 | { | |
83 | std::ostringstream str; | |
84 | dolog(str, s, args...); | |
bbfaaa6f PL |
85 | if(g_syslog) |
86 | syslog(level, "%s", str.str().c_str()); | |
9c9b4998 | 87 | std::cout<<str.str()<<std::endl; |
64e4ebb4 | 88 | } |
89 | ||
b2ad6825 | 90 | |
91 | #define vinfolog if(g_verbose)infolog | |
92 | ||
64e4ebb4 | 93 | template<typename... Args> |
94 | void infolog(const char* s, Args... args) | |
95 | { | |
fb7f8ec3 | 96 | genlog(LOG_INFO, s, args...); |
64e4ebb4 | 97 | } |
98 | ||
99 | template<typename... Args> | |
100 | void warnlog(const char* s, Args... args) | |
101 | { | |
102 | genlog(LOG_WARNING, s, args...); | |
103 | } | |
104 | ||
105 | template<typename... Args> | |
106 | void errlog(const char* s, Args... args) | |
107 | { | |
108 | genlog(LOG_ERR, s, args...); | |
109 | } | |
b2ad6825 | 110 |