Line data Source code
1 : /*
2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc.
3 : *
4 : * Author: Julien Bonjean <julien.bonjean@savoirfairelinux.com>
5 : * Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program; if not, write to the Free Software
19 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 : */
21 :
22 : #pragma once
23 :
24 : #include "jami/def.h"
25 :
26 : //#define __STDC_FORMAT_MACROS 1
27 : #include <fmt/core.h>
28 : #include <fmt/format.h>
29 : #include <fmt/chrono.h>
30 : #include <fmt/printf.h>
31 : #if __has_include(<fmt/std.h>)
32 : #include <fmt/std.h>
33 : #else
34 : #include <fmt/ostream.h>
35 : #endif
36 : #include <opendht/logger.h>
37 : #include <cinttypes> // for PRIx64
38 : #include <cstdarg>
39 :
40 : #include <atomic>
41 : #include <sstream>
42 : #include <string>
43 : #include "string_utils.h" // to_string
44 :
45 : #ifdef __ANDROID__
46 :
47 : #include <android/log.h>
48 : #define LOG_ERR ANDROID_LOG_ERROR
49 : #define LOG_WARNING ANDROID_LOG_WARN
50 : #define LOG_INFO ANDROID_LOG_INFO
51 : #define LOG_DEBUG ANDROID_LOG_DEBUG
52 :
53 : #elif defined(_WIN32)
54 :
55 : #include "winsyslog.h"
56 : #define LOG_ERR EVENTLOG_ERROR_TYPE
57 : #define LOG_WARNING EVENTLOG_WARNING_TYPE
58 : #define LOG_INFO EVENTLOG_INFORMATION_TYPE
59 : #define LOG_DEBUG EVENTLOG_SUCCESS
60 :
61 : #else
62 :
63 : #include <syslog.h> // Defines LOG_XXXX
64 :
65 : #endif /* __ANDROID__ / _WIN32 */
66 :
67 : #if defined(_WIN32) && !defined(_MSC_VER)
68 : #define PRINTF_ATTRIBUTE(a, b) __attribute__((format(gnu_printf, a, b)))
69 : #elif defined(__GNUC__)
70 : #define PRINTF_ATTRIBUTE(a, b) __attribute__((format(printf, a, b)))
71 : #else
72 : #define PRINTF_ATTRIBUTE(a, b)
73 : #endif
74 :
75 : namespace jami {
76 :
77 : /**
78 : * Thread-safe function to print the stringified contents of errno
79 : */
80 : void strErr();
81 :
82 : ///
83 : /// Level-driven logging class that support printf and C++ stream logging fashions.
84 : ///
85 : class Logger
86 : {
87 : public:
88 :
89 : class Handler;
90 : struct Msg;
91 :
92 36895 : Logger(int level, const char* file, int line, bool linefeed)
93 36895 : : level_ {level}
94 36895 : , file_ {file}
95 36895 : , line_ {line}
96 36895 : , linefeed_ {linefeed}
97 36896 : {}
98 :
99 : Logger() = delete;
100 : Logger(const Logger&) = delete;
101 : Logger(Logger&&) = default;
102 :
103 36896 : ~Logger() { log(level_, file_, line_, linefeed_, "%s", os_.str().c_str()); }
104 :
105 : template<typename T>
106 121159 : inline Logger& operator<<(const T& value)
107 : {
108 121159 : os_ << value;
109 121159 : return *this;
110 : }
111 :
112 100856 : constexpr static int dhtLevel(dht::log::LogLevel level) {
113 100856 : switch (level) {
114 97392 : case dht::log::LogLevel::debug:
115 97392 : return LOG_DEBUG;
116 2136 : case dht::log::LogLevel::warning:
117 2136 : return LOG_WARNING;
118 1328 : case dht::log::LogLevel::error:
119 : default:
120 1328 : return LOG_ERR;
121 : }
122 : }
123 :
124 : LIBJAMI_PUBLIC
125 : static void write(int level, const char* file, int line, std::string&& message);
126 :
127 100865 : static inline void writeDht(dht::log::LogLevel level, std::string&& message) {
128 100865 : write(dhtLevel(level), nullptr, 0, std::move(message));
129 100888 : }
130 2311 : static inline std::shared_ptr<dht::log::Logger> dhtLogger() {
131 4622 : return std::make_shared<dht::Logger>(&Logger::writeDht);
132 : }
133 :
134 : ///
135 : /// Printf fashion logging.
136 : ///
137 : /// Example: JAMI_DBG("%s", "Hello, World!")
138 : ///
139 : LIBJAMI_PUBLIC
140 : static void log(int level, const char* file, int line, bool linefeed, const char* const fmt, ...)
141 : PRINTF_ATTRIBUTE(5, 6);
142 :
143 : ///
144 : /// Printf fashion logging (using va_list parameters)
145 : ///
146 : LIBJAMI_PUBLIC
147 : static void vlog(int level, const char* file, int 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 : ///
160 : /// Stream fashion logging.
161 : ///
162 : /// Example: JAMI_DBG() << "Hello, World!"
163 : ///
164 36895 : static Logger log(int level, const char* file, int line, bool linefeed)
165 : {
166 36895 : return {level, file, line, linefeed};
167 : }
168 :
169 : private:
170 :
171 : int level_; ///< LOG_XXXX values
172 : const char* const file_; ///< contextual filename (printed as header)
173 : const int line_; ///< contextual line number (printed as header)
174 : bool linefeed_ {
175 : true}; ///< true if a '\n' (or any platform equivalent) has to be put at line end in consoleMode
176 : std::ostringstream os_; ///< string stream used with C++ stream style (stream operator<<)
177 : };
178 :
179 : namespace log {
180 :
181 : template<typename S, typename... Args>
182 45082 : void info(const char* file, int line, S&& format, Args&&... args) {
183 90163 : Logger::write(LOG_INFO, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
184 45082 : }
185 :
186 : template<typename S, typename... Args>
187 202149 : void dbg(const char* file, int line, S&& format, Args&&... args) {
188 404315 : Logger::write(LOG_DEBUG, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
189 202181 : }
190 :
191 : template<typename S, typename... Args>
192 23554 : void warn(const char* file, int line, S&& format, Args&&... args) {
193 47108 : Logger::write(LOG_WARNING, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
194 23555 : }
195 :
196 : template<typename S, typename... Args>
197 150 : void error(const char* file, int line, S&& format, Args&&... args) {
198 300 : Logger::write(LOG_ERR, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
199 150 : }
200 :
201 : }
202 :
203 : // We need to use macros for contextual information
204 : #define JAMI_INFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
205 : #define JAMI_DBG(...) ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__)
206 : #define JAMI_WARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, true, ##__VA_ARGS__)
207 : #define JAMI_ERR(...) ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, true, ##__VA_ARGS__)
208 :
209 : #define JAMI_XINFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, false, ##__VA_ARGS__)
210 : #define JAMI_XDBG(...) ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, false, ##__VA_ARGS__)
211 : #define JAMI_XWARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, false, ##__VA_ARGS__)
212 : #define JAMI_XERR(...) ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, false, ##__VA_ARGS__)
213 :
214 : #define JAMI_LOG(formatstr, ...) ::jami::log::info(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
215 : #define JAMI_DEBUG(formatstr, ...) if(::jami::Logger::debugEnabled()) { ::jami::log::dbg(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__); }
216 : #define JAMI_WARNING(formatstr, ...) ::jami::log::warn(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
217 : #define JAMI_ERROR(formatstr, ...) ::jami::log::error(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
218 :
219 : } // namespace jami
|