Line data Source code
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 :
69 : namespace jami {
70 :
71 : /**
72 : * Thread-safe function to print the stringified contents of errno
73 : */
74 : void strErr();
75 :
76 : namespace Logger {
77 :
78 : constexpr int
79 86095 : dhtLevel(dht::log::LogLevel level)
80 : {
81 86095 : switch (level) {
82 77541 : case dht::log::LogLevel::debug:
83 77541 : return LOG_DEBUG;
84 3417 : case dht::log::LogLevel::warning:
85 3417 : return LOG_WARNING;
86 5137 : case dht::log::LogLevel::error:
87 : default:
88 5137 : return LOG_ERR;
89 : }
90 : }
91 :
92 : LIBJAMI_PUBLIC
93 : void write(int level, std::string_view file, unsigned line, bool linefeed, std::string_view tag, std::string&& message);
94 :
95 : inline void
96 86110 : writeDht(dht::log::source_loc loc, dht::log::LogLevel level, std::string_view tag, std::string&& message)
97 : {
98 86110 : write(dhtLevel(level), loc.file, loc.line, true, tag, std::move(message));
99 86128 : }
100 :
101 : inline std::shared_ptr<dht::log::Logger>
102 1645 : dhtLogger(const std::string& tag = {})
103 : {
104 3290 : return std::make_shared<dht::Logger>(&Logger::writeDht, tag);
105 : }
106 :
107 : void setConsoleLog(bool enable);
108 : void setSysLog(bool enable);
109 : void setMonitorLog(bool enable);
110 : void setFileLog(const std::string& path);
111 :
112 : void setDebugMode(bool enable);
113 : bool debugEnabled();
114 :
115 : void fini();
116 : } // namespace Logger
117 :
118 : namespace log {
119 :
120 : template<typename S, typename... Args>
121 : void
122 97600 : info(const char* file, unsigned line, S&& format, Args&&... args)
123 : {
124 195201 : Logger::write(LOG_INFO, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
125 97606 : }
126 : template<typename S, typename... Args>
127 : void
128 88666 : dbg(const char* file, unsigned line, S&& format, Args&&... args)
129 : {
130 177340 : Logger::write(LOG_DEBUG, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
131 88677 : }
132 : template<typename S, typename... Args>
133 : void
134 31187 : warn(const char* file, unsigned line, S&& format, Args&&... args)
135 : {
136 62373 : Logger::write(LOG_WARNING, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
137 31187 : }
138 : template<typename S, typename... Args>
139 : void
140 1083 : error(const char* file, unsigned line, S&& format, Args&&... args)
141 : {
142 2166 : Logger::write(LOG_ERR, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
143 1083 : }
144 :
145 : template<typename S, typename... Args>
146 : void
147 : xinfo(const char* file, unsigned line, S&& format, Args&&... args)
148 : {
149 : Logger::write(LOG_INFO, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
150 : }
151 : template<typename S, typename... Args>
152 : void
153 3 : xdbg(const char* file, unsigned line, S&& format, Args&&... args)
154 : {
155 6 : Logger::write(LOG_DEBUG, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
156 3 : }
157 : template<typename S, typename... Args>
158 : void
159 0 : xwarn(const char* file, unsigned line, S&& format, Args&&... args)
160 : {
161 0 : Logger::write(LOG_WARNING, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
162 0 : }
163 : template<typename S, typename... Args>
164 : void
165 0 : xerror(const char* file, unsigned line, S&& format, Args&&... args)
166 : {
167 0 : Logger::write(LOG_ERR, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
168 0 : }
169 :
170 : } // namespace log
171 :
172 : // We need to use macros for contextual information
173 : #define JAMI_XINFO(formatstr, ...) ::jami::log::xinfo(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
174 : #define JAMI_XDBG(formatstr, ...) ::jami::log::xdbg(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
175 : #define JAMI_XWARN(formatstr, ...) ::jami::log::xwarn(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
176 : #define JAMI_XERR(formatstr, ...) ::jami::log::xerror(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
177 :
178 : #define JAMI_LOG(formatstr, ...) ::jami::log::info(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
179 : #define JAMI_DEBUG(formatstr, ...) \
180 : if (::jami::Logger::debugEnabled()) { \
181 : ::jami::log::dbg(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__); \
182 : }
183 : #define JAMI_WARNING(formatstr, ...) ::jami::log::warn(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
184 : #define JAMI_ERROR(formatstr, ...) ::jami::log::error(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
185 :
186 : } // namespace jami
|