LCOV - code coverage report
Current view: top level - foo/src - logger.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 39 45 86.7 %
Date: 2025-12-18 10:07:43 Functions: 562 1429 39.3 %

          Line data    Source code
       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             : 
      72             : namespace jami {
      73             : 
      74             : /**
      75             :  * Thread-safe function to print the stringified contents of errno
      76             :  */
      77             : void strErr();
      78             : 
      79             : ///
      80             : /// Level-driven logging class that support printf and C++ stream logging fashions.
      81             : ///
      82             : class Logger
      83             : {
      84             : public:
      85             :     class Handler;
      86             :     struct Msg;
      87             : 
      88         460 :     Logger(int level, const char* file, int line, bool linefeed)
      89         460 :         : level_ {level}
      90         460 :         , file_ {file}
      91         460 :         , line_ {line}
      92         460 :         , linefeed_ {linefeed}
      93         460 :     {}
      94             : 
      95             :     Logger() = delete;
      96             :     Logger(const Logger&) = delete;
      97             :     Logger(Logger&&) = default;
      98             : 
      99         460 :     ~Logger() { log(level_, file_, line_, linefeed_, "%s", os_.str().c_str()); }
     100             : 
     101             :     template<typename T>
     102        1686 :     inline Logger& operator<<(const T& value)
     103             :     {
     104        1686 :         os_ << value;
     105        1686 :         return *this;
     106             :     }
     107             : 
     108      105202 :     constexpr static int dhtLevel(dht::log::LogLevel level)
     109             :     {
     110      105202 :         switch (level) {
     111       95576 :         case dht::log::LogLevel::debug:
     112       95576 :             return LOG_DEBUG;
     113        4424 :         case dht::log::LogLevel::warning:
     114        4424 :             return LOG_WARNING;
     115        5202 :         case dht::log::LogLevel::error:
     116             :         default:
     117        5202 :             return LOG_ERR;
     118             :         }
     119             :     }
     120             : 
     121             :     LIBJAMI_PUBLIC
     122             :     static void write(int level, const char* file, int line, bool linefeed, std::string&& message);
     123             : 
     124      105210 :     static inline void writeDht(dht::log::LogLevel level, std::string&& message)
     125             :     {
     126      105210 :         write(dhtLevel(level), nullptr, 0, true, std::move(message));
     127      105239 :     }
     128        2312 :     static inline std::shared_ptr<dht::log::Logger> dhtLogger()
     129             :     {
     130        4624 :         return std::make_shared<dht::Logger>(&Logger::writeDht);
     131             :     }
     132             : 
     133             :     ///
     134             :     /// Printf fashion logging.
     135             :     ///
     136             :     /// Example: JAMI_DBG("%s", "Hello, World!")
     137             :     ///
     138             :     LIBJAMI_PUBLIC
     139             :     static void log(int level, const char* file, int line, bool linefeed, const char* const fmt, ...)
     140             :         PRINTF_ATTRIBUTE(5, 6);
     141             : 
     142             :     ///
     143             :     /// Printf fashion logging (using va_list parameters)
     144             :     ///
     145             :     LIBJAMI_PUBLIC
     146             :     static void vlog(int level, const char* file, int line, bool linefeed, const char* fmt, va_list);
     147             : 
     148             :     static void setConsoleLog(bool enable);
     149             :     static void setSysLog(bool enable);
     150             :     static void setMonitorLog(bool enable);
     151             :     static void setFileLog(const std::string& path);
     152             : 
     153             :     static void setDebugMode(bool enable);
     154             :     static bool debugEnabled();
     155             : 
     156             :     static void fini();
     157             : 
     158             :     ///
     159             :     /// Stream fashion logging.
     160             :     ///
     161             :     /// Example: JAMI_DBG() << "Hello, World!"
     162             :     ///
     163         460 :     static Logger log(int level, const char* file, int line, bool linefeed) { return {level, file, line, linefeed}; }
     164             : 
     165             : private:
     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_ {true};   ///< true if a '\n' (or any platform equivalent) has to be put at line end in consoleMode
     170             :     std::ostringstream os_;  ///< string stream used with C++ stream style (stream operator<<)
     171             : };
     172             : 
     173             : namespace log {
     174             : 
     175             : template<typename S, typename... Args>
     176             : void
     177       68758 : info(const char* file, int line, S&& format, Args&&... args)
     178             : {
     179      137513 :     Logger::write(LOG_INFO, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     180       68761 : }
     181             : template<typename S, typename... Args>
     182             : void
     183       67701 : dbg(const char* file, int line, S&& format, Args&&... args)
     184             : {
     185      135419 :     Logger::write(LOG_DEBUG, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     186       67725 : }
     187             : template<typename S, typename... Args>
     188             : void
     189       23668 : warn(const char* file, int line, S&& format, Args&&... args)
     190             : {
     191       47335 :     Logger::write(LOG_WARNING, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     192       23668 : }
     193             : template<typename S, typename... Args>
     194             : void
     195         872 : error(const char* file, int line, S&& format, Args&&... args)
     196             : {
     197        1744 :     Logger::write(LOG_ERR, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     198         872 : }
     199             : 
     200             : template<typename S, typename... Args>
     201             : void
     202             : xinfo(const char* file, int line, S&& format, Args&&... args)
     203             : {
     204             :     Logger::write(LOG_INFO, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     205             : }
     206             : template<typename S, typename... Args>
     207             : void
     208           4 : xdbg(const char* file, int line, S&& format, Args&&... args)
     209             : {
     210           8 :     Logger::write(LOG_DEBUG, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     211           4 : }
     212             : template<typename S, typename... Args>
     213             : void
     214           0 : xwarn(const char* file, int line, S&& format, Args&&... args)
     215             : {
     216           0 :     Logger::write(LOG_WARNING, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     217           0 : }
     218             : template<typename S, typename... Args>
     219             : void
     220           0 : xerror(const char* file, int line, S&& format, Args&&... args)
     221             : {
     222           0 :     Logger::write(LOG_ERR, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     223           0 : }
     224             : 
     225             : } // namespace log
     226             : 
     227             : // We need to use macros for contextual information
     228             : #define JAMI_INFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
     229             : #define JAMI_DBG(...)  ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__)
     230             : #define JAMI_WARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, true, ##__VA_ARGS__)
     231             : #define JAMI_ERR(...)  ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, true, ##__VA_ARGS__)
     232             : 
     233             : #define JAMI_XINFO(formatstr, ...) ::jami::log::xinfo(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     234             : #define JAMI_XDBG(formatstr, ...)  ::jami::log::xdbg(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     235             : #define JAMI_XWARN(formatstr, ...) ::jami::log::xwarn(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     236             : #define JAMI_XERR(formatstr, ...)  ::jami::log::xerror(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     237             : 
     238             : #define JAMI_LOG(formatstr, ...) ::jami::log::info(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
     239             : #define JAMI_DEBUG(formatstr, ...) \
     240             :     if (::jami::Logger::debugEnabled()) { \
     241             :         ::jami::log::dbg(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__); \
     242             :     }
     243             : #define JAMI_WARNING(formatstr, ...) ::jami::log::warn(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
     244             : #define JAMI_ERROR(formatstr, ...)   ::jami::log::error(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
     245             : 
     246             : } // namespace jami

Generated by: LCOV version 1.14