Line data Source code
1 : /*
2 : * Copyright (C) 2004-2024 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/printf.h>
26 : #if __has_include(<fmt/std.h>)
27 : #include <fmt/std.h>
28 : #else
29 : #include <fmt/ostream.h>
30 : #endif
31 : #include <opendht/logger.h>
32 : #include <cinttypes> // for PRIx64
33 : #include <cstdarg>
34 :
35 : #include <atomic>
36 : #include <sstream>
37 : #include <string>
38 : #include "string_utils.h" // to_string
39 :
40 : #ifdef __ANDROID__
41 :
42 : #include <android/log.h>
43 : #define LOG_ERR ANDROID_LOG_ERROR
44 : #define LOG_WARNING ANDROID_LOG_WARN
45 : #define LOG_INFO ANDROID_LOG_INFO
46 : #define LOG_DEBUG ANDROID_LOG_DEBUG
47 :
48 : #elif defined(_WIN32)
49 :
50 : #include "winsyslog.h"
51 : #define LOG_ERR EVENTLOG_ERROR_TYPE
52 : #define LOG_WARNING EVENTLOG_WARNING_TYPE
53 : #define LOG_INFO EVENTLOG_INFORMATION_TYPE
54 : #define LOG_DEBUG EVENTLOG_SUCCESS
55 :
56 : #else
57 :
58 : #include <syslog.h> // Defines LOG_XXXX
59 :
60 : #endif /* __ANDROID__ / _WIN32 */
61 :
62 : #if defined(_WIN32) && !defined(_MSC_VER)
63 : #define PRINTF_ATTRIBUTE(a, b) __attribute__((format(gnu_printf, a, b)))
64 : #elif defined(__GNUC__)
65 : #define PRINTF_ATTRIBUTE(a, b) __attribute__((format(printf, a, b)))
66 : #else
67 : #define PRINTF_ATTRIBUTE(a, b)
68 : #endif
69 :
70 : namespace jami {
71 :
72 : /**
73 : * Thread-safe function to print the stringified contents of errno
74 : */
75 : void strErr();
76 :
77 : ///
78 : /// Level-driven logging class that support printf and C++ stream logging fashions.
79 : ///
80 : class Logger
81 : {
82 : public:
83 :
84 : class Handler;
85 : struct Msg;
86 :
87 654 : Logger(int level, const char* file, int line, bool linefeed)
88 654 : : level_ {level}
89 654 : , file_ {file}
90 654 : , line_ {line}
91 654 : , linefeed_ {linefeed}
92 654 : {}
93 :
94 : Logger() = delete;
95 : Logger(const Logger&) = delete;
96 : Logger(Logger&&) = default;
97 :
98 654 : ~Logger() { log(level_, file_, line_, linefeed_, "%s", os_.str().c_str()); }
99 :
100 : template<typename T>
101 2180 : inline Logger& operator<<(const T& value)
102 : {
103 2180 : os_ << value;
104 2180 : return *this;
105 : }
106 :
107 105565 : constexpr static int dhtLevel(dht::log::LogLevel level) {
108 105565 : switch (level) {
109 96034 : case dht::log::LogLevel::debug:
110 96034 : return LOG_DEBUG;
111 4324 : case dht::log::LogLevel::warning:
112 4324 : return LOG_WARNING;
113 5207 : case dht::log::LogLevel::error:
114 : default:
115 5207 : return LOG_ERR;
116 : }
117 : }
118 :
119 : LIBJAMI_PUBLIC
120 : static void write(int level, const char* file, int line, std::string&& message);
121 :
122 105566 : static inline void writeDht(dht::log::LogLevel level, std::string&& message) {
123 105566 : write(dhtLevel(level), nullptr, 0, std::move(message));
124 105597 : }
125 2336 : static inline std::shared_ptr<dht::log::Logger> dhtLogger() {
126 4672 : return std::make_shared<dht::Logger>(&Logger::writeDht);
127 : }
128 :
129 : ///
130 : /// Printf fashion logging.
131 : ///
132 : /// Example: JAMI_DBG("%s", "Hello, World!")
133 : ///
134 : LIBJAMI_PUBLIC
135 : static void log(int level, const char* file, int line, bool linefeed, const char* const fmt, ...)
136 : PRINTF_ATTRIBUTE(5, 6);
137 :
138 : ///
139 : /// Printf fashion logging (using va_list parameters)
140 : ///
141 : LIBJAMI_PUBLIC
142 : static void vlog(int level, const char* file, int line, bool linefeed, const char* fmt, va_list);
143 :
144 : static void setConsoleLog(bool enable);
145 : static void setSysLog(bool enable);
146 : static void setMonitorLog(bool enable);
147 : static void setFileLog(const std::string& path);
148 :
149 : static void setDebugMode(bool enable);
150 : static bool debugEnabled();
151 :
152 : static void fini();
153 :
154 : ///
155 : /// Stream fashion logging.
156 : ///
157 : /// Example: JAMI_DBG() << "Hello, World!"
158 : ///
159 654 : static Logger log(int level, const char* file, int line, bool linefeed)
160 : {
161 654 : return {level, file, line, linefeed};
162 : }
163 :
164 : private:
165 :
166 : int level_; ///< LOG_XXXX values
167 : const char* const file_; ///< contextual filename (printed as header)
168 : const int line_; ///< contextual line number (printed as header)
169 : bool linefeed_ {
170 : true}; ///< true if a '\n' (or any platform equivalent) has to be put at line end in consoleMode
171 : std::ostringstream os_; ///< string stream used with C++ stream style (stream operator<<)
172 : };
173 :
174 : namespace log {
175 :
176 : template<typename S, typename... Args>
177 41782 : void info(const char* file, int line, S&& format, Args&&... args) {
178 83564 : Logger::write(LOG_INFO, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
179 41782 : }
180 :
181 : template<typename S, typename... Args>
182 83185 : void dbg(const char* file, int line, S&& format, Args&&... args) {
183 166371 : Logger::write(LOG_DEBUG, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
184 83191 : }
185 :
186 : template<typename S, typename... Args>
187 25046 : void warn(const char* file, int line, S&& format, Args&&... args) {
188 50089 : Logger::write(LOG_WARNING, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
189 25049 : }
190 :
191 : template<typename S, typename... Args>
192 146 : void error(const char* file, int line, S&& format, Args&&... args) {
193 292 : Logger::write(LOG_ERR, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
194 146 : }
195 :
196 : }
197 :
198 : // We need to use macros for contextual information
199 : #define JAMI_INFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
200 : #define JAMI_DBG(...) ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__)
201 : #define JAMI_WARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, true, ##__VA_ARGS__)
202 : #define JAMI_ERR(...) ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, true, ##__VA_ARGS__)
203 :
204 : #define JAMI_XINFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, false, ##__VA_ARGS__)
205 : #define JAMI_XDBG(...) ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, false, ##__VA_ARGS__)
206 : #define JAMI_XWARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, false, ##__VA_ARGS__)
207 : #define JAMI_XERR(...) ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, false, ##__VA_ARGS__)
208 :
209 : #define JAMI_LOG(formatstr, ...) ::jami::log::info(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
210 : #define JAMI_DEBUG(formatstr, ...) if(::jami::Logger::debugEnabled()) { ::jami::log::dbg(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__); }
211 : #define JAMI_WARNING(formatstr, ...) ::jami::log::warn(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
212 : #define JAMI_ERROR(formatstr, ...) ::jami::log::error(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
213 :
214 : } // namespace jami
|