mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 01:51:38 +08:00
Moved default sync factory to seperate file to avoid cyclic includes
This commit is contained in:
parent
c35f33e61a
commit
eea9d6136f
@ -222,6 +222,8 @@ std::unique_ptr<T> make_unique(Args &&... args)
|
||||
}
|
||||
#endif
|
||||
} // namespace details
|
||||
|
||||
|
||||
} // namespace spdlog
|
||||
|
||||
#ifdef SPDLOG_HEADER_ONLY
|
||||
|
@ -3,8 +3,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// Loggers registy of unique name->logger pointer
|
||||
// An attempt to create a logger with an already existing name will be ignored
|
||||
// Loggers registry of unique name->logger pointer
|
||||
// An attempt to create a logger with an already existing name will result with spdlog_ex exception.
|
||||
// If user requests a non existing logger, nullptr will be returned
|
||||
// This class is thread safe
|
||||
|
||||
|
22
include/spdlog/details/synchronous_factory.h
Normal file
22
include/spdlog/details/synchronous_factory.h
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright(c) 2015-present Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "registry.h"
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
// Default logger factory- creates synchronous loggers
|
||||
class logger;
|
||||
|
||||
struct synchronous_factory {
|
||||
template<typename Sink, typename... SinkArgs>
|
||||
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... args) {
|
||||
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
||||
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));
|
||||
details::registry::instance().initialize_logger(new_logger);
|
||||
return new_logger;
|
||||
}
|
||||
};
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/details/os.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
|
||||
#include <android/log.h>
|
||||
#include <chrono>
|
||||
@ -99,13 +100,13 @@ using android_sink_st = android_sink<details::null_mutex>;
|
||||
|
||||
// Create and register android syslog logger
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog")
|
||||
{
|
||||
return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog")
|
||||
{
|
||||
return Factory::template create<sinks::android_sink_st>(logger_name, tag);
|
||||
|
@ -77,6 +77,7 @@ private:
|
||||
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end);
|
||||
};
|
||||
|
||||
|
||||
using ansicolor_stdout_sink_mt = ansicolor_sink<details::console_stdout, details::console_mutex>;
|
||||
using ansicolor_stdout_sink_st = ansicolor_sink<details::console_stdout, details::console_nullmutex>;
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "spdlog/details/file_helper.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
@ -38,13 +39,13 @@ using basic_file_sink_st = basic_file_sink<details::null_mutex>;
|
||||
//
|
||||
// factory functions
|
||||
//
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false)
|
||||
{
|
||||
return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate);
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false)
|
||||
{
|
||||
return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate);
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "spdlog/fmt/fmt.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/details/os.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
@ -120,14 +121,14 @@ using daily_file_sink_st = daily_file_sink<details::null_mutex>;
|
||||
//
|
||||
// factory functions
|
||||
//
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> daily_logger_mt(
|
||||
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
|
||||
{
|
||||
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate);
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> daily_logger_st(
|
||||
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
|
||||
{
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
@ -24,7 +25,7 @@ using null_sink_st = null_sink<details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> null_logger_mt(const std::string &logger_name)
|
||||
{
|
||||
auto null_logger = Factory::template create<sinks::null_sink_mt>(logger_name);
|
||||
@ -32,7 +33,7 @@ inline std::shared_ptr<logger> null_logger_mt(const std::string &logger_name)
|
||||
return null_logger;
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> null_logger_st(const std::string &logger_name)
|
||||
{
|
||||
auto null_logger = Factory::template create<sinks::null_sink_st>(logger_name);
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/details/file_helper.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
@ -56,14 +57,14 @@ using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
|
||||
// factory functions
|
||||
//
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> rotating_logger_mt(
|
||||
const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false)
|
||||
{
|
||||
return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files, rotate_on_open);
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> rotating_logger_st(
|
||||
const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false)
|
||||
{
|
||||
|
@ -9,6 +9,8 @@
|
||||
#include "spdlog/sinks/ansicolor_sink.h"
|
||||
#endif
|
||||
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
#ifdef _WIN32
|
||||
@ -24,16 +26,17 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st;
|
||||
#endif
|
||||
} // namespace sinks
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
|
||||
} // namespace spdlog
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "spdlog/details/console_globals.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/details/pattern_formatter.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
@ -70,25 +71,25 @@ using stderr_sink_st = stdout_sink<details::console_stderr, details::console_nul
|
||||
} // namespace sinks
|
||||
|
||||
// factory methods
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::stdout_sink_st>(logger_name);
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::stderr_sink_st>(logger_name);
|
||||
|
@ -4,6 +4,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
@ -65,13 +66,13 @@ using systemd_sink_st = systemd_sink<details::null_mutex>;
|
||||
} // namespace sinks
|
||||
|
||||
// Create and register a syslog logger
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> systemd_logger_mt(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::systemd_sink_mt>(logger_name);
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> systemd_logger_st(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::systemd_sink_st>(logger_name);
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "spdlog/details/registry.h"
|
||||
#include "spdlog/logger.h"
|
||||
#include "spdlog/version.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
@ -21,19 +22,6 @@
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
// Default logger factory- creates synchronous loggers
|
||||
struct synchronous_factory
|
||||
{
|
||||
template<typename Sink, typename... SinkArgs>
|
||||
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... args)
|
||||
{
|
||||
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
||||
auto new_logger = std::make_shared<logger>(std::move(logger_name), std::move(sink));
|
||||
details::registry::instance().initialize_logger(new_logger);
|
||||
return new_logger;
|
||||
}
|
||||
};
|
||||
|
||||
using default_factory = synchronous_factory;
|
||||
|
||||
// Create and register a logger with a templated sink type
|
||||
|
Loading…
Reference in New Issue
Block a user