Ring Daemon 16.0.0
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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 <cinttypes> // for PRIx64
35#include <cstdarg>
36
37#include <atomic>
38#include <sstream>
39#include <string>
40#include "string_utils.h" // to_string
41
42#ifdef __ANDROID__
43
44#include <android/log.h>
45#define LOG_ERR ANDROID_LOG_ERROR
46#define LOG_WARNING ANDROID_LOG_WARN
47#define LOG_INFO ANDROID_LOG_INFO
48#define LOG_DEBUG ANDROID_LOG_DEBUG
49
50#elif defined(_WIN32)
51
52#include "winsyslog.h"
53#define LOG_ERR EVENTLOG_ERROR_TYPE
54#define LOG_WARNING EVENTLOG_WARNING_TYPE
55#define LOG_INFO EVENTLOG_INFORMATION_TYPE
56#define LOG_DEBUG EVENTLOG_SUCCESS
57
58#else
59
60#include <syslog.h> // Defines LOG_XXXX
61
62#endif /* __ANDROID__ / _WIN32 */
63
64#if defined(_WIN32) && !defined(_MSC_VER)
65#define PRINTF_ATTRIBUTE(a, b) __attribute__((format(gnu_printf, a, b)))
66#elif defined(__GNUC__)
67#define PRINTF_ATTRIBUTE(a, b) __attribute__((format(printf, a, b)))
68#else
69#define PRINTF_ATTRIBUTE(a, b)
70#endif
71
72namespace jami {
73
77void strErr();
78
82class Logger
83{
84public:
85
86 class Handler;
87 struct Msg;
88
89 Logger(int level, const char* file, int line, bool linefeed)
90 : level_ {level}
91 , file_ {file}
92 , line_ {line}
93 , linefeed_ {linefeed}
94 {}
95
96 Logger() = delete;
97 Logger(const Logger&) = delete;
98 Logger(Logger&&) = default;
99
100 ~Logger() { log(level_, file_, line_, linefeed_, "%s", os_.str().c_str()); }
101
102 template<typename T>
103 inline Logger& operator<<(const T& value)
104 {
105 os_ << value;
106 return *this;
107 }
108
109 constexpr static int dhtLevel(dht::log::LogLevel level) {
110 switch (level) {
111 case dht::log::LogLevel::debug:
112 return LOG_DEBUG;
113 case dht::log::LogLevel::warning:
114 return LOG_WARNING;
115 case dht::log::LogLevel::error:
116 default:
117 return LOG_ERR;
118 }
119 }
120
122 static void write(int level, const char* file, int line, bool linefeed, std::string&& message);
123
124 static inline void writeDht(dht::log::LogLevel level, std::string&& message) {
125 write(dhtLevel(level), nullptr, 0, true, std::move(message));
126 }
127 static inline std::shared_ptr<dht::log::Logger> dhtLogger() {
128 return std::make_shared<dht::Logger>(&Logger::writeDht);
129 }
130
137 static void log(int level, const char* file, int line, bool linefeed, const char* const fmt, ...)
138 PRINTF_ATTRIBUTE(5, 6);
139
144 static void vlog(int level, const char* file, int line, bool linefeed, const char* fmt, va_list);
145
146 static void setConsoleLog(bool enable);
147 static void setSysLog(bool enable);
148 static void setMonitorLog(bool enable);
149 static void setFileLog(const std::string& path);
150
151 static void setDebugMode(bool enable);
152 static bool debugEnabled();
153
154 static void fini();
155
161 static Logger log(int level, const char* file, int line, bool linefeed)
162 {
163 return {level, file, line, linefeed};
164 }
165
166private:
167
168 int level_;
169 const char* const file_;
170 const int line_;
171 bool linefeed_ {
172 true};
173 std::ostringstream os_;
174};
175
176namespace log {
177
178template<typename S, typename... Args>
179void info(const char* file, int line, S&& format, Args&&... args) {
180 Logger::write(LOG_INFO, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
181}
182template<typename S, typename... Args>
183void dbg(const char* file, int line, S&& format, Args&&... args) {
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 warn(const char* file, int line, S&& format, Args&&... args) {
188 Logger::write(LOG_WARNING, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
189}
190template<typename S, typename... Args>
191void error(const char* file, int line, S&& format, Args&&... args) {
192 Logger::write(LOG_ERR, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
193}
194
195template<typename S, typename... Args>
196void xinfo(const char* file, int line, S&& format, Args&&... args) {
197 Logger::write(LOG_INFO, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
198}
199template<typename S, typename... Args>
200void xdbg(const char* file, int line, S&& format, Args&&... args) {
201 Logger::write(LOG_DEBUG, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
202}
203template<typename S, typename... Args>
204void xwarn(const char* file, int line, S&& format, Args&&... args) {
205 Logger::write(LOG_WARNING, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
206}
207template<typename S, typename... Args>
208void xerror(const char* file, int line, S&& format, Args&&... args) {
209 Logger::write(LOG_ERR, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
210}
211
212}
213
214// We need to use macros for contextual information
215#define JAMI_INFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
216#define JAMI_DBG(...) ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__)
217#define JAMI_WARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, true, ##__VA_ARGS__)
218#define JAMI_ERR(...) ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, true, ##__VA_ARGS__)
219
220#define JAMI_XINFO(formatstr, ...) ::jami::log::xinfo(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
221#define JAMI_XDBG(formatstr, ...) ::jami::log::xdbg(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
222#define JAMI_XWARN(formatstr, ...) ::jami::log::xwarn(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
223#define JAMI_XERR(formatstr, ...) ::jami::log::xerror(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
224
225#define JAMI_LOG(formatstr, ...) ::jami::log::info(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
226#define JAMI_DEBUG(formatstr, ...) if(::jami::Logger::debugEnabled()) { ::jami::log::dbg(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__); }
227#define JAMI_WARNING(formatstr, ...) ::jami::log::warn(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
228#define JAMI_ERROR(formatstr, ...) ::jami::log::error(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
229
230} // namespace jami
Level-driven logging class that support printf and C++ stream logging fashions.
Definition logger.h:83
Logger & operator<<(const T &value)
Definition logger.h:103
Logger()=delete
static void setMonitorLog(bool enable)
Definition logger.cpp:437
static bool debugEnabled()
Definition logger.cpp:561
Logger(const Logger &)=delete
static void setDebugMode(bool enable)
Definition logger.cpp:555
static void fini()
Definition logger.cpp:600
static LIBJAMI_PUBLIC void static LIBJAMI_PUBLIC void vlog(int level, const char *file, int line, bool linefeed, const char *fmt, va_list)
Printf fashion logging (using va_list parameters)
Definition logger.cpp:567
static constexpr int dhtLevel(dht::log::LogLevel level)
Definition logger.h:109
static std::shared_ptr< dht::log::Logger > dhtLogger()
Definition logger.h:127
static LIBJAMI_PUBLIC void log(int level, const char *file, int line, bool linefeed, const char *const fmt,...) PRINTF_ATTRIBUTE(5
Printf fashion logging.
Definition logger.cpp:532
Logger(int level, const char *file, int line, bool linefeed)
Definition logger.h:89
static void setSysLog(bool enable)
Definition logger.cpp:413
static void setFileLog(const std::string &path)
Definition logger.cpp:526
Logger(Logger &&)=default
static void setConsoleLog(bool enable)
Definition logger.cpp:354
static void writeDht(dht::log::LogLevel level, std::string &&message)
Definition logger.h:124
static LIBJAMI_PUBLIC void write(int level, const char *file, int line, bool linefeed, std::string &&message)
Definition logger.cpp:588
#define LIBJAMI_PUBLIC
Definition def.h:42
#define PRINTF_ATTRIBUTE(a, b)
Definition logger.h:69
void emitSignal(Args... args)
Definition ring_signal.h:64
void strErr()
Thread-safe function to print the stringified contents of errno.
Definition logger.cpp:105