LCOV - code coverage report
Current view: top level - foo/src - logger.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 40 46 87.0 %
Date: 2026-04-01 09:29:43 Functions: 605 1704 35.5 %

          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             : ///
      77             : /// Level-driven logging class that support printf and C++ stream logging fashions.
      78             : ///
      79             : class Logger
      80             : {
      81             : public:
      82             :     class Handler;
      83             :     struct Msg;
      84             : 
      85         459 :     Logger(int level, const char* file, unsigned line, bool linefeed)
      86         459 :         : level_ {level}
      87         459 :         , file_ {file}
      88         459 :         , line_ {line}
      89         459 :         , linefeed_ {linefeed}
      90         459 :     {}
      91             : 
      92             :     Logger() = delete;
      93             :     Logger(const Logger&) = delete;
      94             :     Logger(Logger&&) noexcept = default;
      95             : 
      96         459 :     ~Logger() { log(level_, file_, line_, linefeed_, "%s", os_.str().c_str()); }
      97             : 
      98             :     template<typename T>
      99        1663 :     inline Logger& operator<<(const T& value)
     100             :     {
     101        1663 :         os_ << value;
     102        1664 :         return *this;
     103             :     }
     104             : 
     105       80299 :     constexpr static int dhtLevel(dht::log::LogLevel level)
     106             :     {
     107       80299 :         switch (level) {
     108       71459 :         case dht::log::LogLevel::debug:
     109       71459 :             return LOG_DEBUG;
     110        3487 :         case dht::log::LogLevel::warning:
     111        3487 :             return LOG_WARNING;
     112        5353 :         case dht::log::LogLevel::error:
     113             :         default:
     114        5353 :             return LOG_ERR;
     115             :         }
     116             :     }
     117             : 
     118             :     LIBJAMI_PUBLIC
     119             :     static void write(
     120             :         int level, std::string_view file, unsigned line, bool linefeed, std::string_view tag, std::string&& message);
     121             : 
     122       80302 :     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       80302 :         write(dhtLevel(level), loc.file, loc.line, true, tag, std::move(message));
     128       80311 :     }
     129        1641 :     static inline std::shared_ptr<dht::log::Logger> dhtLogger(const std::string& tag = {})
     130             :     {
     131        3282 :         return std::make_shared<dht::Logger>(&Logger::writeDht, tag);
     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, unsigned 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, 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         459 :     static Logger log(int level, const char* file, unsigned line, bool linefeed)
     160             :     {
     161         459 :         return {level, file, line, linefeed};
     162             :     }
     163             : 
     164             : private:
     165             :     int level_;              ///< LOG_XXXX values
     166             :     const char* const file_; ///< contextual filename (printed as header)
     167             :     const unsigned line_;    ///< contextual line number (printed as header)
     168             :     bool linefeed_ {true};   ///< true if a '\n' (or any platform equivalent) has to be put at line end in consoleMode
     169             :     std::ostringstream os_;  ///< string stream used with C++ stream style (stream operator<<)
     170             : };
     171             : 
     172             : namespace log {
     173             : 
     174             : template<typename S, typename... Args>
     175             : void
     176       70484 : info(const char* file, unsigned line, S&& format, Args&&... args)
     177             : {
     178      140971 :     Logger::write(LOG_INFO, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     179       70485 : }
     180             : template<typename S, typename... Args>
     181             : void
     182       77830 : dbg(const char* file, unsigned line, S&& format, Args&&... args)
     183             : {
     184      155663 :     Logger::write(LOG_DEBUG, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     185       77835 : }
     186             : template<typename S, typename... Args>
     187             : void
     188       24291 : warn(const char* file, unsigned line, S&& format, Args&&... args)
     189             : {
     190       48583 :     Logger::write(LOG_WARNING, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     191       24295 : }
     192             : template<typename S, typename... Args>
     193             : void
     194         885 : error(const char* file, unsigned line, S&& format, Args&&... args)
     195             : {
     196        1770 :     Logger::write(LOG_ERR, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     197         885 : }
     198             : 
     199             : template<typename S, typename... Args>
     200             : void
     201             : xinfo(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             : }
     205             : template<typename S, typename... Args>
     206             : void
     207           4 : xdbg(const char* file, unsigned line, S&& format, Args&&... args)
     208             : {
     209           8 :     Logger::write(LOG_DEBUG, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     210           4 : }
     211             : template<typename S, typename... Args>
     212             : void
     213           0 : xwarn(const char* file, unsigned line, S&& format, Args&&... args)
     214             : {
     215           0 :     Logger::write(LOG_WARNING, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     216           0 : }
     217             : template<typename S, typename... Args>
     218             : void
     219           0 : xerror(const char* file, unsigned line, S&& format, Args&&... args)
     220             : {
     221           0 :     Logger::write(LOG_ERR, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     222           0 : }
     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

Generated by: LCOV version 1.14