mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-30 02:19:35 +08:00
updated example and added more creation functions
This commit is contained in:
@@ -41,10 +41,10 @@ inline const char* to_str(spdlog::level::level_enum l)
|
||||
//
|
||||
// Log exception
|
||||
//
|
||||
class fflog_exception : public std::exception
|
||||
class spdlog_ex : public std::exception
|
||||
{
|
||||
public:
|
||||
fflog_exception(const std::string& msg) :_msg(msg) {};
|
||||
spdlog_ex(const std::string& msg) :_msg(msg) {};
|
||||
const char* what() const throw() override {
|
||||
return _msg.c_str();
|
||||
}
|
||||
|
@@ -54,7 +54,7 @@ public:
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms_bewteen_tries));
|
||||
}
|
||||
|
||||
throw fflog_exception("Failed opening file " + filename + " for writing");
|
||||
throw spdlog_ex("Failed opening file " + filename + " for writing");
|
||||
}
|
||||
|
||||
void close()
|
||||
@@ -71,7 +71,7 @@ public:
|
||||
auto& buf = msg.formatted.buf();
|
||||
size_t size = buf.size();
|
||||
if(std::fwrite(buf.data(), sizeof(char), size, _fd) != size)
|
||||
throw fflog_exception("Failed writing to file " + _filename);
|
||||
throw spdlog_ex("Failed writing to file " + _filename);
|
||||
|
||||
if(--_flush_countdown == 0)
|
||||
{
|
||||
|
@@ -116,8 +116,16 @@ inline void spdlog::logger::stop_logging()
|
||||
|
||||
inline void spdlog::logger::_variadic_log(spdlog::details::line_logger&) {}
|
||||
|
||||
template <typename Last>
|
||||
inline void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const Last& last)
|
||||
{
|
||||
l.write(last);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename First, typename... Rest>
|
||||
void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const First& first, const Rest&... rest)
|
||||
inline void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const First& first, const Rest&... rest)
|
||||
{
|
||||
l.write(first);
|
||||
l.write(' ');
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
// Loggers registy of unique name->logger pointer
|
||||
// If 2 loggers with same name are added, the second will be overrun the first
|
||||
// An attempt to create a logger with an alreasy existing name will be ignored
|
||||
// If user requests a non existing logger, nullptr will be returned
|
||||
// This class is thread safe
|
||||
|
||||
@@ -16,10 +16,10 @@ namespace details {
|
||||
|
||||
class registry {
|
||||
public:
|
||||
std::shared_ptr<logger> get(const std::string& name)
|
||||
std::shared_ptr<logger> get(const std::string& logger_name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
auto found = _loggers.find(name);
|
||||
auto found = _loggers.find(logger_name);
|
||||
return found == _loggers.end() ? nullptr : found->second;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,11 @@ public:
|
||||
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
//If already exists, just return it
|
||||
auto found = _loggers.find(logger_name);
|
||||
if (found != _loggers.end())
|
||||
return found->second;
|
||||
|
||||
auto new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
|
||||
new_logger->set_formatter(_formatter);
|
||||
new_logger->set_level(_level);
|
||||
|
@@ -4,12 +4,62 @@
|
||||
// Global registry functions
|
||||
//
|
||||
#include "registry.h"
|
||||
#include "../sinks/file_sinks.h"
|
||||
#include "../sinks/stdout_sinks.h"
|
||||
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
|
||||
{
|
||||
return details::registry::instance().get(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, size_t flush_inverval)
|
||||
{
|
||||
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, "txt", max_file_size, max_files, flush_inverval);
|
||||
}
|
||||
|
||||
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, size_t flush_inverval)
|
||||
{
|
||||
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, "txt", max_file_size, max_files, flush_inverval);
|
||||
}
|
||||
|
||||
// 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, size_t flush_inverval)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, "txt", flush_inverval);
|
||||
}
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, size_t flush_inverval)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", flush_inverval);
|
||||
}
|
||||
|
||||
|
||||
// Create stdout/stderr loggers
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name)
|
||||
{
|
||||
return create<spdlog::sinks::stdout_sink_mt>(logger_name);
|
||||
}
|
||||
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name)
|
||||
{
|
||||
return create<spdlog::sinks::stdout_sink_st>(logger_name);
|
||||
}
|
||||
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name)
|
||||
{
|
||||
return create<spdlog::sinks::stderr_sink_mt>(logger_name);
|
||||
}
|
||||
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name)
|
||||
{
|
||||
return create<spdlog::sinks::stderr_sink_st>(logger_name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks)
|
||||
{
|
||||
return details::registry::instance().create(logger_name, sinks);
|
||||
|
@@ -64,6 +64,8 @@ private:
|
||||
std::vector<sink_ptr> _sinks;
|
||||
std::atomic_int _level;
|
||||
void _variadic_log(details::line_logger& l);
|
||||
template <typename Last>
|
||||
inline void _variadic_log(spdlog::details::line_logger& l, const Last& last);
|
||||
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);
|
||||
|
@@ -48,8 +48,8 @@ class rotating_file_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
rotating_file_sink(const std::string &base_filename, const std::string &extension,
|
||||
const std::size_t max_size, const std::size_t max_files,
|
||||
const std::size_t flush_inverval=0):
|
||||
std::size_t max_size, std::size_t max_files,
|
||||
std::size_t flush_inverval=0):
|
||||
_base_filename(base_filename),
|
||||
_extension(extension),
|
||||
_max_size(max_size),
|
||||
@@ -104,7 +104,7 @@ private:
|
||||
std::remove(target.c_str());
|
||||
if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str()))
|
||||
{
|
||||
throw fflog_exception("rotating_file_sink: failed renaming " + src + " to " + target);
|
||||
throw spdlog_ex("rotating_file_sink: failed renaming " + src + " to " + target);
|
||||
}
|
||||
}
|
||||
auto cur_name = _file_helper.filename();
|
||||
|
@@ -29,47 +29,57 @@ namespace spdlog
|
||||
std::shared_ptr<logger> get(const std::string& name);
|
||||
|
||||
|
||||
// Example:
|
||||
// auto logger = spdlog::create("mylog", {sink1, sink2});
|
||||
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
|
||||
|
||||
|
||||
// Example (create a logger with daily rotating file):
|
||||
// using namespace spdlog::sinks;
|
||||
// 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&... args);
|
||||
|
||||
// Example:
|
||||
// using namespace spdlog::sinks;
|
||||
// std::vector<spdlog::sink_ptr> mySinks;
|
||||
// mySinks.push_back(std::make_shared<rotating_file_sink_mt>("filename", "txt", 1024 * 1024 * 5, 10));
|
||||
// mySinks.push_back(std::make_shared<stdout_sink_mt>());
|
||||
// spdlog::create("mylog", mySinks.begin(), mySinks.end());
|
||||
template<class It>
|
||||
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
|
||||
|
||||
|
||||
// Set global formatting
|
||||
// Example:
|
||||
// spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %t");
|
||||
void set_pattern(const std::string& format_string);
|
||||
|
||||
|
||||
// Set global formatter object
|
||||
void set_formatter(formatter_ptr f);
|
||||
|
||||
|
||||
//Set global active logging level
|
||||
//Set global logging level
|
||||
void set_level(level::level_enum log_level);
|
||||
|
||||
|
||||
// Create 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, size_t flush_inverval = 0);
|
||||
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, size_t flush_inverval = 0);
|
||||
|
||||
// Create file logger which creates new file at midnight):
|
||||
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const std::string& filename, size_t flush_inverval = 0);
|
||||
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const std::string& filename, size_t flush_inverval = 0);
|
||||
|
||||
|
||||
// Create 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);
|
||||
|
||||
|
||||
|
||||
//
|
||||
// Create a logger with multiple sinks
|
||||
//
|
||||
std::shared_ptr<logger> create(const std::string& 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);
|
||||
|
||||
|
||||
// Create 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&... args);
|
||||
|
||||
|
||||
|
||||
// Set global formatter object
|
||||
void set_formatter(formatter_ptr f);
|
||||
|
||||
//Stop all loggers
|
||||
void stop();
|
||||
|
||||
|
||||
//
|
||||
// Trace macro to turn on/off at compile time
|
||||
// Trace macro enabled only at debug compile
|
||||
// Example: SPDLOG_TRACE(my_logger, "Some trace message");
|
||||
//
|
||||
#ifdef _DEBUG
|
||||
|
Reference in New Issue
Block a user