My Project 3.6.0
C++ Distributed Hash Table
Loading...
Searching...
No Matches
logger.h
1/*
2 * Copyright (C) 2014-2025 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#include "infohash.h"
22
23#include <fmt/format.h>
24#include <fmt/printf.h>
25
26#include <functional>
27#include <string_view>
28#include <cstdarg>
29
30namespace dht {
31namespace log {
32
33enum class LogLevel { debug, warning, error };
34
35using LogMethod = std::function<void(LogLevel, std::string&&)>;
36
37struct OPENDHT_PUBLIC Logger
38{
39 Logger() = delete;
40 Logger(LogMethod&& l)
41 : logger(std::move(l))
42 {
43 if (!logger)
44 throw std::invalid_argument {"logger and loggerf must be set"};
45 }
46 void setFilter(const InfoHash& f)
47 {
48 filter_ = f;
49 filterEnable_ = static_cast<bool>(filter_);
50 }
51 inline void log0(LogLevel level, fmt::string_view format, fmt::printf_args args) const
52 {
53 if (not filterEnable_)
54 logger(level, fmt::vsprintf(format, args));
55 }
56 inline void log1(LogLevel level, const InfoHash& f, fmt::string_view format, fmt::printf_args args) const
57 {
58 if (not filterEnable_ or f == filter_)
59 logger(level, fmt::vsprintf(format, args));
60 }
61 inline void log2(
62 LogLevel level, const InfoHash& f1, const InfoHash& f2, fmt::string_view format, fmt::printf_args args) const
63 {
64 if (not filterEnable_ or f1 == filter_ or f2 == filter_)
65 logger(level, fmt::vsprintf(format, args));
66 }
67 template<typename... Args>
68 inline void debug(fmt::format_string<Args...> format, Args&&... args) const
69 {
70 logger(LogLevel::debug, fmt::format(format, std::forward<Args>(args)...));
71 }
72 template<typename... Args>
73 inline void warn(fmt::format_string<Args...> format, Args&&... args) const
74 {
75 logger(LogLevel::warning, fmt::format(format, std::forward<Args>(args)...));
76 }
77 template<typename... Args>
78 inline void error(fmt::format_string<Args...> format, Args&&... args) const
79 {
80 logger(LogLevel::error, fmt::format(format, std::forward<Args>(args)...));
81 }
82 template<typename... T>
83 inline void d(fmt::format_string<T...> format, T&&... args) const
84 {
85 log0(LogLevel::debug, format, fmt::make_printf_args(args...));
86 }
87 template<typename... T>
88 inline void d(const InfoHash& f, fmt::format_string<T...> format, T&&... args) const
89 {
90 log1(LogLevel::debug, f, format, fmt::make_printf_args(args...));
91 }
92 template<typename... T>
93 inline void d(const InfoHash& f1, const InfoHash& f2, fmt::format_string<T...> format, T&&... args) const
94 {
95 log2(LogLevel::debug, f1, f2, format, fmt::make_printf_args(args...));
96 }
97 template<typename... T>
98 inline void w(fmt::format_string<T...> format, T&&... args) const
99 {
100 log0(LogLevel::warning, format, fmt::make_printf_args(args...));
101 }
102 template<typename... T>
103 inline void w(const InfoHash& f, fmt::format_string<T...> format, T&&... args) const
104 {
105 log1(LogLevel::warning, f, format, fmt::make_printf_args(args...));
106 }
107 template<typename... T>
108 inline void w(const InfoHash& f1, const InfoHash& f2, fmt::format_string<T...> format, T&&... args) const
109 {
110 log2(LogLevel::warning, f1, f2, format, fmt::make_printf_args(args...));
111 }
112 template<typename... T>
113 inline void e(fmt::format_string<T...> format, T&&... args) const
114 {
115 log0(LogLevel::error, format, fmt::make_printf_args(args...));
116 }
117 template<typename... T>
118 inline void e(const InfoHash& f, fmt::format_string<T...> format, T&&... args) const
119 {
120 log1(LogLevel::error, f, format, fmt::make_printf_args(args...));
121 }
122 template<typename... T>
123 inline void e(const InfoHash& f1, const InfoHash& f2, fmt::format_string<T...> format, T&&... args) const
124 {
125 log2(LogLevel::error, f1, f2, format, fmt::make_printf_args(args...));
126 }
127
128private:
129 LogMethod logger = {};
130 bool filterEnable_ {false};
131 InfoHash filter_ {};
132};
133
134} // namespace log
135using Logger = log::Logger;
136} // namespace dht