spdlog/include/spitlog/logger.h

104 lines
3.1 KiB
C
Raw Normal View History

2014-03-22 20:11:17 +08:00
#pragma once
// Thread safe logger
2014-10-11 02:17:26 +08:00
// Has name, log level, vector of std::shared sink pointers and formatter
// Upon each log write the logger:
// 1. Checks if its log level is enough to log the message
// 2. Format the message using the formatter function
// 3. Pass the formatted message to its sinks to performa the actual logging
2014-10-11 02:17:26 +08:00
2014-03-22 20:11:17 +08:00
#include<vector>
#include<memory>
#include<atomic>
2014-05-08 07:23:07 +08:00
#include <sstream>
2014-10-25 05:32:56 +08:00
#include <exception>
2014-03-22 20:11:17 +08:00
#include "sinks/base_sink.h"
2014-10-10 08:36:50 +08:00
#include "common.h"
2014-10-24 23:01:11 +08:00
namespace spitlog
2014-03-22 20:11:17 +08:00
{
namespace details
{
class line_logger;
}
class logger
{
public:
logger(const std::string& name, sinks_init_list);
template<class It>
2014-10-14 13:00:39 +08:00
logger(const std::string& name, const It& begin, const It& end);
2014-10-25 05:44:02 +08:00
void set_format(const std::string&);
void set_formatter(formatter_ptr);
formatter_ptr get_formatter() const;
2014-03-22 20:11:17 +08:00
logger(const logger&) = delete;
logger& operator=(const logger&) = delete;
2014-10-24 23:01:11 +08:00
void set_level(level::level_enum);
2014-10-11 02:32:10 +08:00
level::level_enum level() const;
2014-10-11 02:17:26 +08:00
const std::string& name() const;
2014-10-11 02:32:10 +08:00
bool should_log(level::level_enum) const;
2014-03-31 07:29:57 +08:00
2014-10-14 09:14:35 +08:00
template <typename... Args> details::line_logger log(level::level_enum lvl, const Args&... args);
template <typename... Args> details::line_logger trace(const Args&... args);
template <typename... Args> details::line_logger debug(const Args&... args);
template <typename... Args> details::line_logger info(const Args&... args);
2014-10-14 09:14:35 +08:00
template <typename... Args> details::line_logger warn(const Args&... args);
template <typename... Args> details::line_logger error(const Args&... args);
template <typename... Args> details::line_logger critical(const Args&... args);
2014-10-24 23:01:11 +08:00
2014-03-22 20:11:17 +08:00
private:
friend details::line_logger;
2014-10-11 02:32:10 +08:00
std::string _name;
2014-03-22 20:11:17 +08:00
formatter_ptr _formatter;
2014-10-14 13:00:39 +08:00
std::vector<sink_ptr> _sinks;
2014-10-11 02:17:26 +08:00
std::atomic_int _level;
void _variadic_log(details::line_logger& l);
template <typename First, typename... Rest>
void _variadic_log(details::line_logger&l, const First& first, const Rest&... rest);
void _log_msg(details::log_msg& msg);
};
2014-10-24 23:01:11 +08:00
2014-10-25 05:32:56 +08:00
2014-10-24 23:01:11 +08:00
//
// Registry functions for easy loggers creation and retrieval
// example
// auto console_logger = spitlog::create("my_logger", spitlog::sinks<stdout_sink_mt>);
// auto same_logger = spitlog::get("my_logger");
2014-10-24 23:01:11 +08:00
// auto file_logger = c11
//
std::shared_ptr<logger> get(const std::string& name);
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
template <typename Sink, typename... Args>
std::shared_ptr<spitlog::logger> create(const std::string& logger_name, const Args&... args);
2014-10-24 23:01:11 +08:00
template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
2014-10-25 05:32:56 +08:00
2014-10-25 05:44:02 +08:00
void set_formatter(formatter_ptr f);
2014-10-25 05:32:56 +08:00
void set_format(const std::string& format_string);
2014-03-22 20:11:17 +08:00
}
//
2014-10-24 23:01:11 +08:00
// Trace & debug macros
//
#ifdef FFLOG_ENABLE_TRACE
#define FFLOG_TRACE(logger, ...) logger->log(spitlog::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__)
#else
#define FFLOG_TRACE(logger, ...) {}
#endif
#ifdef FFLOG_ENABLE_DEBUG
#define FFLOG_DEBUG(logger, ...) logger->log(spitlog::level::DEBUG, __VA_ARGS__)
#else
#define FFLOG_DEBUG(logger, ...) {}
#endif
2014-10-25 05:44:02 +08:00
#include "./details/logger_impl.h"