|
- #pragma once
-
- #include <sstream>
- #include <cpplogging/types.h>
-
- #define cpplogging_log(p_logger, p_level) \
- if (p_logger.is_enabled(::cpplogging::log_level::p_level)) \
- p_logger.log() \
- .level(::cpplogging::log_level::p_level) \
- .file(__FILE__) \
- .line(__LINE__)
-
- namespace cpplogging
- {
-
- struct logger;
-
- namespace __impl
- {
-
- /**
- * @brief Helper class to create a log entry.
- */
- struct log_helper
- : public std::ostringstream
- {
- private:
- logger& _logger;
- log_entry_ptr_s _entry;
-
- private:
- /**
- * @brief Move constructor.
- */
- log_helper(log_helper&&) = delete;
-
- /**
- * @brief Copy constructor.
- */
- log_helper(const log_helper&) = delete;
-
- public:
- /**
- * @brief Constructor.
- *
- * @param[in] p_logger Logger this log helper belongs to.
- */
- inline log_helper(logger& p_logger);
-
- /**
- * @brief Constructor.
- *
- * @param[in] p_logger Logger this log helper belongs to.
- * @param[in] p_entry Log entry to create.
- */
- inline log_helper(logger& p_logger, const log_entry_ptr_s& p_entry);
-
- /**
- * @brief Destructor. Will finally write the log entry.
- */
- inline ~log_helper();
-
- /**
- * @brief Set the log level of the entry,
- */
- inline log_helper& level(log_level level);
-
- /**
- * @brief Set the sender of the log entry.
- */
- inline log_helper& sender(const void * sender);
-
- /**
- * @brief Set the sender and the sender type of the log entry.
- */
- template<typename T_sender>
- inline log_helper& sender(const T_sender * sender);
-
- /**
- * @brief Set the sender type of the log entry.
- */
- inline log_helper& sender_type(const std::string& sender_type);
-
- /**
- * @brief Set the filename of the log entry.
- */
- inline log_helper& file(const char * file);
-
- /**
- * @brief Set the line number of the log entry.
- */
- inline log_helper& line(int line);
-
- /**
- * @brief Set the message of the log entry.
- */
- inline log_helper& message(const std::string& msg);
-
- /**
- * @brief Add the passed string to the log message.
- */
- inline log_helper& add_message(const std::string& msg);
-
- /**
- * @brief Set the message of the entry using format string.
- */
- template<typename... T_args>
- inline log_helper& format(const std::string& fmt, T_args&&... args);
-
- /**
- * @brief Add a message to the entry using format string.
- */
- template<typename... T_args>
- inline log_helper& add_format(const std::string& fmt, T_args&&... args);
-
- private:
- /**
- * @brief Format a string.
- *
- * @tparam T_args Arguments of the format string.
- *
- * @param[in] fmt Format string.
- * @param[in] sz Size of the buffer to use for the formatting.
- * @param[in] args Arguments of the format.
- */
- template<typename... T_args>
- static inline std::string format(const std::string& fmt, size_t sz, T_args&&... args);
- };
-
- }
-
- /**
- * @brief Class to create log messages.
- */
- struct logger
- {
- public:
- friend __impl::log_helper;
-
- private:
- std::string _name; //!< name of the logger
-
- private:
- /**
- * @brief Move constructor.
- */
- logger(logger&&) = delete;
-
- /**
- * @brief Copy constructor.
- */
- logger(const logger&) = delete;
-
- public:
- /**
- * @brief Constructor.
- */
- inline logger(const std::string name);
-
- /**
- * @brief Destructor.
- */
- virtual ~logger() = default;
-
- /**
- * @brief Get the name of the logger.
- */
- inline const std::string& name() const;
-
- /**
- * @brief Check if the passed log level is enabled.
- *
- * @param[in] level Level to check.
- *
- * @retval true The passed log level is enabled.
- * @retval false The passed log level is disabled.
- */
- virtual bool is_enabled(log_level level) const = 0;
-
- /**
- * @brief Write a log entry,
- *
- * @return Log helper to create log entry.
- */
- inline __impl::log_helper log();
-
- protected:
- /**
- * @brief Write a log entry.
- *
- * @param[in] entry Log entry to write.
- */
- virtual void write_entry(const log_entry_ptr_s& entry) const = 0;
-
- public:
- /**
- * @brief Get the instance on an logger with the passed name.
- *
- * @param[in] name Name of the loggger.
- *
- * @return Instance of the requested logger.
- */
- static logger& get(const std::string& name = "");
- };
-
- }
-
- #include "logger.inl"
|