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