C++ Distributed Hash Table
log_enable.h
1 /*
2  * Copyright (C) 2014-2022 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 #include <cstdarg>
28 
29 #ifndef OPENDHT_LOG
30 #define OPENDHT_LOG true
31 #endif
32 
33 namespace dht {
34 
35 // Logging related utility functions
36 
40 struct LogMethod {
41  LogMethod() = default;
42 
43  LogMethod(LogMethod&& l) : func(std::move(l.func)) {}
44  LogMethod(const LogMethod& l) : func(l.func) {}
45 
46  LogMethod& operator=(dht::LogMethod&& l) {
47  func = std::forward<LogMethod>(l.func);
48  return *this;
49  }
50  LogMethod& operator=(const dht::LogMethod& l) {
51  func = l.func;
52  return *this;
53  }
54 
55  template<typename T>
56  explicit LogMethod(T&& t) : func(std::forward<T>(t)) {}
57 
58  template<typename T>
59  LogMethod(const T& t) : func(t) {}
60 
61  void operator()(char const* format, ...) const {
62  va_list args;
63  va_start(args, format);
64  func(format, args);
65  va_end(args);
66  }
67  inline void log(char const* format, va_list args) const {
68  func(format, args);
69  }
70  explicit operator bool() const {
71  return (bool)func;
72  }
73 
74  void logPrintable(const uint8_t *buf, size_t buflen) const {
75  std::string buf_clean(buflen, '\0');
76  for (size_t i=0; i<buflen; i++)
77  buf_clean[i] = isprint(buf[i]) ? buf[i] : '.';
78  (*this)("%s", buf_clean.c_str());
79  }
80 private:
81  std::function<void(char const*, va_list)> func;
82 };
83 
84 struct Logger {
85  LogMethod ERR = {};
86  LogMethod WARN = {};
87  LogMethod DBG = {};
88 
89  Logger() = default;
90  Logger(LogMethod&& err, LogMethod&& warn, LogMethod&& dbg)
91  : ERR(std::move(err)), WARN(std::move(warn)), DBG(std::move(dbg)) {}
92  void setFilter(const InfoHash& f) {
93  filter_ = f;
94  filterEnable_ = static_cast<bool>(filter_);
95  }
96  inline void log0(const LogMethod& logger, char const* format, va_list args) const {
97 #if OPENDHT_LOG
98  if (logger and not filterEnable_)
99  logger.log(format, args);
100 #endif
101  }
102  inline void log1(const LogMethod& logger, const InfoHash& f, char const* format, va_list args) const {
103 #if OPENDHT_LOG
104  if (logger and (not filterEnable_ or f == filter_))
105  logger.log(format, args);
106 #endif
107  }
108  inline void log2(const LogMethod& logger, const InfoHash& f1, const InfoHash& f2, char const* format, va_list args) const {
109 #if OPENDHT_LOG
110  if (logger and (not filterEnable_ or f1 == filter_ or f2 == filter_))
111  logger.log(format, args);
112 #endif
113  }
114  inline void d(char const* format, ...) const {
115 #if OPENDHT_LOG
116  va_list args;
117  va_start(args, format);
118  log0(DBG, format, args);
119  va_end(args);
120 #endif
121  }
122  inline void d(const InfoHash& f, char const* format, ...) const {
123 #if OPENDHT_LOG
124  va_list args;
125  va_start(args, format);
126  log1(DBG, f, format, args);
127  va_end(args);
128 #endif
129  }
130  inline void d(const InfoHash& f1, const InfoHash& f2, char const* format, ...) const {
131 #if OPENDHT_LOG
132  va_list args;
133  va_start(args, format);
134  log2(DBG, f1, f2, format, args);
135  va_end(args);
136 #endif
137  }
138  inline void w(char const* format, ...) const {
139 #if OPENDHT_LOG
140  va_list args;
141  va_start(args, format);
142  log0(WARN, format, args);
143  va_end(args);
144 #endif
145  }
146  inline void w(const InfoHash& f, char const* format, ...) const {
147 #if OPENDHT_LOG
148  va_list args;
149  va_start(args, format);
150  log1(WARN, f, format, args);
151  va_end(args);
152 #endif
153  }
154  inline void w(const InfoHash& f1, const InfoHash& f2, char const* format, ...) const {
155 #if OPENDHT_LOG
156  va_list args;
157  va_start(args, format);
158  log2(WARN, f1, f2, format, args);
159  va_end(args);
160 #endif
161  }
162  inline void e(char const* format, ...) const {
163 #if OPENDHT_LOG
164  va_list args;
165  va_start(args, format);
166  log0(ERR, format, args);
167  va_end(args);
168 #endif
169  }
170  inline void e(const InfoHash& f, char const* format, ...) const {
171 #if OPENDHT_LOG
172  va_list args;
173  va_start(args, format);
174  log1(ERR, f, format, args);
175  va_end(args);
176 #endif
177  }
178  inline void e(const InfoHash& f1, const InfoHash& f2, char const* format, ...) const {
179 #if OPENDHT_LOG
180  va_list args;
181  va_start(args, format);
182  log2(ERR, f1, f2, format, args);
183  va_end(args);
184 #endif
185  }
186 private:
187  bool filterEnable_ {false};
188  InfoHash filter_ {};
189 };
190 
191 }
Definition: callbacks.h:35