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-02-28 10:41:24 Functions: 598 1737 34.4 %

          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 <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         442 :     Logger(int level, const char* file, unsigned line, bool linefeed)
      89         442 :         : level_ {level}
      90         442 :         , file_ {file}
      91         442 :         , line_ {line}
      92         442 :         , linefeed_ {linefeed}
      93         442 :     {}
      94             : 
      95             :     Logger() = delete;
      96             :     Logger(const Logger&) = delete;
      97             :     Logger(Logger&&) = default;
      98             : 
      99         442 :     ~Logger() { log(level_, file_, line_, linefeed_, "%s", os_.str().c_str()); }
     100             : 
     101             :     template<typename T>
     102        1589 :     inline Logger& operator<<(const T& value)
     103             :     {
     104        1589 :         os_ << value;
     105        1589 :         return *this;
     106             :     }
     107             : 
     108      105080 :     constexpr static int dhtLevel(dht::log::LogLevel level)
     109             :     {
     110      105080 :         switch (level) {
     111       95457 :         case dht::log::LogLevel::debug:
     112       95457 :             return LOG_DEBUG;
     113        4392 :         case dht::log::LogLevel::warning:
     114        4392 :             return LOG_WARNING;
     115        5231 :         case dht::log::LogLevel::error:
     116             :         default:
     117        5231 :             return LOG_ERR;
     118             :         }
     119             :     }
     120             : 
     121             :     LIBJAMI_PUBLIC
     122             :     static void write(
     123             :         int level, std::string_view file, unsigned line, bool linefeed, std::string_view tag, std::string&& message);
     124             : 
     125      105089 :     static inline void writeDht(dht::log::source_loc loc,
     126             :                                 dht::log::LogLevel level,
     127             :                                 std::string_view tag,
     128             :                                 std::string&& message)
     129             :     {
     130      105089 :         write(dhtLevel(level), loc.file, loc.line, true, tag, std::move(message));
     131      105108 :     }
     132        1626 :     static inline std::shared_ptr<dht::log::Logger> dhtLogger(const std::string& tag = {})
     133             :     {
     134        3252 :         return std::make_shared<dht::Logger>(&Logger::writeDht, tag);
     135             :     }
     136             : 
     137             :     ///
     138             :     /// Printf fashion logging.
     139             :     ///
     140             :     /// Example: JAMI_DBG("%s", "Hello, World!")
     141             :     ///
     142             :     LIBJAMI_PUBLIC
     143             :     static void log(int level, const char* file, unsigned line, bool linefeed, const char* const fmt, ...)
     144             :         PRINTF_ATTRIBUTE(5, 6);
     145             : 
     146             :     ///
     147             :     /// Printf fashion logging (using va_list parameters)
     148             :     ///
     149             :     LIBJAMI_PUBLIC
     150             :     static void vlog(int level, const char* file, unsigned line, bool linefeed, const char* fmt, va_list);
     151             : 
     152             :     static void setConsoleLog(bool enable);
     153             :     static void setSysLog(bool enable);
     154             :     static void setMonitorLog(bool enable);
     155             :     static void setFileLog(const std::string& path);
     156             : 
     157             :     static void setDebugMode(bool enable);
     158             :     static bool debugEnabled();
     159             : 
     160             :     static void fini();
     161             : 
     162         442 :     static Logger log(int level, const char* file, unsigned line, bool linefeed)
     163             :     {
     164         442 :         return {level, file, line, linefeed};
     165             :     }
     166             : 
     167             : private:
     168             :     int level_;              ///< LOG_XXXX values
     169             :     const char* const file_; ///< contextual filename (printed as header)
     170             :     const unsigned line_;    ///< contextual line number (printed as header)
     171             :     bool linefeed_ {true};   ///< true if a '\n' (or any platform equivalent) has to be put at line end in consoleMode
     172             :     std::ostringstream os_;  ///< string stream used with C++ stream style (stream operator<<)
     173             : };
     174             : 
     175             : namespace log {
     176             : 
     177             : template<typename S, typename... Args>
     178             : void
     179       70166 : info(const char* file, unsigned line, S&& format, Args&&... args)
     180             : {
     181      140330 :     Logger::write(LOG_INFO, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     182       70168 : }
     183             : template<typename S, typename... Args>
     184             : void
     185       77593 : dbg(const char* file, unsigned line, S&& format, Args&&... args)
     186             : {
     187      155187 :     Logger::write(LOG_DEBUG, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     188       77594 : }
     189             : template<typename S, typename... Args>
     190             : void
     191       23997 : warn(const char* file, unsigned line, S&& format, Args&&... args)
     192             : {
     193       47998 :     Logger::write(LOG_WARNING, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     194       23995 : }
     195             : template<typename S, typename... Args>
     196             : void
     197         875 : error(const char* file, unsigned line, S&& format, Args&&... args)
     198             : {
     199        1750 :     Logger::write(LOG_ERR, file, line, true, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     200         875 : }
     201             : 
     202             : template<typename S, typename... Args>
     203             : void
     204             : xinfo(const char* file, unsigned line, S&& format, Args&&... args)
     205             : {
     206             :     Logger::write(LOG_INFO, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     207             : }
     208             : template<typename S, typename... Args>
     209             : void
     210           4 : xdbg(const char* file, unsigned line, S&& format, Args&&... args)
     211             : {
     212           8 :     Logger::write(LOG_DEBUG, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     213           4 : }
     214             : template<typename S, typename... Args>
     215             : void
     216           0 : xwarn(const char* file, unsigned line, S&& format, Args&&... args)
     217             : {
     218           0 :     Logger::write(LOG_WARNING, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     219           0 : }
     220             : template<typename S, typename... Args>
     221             : void
     222           0 : xerror(const char* file, unsigned line, S&& format, Args&&... args)
     223             : {
     224           0 :     Logger::write(LOG_ERR, file, line, false, {}, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     225           0 : }
     226             : 
     227             : } // namespace log
     228             : 
     229             : // We need to use macros for contextual information
     230             : #define JAMI_INFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
     231             : #define JAMI_DBG(...)  ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__)
     232             : #define JAMI_WARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, true, ##__VA_ARGS__)
     233             : #define JAMI_ERR(...)  ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, true, ##__VA_ARGS__)
     234             : 
     235             : #define JAMI_XINFO(formatstr, ...) ::jami::log::xinfo(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     236             : #define JAMI_XDBG(formatstr, ...)  ::jami::log::xdbg(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     237             : #define JAMI_XWARN(formatstr, ...) ::jami::log::xwarn(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     238             : #define JAMI_XERR(formatstr, ...)  ::jami::log::xerror(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     239             : 
     240             : #define JAMI_LOG(formatstr, ...) ::jami::log::info(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
     241             : #define JAMI_DEBUG(formatstr, ...) \
     242             :     if (::jami::Logger::debugEnabled()) { \
     243             :         ::jami::log::dbg(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__); \
     244             :     }
     245             : #define JAMI_WARNING(formatstr, ...) ::jami::log::warn(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
     246             : #define JAMI_ERROR(formatstr, ...)   ::jami::log::error(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
     247             : 
     248             : } // namespace jami

Generated by: LCOV version 1.14