customizable type of logger name via define

pull/89/head
Denis Ivaykin 10 years ago
parent 06e0b0387a
commit e2230cec47

@ -53,20 +53,20 @@ class async_logger :public logger
{
public:
template<class It>
async_logger(const std::string& name,
async_logger(const SPDLOG_NAME_TYPE_REF name,
const It& begin,
const It& end,
size_t queue_size,
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
const std::function<void()>& worker_warmup_cb = nullptr);
async_logger(const std::string& logger_name,
async_logger(const SPDLOG_NAME_TYPE_REF logger_name,
sinks_init_list sinks,
size_t queue_size,
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
const std::function<void()>& worker_warmup_cb = nullptr);
async_logger(const std::string& logger_name,
async_logger(const SPDLOG_NAME_TYPE_REF logger_name,
sink_ptr single_sink,
size_t queue_size,
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,

@ -34,7 +34,7 @@
template<class It>
inline spdlog::async_logger::async_logger(const std::string& logger_name,
inline spdlog::async_logger::async_logger(const SPDLOG_NAME_TYPE_REF logger_name,
const It& begin,
const It& end,
size_t queue_size,
@ -45,14 +45,14 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name,
{
}
inline spdlog::async_logger::async_logger(const std::string& logger_name,
inline spdlog::async_logger::async_logger(const SPDLOG_NAME_TYPE_REF logger_name,
sinks_init_list sinks,
size_t queue_size,
const async_overflow_policy overflow_policy,
const std::function<void()>& worker_warmup_cb) :
async_logger(logger_name, sinks.begin(), sinks.end(), queue_size, overflow_policy, worker_warmup_cb) {}
inline spdlog::async_logger::async_logger(const std::string& logger_name,
inline spdlog::async_logger::async_logger(const SPDLOG_NAME_TYPE_REF logger_name,
sink_ptr single_sink,
size_t queue_size,
const async_overflow_policy overflow_policy,

@ -33,7 +33,7 @@
// create logger with given name, sinks and the default pattern formatter
// all other ctors will call this one
template<class It>
inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) :
inline spdlog::logger::logger(const SPDLOG_NAME_TYPE_REF logger_name, const It& begin, const It& end) :
_name(logger_name),
_sinks(begin, end),
_formatter(std::make_shared<pattern_formatter>("%+"))
@ -44,12 +44,12 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c
}
// ctor with sinks as init list
inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list) :
inline spdlog::logger::logger(const SPDLOG_NAME_TYPE_REF logger_name, sinks_init_list sinks_list) :
logger(logger_name, sinks_list.begin(), sinks_list.end()) {}
// ctor with single sink
inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink) :
inline spdlog::logger::logger(const SPDLOG_NAME_TYPE_REF logger_name, spdlog::sink_ptr single_sink) :
logger(logger_name, { single_sink }) {}
@ -273,7 +273,7 @@ inline spdlog::details::line_logger spdlog::logger::force_log(level::level_enum
//
// name and level
//
inline const std::string& spdlog::logger::name() const
inline const SPDLOG_NAME_TYPE_REF spdlog::logger::name() const
{
return _name;
}

@ -52,7 +52,7 @@ public:
}
std::shared_ptr<logger> get(const std::string& logger_name)
std::shared_ptr<logger> get(const SPDLOG_NAME_TYPE_REF logger_name)
{
std::lock_guard<std::mutex> lock(_mutex);
auto found = _loggers.find(logger_name);
@ -60,7 +60,7 @@ public:
}
template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
std::shared_ptr<logger> create(const SPDLOG_NAME_TYPE_REF logger_name, const It& sinks_begin, const It& sinks_end)
{
std::shared_ptr<logger> new_logger;
@ -81,7 +81,7 @@ public:
return new_logger;
}
void drop(const std::string& logger_name)
void drop(const SPDLOG_NAME_TYPE_REF logger_name)
{
std::lock_guard<std::mutex> lock(_mutex);
_loggers.erase(logger_name);
@ -92,12 +92,12 @@ public:
std::lock_guard<std::mutex> lock(_mutex);
_loggers.clear();
}
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks)
std::shared_ptr<logger> create(const SPDLOG_NAME_TYPE_REF logger_name, sinks_init_list sinks)
{
return create(logger_name, sinks.begin(), sinks.end());
}
std::shared_ptr<logger> create(const std::string& logger_name, sink_ptr sink)
std::shared_ptr<logger> create(const SPDLOG_NAME_TYPE_REF logger_name, sink_ptr sink)
{
return create(logger_name, { sink });
}
@ -153,14 +153,14 @@ private:
{
auto logger_name = logger->name();
if (_loggers.find(logger_name) != std::end(_loggers))
throw spdlog_ex("logger with name " + logger_name + " already exists");
throw spdlog_ex("logger with name already exists");
_loggers[logger->name()] = logger;
}
registry() = default;
registry(const registry&) = delete;
registry& operator=(const registry&) = delete;
std::mutex _mutex;
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
std::unordered_map <SPDLOG_NAME_TYPE, std::shared_ptr<logger>> _loggers;
formatter_ptr _formatter;
level::level_enum _level = level::info;
bool _async_mode = false;

@ -37,62 +37,62 @@ inline void spdlog::register_logger(std::shared_ptr<logger> logger)
return details::registry::instance().register_logger(logger);
}
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
inline std::shared_ptr<spdlog::logger> spdlog::get(const SPDLOG_NAME_TYPE_REF name)
{
return details::registry::instance().get(name);
}
inline void spdlog::drop(const std::string &name)
inline void spdlog::drop(const SPDLOG_NAME_TYPE_REF name)
{
details::registry::instance().drop(name);
}
// Create multi/single threaded rotating file logger
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush)
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush)
{
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, "txt", max_file_size, max_files, force_flush);
}
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush)
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush)
{
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, "txt", max_file_size, max_files, force_flush);
}
// Create file logger which creates new file at midnight):
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush)
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, int hour, int minute, bool force_flush)
{
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, "txt", hour, minute, force_flush);
}
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush)
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, int hour, int minute, bool force_flush)
{
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", hour, minute, force_flush);
}
// Create stdout/stderr loggers
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name)
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name)
{
return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_mt::instance());
}
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name)
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const SPDLOG_NAME_TYPE_REF logger_name)
{
return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_st::instance());
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name)
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name)
{
return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_mt::instance());
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name)
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const SPDLOG_NAME_TYPE_REF logger_name)
{
return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_st::instance());
}
#ifdef __linux__
// Create syslog logger
inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string& logger_name, const std::string& syslog_ident, int syslog_option)
inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& syslog_ident, int syslog_option)
{
return create<spdlog::sinks::syslog_sink>(logger_name, syslog_ident, syslog_option);
}
@ -101,14 +101,14 @@ inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string&
//Create logger with multiple sinks
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks)
inline std::shared_ptr<spdlog::logger> spdlog::create(const SPDLOG_NAME_TYPE_REF logger_name, spdlog::sinks_init_list sinks)
{
return details::registry::instance().create(logger_name, sinks);
}
template <typename Sink, typename... Args>
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const Args&... args)
inline std::shared_ptr<spdlog::logger> spdlog::create(const SPDLOG_NAME_TYPE_REF logger_name, const Args&... args)
{
sink_ptr sink = std::make_shared<Sink>(args...);
return details::registry::instance().create(logger_name, { sink });
@ -116,7 +116,7 @@ inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_
template<class It>
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
inline std::shared_ptr<spdlog::logger> spdlog::create(const SPDLOG_NAME_TYPE_REF logger_name, const It& sinks_begin, const It& sinks_end)
{
return details::registry::instance().create(logger_name, sinks_begin, sinks_end);
}

@ -47,10 +47,10 @@ class line_logger;
class logger
{
public:
logger(const std::string& logger_name, sink_ptr single_sink);
logger(const std::string& name, sinks_init_list);
logger(const SPDLOG_NAME_TYPE_REF logger_name, sink_ptr single_sink);
logger(const SPDLOG_NAME_TYPE_REF name, sinks_init_list);
template<class It>
logger(const std::string& name, const It& begin, const It& end);
logger(const SPDLOG_NAME_TYPE_REF name, const It& begin, const It& end);
virtual ~logger();
logger(const logger&) = delete;
@ -59,7 +59,7 @@ public:
void set_level(level::level_enum);
level::level_enum level() const;
const std::string& name() const;
const SPDLOG_NAME_TYPE_REF name() const;
bool should_log(level::level_enum) const;
// logger.info(cppformat_string, arg1, arg2, arg3, ...) call style
@ -120,7 +120,7 @@ protected:
friend details::line_logger;
std::string _name;
SPDLOG_NAME_TYPE _name;
std::vector<sink_ptr> _sinks;
formatter_ptr _formatter;
std::atomic_int _level;

@ -29,6 +29,10 @@
#pragma once
#include "tweakme.h"
#ifndef SPDLOG_NAME_TYPE
#define SPDLOG_NAME_TYPE std::string
#define SPDLOG_NAME_TYPE_REF std::string&
#endif
#include "common.h"
#include "logger.h"
@ -41,7 +45,7 @@ namespace spdlog
// auto logger = spdlog::get("mylog");
// logger.info("This is another message" , x, y, z);
// logger.info() << "This is another message" << x << y << z;
std::shared_ptr<logger> get(const std::string& name);
std::shared_ptr<logger> get(const SPDLOG_NAME_TYPE_REF name);
//
// Set global formatting
@ -76,50 +80,50 @@ void set_sync_mode();
//
// Create and register multi/single threaded rotating file logger
//
std::shared_ptr<logger> rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false);
std::shared_ptr<logger> rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false);
std::shared_ptr<logger> rotating_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false);
std::shared_ptr<logger> rotating_logger_st(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false);
//
// Create file logger which creates new file on the given time (default in midnight):
//
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false);
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false);
std::shared_ptr<logger> daily_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false);
std::shared_ptr<logger> daily_logger_st(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false);
//
// Create and register stdout/stderr loggers
//
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name);
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name);
std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name);
std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);
std::shared_ptr<logger> stdout_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name);
std::shared_ptr<logger> stdout_logger_st(const SPDLOG_NAME_TYPE_REF logger_name);
std::shared_ptr<logger> stderr_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name);
std::shared_ptr<logger> stderr_logger_st(const SPDLOG_NAME_TYPE_REF logger_name);
//
// Create and register a syslog logger
//
#ifdef __linux__
std::shared_ptr<logger> syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0);
std::shared_ptr<logger> syslog_logger(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& ident = "", int syslog_option = 0);
#endif
// Create and register a logger with multiple sinks
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
std::shared_ptr<logger> create(const SPDLOG_NAME_TYPE_REF logger_name, sinks_init_list sinks);
template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
std::shared_ptr<logger> create(const SPDLOG_NAME_TYPE_REF logger_name, const It& sinks_begin, const It& sinks_end);
// Create and register a logger with templated sink type
// Example: spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename", "txt");
template <typename Sink, typename... Args>
std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Args&...);
std::shared_ptr<spdlog::logger> create(const SPDLOG_NAME_TYPE_REF logger_name, const Args&...);
// Register the given logger with the given name
void register_logger(std::shared_ptr<logger> logger);
// Drop the reference to the given logger
void drop(const std::string &name);
void drop(const SPDLOG_NAME_TYPE_REF name);
// Drop all references
void drop_all();

@ -64,3 +64,10 @@
// #define SPDLOG_DEBUG_ON
// #define SPDLOG_TRACE_ON
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable the SPDLOG_DEBUG/SPDLOG_TRACE macros
// #define SPDLOG_NAME_TYPE int
// #define SPDLOG_NAME_TYPE_REF int
///////////////////////////////////////////////////////////////////////////////

Loading…
Cancel
Save