mirror of
https://github.com/gabime/spdlog.git
synced 2025-11-16 09:28:56 +08:00
updated example and added more creation functions
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user