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: 2025-08-24 09:11:10 Functions: 519 1346 38.6 %

          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             : 
      86             :     class Handler;
      87             :     struct Msg;
      88             : 
      89         650 :     Logger(int level, const char* file, int line, bool linefeed)
      90         650 :         : level_ {level}
      91         650 :         , file_ {file}
      92         650 :         , line_ {line}
      93         650 :         , linefeed_ {linefeed}
      94         650 :     {}
      95             : 
      96             :     Logger() = delete;
      97             :     Logger(const Logger&) = delete;
      98             :     Logger(Logger&&) = default;
      99             : 
     100         650 :     ~Logger() { log(level_, file_, line_, linefeed_, "%s", os_.str().c_str()); }
     101             : 
     102             :     template<typename T>
     103        2168 :     inline Logger& operator<<(const T& value)
     104             :     {
     105        2168 :         os_ << value;
     106        2168 :         return *this;
     107             :     }
     108             : 
     109       94788 :     constexpr static int dhtLevel(dht::log::LogLevel level) {
     110       94788 :         switch (level) {
     111       85746 :         case dht::log::LogLevel::debug:
     112       85746 :             return LOG_DEBUG;
     113        4073 :         case dht::log::LogLevel::warning:
     114        4073 :             return LOG_WARNING;
     115        4969 :         case dht::log::LogLevel::error:
     116             :         default:
     117        4969 :                 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       94788 :     static inline void writeDht(dht::log::LogLevel level, std::string&& message) {
     125       94788 :         write(dhtLevel(level), nullptr, 0, true, std::move(message));
     126       94788 :     }
     127        1930 :     static inline std::shared_ptr<dht::log::Logger> dhtLogger() {
     128        3860 :         return std::make_shared<dht::Logger>(&Logger::writeDht);
     129             :     }
     130             : 
     131             :     ///
     132             :     /// Printf fashion logging.
     133             :     ///
     134             :     /// Example: JAMI_DBG("%s", "Hello, World!")
     135             :     ///
     136             :     LIBJAMI_PUBLIC
     137             :     static void log(int level, const char* file, int line, bool linefeed, const char* const fmt, ...)
     138             :         PRINTF_ATTRIBUTE(5, 6);
     139             : 
     140             :     ///
     141             :     /// Printf fashion logging (using va_list parameters)
     142             :     ///
     143             :     LIBJAMI_PUBLIC
     144             :     static void vlog(int level, const char* file, int line, bool linefeed, const char* fmt, va_list);
     145             : 
     146             :     static void setConsoleLog(bool enable);
     147             :     static void setSysLog(bool enable);
     148             :     static void setMonitorLog(bool enable);
     149             :     static void setFileLog(const std::string& path);
     150             : 
     151             :     static void setDebugMode(bool enable);
     152             :     static bool debugEnabled();
     153             : 
     154             :     static void fini();
     155             : 
     156             :     ///
     157             :     /// Stream fashion logging.
     158             :     ///
     159             :     /// Example: JAMI_DBG() << "Hello, World!"
     160             :     ///
     161         650 :     static Logger log(int level, const char* file, int line, bool linefeed)
     162             :     {
     163         650 :         return {level, file, line, linefeed};
     164             :     }
     165             : 
     166             : private:
     167             : 
     168             :     int level_;              ///< LOG_XXXX values
     169             :     const char* const file_; ///< contextual filename (printed as header)
     170             :     const int line_;         ///< contextual line number (printed as header)
     171             :     bool linefeed_ {
     172             :         true}; ///< true if a '\n' (or any platform equivalent) has to be put at line end in consoleMode
     173             :     std::ostringstream os_; ///< string stream used with C++ stream style (stream operator<<)
     174             : };
     175             : 
     176             : namespace log {
     177             : 
     178             : template<typename S, typename... Args>
     179       65481 : void info(const char* file, int line, S&& format, Args&&... args) {
     180      130962 :     Logger::write(LOG_INFO, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     181       65481 : }
     182             : template<typename S, typename... Args>
     183       68084 : void dbg(const char* file, int line, S&& format, Args&&... args) {
     184      136168 :     Logger::write(LOG_DEBUG, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     185       68084 : }
     186             : template<typename S, typename... Args>
     187       15207 : void warn(const char* file, int line, S&& format, Args&&... args) {
     188       30414 :     Logger::write(LOG_WARNING, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     189       15207 : }
     190             : template<typename S, typename... Args>
     191         144 : void error(const char* file, int line, S&& format, Args&&... args) {
     192         288 :     Logger::write(LOG_ERR, file, line, true, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     193         144 : }
     194             : 
     195             : template<typename S, typename... Args>
     196             : void xinfo(const char* file, int line, S&& format, Args&&... args) {
     197             :     Logger::write(LOG_INFO, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     198             : }
     199             : template<typename S, typename... Args>
     200          26 : void xdbg(const char* file, int line, S&& format, Args&&... args) {
     201          52 :     Logger::write(LOG_DEBUG, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     202          26 : }
     203             : template<typename S, typename... Args>
     204           0 : void xwarn(const char* file, int line, S&& format, Args&&... args) {
     205           0 :     Logger::write(LOG_WARNING, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     206           0 : }
     207             : template<typename S, typename... Args>
     208           0 : void xerror(const char* file, int line, S&& format, Args&&... args) {
     209           0 :     Logger::write(LOG_ERR, file, line, false, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
     210           0 : }
     211             : 
     212             : }
     213             : 
     214             : // We need to use macros for contextual information
     215             : #define JAMI_INFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
     216             : #define JAMI_DBG(...)  ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__)
     217             : #define JAMI_WARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, true, ##__VA_ARGS__)
     218             : #define JAMI_ERR(...)  ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, true, ##__VA_ARGS__)
     219             : 
     220             : #define JAMI_XINFO(formatstr, ...) ::jami::log::xinfo(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     221             : #define JAMI_XDBG(formatstr, ...)  ::jami::log::xdbg(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     222             : #define JAMI_XWARN(formatstr, ...) ::jami::log::xwarn(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     223             : #define JAMI_XERR(formatstr, ...)  ::jami::log::xerror(__FILE__, __LINE__, FMT_COMPILE(formatstr), ##__VA_ARGS__)
     224             : 
     225             : #define JAMI_LOG(formatstr, ...) ::jami::log::info(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
     226             : #define JAMI_DEBUG(formatstr, ...) if(::jami::Logger::debugEnabled()) { ::jami::log::dbg(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__); }
     227             : #define JAMI_WARNING(formatstr, ...) ::jami::log::warn(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
     228             : #define JAMI_ERROR(formatstr, ...) ::jami::log::error(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
     229             : 
     230             : } // namespace jami

Generated by: LCOV version 1.14