Ring Daemon
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include "jami/def.h"
20
21// #define __STDC_FORMAT_MACROS 1
22#include <fmt/core.h>
23#include <fmt/format.h>
24#include <fmt/chrono.h>
25#include <fmt/compile.h>
26#include <fmt/printf.h>
27#if __has_include(<fmt/std.h>)
28#include <fmt/std.h>
29#else
30#include <fmt/ostream.h>
31#endif
32
33#include <opendht/logger.h>
34#include <cstdarg>
35
36#include <sstream>
37#include <string>
38
39#ifdef __ANDROID__
40
41#include <android/log.h>
42#define LOG_ERR ANDROID_LOG_ERROR
43#define LOG_WARNING ANDROID_LOG_WARN
44#define LOG_INFO ANDROID_LOG_INFO
45#define LOG_DEBUG ANDROID_LOG_DEBUG
46
47#elif defined(_WIN32)
48
49#include "winsyslog.h"
50#define LOG_ERR EVENTLOG_ERROR_TYPE
51#define LOG_WARNING EVENTLOG_WARNING_TYPE
52#define LOG_INFO EVENTLOG_INFORMATION_TYPE
53#define LOG_DEBUG EVENTLOG_SUCCESS
54
55#else
56
57#include <syslog.h> // Defines LOG_XXXX
58
59#endif /* __ANDROID__ / _WIN32 */
60
61#if defined(_WIN32) && !defined(_MSC_VER)
62#define PRINTF_ATTRIBUTE(a, b) __attribute__((format(gnu_printf, a, b)))
63#elif defined(__GNUC__)
64#define PRINTF_ATTRIBUTE(a, b) __attribute__((format(printf, a, b)))
65#else
66#define PRINTF_ATTRIBUTE(a, b)
67#endif
68
69namespace jami {
70
74void strErr();
75
79class Logger
80{
81public:
82 class Handler;
83 struct Msg;
84
85 Logger(int level, const char* file, unsigned line, bool linefeed)
86 : level_ {level}
87 , file_ {file}
88 , line_ {line}
89 , linefeed_ {linefeed}
90 {}
91
92 Logger() = delete;
93 Logger(const Logger&) = delete;
95
96 ~Logger() { log(level_, file_, line_, linefeed_, "%s", os_.str().c_str()); }
97
98 template<typename T>
99 inline Logger& operator<<(const T& value)
100 {
101 os_ << value;
102 return *this;
103 }
104
105 constexpr static int dhtLevel(dht::log::LogLevel level)
106 {
107 switch (level) {
108 case dht::log::LogLevel::debug:
109 return LOG_DEBUG;
110 case dht::log::LogLevel::warning:
111 return LOG_WARNING;
112 case dht::log::LogLevel::error:
113 default:
114 return LOG_ERR;
115 }
116 }
117
119 static void write(
120 int level, std::string_view file, unsigned line, bool linefeed, std::string_view tag, std::string&& message);
121
122 static inline void writeDht(dht::log::source_loc loc,
123 dht::log::LogLevel level,
124 std::string_view tag,
125 std::string&& message)
126 {
127 write(dhtLevel(level), loc.file, loc.line, true, tag, std::move(message));
128 }
129 static inline std::shared_ptr<dht::log::Logger> dhtLogger(const std::string& tag = {})
130 {
131 return std::make_shared<dht::Logger>(&Logger::writeDht, tag);
132 }
133
140 static void log(int level, const char* file, unsigned line, bool linefeed, const char* const fmt, ...)
141 PRINTF_ATTRIBUTE(5, 6);
142
147 static void vlog(int level, const char* file, unsigned line, bool linefeed, const char* fmt, va_list);
148
149 static void setConsoleLog(bool enable);
150 static void setSysLog(bool enable);
151 static void setMonitorLog(bool enable);
152 static void setFileLog(const std::string& path);
153
154 static void setDebugMode(bool enable);
155 static bool debugEnabled();
156
157 static void fini();
158
159 static Logger log(int level, const char* file, unsigned line, bool linefeed)
160 {
161 return {level, file, line, linefeed};
162 }
163
164private:
165 int level_;
166 const char* const file_;
167 const unsigned line_;
168 bool linefeed_ {true};
169 std::ostringstream os_;
170};
171
172namespace log {
173
174template<typename S, typename... Args>
175void
176info(const char* file, unsigned line, S&& format, Args&&... args)
177{
178 Logger::write(LOG_INFO, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
179}
180template<typename S, typename... Args>
181void
182dbg(const char* file, unsigned line, S&& format, Args&&... args)
183{
184 Logger::write(LOG_DEBUG, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
185}
186template<typename S, typename... Args>
187void
188warn(const char* file, unsigned line, S&& format, Args&&... args)
189{
190 Logger::write(LOG_WARNING, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
191}
192template<typename S, typename... Args>
193void
194error(const char* file, unsigned line, S&& format, Args&&... args)
195{
196 Logger::write(LOG_ERR, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
197}
198
199template<typename S, typename... Args>
200void
201xinfo(const char* file, unsigned line, S&& format, Args&&... args)
202{
203 Logger::write(LOG_INFO, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
204}
205template<typename S, typename... Args>
206void
207xdbg(const char* file, unsigned line, S&& format, Args&&... args)
208{
209 Logger::write(LOG_DEBUG, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
210}
211template<typename S, typename... Args>
212void
213xwarn(const char* file, unsigned line, S&& format, Args&&... args)
214{
215 Logger::write(LOG_WARNING, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
216}
217template<typename S, typename... Args>
218void
219xerror(const char* file, unsigned line, S&& format, Args&&... args)
220{
221 Logger::write(LOG_ERR, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
222}
223
224} // namespace log
225
226// We need to use macros for contextual information
227#define JAMI_INFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
228#define JAMI_DBG(...) ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__)
229#define JAMI_WARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, true, ##__VA_ARGS__)
230#define JAMI_ERR(...) ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, true, ##__VA_ARGS__)
231
232#define JAMI_XINFO(formatstr, ...) ::jami::log::xinfo(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
233#define JAMI_XDBG(formatstr, ...) ::jami::log::xdbg(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
234#define JAMI_XWARN(formatstr, ...) ::jami::log::xwarn(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
235#define JAMI_XERR(formatstr, ...) ::jami::log::xerror(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
236
237#define JAMI_LOG(formatstr, ...) ::jami::log::info(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
238#define JAMI_DEBUG(formatstr, ...) \
239 if (::jami::Logger::debugEnabled()) { \
240 ::jami::log::dbg(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__); \
241 }
242#define JAMI_WARNING(formatstr, ...) ::jami::log::warn(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
243#define JAMI_ERROR(formatstr, ...) ::jami::log::error(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
244
245} // namespace jami
Level-driven logging class that support printf and C++ stream logging fashions.
Definition logger.h:80
Logger & operator<<(const T &value)
Definition logger.h:99
Logger()=delete
static void setMonitorLog(bool enable)
Definition logger.cpp:585
static void writeDht(dht::log::source_loc loc, dht::log::LogLevel level, std::string_view tag, std::string &&message)
Definition logger.h:122
static bool debugEnabled()
Definition logger.cpp:614
Logger(const Logger &)=delete
static void setDebugMode(bool enable)
Definition logger.cpp:608
static void fini()
Definition logger.cpp:647
static constexpr int dhtLevel(dht::log::LogLevel level)
Definition logger.h:105
static LIBJAMI_PUBLIC void write(int level, std::string_view file, unsigned line, bool linefeed, std::string_view tag, std::string &&message)
Definition logger.cpp:636
static LIBJAMI_PUBLIC void static LIBJAMI_PUBLIC void vlog(int level, const char *file, unsigned line, bool linefeed, const char *fmt, va_list)
Printf fashion logging (using va_list parameters)
Definition logger.cpp:620
Logger(Logger &&) noexcept=default
static void setSysLog(bool enable)
Definition logger.cpp:579
static std::shared_ptr< dht::log::Logger > dhtLogger(const std::string &tag={})
Definition logger.h:129
static LIBJAMI_PUBLIC void log(int level, const char *file, unsigned line, bool linefeed, const char *const fmt,...) PRINTF_ATTRIBUTE(5
Printf fashion logging.
Definition logger.cpp:597
static void setFileLog(const std::string &path)
Definition logger.cpp:591
static void setConsoleLog(bool enable)
Definition logger.cpp:573
Logger(int level, const char *file, unsigned line, bool linefeed)
Definition logger.h:85
#define LIBJAMI_PUBLIC
Definition def.h:42
#define PRINTF_ATTRIBUTE(a, b)
Definition logger.h:66
void emitSignal(Args... args)
Definition jami_signal.h:64
void strErr()
Thread-safe function to print the stringified contents of errno.
Definition logger.cpp:102