C++ Distributed Hash Table
log_enable.h
1 /*
2  * Copyright (C) 2016 Savoir-faire Linux Inc.
3  * Author : Adrien BĂ©raud <adrien.beraud@savoirfairelinux.com>
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 as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "infohash.h"
26 
27 #ifndef OPENDHT_LOG
28 #define OPENDHT_LOG true
29 #endif
30 
31 namespace dht {
32 
33 // Logging related utility functions
34 
38 inline void NOLOG(char const*, va_list) {}
39 
43 struct LogMethod {
44  LogMethod() = default;
45 
46  LogMethod(LogMethod&& l) : func(std::move(l.func)) {}
47  LogMethod(const LogMethod& l) : func(l.func) {}
48 
49  LogMethod& operator=(dht::LogMethod&& l) {
50  func = std::forward<LogMethod>(l.func);
51  return *this;
52  }
53  LogMethod& operator=(const dht::LogMethod& l) {
54  func = l.func;
55  return *this;
56  }
57 
58  template<typename T>
59  explicit LogMethod(T&& t) : func(std::forward<T>(t)) {}
60 
61  template<typename T>
62  LogMethod(const T& t) : func(t) {}
63 
64  void operator()(char const* format, ...) const {
65  va_list args;
66  va_start(args, format);
67  func(format, args);
68  va_end(args);
69  }
70  void log(char const* format, va_list args) const {
71  func(format, args);
72  }
73  explicit operator bool() const {
74  return (bool)func;
75  }
76 
77  void logPrintable(const uint8_t *buf, size_t buflen) const {
78  std::string buf_clean(buflen, '\0');
79  for (size_t i=0; i<buflen; i++)
80  buf_clean[i] = isprint(buf[i]) ? buf[i] : '.';
81  (*this)("%s", buf_clean.c_str());
82  }
83 private:
84  std::function<void(char const*, va_list)> func;
85 };
86 
87 struct Logger {
88  LogMethod DBG = NOLOG;
89  LogMethod WARN = NOLOG;
90  LogMethod ERR = NOLOG;
91  void setFilter(const InfoHash& f) {
92  filter_ = f;
93  filterEnable_ = static_cast<bool>(filter_);
94  }
95  inline void log0(const LogMethod& logger, char const* format, va_list args) const {
96 #if OPENDHT_LOG
97  if (logger and not filterEnable_)
98  logger.log(format, args);
99 #endif
100  }
101  inline void log1(const LogMethod& logger, const InfoHash& f, char const* format, va_list args) const {
102 #if OPENDHT_LOG
103  if (logger and (not filterEnable_ or f == filter_))
104  logger.log(format, args);
105 #endif
106  }
107  inline void log2(const LogMethod& logger, const InfoHash& f1, const InfoHash& f2, char const* format, va_list args) const {
108 #if OPENDHT_LOG
109  if (logger and (not filterEnable_ or f1 == filter_ or f2 == filter_))
110  logger.log(format, args);
111 #endif
112  }
113  inline void d(char const* format, ...) const {
114 #if OPENDHT_LOG
115  va_list args;
116  va_start(args, format);
117  log0(DBG, format, args);
118  va_end(args);
119 #endif
120  }
121  inline void d(const InfoHash& f, char const* format, ...) const {
122 #if OPENDHT_LOG
123  va_list args;
124  va_start(args, format);
125  log1(DBG, f, format, args);
126  va_end(args);
127 #endif
128  }
129  inline void d(const InfoHash& f1, const InfoHash& f2, char const* format, ...) const {
130 #if OPENDHT_LOG
131  va_list args;
132  va_start(args, format);
133  log2(DBG, f1, f2, format, args);
134  va_end(args);
135 #endif
136  }
137  inline void w(char const* format, ...) const {
138 #if OPENDHT_LOG
139  va_list args;
140  va_start(args, format);
141  log0(WARN, format, args);
142  va_end(args);
143 #endif
144  }
145  inline void w(const InfoHash& f, char const* format, ...) const {
146 #if OPENDHT_LOG
147  va_list args;
148  va_start(args, format);
149  log1(WARN, f, format, args);
150  va_end(args);
151 #endif
152  }
153  inline void w(const InfoHash& f1, const InfoHash& f2, char const* format, ...) const {
154 #if OPENDHT_LOG
155  va_list args;
156  va_start(args, format);
157  log2(WARN, f1, f2, format, args);
158  va_end(args);
159 #endif
160  }
161  inline void e(char const* format, ...) const {
162 #if OPENDHT_LOG
163  va_list args;
164  va_start(args, format);
165  log0(ERR, format, args);
166  va_end(args);
167 #endif
168  }
169  inline void e(const InfoHash& f, char const* format, ...) const {
170 #if OPENDHT_LOG
171  va_list args;
172  va_start(args, format);
173  log1(ERR, f, format, args);
174  va_end(args);
175 #endif
176  }
177  inline void e(const InfoHash& f1, const InfoHash& f2, char const* format, ...) const {
178 #if OPENDHT_LOG
179  va_list args;
180  va_start(args, format);
181  log2(ERR, f1, f2, format, args);
182  va_end(args);
183 #endif
184  }
185 private:
186  bool filterEnable_ {false};
187  InfoHash filter_ {};
188 };
189 
190 }
void NOLOG(char const *, va_list)
Definition: log_enable.h:38
Definition: callbacks.h:34