mirror of
https://github.com/gabime/spdlog.git
synced 2025-04-01 02:42:41 +08:00
Merge pull request #647 from DanielChabrowski/clang-tidy
Applied some of clang-tidy fixes
This commit is contained in:
commit
2e973c8b3d
@ -31,7 +31,7 @@ namespace details
|
|||||||
class async_log_helper;
|
class async_log_helper;
|
||||||
}
|
}
|
||||||
|
|
||||||
class async_logger SPDLOG_FINAL :public logger
|
class async_logger SPDLOG_FINAL : public logger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
template<class It>
|
template<class It>
|
||||||
@ -44,7 +44,7 @@ public:
|
|||||||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||||
const std::function<void()>& worker_teardown_cb = nullptr);
|
const std::function<void()>& worker_teardown_cb = nullptr);
|
||||||
|
|
||||||
async_logger(const std::string& logger_name,
|
async_logger(const std::string& name,
|
||||||
sinks_init_list sinks,
|
sinks_init_list sinks,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||||
@ -52,7 +52,7 @@ public:
|
|||||||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||||
const std::function<void()>& worker_teardown_cb = nullptr);
|
const std::function<void()>& worker_teardown_cb = nullptr);
|
||||||
|
|
||||||
async_logger(const std::string& logger_name,
|
async_logger(const std::string& name,
|
||||||
sink_ptr single_sink,
|
sink_ptr single_sink,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||||
@ -65,8 +65,8 @@ public:
|
|||||||
void flush() override;
|
void flush() override;
|
||||||
|
|
||||||
// Error handler
|
// Error handler
|
||||||
virtual void set_error_handler(log_err_handler) override;
|
void set_error_handler(log_err_handler) override;
|
||||||
virtual log_err_handler error_handler() override;
|
log_err_handler error_handler() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(details::log_msg& msg) override;
|
void _sink_it(details::log_msg& msg) override;
|
||||||
@ -78,5 +78,4 @@ private:
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#include "details/async_logger_impl.h"
|
#include "details/async_logger_impl.h"
|
||||||
|
@ -75,7 +75,7 @@ using log_err_handler = std::function<void(const std::string &err_msg)>;
|
|||||||
//Log level enum
|
//Log level enum
|
||||||
namespace level
|
namespace level
|
||||||
{
|
{
|
||||||
typedef enum
|
enum level_enum
|
||||||
{
|
{
|
||||||
trace = 0,
|
trace = 0,
|
||||||
debug = 1,
|
debug = 1,
|
||||||
@ -84,7 +84,7 @@ typedef enum
|
|||||||
err = 4,
|
err = 4,
|
||||||
critical = 5,
|
critical = 5,
|
||||||
off = 6
|
off = 6
|
||||||
} level_enum;
|
};
|
||||||
|
|
||||||
#if !defined(SPDLOG_LEVEL_NAMES)
|
#if !defined(SPDLOG_LEVEL_NAMES)
|
||||||
#define SPDLOG_LEVEL_NAMES { "trace", "debug", "info", "warning", "error", "critical", "off" }
|
#define SPDLOG_LEVEL_NAMES { "trace", "debug", "info", "warning", "error", "critical", "off" }
|
||||||
@ -106,7 +106,6 @@ using level_hasher = std::hash<int>;
|
|||||||
|
|
||||||
} //level
|
} //level
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Async overflow policy - block by default.
|
// Async overflow policy - block by default.
|
||||||
//
|
//
|
||||||
@ -136,22 +135,24 @@ namespace os
|
|||||||
std::string errno_str(int err_num);
|
std::string errno_str(int err_num);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class spdlog_ex: public std::exception
|
class spdlog_ex : public std::exception
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
spdlog_ex(const std::string& msg):_msg(msg)
|
explicit spdlog_ex(std::string msg) : _msg(std::move(msg))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
spdlog_ex(const std::string& msg, int last_errno)
|
spdlog_ex(const std::string& msg, int last_errno)
|
||||||
{
|
{
|
||||||
_msg = msg + ": " + details::os::errno_str(last_errno);
|
_msg = msg + ": " + details::os::errno_str(last_errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* what() const SPDLOG_NOEXCEPT override
|
const char* what() const SPDLOG_NOEXCEPT override
|
||||||
{
|
{
|
||||||
return _msg.c_str();
|
return _msg.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _msg;
|
std::string _msg;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -163,5 +164,4 @@ using filename_t = std::wstring;
|
|||||||
using filename_t = std::string;
|
using filename_t = std::string;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
} //spdlog
|
} //spdlog
|
||||||
|
@ -43,6 +43,7 @@ class async_log_helper
|
|||||||
flush,
|
flush,
|
||||||
terminate
|
terminate
|
||||||
};
|
};
|
||||||
|
|
||||||
struct async_msg
|
struct async_msg
|
||||||
{
|
{
|
||||||
std::string logger_name;
|
std::string logger_name;
|
||||||
@ -56,8 +57,14 @@ class async_log_helper
|
|||||||
async_msg() = default;
|
async_msg() = default;
|
||||||
~async_msg() = default;
|
~async_msg() = default;
|
||||||
|
|
||||||
|
explicit async_msg(async_msg_type m_type) :
|
||||||
|
level(level::info),
|
||||||
|
thread_id(0),
|
||||||
|
msg_type(m_type),
|
||||||
|
msg_id(0)
|
||||||
|
{}
|
||||||
|
|
||||||
async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
async_msg(async_msg&& other) SPDLOG_NOEXCEPT :
|
||||||
logger_name(std::move(other.logger_name)),
|
logger_name(std::move(other.logger_name)),
|
||||||
level(std::move(other.level)),
|
level(std::move(other.level)),
|
||||||
time(std::move(other.time)),
|
time(std::move(other.time)),
|
||||||
@ -67,13 +74,6 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|||||||
msg_id(other.msg_id)
|
msg_id(other.msg_id)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
async_msg(async_msg_type m_type):
|
|
||||||
level(level::info),
|
|
||||||
thread_id(0),
|
|
||||||
msg_type(m_type),
|
|
||||||
msg_id(0)
|
|
||||||
{}
|
|
||||||
|
|
||||||
async_msg& operator=(async_msg&& other) SPDLOG_NOEXCEPT
|
async_msg& operator=(async_msg&& other) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
logger_name = std::move(other.logger_name);
|
logger_name = std::move(other.logger_name);
|
||||||
@ -91,7 +91,7 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|||||||
async_msg& operator=(const async_msg& other) = delete;
|
async_msg& operator=(const async_msg& other) = delete;
|
||||||
|
|
||||||
// construct from log_msg
|
// construct from log_msg
|
||||||
async_msg(const details::log_msg& m):
|
explicit async_msg(const details::log_msg& m):
|
||||||
level(m.level),
|
level(m.level),
|
||||||
time(m.time),
|
time(m.time),
|
||||||
thread_id(m.thread_id),
|
thread_id(m.thread_id),
|
||||||
@ -104,7 +104,6 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// copy into log_msg
|
// copy into log_msg
|
||||||
void fill_log_msg(log_msg &msg)
|
void fill_log_msg(log_msg &msg)
|
||||||
{
|
{
|
||||||
@ -118,28 +117,29 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
|
|||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using item_type = async_msg;
|
using item_type = async_msg;
|
||||||
using q_type = details::mpmc_bounded_queue<item_type>;
|
using q_type = details::mpmc_bounded_queue<item_type>;
|
||||||
|
|
||||||
using clock = std::chrono::steady_clock;
|
using clock = std::chrono::steady_clock;
|
||||||
|
|
||||||
|
|
||||||
async_log_helper(formatter_ptr formatter,
|
async_log_helper(formatter_ptr formatter,
|
||||||
const std::vector<sink_ptr>& sinks,
|
std::vector<sink_ptr> sinks,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const log_err_handler err_handler,
|
const log_err_handler err_handler,
|
||||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||||
const std::function<void()>& worker_warmup_cb = nullptr,
|
std::function<void()> worker_warmup_cb = nullptr,
|
||||||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||||
const std::function<void()>& worker_teardown_cb = nullptr);
|
std::function<void()> worker_teardown_cb = nullptr);
|
||||||
|
|
||||||
void log(const details::log_msg& msg);
|
void log(const details::log_msg& msg);
|
||||||
|
|
||||||
// stop logging and join the back thread
|
// stop logging and join the back thread
|
||||||
~async_log_helper();
|
~async_log_helper();
|
||||||
|
|
||||||
void set_formatter(formatter_ptr);
|
async_log_helper(const async_log_helper&) = delete;
|
||||||
|
async_log_helper& operator=(const async_log_helper&) = delete;
|
||||||
|
|
||||||
|
void set_formatter(formatter_ptr msg_formatter);
|
||||||
|
|
||||||
void flush(bool wait_for_q);
|
void flush(bool wait_for_q);
|
||||||
|
|
||||||
@ -158,7 +158,6 @@ private:
|
|||||||
|
|
||||||
bool _terminate_requested;
|
bool _terminate_requested;
|
||||||
|
|
||||||
|
|
||||||
// overflow policy
|
// overflow policy
|
||||||
const async_overflow_policy _overflow_policy;
|
const async_overflow_policy _overflow_policy;
|
||||||
|
|
||||||
@ -200,23 +199,23 @@ private:
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
inline spdlog::details::async_log_helper::async_log_helper(
|
inline spdlog::details::async_log_helper::async_log_helper(
|
||||||
formatter_ptr formatter,
|
formatter_ptr formatter,
|
||||||
const std::vector<sink_ptr>& sinks,
|
std::vector<sink_ptr> sinks,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
log_err_handler err_handler,
|
log_err_handler err_handler,
|
||||||
const async_overflow_policy overflow_policy,
|
const async_overflow_policy overflow_policy,
|
||||||
const std::function<void()>& worker_warmup_cb,
|
std::function<void()> worker_warmup_cb,
|
||||||
const std::chrono::milliseconds& flush_interval_ms,
|
const std::chrono::milliseconds& flush_interval_ms,
|
||||||
const std::function<void()>& worker_teardown_cb):
|
std::function<void()> worker_teardown_cb):
|
||||||
_formatter(formatter),
|
_formatter(std::move(formatter)),
|
||||||
_sinks(sinks),
|
_sinks(std::move(sinks)),
|
||||||
_q(queue_size),
|
_q(queue_size),
|
||||||
_err_handler(err_handler),
|
_err_handler(std::move(err_handler)),
|
||||||
_flush_requested(false),
|
_flush_requested(false),
|
||||||
_terminate_requested(false),
|
_terminate_requested(false),
|
||||||
_overflow_policy(overflow_policy),
|
_overflow_policy(overflow_policy),
|
||||||
_worker_warmup_cb(worker_warmup_cb),
|
_worker_warmup_cb(std::move(worker_warmup_cb)),
|
||||||
_flush_interval_ms(flush_interval_ms),
|
_flush_interval_ms(flush_interval_ms),
|
||||||
_worker_teardown_cb(worker_teardown_cb)
|
_worker_teardown_cb(std::move(worker_teardown_cb))
|
||||||
{
|
{
|
||||||
_worker_thread = std::thread(&async_log_helper::worker_loop, this);
|
_worker_thread = std::thread(&async_log_helper::worker_loop, this);
|
||||||
}
|
}
|
||||||
@ -328,13 +327,10 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
|
|||||||
|
|
||||||
// Handle empty queue..
|
// Handle empty queue..
|
||||||
// This is the only place where the queue can terminate or flush to avoid losing messages already in the queue
|
// This is the only place where the queue can terminate or flush to avoid losing messages already in the queue
|
||||||
else
|
|
||||||
{
|
|
||||||
auto now = details::os::now();
|
auto now = details::os::now();
|
||||||
handle_flush_interval(now, last_flush);
|
handle_flush_interval(now, last_flush);
|
||||||
sleep_or_yield(now, last_pop);
|
sleep_or_yield(now, last_pop);
|
||||||
return !_terminate_requested;
|
return !_terminate_requested;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// flush all sinks if _flush_interval_ms has expired
|
// flush all sinks if _flush_interval_ms has expired
|
||||||
@ -352,10 +348,9 @@ inline void spdlog::details::async_log_helper::handle_flush_interval(log_clock::
|
|||||||
|
|
||||||
inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter)
|
inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter)
|
||||||
{
|
{
|
||||||
_formatter = msg_formatter;
|
_formatter = std::move(msg_formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// spin, yield or sleep. use the time passed since last message as a hint
|
// spin, yield or sleep. use the time passed since last message as a hint
|
||||||
inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_clock::time_point& now, const spdlog::log_clock::time_point& last_op_time)
|
inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_clock::time_point& now, const spdlog::log_clock::time_point& last_op_time)
|
||||||
{
|
{
|
||||||
@ -392,8 +387,5 @@ inline void spdlog::details::async_log_helper::wait_empty_q()
|
|||||||
|
|
||||||
inline void spdlog::details::async_log_helper::set_error_handler(spdlog::log_err_handler err_handler)
|
inline void spdlog::details::async_log_helper::set_error_handler(spdlog::log_err_handler err_handler)
|
||||||
{
|
{
|
||||||
_err_handler = err_handler;
|
_err_handler = std::move(err_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
template<class It>
|
template<class It>
|
||||||
inline spdlog::async_logger::async_logger(const std::string& logger_name,
|
inline spdlog::async_logger::async_logger(const std::string& name,
|
||||||
const It& begin,
|
const It& begin,
|
||||||
const It& end,
|
const It& end,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
@ -25,30 +25,30 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name,
|
|||||||
const std::function<void()>& worker_warmup_cb,
|
const std::function<void()>& worker_warmup_cb,
|
||||||
const std::chrono::milliseconds& flush_interval_ms,
|
const std::chrono::milliseconds& flush_interval_ms,
|
||||||
const std::function<void()>& worker_teardown_cb) :
|
const std::function<void()>& worker_teardown_cb) :
|
||||||
logger(logger_name, begin, end),
|
logger(name, begin, end),
|
||||||
_async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, _err_handler, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
|
_async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, _err_handler, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
inline spdlog::async_logger::async_logger(const std::string& logger_name,
|
inline spdlog::async_logger::async_logger(const std::string& name,
|
||||||
sinks_init_list sinks_list,
|
sinks_init_list sinks,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy,
|
const async_overflow_policy overflow_policy,
|
||||||
const std::function<void()>& worker_warmup_cb,
|
const std::function<void()>& worker_warmup_cb,
|
||||||
const std::chrono::milliseconds& flush_interval_ms,
|
const std::chrono::milliseconds& flush_interval_ms,
|
||||||
const std::function<void()>& worker_teardown_cb) :
|
const std::function<void()>& worker_teardown_cb) :
|
||||||
async_logger(logger_name, sinks_list.begin(), sinks_list.end(), queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
|
async_logger(name, sinks.begin(), sinks.end(), queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
|
||||||
|
|
||||||
inline spdlog::async_logger::async_logger(const std::string& logger_name,
|
inline spdlog::async_logger::async_logger(const std::string& name,
|
||||||
sink_ptr single_sink,
|
sink_ptr single_sink,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy,
|
const async_overflow_policy overflow_policy,
|
||||||
const std::function<void()>& worker_warmup_cb,
|
const std::function<void()>& worker_warmup_cb,
|
||||||
const std::chrono::milliseconds& flush_interval_ms,
|
const std::chrono::milliseconds& flush_interval_ms,
|
||||||
const std::function<void()>& worker_teardown_cb) :
|
const std::function<void()>& worker_teardown_cb) :
|
||||||
async_logger(logger_name,
|
async_logger(name,
|
||||||
{
|
{
|
||||||
single_sink
|
std::move(single_sink)
|
||||||
}, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
|
}, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,9 +31,7 @@ public:
|
|||||||
const int open_tries = 5;
|
const int open_tries = 5;
|
||||||
const int open_interval = 10;
|
const int open_interval = 10;
|
||||||
|
|
||||||
explicit file_helper() :
|
explicit file_helper() = default;
|
||||||
_fd(nullptr)
|
|
||||||
{}
|
|
||||||
|
|
||||||
file_helper(const file_helper&) = delete;
|
file_helper(const file_helper&) = delete;
|
||||||
file_helper& operator=(const file_helper&) = delete;
|
file_helper& operator=(const file_helper&) = delete;
|
||||||
@ -46,7 +44,6 @@ public:
|
|||||||
|
|
||||||
void open(const filename_t& fname, bool truncate = false)
|
void open(const filename_t& fname, bool truncate = false)
|
||||||
{
|
{
|
||||||
|
|
||||||
close();
|
close();
|
||||||
auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
|
auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
|
||||||
_filename = fname;
|
_filename = fname;
|
||||||
@ -76,7 +73,7 @@ public:
|
|||||||
|
|
||||||
void close()
|
void close()
|
||||||
{
|
{
|
||||||
if (_fd)
|
if (_fd != nullptr)
|
||||||
{
|
{
|
||||||
std::fclose(_fd);
|
std::fclose(_fd);
|
||||||
_fd = nullptr;
|
_fd = nullptr;
|
||||||
@ -93,8 +90,10 @@ public:
|
|||||||
|
|
||||||
size_t size() const
|
size_t size() const
|
||||||
{
|
{
|
||||||
if (!_fd)
|
if (_fd == nullptr)
|
||||||
|
{
|
||||||
throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
|
throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
|
||||||
|
}
|
||||||
return os::filesize(_fd);
|
return os::filesize(_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,8 +136,9 @@ public:
|
|||||||
// finally - return a valid base and extension tuple
|
// finally - return a valid base and extension tuple
|
||||||
return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
|
return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FILE* _fd;
|
FILE* _fd{ nullptr };
|
||||||
filename_t _filename;
|
filename_t _filename;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
#include "../common.h"
|
#include "../common.h"
|
||||||
#include "../details/os.h"
|
#include "../details/os.h"
|
||||||
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
@ -21,8 +20,7 @@ struct log_msg
|
|||||||
log_msg() = default;
|
log_msg() = default;
|
||||||
log_msg(const std::string *loggers_name, level::level_enum lvl) :
|
log_msg(const std::string *loggers_name, level::level_enum lvl) :
|
||||||
logger_name(loggers_name),
|
logger_name(loggers_name),
|
||||||
level(lvl),
|
level(lvl)
|
||||||
msg_id(0)
|
|
||||||
{
|
{
|
||||||
#ifndef SPDLOG_NO_DATETIME
|
#ifndef SPDLOG_NO_DATETIME
|
||||||
time = os::now();
|
time = os::now();
|
||||||
@ -38,13 +36,13 @@ struct log_msg
|
|||||||
log_msg(log_msg&& other) = delete;
|
log_msg(log_msg&& other) = delete;
|
||||||
|
|
||||||
|
|
||||||
const std::string *logger_name;
|
const std::string *logger_name{ nullptr };
|
||||||
level::level_enum level;
|
level::level_enum level;
|
||||||
log_clock::time_point time;
|
log_clock::time_point time;
|
||||||
size_t thread_id;
|
size_t thread_id;
|
||||||
fmt::MemoryWriter raw;
|
fmt::MemoryWriter raw;
|
||||||
fmt::MemoryWriter formatted;
|
fmt::MemoryWriter formatted;
|
||||||
size_t msg_id;
|
size_t msg_id{ 0 };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
// create logger with given name, sinks and the default pattern formatter
|
// create logger with given name, sinks and the default pattern formatter
|
||||||
// all other ctors will call this one
|
// all other ctors will call this one
|
||||||
template<class It>
|
template<class It>
|
||||||
inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end):
|
inline spdlog::logger::logger(std::string name, const It& begin, const It& end):
|
||||||
_name(logger_name),
|
_name(std::move(name)),
|
||||||
_sinks(begin, end),
|
_sinks(begin, end),
|
||||||
_formatter(std::make_shared<pattern_formatter>("%+")),
|
_formatter(std::make_shared<pattern_formatter>("%+")),
|
||||||
_level(level::info),
|
_level(level::info),
|
||||||
@ -30,16 +30,16 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ctor with sinks as init list
|
// 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 std::string& name, sinks_init_list sinks):
|
||||||
logger(logger_name, sinks_list.begin(), sinks_list.end())
|
logger(name, sinks.begin(), sinks.end())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
// ctor with single sink
|
// ctor with single sink
|
||||||
inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink):
|
inline spdlog::logger::logger(const std::string& name, spdlog::sink_ptr single_sink):
|
||||||
logger(logger_name,
|
logger(name,
|
||||||
{
|
{
|
||||||
single_sink
|
std::move(single_sink)
|
||||||
})
|
})
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ inline spdlog::logger::~logger() = default;
|
|||||||
|
|
||||||
inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
|
inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
|
||||||
{
|
{
|
||||||
_set_formatter(msg_formatter);
|
_set_formatter(std::move(msg_formatter));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time_type pattern_time)
|
inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time_type pattern_time)
|
||||||
@ -281,7 +281,7 @@ inline void spdlog::logger::set_level(spdlog::level::level_enum log_level)
|
|||||||
|
|
||||||
inline void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler)
|
inline void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler)
|
||||||
{
|
{
|
||||||
_err_handler = err_handler;
|
_err_handler = std::move(err_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline spdlog::log_err_handler spdlog::logger::error_handler()
|
inline spdlog::log_err_handler spdlog::logger::error_handler()
|
||||||
@ -289,7 +289,6 @@ inline spdlog::log_err_handler spdlog::logger::error_handler()
|
|||||||
return _err_handler;
|
return _err_handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void spdlog::logger::flush_on(level::level_enum log_level)
|
inline void spdlog::logger::flush_on(level::level_enum log_level)
|
||||||
{
|
{
|
||||||
_flush_level.store(log_level);
|
_flush_level.store(log_level);
|
||||||
@ -330,9 +329,10 @@ inline void spdlog::logger::_set_pattern(const std::string& pattern, pattern_tim
|
|||||||
{
|
{
|
||||||
_formatter = std::make_shared<pattern_formatter>(pattern, pattern_time);
|
_formatter = std::make_shared<pattern_formatter>(pattern, pattern_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter)
|
inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter)
|
||||||
{
|
{
|
||||||
_formatter = msg_formatter;
|
_formatter = std::move(msg_formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::logger::flush()
|
inline void spdlog::logger::flush()
|
||||||
|
@ -53,13 +53,13 @@ namespace spdlog
|
|||||||
namespace details
|
namespace details
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename T>
|
template <typename T>
|
||||||
class mpmc_bounded_queue
|
class mpmc_bounded_queue
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using item_type = T;
|
using item_type = T;
|
||||||
mpmc_bounded_queue(size_t buffer_size)
|
|
||||||
|
explicit mpmc_bounded_queue(size_t buffer_size)
|
||||||
:max_size_(buffer_size),
|
:max_size_(buffer_size),
|
||||||
buffer_(new cell_t[buffer_size]),
|
buffer_(new cell_t[buffer_size]),
|
||||||
buffer_mask_(buffer_size - 1)
|
buffer_mask_(buffer_size - 1)
|
||||||
@ -79,6 +79,8 @@ public:
|
|||||||
delete[] buffer_;
|
delete[] buffer_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mpmc_bounded_queue(mpmc_bounded_queue const&) = delete;
|
||||||
|
void operator=(mpmc_bounded_queue const&) = delete;
|
||||||
|
|
||||||
bool enqueue(T&& data)
|
bool enqueue(T&& data)
|
||||||
{
|
{
|
||||||
@ -157,7 +159,7 @@ private:
|
|||||||
size_t const max_size_;
|
size_t const max_size_;
|
||||||
|
|
||||||
static size_t const cacheline_size = 64;
|
static size_t const cacheline_size = 64;
|
||||||
typedef char cacheline_pad_t[cacheline_size];
|
using cacheline_pad_t = char[cacheline_size];
|
||||||
|
|
||||||
cacheline_pad_t pad0_;
|
cacheline_pad_t pad0_;
|
||||||
cell_t* const buffer_;
|
cell_t* const buffer_;
|
||||||
@ -167,9 +169,6 @@ private:
|
|||||||
cacheline_pad_t pad2_;
|
cacheline_pad_t pad2_;
|
||||||
std::atomic<size_t> dequeue_pos_;
|
std::atomic<size_t> dequeue_pos_;
|
||||||
cacheline_pad_t pad3_;
|
cacheline_pad_t pad3_;
|
||||||
|
|
||||||
mpmc_bounded_queue(mpmc_bounded_queue const&) = delete;
|
|
||||||
void operator= (mpmc_bounded_queue const&) = delete;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // ns details
|
} // ns details
|
||||||
|
@ -27,7 +27,7 @@ struct null_atomic_int
|
|||||||
int value;
|
int value;
|
||||||
null_atomic_int() = default;
|
null_atomic_int() = default;
|
||||||
|
|
||||||
null_atomic_int(int val):value(val)
|
explicit null_atomic_int(int val) : value(val)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
int load(std::memory_order) const
|
int load(std::memory_order) const
|
||||||
|
@ -81,10 +81,10 @@ inline std::tm localtime(const std::time_t &time_tt)
|
|||||||
{
|
{
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::tm tm;
|
std::tm tm {};
|
||||||
localtime_s(&tm, &time_tt);
|
localtime_s(&tm, &time_tt);
|
||||||
#else
|
#else
|
||||||
std::tm tm;
|
std::tm tm {};
|
||||||
localtime_r(&time_tt, &tm);
|
localtime_r(&time_tt, &tm);
|
||||||
#endif
|
#endif
|
||||||
return tm;
|
return tm;
|
||||||
@ -96,15 +96,14 @@ inline std::tm localtime()
|
|||||||
return localtime(now_t);
|
return localtime(now_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline std::tm gmtime(const std::time_t &time_tt)
|
inline std::tm gmtime(const std::time_t &time_tt)
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::tm tm;
|
std::tm tm {};
|
||||||
gmtime_s(&tm, &time_tt);
|
gmtime_s(&tm, &time_tt);
|
||||||
#else
|
#else
|
||||||
std::tm tm;
|
std::tm tm {};
|
||||||
gmtime_r(&time_tt, &tm);
|
gmtime_r(&time_tt, &tm);
|
||||||
#endif
|
#endif
|
||||||
return tm;
|
return tm;
|
||||||
@ -170,7 +169,7 @@ inline void prevent_child_fd(FILE *f)
|
|||||||
|
|
||||||
|
|
||||||
//fopen_s on non windows for writing
|
//fopen_s on non windows for writing
|
||||||
inline int fopen_s(FILE** fp, const filename_t& filename, const filename_t& mode)
|
inline bool fopen_s(FILE** fp, const filename_t& filename, const filename_t& mode)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#ifdef SPDLOG_WCHAR_FILENAMES
|
#ifdef SPDLOG_WCHAR_FILENAMES
|
||||||
@ -220,7 +219,7 @@ inline bool file_exists(const filename_t& filename)
|
|||||||
#endif
|
#endif
|
||||||
return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY));
|
return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY));
|
||||||
#else //common linux/unix all have the stat system call
|
#else //common linux/unix all have the stat system call
|
||||||
struct stat buffer;
|
struct stat buffer {};
|
||||||
return (stat(filename.c_str(), &buffer) == 0);
|
return (stat(filename.c_str(), &buffer) == 0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -250,11 +249,11 @@ inline size_t filesize(FILE *f)
|
|||||||
int fd = fileno(f);
|
int fd = fileno(f);
|
||||||
//64 bits(but not in osx or cygwin, where fstat64 is deprecated)
|
//64 bits(but not in osx or cygwin, where fstat64 is deprecated)
|
||||||
#if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) && !defined(__CYGWIN__)
|
#if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) && !defined(__CYGWIN__)
|
||||||
struct stat64 st;
|
struct stat64 st {};
|
||||||
if (fstat64(fd, &st) == 0)
|
if (fstat64(fd, &st) == 0)
|
||||||
return static_cast<size_t>(st.st_size);
|
return static_cast<size_t>(st.st_size);
|
||||||
#else // unix 32 bits or cygwin
|
#else // unix 32 bits or cygwin
|
||||||
struct stat st;
|
struct stat st {};
|
||||||
if (fstat(fd, &st) == 0)
|
if (fstat(fd, &st) == 0)
|
||||||
return static_cast<size_t>(st.st_size);
|
return static_cast<size_t>(st.st_size);
|
||||||
#endif
|
#endif
|
||||||
@ -318,9 +317,9 @@ inline int utc_minutes_offset(const std::tm& tm = details::os::localtime())
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
long int offset_seconds = helper::calculate_gmt_offset(tm);
|
auto offset_seconds = helper::calculate_gmt_offset(tm);
|
||||||
#else
|
#else
|
||||||
long int offset_seconds = tm.tm_gmtoff;
|
auto offset_seconds = tm.tm_gmtoff;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return static_cast<int>(offset_seconds / 60);
|
return static_cast<int>(offset_seconds / 60);
|
||||||
@ -403,10 +402,7 @@ inline std::string errno_to_string(char buf[256], int res)
|
|||||||
{
|
{
|
||||||
return std::string(buf);
|
return std::string(buf);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
return "Unknown error";
|
return "Unknown error";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return errno string (thread safe)
|
// Return errno string (thread safe)
|
||||||
@ -482,9 +478,9 @@ inline bool in_terminal(FILE* file)
|
|||||||
{
|
{
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
return _isatty(_fileno(file)) ? true : false;
|
return _isatty(_fileno(file)) != 0;
|
||||||
#else
|
#else
|
||||||
return isatty(fileno(file)) ? true : false;
|
return isatty(fileno(file)) != 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
} //os
|
} //os
|
||||||
|
@ -27,27 +27,23 @@ namespace details
|
|||||||
class flag_formatter
|
class flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~flag_formatter()
|
virtual ~flag_formatter() = default;
|
||||||
{}
|
|
||||||
virtual void format(details::log_msg& msg, const std::tm& tm_time) = 0;
|
virtual void format(details::log_msg& msg, const std::tm& tm_time) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
// name & level pattern appenders
|
// name & level pattern appenders
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
namespace
|
class name_formatter : public flag_formatter
|
||||||
{
|
|
||||||
class name_formatter:public flag_formatter
|
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
msg.formatted << *msg.logger_name;
|
msg.formatted << *msg.logger_name;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// log level appender
|
// log level appender
|
||||||
class level_formatter:public flag_formatter
|
class level_formatter : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -56,7 +52,7 @@ class level_formatter:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// short log level appender
|
// short log level appender
|
||||||
class short_level_formatter:public flag_formatter
|
class short_level_formatter : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -80,7 +76,7 @@ static int to12h(const tm& t)
|
|||||||
|
|
||||||
//Abbreviated weekday name
|
//Abbreviated weekday name
|
||||||
static const std::string days[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
static const std::string days[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
||||||
class a_formatter:public flag_formatter
|
class a_formatter : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -90,7 +86,7 @@ class a_formatter:public flag_formatter
|
|||||||
|
|
||||||
//Full weekday name
|
//Full weekday name
|
||||||
static const std::string full_days[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
|
static const std::string full_days[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
|
||||||
class A_formatter:public flag_formatter
|
class A_formatter : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -100,7 +96,7 @@ class A_formatter:public flag_formatter
|
|||||||
|
|
||||||
//Abbreviated month
|
//Abbreviated month
|
||||||
static const std::string months[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
|
static const std::string months[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
|
||||||
class b_formatter:public flag_formatter
|
class b_formatter : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -110,7 +106,7 @@ class b_formatter:public flag_formatter
|
|||||||
|
|
||||||
//Full month name
|
//Full month name
|
||||||
static const std::string full_months[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
|
static const std::string full_months[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
|
||||||
class B_formatter:public flag_formatter
|
class B_formatter : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -118,7 +114,6 @@ class B_formatter:public flag_formatter
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//write 2 ints separated by sep with padding of 2
|
//write 2 ints separated by sep with padding of 2
|
||||||
static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, char sep)
|
static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, char sep)
|
||||||
{
|
{
|
||||||
@ -133,9 +128,8 @@ static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, int v
|
|||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Date and time representation (Thu Aug 23 15:35:46 2014)
|
//Date and time representation (Thu Aug 23 15:35:46 2014)
|
||||||
class c_formatter SPDLOG_FINAL:public flag_formatter
|
class c_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -144,9 +138,8 @@ class c_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// year - 2 digit
|
// year - 2 digit
|
||||||
class C_formatter SPDLOG_FINAL:public flag_formatter
|
class C_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -154,10 +147,8 @@ class C_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
|
// Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
|
||||||
class D_formatter SPDLOG_FINAL:public flag_formatter
|
class D_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -165,9 +156,8 @@ class D_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// year - 4 digit
|
// year - 4 digit
|
||||||
class Y_formatter SPDLOG_FINAL:public flag_formatter
|
class Y_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -176,7 +166,7 @@ class Y_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// month 1-12
|
// month 1-12
|
||||||
class m_formatter SPDLOG_FINAL:public flag_formatter
|
class m_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -185,7 +175,7 @@ class m_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// day of month 1-31
|
// day of month 1-31
|
||||||
class d_formatter SPDLOG_FINAL:public flag_formatter
|
class d_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -194,7 +184,7 @@ class d_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// hours in 24 format 0-23
|
// hours in 24 format 0-23
|
||||||
class H_formatter SPDLOG_FINAL:public flag_formatter
|
class H_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -203,7 +193,7 @@ class H_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// hours in 12 format 1-12
|
// hours in 12 format 1-12
|
||||||
class I_formatter SPDLOG_FINAL:public flag_formatter
|
class I_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -212,7 +202,7 @@ class I_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// minutes 0-59
|
// minutes 0-59
|
||||||
class M_formatter SPDLOG_FINAL:public flag_formatter
|
class M_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -221,7 +211,7 @@ class M_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// seconds 0-59
|
// seconds 0-59
|
||||||
class S_formatter SPDLOG_FINAL:public flag_formatter
|
class S_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -230,7 +220,7 @@ class S_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// milliseconds
|
// milliseconds
|
||||||
class e_formatter SPDLOG_FINAL:public flag_formatter
|
class e_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -241,7 +231,7 @@ class e_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// microseconds
|
// microseconds
|
||||||
class f_formatter SPDLOG_FINAL:public flag_formatter
|
class f_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -252,7 +242,7 @@ class f_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// nanoseconds
|
// nanoseconds
|
||||||
class F_formatter SPDLOG_FINAL:public flag_formatter
|
class F_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -262,7 +252,7 @@ class F_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class E_formatter SPDLOG_FINAL:public flag_formatter
|
class E_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -273,7 +263,7 @@ class E_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// AM/PM
|
// AM/PM
|
||||||
class p_formatter SPDLOG_FINAL:public flag_formatter
|
class p_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -281,9 +271,8 @@ class p_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// 12 hour clock 02:55:02 pm
|
// 12 hour clock 02:55:02 pm
|
||||||
class r_formatter SPDLOG_FINAL:public flag_formatter
|
class r_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -292,7 +281,7 @@ class r_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 24-hour HH:MM time, equivalent to %H:%M
|
// 24-hour HH:MM time, equivalent to %H:%M
|
||||||
class R_formatter SPDLOG_FINAL:public flag_formatter
|
class R_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -301,7 +290,7 @@ class R_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
|
// ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
|
||||||
class T_formatter SPDLOG_FINAL:public flag_formatter
|
class T_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -310,13 +299,12 @@ class T_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// ISO 8601 offset from UTC in timezone (+-HH:MM)
|
// ISO 8601 offset from UTC in timezone (+-HH:MM)
|
||||||
class z_formatter SPDLOG_FINAL:public flag_formatter
|
class z_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
const std::chrono::seconds cache_refresh = std::chrono::seconds(5);
|
const std::chrono::seconds cache_refresh = std::chrono::seconds(5);
|
||||||
|
|
||||||
z_formatter():_last_update(std::chrono::seconds(0)), _offset_minutes(0)
|
z_formatter() = default;
|
||||||
{}
|
|
||||||
z_formatter(const z_formatter&) = delete;
|
z_formatter(const z_formatter&) = delete;
|
||||||
z_formatter& operator=(const z_formatter&) = delete;
|
z_formatter& operator=(const z_formatter&) = delete;
|
||||||
|
|
||||||
@ -347,13 +335,12 @@ public:
|
|||||||
pad_n_join(msg.formatted, h, m, ':');
|
pad_n_join(msg.formatted, h, m, ':');
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
log_clock::time_point _last_update;
|
log_clock::time_point _last_update{ std::chrono::seconds(0) };
|
||||||
int _offset_minutes;
|
int _offset_minutes{ 0 };
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
|
|
||||||
int get_cached_offset(const log_msg& msg, const std::tm& tm_time)
|
int get_cached_offset(const log_msg& msg, const std::tm& tm_time)
|
||||||
{
|
{
|
||||||
using namespace std::chrono;
|
|
||||||
std::lock_guard<std::mutex> l(_mutex);
|
std::lock_guard<std::mutex> l(_mutex);
|
||||||
if (msg.time - _last_update >= cache_refresh)
|
if (msg.time - _last_update >= cache_refresh)
|
||||||
{
|
{
|
||||||
@ -364,10 +351,8 @@ private:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Thread id
|
// Thread id
|
||||||
class t_formatter SPDLOG_FINAL:public flag_formatter
|
class t_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -376,7 +361,7 @@ class t_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Current pid
|
// Current pid
|
||||||
class pid_formatter SPDLOG_FINAL:public flag_formatter
|
class pid_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -385,7 +370,7 @@ class pid_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
// message counter formatter
|
// message counter formatter
|
||||||
class i_formatter SPDLOG_FINAL :public flag_formatter
|
class i_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -393,7 +378,7 @@ class i_formatter SPDLOG_FINAL :public flag_formatter
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class v_formatter SPDLOG_FINAL:public flag_formatter
|
class v_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm&) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
@ -401,7 +386,7 @@ class v_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ch_formatter SPDLOG_FINAL:public flag_formatter
|
class ch_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit ch_formatter(char ch): _ch(ch)
|
explicit ch_formatter(char ch): _ch(ch)
|
||||||
@ -416,11 +401,11 @@ private:
|
|||||||
|
|
||||||
|
|
||||||
//aggregate user chars to display as is
|
//aggregate user chars to display as is
|
||||||
class aggregate_formatter SPDLOG_FINAL:public flag_formatter
|
class aggregate_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
aggregate_formatter()
|
aggregate_formatter() = default;
|
||||||
{}
|
|
||||||
void add_ch(char ch)
|
void add_ch(char ch)
|
||||||
{
|
{
|
||||||
_str += ch;
|
_str += ch;
|
||||||
@ -435,7 +420,7 @@ private:
|
|||||||
|
|
||||||
// Full info formatter
|
// Full info formatter
|
||||||
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
|
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
|
||||||
class full_formatter SPDLOG_FINAL:public flag_formatter
|
class full_formatter SPDLOG_FINAL : public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
@ -487,8 +472,9 @@ class full_formatter SPDLOG_FINAL:public flag_formatter
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// pattern_formatter inline impl
|
// pattern_formatter inline impl
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern, pattern_time_type pattern_time, const std::string& eol)
|
inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern, pattern_time_type pattern_time, std::string eol) :
|
||||||
: _eol(eol), _pattern_time(pattern_time)
|
_eol(std::move(eol)),
|
||||||
|
_pattern_time(pattern_time)
|
||||||
{
|
{
|
||||||
compile_pattern(pattern);
|
compile_pattern(pattern);
|
||||||
}
|
}
|
||||||
@ -663,8 +649,9 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
|
|||||||
inline std::tm spdlog::pattern_formatter::get_time(details::log_msg& msg)
|
inline std::tm spdlog::pattern_formatter::get_time(details::log_msg& msg)
|
||||||
{
|
{
|
||||||
if (_pattern_time == pattern_time_type::local)
|
if (_pattern_time == pattern_time_type::local)
|
||||||
|
{
|
||||||
return details::os::localtime(log_clock::to_time_t(msg.time));
|
return details::os::localtime(log_clock::to_time_t(msg.time));
|
||||||
else
|
}
|
||||||
return details::os::gmtime(log_clock::to_time_t(msg.time));
|
return details::os::gmtime(log_clock::to_time_t(msg.time));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,9 +26,12 @@ namespace spdlog
|
|||||||
{
|
{
|
||||||
namespace details
|
namespace details
|
||||||
{
|
{
|
||||||
template <class Mutex> class registry_t
|
template <class Mutex>
|
||||||
|
class registry_t
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
registry_t<Mutex>(const registry_t<Mutex>&) = delete;
|
||||||
|
registry_t<Mutex>& operator=(const registry_t<Mutex>&) = delete;
|
||||||
|
|
||||||
void register_logger(std::shared_ptr<logger> logger)
|
void register_logger(std::shared_ptr<logger> logger)
|
||||||
{
|
{
|
||||||
@ -38,7 +41,6 @@ public:
|
|||||||
_loggers[logger_name] = logger;
|
_loggers[logger_name] = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::shared_ptr<logger> get(const std::string& logger_name)
|
std::shared_ptr<logger> get(const std::string& logger_name)
|
||||||
{
|
{
|
||||||
std::lock_guard<Mutex> lock(_mutex);
|
std::lock_guard<Mutex> lock(_mutex);
|
||||||
@ -111,6 +113,7 @@ public:
|
|||||||
std::lock_guard<Mutex> lock(_mutex);
|
std::lock_guard<Mutex> lock(_mutex);
|
||||||
_loggers.clear();
|
_loggers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks)
|
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks)
|
||||||
{
|
{
|
||||||
return create(logger_name, sinks.begin(), sinks.end());
|
return create(logger_name, sinks.begin(), sinks.end());
|
||||||
@ -194,15 +197,14 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
registry_t<Mutex>() {}
|
registry_t<Mutex>() = default;
|
||||||
registry_t<Mutex>(const registry_t<Mutex>&) = delete;
|
|
||||||
registry_t<Mutex>& operator=(const registry_t<Mutex>&) = delete;
|
|
||||||
|
|
||||||
void throw_if_exists(const std::string &logger_name)
|
void throw_if_exists(const std::string &logger_name)
|
||||||
{
|
{
|
||||||
if (_loggers.find(logger_name) != _loggers.end())
|
if (_loggers.find(logger_name) != _loggers.end())
|
||||||
throw spdlog_ex("logger with name '" + logger_name + "' already exists");
|
throw spdlog_ex("logger with name '" + logger_name + "' already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
Mutex _mutex;
|
Mutex _mutex;
|
||||||
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
|
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
|
||||||
formatter_ptr _formatter;
|
formatter_ptr _formatter;
|
||||||
@ -212,14 +214,16 @@ private:
|
|||||||
bool _async_mode = false;
|
bool _async_mode = false;
|
||||||
size_t _async_q_size = 0;
|
size_t _async_q_size = 0;
|
||||||
async_overflow_policy _overflow_policy = async_overflow_policy::block_retry;
|
async_overflow_policy _overflow_policy = async_overflow_policy::block_retry;
|
||||||
std::function<void()> _worker_warmup_cb = nullptr;
|
std::function<void()> _worker_warmup_cb;
|
||||||
std::chrono::milliseconds _flush_interval_ms;
|
std::chrono::milliseconds _flush_interval_ms;
|
||||||
std::function<void()> _worker_teardown_cb = nullptr;
|
std::function<void()> _worker_teardown_cb;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef SPDLOG_NO_REGISTRY_MUTEX
|
#ifdef SPDLOG_NO_REGISTRY_MUTEX
|
||||||
typedef registry_t<spdlog::details::null_mutex> registry;
|
using registry = registry_t<spdlog::details::null_mutex>;
|
||||||
#else
|
#else
|
||||||
typedef registry_t<std::mutex> registry;
|
using registry = registry_t<std::mutex>;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
|
|
||||||
inline void spdlog::register_logger(std::shared_ptr<logger> logger)
|
inline void spdlog::register_logger(std::shared_ptr<logger> logger)
|
||||||
{
|
{
|
||||||
return details::registry::instance().register_logger(logger);
|
return details::registry::instance().register_logger(std::move(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
|
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
|
||||||
@ -183,13 +183,11 @@ inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Create logger with multiple sinks
|
//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 std::string& logger_name, spdlog::sinks_init_list sinks)
|
||||||
{
|
{
|
||||||
return details::registry::instance().create(logger_name, sinks);
|
return details::registry::instance().create(logger_name, sinks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Sink, typename... Args>
|
template <typename Sink, typename... Args>
|
||||||
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, Args... args)
|
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, Args... args)
|
||||||
{
|
{
|
||||||
@ -197,7 +195,6 @@ inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_
|
|||||||
return details::registry::instance().create(logger_name, { sink });
|
return details::registry::instance().create(logger_name, { sink });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class It>
|
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 std::string& logger_name, const It& sinks_begin, const It& sinks_end)
|
||||||
{
|
{
|
||||||
@ -224,7 +221,7 @@ inline std::shared_ptr<spdlog::logger> spdlog::create_async(const std::string& l
|
|||||||
|
|
||||||
inline void spdlog::set_formatter(spdlog::formatter_ptr f)
|
inline void spdlog::set_formatter(spdlog::formatter_ptr f)
|
||||||
{
|
{
|
||||||
details::registry::instance().formatter(f);
|
details::registry::instance().formatter(std::move(f));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::set_pattern(const std::string& format_string)
|
inline void spdlog::set_pattern(const std::string& format_string)
|
||||||
@ -244,10 +241,9 @@ inline void spdlog::flush_on(level::level_enum log_level)
|
|||||||
|
|
||||||
inline void spdlog::set_error_handler(log_err_handler handler)
|
inline void spdlog::set_error_handler(log_err_handler handler)
|
||||||
{
|
{
|
||||||
return details::registry::instance().set_error_handler(handler);
|
return details::registry::instance().set_error_handler(std::move(handler));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)
|
inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)
|
||||||
{
|
{
|
||||||
details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb);
|
details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb);
|
||||||
@ -260,7 +256,7 @@ inline void spdlog::set_sync_mode()
|
|||||||
|
|
||||||
inline void spdlog::apply_all(std::function<void(std::shared_ptr<logger>)> fun)
|
inline void spdlog::apply_all(std::function<void(std::shared_ptr<logger>)> fun)
|
||||||
{
|
{
|
||||||
details::registry::instance().apply_all(fun);
|
details::registry::instance().apply_all(std::move(fun));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::drop_all()
|
inline void spdlog::drop_all()
|
||||||
|
@ -21,18 +21,18 @@ class flag_formatter;
|
|||||||
class formatter
|
class formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~formatter() {}
|
virtual ~formatter() = default;
|
||||||
virtual void format(details::log_msg& msg) = 0;
|
virtual void format(details::log_msg& msg) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class pattern_formatter SPDLOG_FINAL : public formatter
|
class pattern_formatter SPDLOG_FINAL : public formatter
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit pattern_formatter(const std::string& pattern, pattern_time_type pattern_time = pattern_time_type::local, const std::string& eol = spdlog::details::os::default_eol);
|
explicit pattern_formatter(const std::string& pattern, pattern_time_type pattern_time = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
|
||||||
pattern_formatter(const pattern_formatter&) = delete;
|
pattern_formatter(const pattern_formatter&) = delete;
|
||||||
pattern_formatter& operator=(const pattern_formatter&) = delete;
|
pattern_formatter& operator=(const pattern_formatter&) = delete;
|
||||||
void format(details::log_msg& msg) override;
|
void format(details::log_msg& msg) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::string _eol;
|
const std::string _eol;
|
||||||
const std::string _pattern;
|
const std::string _pattern;
|
||||||
@ -45,4 +45,3 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
#include "details/pattern_formatter_impl.h"
|
#include "details/pattern_formatter_impl.h"
|
||||||
|
|
||||||
|
@ -25,16 +25,17 @@ namespace spdlog
|
|||||||
class logger
|
class logger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
logger(const std::string& logger_name, sink_ptr single_sink);
|
logger(const std::string& name, sink_ptr single_sink);
|
||||||
logger(const std::string& name, sinks_init_list);
|
logger(const std::string& name, sinks_init_list sinks);
|
||||||
template<class It>
|
|
||||||
logger(const std::string& name, const It& begin, const It& end);
|
template <class It>
|
||||||
|
logger(std::string name, const It& begin, const It& end);
|
||||||
|
|
||||||
virtual ~logger();
|
virtual ~logger();
|
||||||
|
|
||||||
logger(const logger&) = delete;
|
logger(const logger&) = delete;
|
||||||
logger& operator=(const logger&) = delete;
|
logger& operator=(const logger&) = delete;
|
||||||
|
|
||||||
|
|
||||||
template <typename... Args> void log(level::level_enum lvl, const char* fmt, const Args&... args);
|
template <typename... Args> void log(level::level_enum lvl, const char* fmt, const Args&... args);
|
||||||
template <typename... Args> void log(level::level_enum lvl, const char* msg);
|
template <typename... Args> void log(level::level_enum lvl, const char* msg);
|
||||||
template <typename Arg1, typename... Args> void trace(const char* fmt, const Arg1&, const Args&... args);
|
template <typename Arg1, typename... Args> void trace(const char* fmt, const Arg1&, const Args&... args);
|
||||||
@ -44,7 +45,6 @@ public:
|
|||||||
template <typename Arg1, typename... Args> void error(const char* fmt, const Arg1&, const Args&... args);
|
template <typename Arg1, typename... Args> void error(const char* fmt, const Arg1&, const Args&... args);
|
||||||
template <typename Arg1, typename... Args> void critical(const char* fmt, const Arg1&, const Args&... args);
|
template <typename Arg1, typename... Args> void critical(const char* fmt, const Arg1&, const Args&... args);
|
||||||
|
|
||||||
|
|
||||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
template <typename... Args> void log(level::level_enum lvl, const wchar_t* msg);
|
template <typename... Args> void log(level::level_enum lvl, const wchar_t* msg);
|
||||||
template <typename... Args> void log(level::level_enum lvl, const wchar_t* fmt, const Args&... args);
|
template <typename... Args> void log(level::level_enum lvl, const wchar_t* fmt, const Args&... args);
|
||||||
@ -57,19 +57,19 @@ public:
|
|||||||
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
|
|
||||||
template <typename T> void log(level::level_enum lvl, const T&);
|
template <typename T> void log(level::level_enum lvl, const T&);
|
||||||
template <typename T> void trace(const T&);
|
template <typename T> void trace(const T& msg);
|
||||||
template <typename T> void debug(const T&);
|
template <typename T> void debug(const T& msg);
|
||||||
template <typename T> void info(const T&);
|
template <typename T> void info(const T& msg);
|
||||||
template <typename T> void warn(const T&);
|
template <typename T> void warn(const T& msg);
|
||||||
template <typename T> void error(const T&);
|
template <typename T> void error(const T& msg);
|
||||||
template <typename T> void critical(const T&);
|
template <typename T> void critical(const T& msg);
|
||||||
|
|
||||||
bool should_log(level::level_enum) const;
|
bool should_log(level::level_enum msg_level) const;
|
||||||
void set_level(level::level_enum);
|
void set_level(level::level_enum log_level);
|
||||||
level::level_enum level() const;
|
level::level_enum level() const;
|
||||||
const std::string& name() const;
|
const std::string& name() const;
|
||||||
void set_pattern(const std::string&, pattern_time_type = pattern_time_type::local);
|
void set_pattern(const std::string& pattern, pattern_time_type pattern_time = pattern_time_type::local);
|
||||||
void set_formatter(formatter_ptr);
|
void set_formatter(formatter_ptr msg_formatter);
|
||||||
|
|
||||||
// automatically call flush() if message level >= log_level
|
// automatically call flush() if message level >= log_level
|
||||||
void flush_on(level::level_enum log_level);
|
void flush_on(level::level_enum log_level);
|
||||||
@ -79,22 +79,22 @@ public:
|
|||||||
const std::vector<sink_ptr>& sinks() const;
|
const std::vector<sink_ptr>& sinks() const;
|
||||||
|
|
||||||
// error handler
|
// error handler
|
||||||
virtual void set_error_handler(log_err_handler);
|
virtual void set_error_handler(log_err_handler err_handler);
|
||||||
virtual log_err_handler error_handler();
|
virtual log_err_handler error_handler();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void _sink_it(details::log_msg&);
|
virtual void _sink_it(details::log_msg& msg);
|
||||||
virtual void _set_pattern(const std::string&, pattern_time_type);
|
virtual void _set_pattern(const std::string& pattern, pattern_time_type pattern_time);
|
||||||
virtual void _set_formatter(formatter_ptr);
|
virtual void _set_formatter(formatter_ptr msg_formatter);
|
||||||
|
|
||||||
// default error handler: print the error to stderr with the max rate of 1 message/minute
|
// default error handler: print the error to stderr with the max rate of 1 message/minute
|
||||||
virtual void _default_err_handler(const std::string &msg);
|
virtual void _default_err_handler(const std::string &msg);
|
||||||
|
|
||||||
// return true if the given message level should trigger a flush
|
// return true if the given message level should trigger a flush
|
||||||
bool _should_flush_on(const details::log_msg&);
|
bool _should_flush_on(const details::log_msg& msg);
|
||||||
|
|
||||||
// increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER))
|
// increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER))
|
||||||
void _incr_msg_counter(details::log_msg &msg);
|
void _incr_msg_counter(details::log_msg& msg);
|
||||||
|
|
||||||
const std::string _name;
|
const std::string _name;
|
||||||
std::vector<sink_ptr> _sinks;
|
std::vector<sink_ptr> _sinks;
|
||||||
|
@ -23,10 +23,10 @@ namespace sinks
|
|||||||
* If no color terminal detected, omit the escape codes.
|
* If no color terminal detected, omit the escape codes.
|
||||||
*/
|
*/
|
||||||
template <class Mutex>
|
template <class Mutex>
|
||||||
class ansicolor_sink: public base_sink<Mutex>
|
class ansicolor_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ansicolor_sink(FILE* file): target_file_(file)
|
explicit ansicolor_sink(FILE* file) : target_file_(file)
|
||||||
{
|
{
|
||||||
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
|
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
|
||||||
colors_[level::trace] = cyan;
|
colors_[level::trace] = cyan;
|
||||||
@ -37,7 +37,8 @@ public:
|
|||||||
colors_[level::critical] = bold + on_red;
|
colors_[level::critical] = bold + on_red;
|
||||||
colors_[level::off] = reset;
|
colors_[level::off] = reset;
|
||||||
}
|
}
|
||||||
virtual ~ansicolor_sink()
|
|
||||||
|
~ansicolor_sink() override
|
||||||
{
|
{
|
||||||
_flush();
|
_flush();
|
||||||
}
|
}
|
||||||
@ -79,7 +80,7 @@ public:
|
|||||||
const std::string on_white = "\033[47m";
|
const std::string on_white = "\033[47m";
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void _sink_it(const details::log_msg& msg) override
|
void _sink_it(const details::log_msg& msg) override
|
||||||
{
|
{
|
||||||
// Wrap the originally formatted message in color codes.
|
// Wrap the originally formatted message in color codes.
|
||||||
// If color is not supported in the terminal, log as is instead.
|
// If color is not supported in the terminal, log as is instead.
|
||||||
@ -102,6 +103,7 @@ protected:
|
|||||||
{
|
{
|
||||||
fflush(target_file_);
|
fflush(target_file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* target_file_;
|
FILE* target_file_;
|
||||||
bool should_do_colors_;
|
bool should_do_colors_;
|
||||||
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
|
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
|
||||||
@ -116,6 +118,9 @@ public:
|
|||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<std::mutex>;
|
||||||
|
using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::null_mutex>;
|
||||||
|
|
||||||
template<class Mutex>
|
template<class Mutex>
|
||||||
class ansicolor_stderr_sink: public ansicolor_sink<Mutex>
|
class ansicolor_stderr_sink: public ansicolor_sink<Mutex>
|
||||||
{
|
{
|
||||||
@ -124,11 +129,8 @@ public:
|
|||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef ansicolor_stdout_sink<std::mutex> ansicolor_stdout_sink_mt;
|
using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<std::mutex>;
|
||||||
typedef ansicolor_stdout_sink<details::null_mutex> ansicolor_stdout_sink_st;
|
using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::null_mutex>;
|
||||||
|
|
||||||
typedef ansicolor_stderr_sink<std::mutex> ansicolor_stderr_sink_mt;
|
|
||||||
typedef ansicolor_stderr_sink<details::null_mutex> ansicolor_stderr_sink_st;
|
|
||||||
|
|
||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
@ -22,11 +22,10 @@ namespace spdlog
|
|||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
template<class Mutex>
|
template<class Mutex>
|
||||||
class base_sink:public sink
|
class base_sink : public sink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
base_sink():_mutex() {}
|
base_sink() = default;
|
||||||
virtual ~base_sink() = default;
|
|
||||||
|
|
||||||
base_sink(const base_sink&) = delete;
|
base_sink(const base_sink&) = delete;
|
||||||
base_sink& operator=(const base_sink&) = delete;
|
base_sink& operator=(const base_sink&) = delete;
|
||||||
@ -36,6 +35,7 @@ public:
|
|||||||
std::lock_guard<Mutex> lock(_mutex);
|
std::lock_guard<Mutex> lock(_mutex);
|
||||||
_sink_it(msg);
|
_sink_it(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush() SPDLOG_FINAL override
|
void flush() SPDLOG_FINAL override
|
||||||
{
|
{
|
||||||
std::lock_guard<Mutex> lock(_mutex);
|
std::lock_guard<Mutex> lock(_mutex);
|
||||||
|
@ -22,13 +22,12 @@ namespace spdlog
|
|||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
template<class Mutex>
|
template<class Mutex>
|
||||||
class dist_sink: public base_sink<Mutex>
|
class dist_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit dist_sink() :_sinks() {}
|
explicit dist_sink() :_sinks() {}
|
||||||
dist_sink(const dist_sink&) = delete;
|
dist_sink(const dist_sink&) = delete;
|
||||||
dist_sink& operator=(const dist_sink&) = delete;
|
dist_sink& operator=(const dist_sink&) = delete;
|
||||||
virtual ~dist_sink() = default;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::shared_ptr<sink>> _sinks;
|
std::vector<std::shared_ptr<sink>> _sinks;
|
||||||
@ -51,8 +50,6 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
||||||
void add_sink(std::shared_ptr<sink> sink)
|
void add_sink(std::shared_ptr<sink> sink)
|
||||||
{
|
{
|
||||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
|
||||||
@ -66,7 +63,8 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef dist_sink<std::mutex> dist_sink_mt;
|
using dist_sink_mt = dist_sink<std::mutex>;
|
||||||
typedef dist_sink<details::null_mutex> dist_sink_st;
|
using dist_sink_st = dist_sink<details::null_mutex>;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,8 @@ namespace sinks
|
|||||||
/*
|
/*
|
||||||
* Trivial file sink with single file as target
|
* Trivial file sink with single file as target
|
||||||
*/
|
*/
|
||||||
template<class Mutex>
|
template <class Mutex>
|
||||||
class simple_file_sink SPDLOG_FINAL : public base_sink < Mutex >
|
class simple_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit simple_file_sink(const filename_t &filename, bool truncate = false):_force_flush(false)
|
explicit simple_file_sink(const filename_t &filename, bool truncate = false):_force_flush(false)
|
||||||
@ -46,32 +46,32 @@ protected:
|
|||||||
if(_force_flush)
|
if(_force_flush)
|
||||||
_file_helper.flush();
|
_file_helper.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _flush() override
|
void _flush() override
|
||||||
{
|
{
|
||||||
_file_helper.flush();
|
_file_helper.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
details::file_helper _file_helper;
|
details::file_helper _file_helper;
|
||||||
bool _force_flush;
|
bool _force_flush;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef simple_file_sink<std::mutex> simple_file_sink_mt;
|
using simple_file_sink_mt = simple_file_sink<std::mutex>;
|
||||||
typedef simple_file_sink<details::null_mutex> simple_file_sink_st;
|
using simple_file_sink_st = simple_file_sink<details::null_mutex>;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Rotating file sink based on size
|
* Rotating file sink based on size
|
||||||
*/
|
*/
|
||||||
template<class Mutex>
|
template <class Mutex>
|
||||||
class rotating_file_sink SPDLOG_FINAL : public base_sink < Mutex >
|
class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
rotating_file_sink(const filename_t &base_filename,
|
rotating_file_sink(filename_t base_filename,
|
||||||
std::size_t max_size, std::size_t max_files) :
|
std::size_t max_size, std::size_t max_files) :
|
||||||
_base_filename(base_filename),
|
_base_filename(std::move(base_filename)),
|
||||||
_max_size(max_size),
|
_max_size(max_size),
|
||||||
_max_files(max_files),
|
_max_files(max_files)
|
||||||
_current_size(0),
|
|
||||||
_file_helper()
|
|
||||||
{
|
{
|
||||||
_file_helper.open(calc_filename(_base_filename, 0));
|
_file_helper.open(calc_filename(_base_filename, 0));
|
||||||
_current_size = _file_helper.size(); //expensive. called only once
|
_current_size = _file_helper.size(); //expensive. called only once
|
||||||
@ -82,7 +82,7 @@ public:
|
|||||||
static filename_t calc_filename(const filename_t& filename, std::size_t index)
|
static filename_t calc_filename(const filename_t& filename, std::size_t index)
|
||||||
{
|
{
|
||||||
typename std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
|
typename std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
|
||||||
if (index)
|
if (index != 0u)
|
||||||
{
|
{
|
||||||
filename_t basename, ext;
|
filename_t basename, ext;
|
||||||
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
|
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
|
||||||
@ -112,7 +112,6 @@ protected:
|
|||||||
_file_helper.flush();
|
_file_helper.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Rotate files:
|
// Rotate files:
|
||||||
// log.txt -> log.1.txt
|
// log.txt -> log.1.txt
|
||||||
@ -135,13 +134,14 @@ private:
|
|||||||
throw spdlog_ex("rotating_file_sink: failed removing " + filename_to_str(target), errno);
|
throw spdlog_ex("rotating_file_sink: failed removing " + filename_to_str(target), errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (details::file_helper::file_exists(src) && details::os::rename(src, target))
|
if (details::file_helper::file_exists(src) && details::os::rename(src, target) != 0)
|
||||||
{
|
{
|
||||||
throw spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
|
throw spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_file_helper.reopen(true);
|
_file_helper.reopen(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
filename_t _base_filename;
|
filename_t _base_filename;
|
||||||
std::size_t _max_size;
|
std::size_t _max_size;
|
||||||
std::size_t _max_files;
|
std::size_t _max_files;
|
||||||
@ -149,8 +149,8 @@ private:
|
|||||||
details::file_helper _file_helper;
|
details::file_helper _file_helper;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef rotating_file_sink<std::mutex> rotating_file_sink_mt;
|
using rotating_file_sink_mt = rotating_file_sink<std::mutex>;
|
||||||
typedef rotating_file_sink<details::null_mutex>rotating_file_sink_st;
|
using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Default generator of daily log file names.
|
* Default generator of daily log file names.
|
||||||
@ -195,9 +195,10 @@ class daily_file_sink SPDLOG_FINAL :public base_sink < Mutex >
|
|||||||
public:
|
public:
|
||||||
//create daily file sink which rotates on given time
|
//create daily file sink which rotates on given time
|
||||||
daily_file_sink(
|
daily_file_sink(
|
||||||
const filename_t& base_filename,
|
filename_t base_filename,
|
||||||
int rotation_hour,
|
int rotation_hour,
|
||||||
int rotation_minute) : _base_filename(base_filename),
|
int rotation_minute) :
|
||||||
|
_base_filename(std::move(base_filename)),
|
||||||
_rotation_h(rotation_hour),
|
_rotation_h(rotation_hour),
|
||||||
_rotation_m(rotation_minute)
|
_rotation_m(rotation_minute)
|
||||||
{
|
{
|
||||||
@ -235,9 +236,10 @@ private:
|
|||||||
date.tm_sec = 0;
|
date.tm_sec = 0;
|
||||||
auto rotation_time = std::chrono::system_clock::from_time_t(std::mktime(&date));
|
auto rotation_time = std::chrono::system_clock::from_time_t(std::mktime(&date));
|
||||||
if (rotation_time > now)
|
if (rotation_time > now)
|
||||||
|
{
|
||||||
return rotation_time;
|
return rotation_time;
|
||||||
else
|
}
|
||||||
return std::chrono::system_clock::time_point(rotation_time + std::chrono::hours(24));
|
return{ rotation_time + std::chrono::hours(24) };
|
||||||
}
|
}
|
||||||
|
|
||||||
filename_t _base_filename;
|
filename_t _base_filename;
|
||||||
@ -247,7 +249,8 @@ private:
|
|||||||
details::file_helper _file_helper;
|
details::file_helper _file_helper;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef daily_file_sink<std::mutex> daily_file_sink_mt;
|
using daily_file_sink_mt = daily_file_sink<std::mutex>;
|
||||||
typedef daily_file_sink<details::null_mutex> daily_file_sink_st;
|
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,15 +23,13 @@ namespace sinks
|
|||||||
* MSVC sink (logging using OutputDebugStringA)
|
* MSVC sink (logging using OutputDebugStringA)
|
||||||
*/
|
*/
|
||||||
template<class Mutex>
|
template<class Mutex>
|
||||||
class msvc_sink : public base_sink < Mutex >
|
class msvc_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit msvc_sink()
|
explicit msvc_sink()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg& msg) override
|
void _sink_it(const details::log_msg& msg) override
|
||||||
{
|
{
|
||||||
@ -42,8 +40,8 @@ protected:
|
|||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef msvc_sink<std::mutex> msvc_sink_mt;
|
using msvc_sink_mt = msvc_sink<std::mutex>;
|
||||||
typedef msvc_sink<details::null_mutex> msvc_sink_st;
|
using msvc_sink_st = msvc_sink<details::null_mutex>;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ namespace sinks
|
|||||||
{
|
{
|
||||||
|
|
||||||
template <class Mutex>
|
template <class Mutex>
|
||||||
class null_sink : public base_sink < Mutex >
|
class null_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg&) override
|
void _sink_it(const details::log_msg&) override
|
||||||
@ -26,8 +26,9 @@ protected:
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
};
|
};
|
||||||
typedef null_sink<details::null_mutex> null_sink_st;
|
|
||||||
typedef null_sink<details::null_mutex> null_sink_mt;
|
using null_sink_mt = null_sink<details::null_mutex>;
|
||||||
|
using null_sink_st = null_sink<details::null_mutex>;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,12 @@ namespace spdlog
|
|||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
template<class Mutex>
|
template<class Mutex>
|
||||||
class ostream_sink: public base_sink<Mutex>
|
class ostream_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit ostream_sink(std::ostream& os, bool force_flush=false) :_ostream(os), _force_flush(force_flush) {}
|
explicit ostream_sink(std::ostream& os, bool force_flush=false) :_ostream(os), _force_flush(force_flush) {}
|
||||||
ostream_sink(const ostream_sink&) = delete;
|
ostream_sink(const ostream_sink&) = delete;
|
||||||
ostream_sink& operator=(const ostream_sink&) = delete;
|
ostream_sink& operator=(const ostream_sink&) = delete;
|
||||||
virtual ~ostream_sink() = default;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg& msg) override
|
void _sink_it(const details::log_msg& msg) override
|
||||||
@ -41,7 +40,8 @@ protected:
|
|||||||
bool _force_flush;
|
bool _force_flush;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef ostream_sink<std::mutex> ostream_sink_mt;
|
using ostream_sink_mt = ostream_sink<std::mutex>;
|
||||||
typedef ostream_sink<details::null_mutex> ostream_sink_st;
|
using ostream_sink_st = ostream_sink<details::null_mutex>;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../details/log_msg.h"
|
#include "../details/log_msg.h"
|
||||||
@ -15,12 +14,8 @@ namespace sinks
|
|||||||
class sink
|
class sink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
sink()
|
virtual ~sink() = default;
|
||||||
{
|
|
||||||
_level = level::trace;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~sink() {}
|
|
||||||
virtual void log(const details::log_msg& msg) = 0;
|
virtual void log(const details::log_msg& msg) = 0;
|
||||||
virtual void flush() = 0;
|
virtual void flush() = 0;
|
||||||
|
|
||||||
@ -29,8 +24,7 @@ public:
|
|||||||
level::level_enum level() const;
|
level::level_enum level() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
level_t _level;
|
level_t _level{ level::trace };
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool sink::should_log(level::level_enum msg_level) const
|
inline bool sink::should_log(level::level_enum msg_level) const
|
||||||
@ -50,4 +44,3 @@ inline level::level_enum sink::level() const
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,14 +21,16 @@ template <class Mutex>
|
|||||||
class stdout_sink SPDLOG_FINAL : public base_sink<Mutex>
|
class stdout_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
using MyType = stdout_sink<Mutex>;
|
using MyType = stdout_sink<Mutex>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
stdout_sink()
|
explicit stdout_sink() = default;
|
||||||
{}
|
|
||||||
static std::shared_ptr<MyType> instance()
|
static std::shared_ptr<MyType> instance()
|
||||||
{
|
{
|
||||||
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
|
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg& msg) override
|
void _sink_it(const details::log_msg& msg) override
|
||||||
{
|
{
|
||||||
@ -42,22 +44,23 @@ protected:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef stdout_sink<details::null_mutex> stdout_sink_st;
|
using stdout_sink_mt = stdout_sink<std::mutex>;
|
||||||
typedef stdout_sink<std::mutex> stdout_sink_mt;
|
using stdout_sink_st = stdout_sink<details::null_mutex>;
|
||||||
|
|
||||||
|
|
||||||
template <class Mutex>
|
template <class Mutex>
|
||||||
class stderr_sink SPDLOG_FINAL : public base_sink<Mutex>
|
class stderr_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
using MyType = stderr_sink<Mutex>;
|
using MyType = stderr_sink<Mutex>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
stderr_sink()
|
explicit stderr_sink() = default;
|
||||||
{}
|
|
||||||
static std::shared_ptr<MyType> instance()
|
static std::shared_ptr<MyType> instance()
|
||||||
{
|
{
|
||||||
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
|
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg& msg) override
|
void _sink_it(const details::log_msg& msg) override
|
||||||
{
|
{
|
||||||
@ -71,7 +74,8 @@ protected:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef stderr_sink<std::mutex> stderr_sink_mt;
|
using stderr_sink_mt = stderr_sink<std::mutex>;
|
||||||
typedef stderr_sink<details::null_mutex> stderr_sink_st;
|
using stderr_sink_st = stderr_sink<details::null_mutex>;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,8 @@ public:
|
|||||||
//set ident to be program name if empty
|
//set ident to be program name if empty
|
||||||
::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility);
|
::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility);
|
||||||
}
|
}
|
||||||
~syslog_sink()
|
|
||||||
|
~syslog_sink() override
|
||||||
{
|
{
|
||||||
::closelog();
|
::closelog();
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,8 @@ namespace sinks
|
|||||||
/*
|
/*
|
||||||
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
|
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
|
||||||
*/
|
*/
|
||||||
template<class Mutex>
|
template <class Mutex>
|
||||||
class wincolor_sink: public base_sink<Mutex>
|
class wincolor_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
const WORD BOLD = FOREGROUND_INTENSITY;
|
const WORD BOLD = FOREGROUND_INTENSITY;
|
||||||
@ -42,7 +42,7 @@ public:
|
|||||||
colors_[level::off] = 0;
|
colors_[level::off] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~wincolor_sink()
|
~wincolor_sink() override
|
||||||
{
|
{
|
||||||
this->flush();
|
this->flush();
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void _sink_it(const details::log_msg& msg) override
|
void _sink_it(const details::log_msg& msg) override
|
||||||
{
|
{
|
||||||
auto color = colors_[msg.level];
|
auto color = colors_[msg.level];
|
||||||
auto orig_attribs = set_console_attribs(color);
|
auto orig_attribs = set_console_attribs(color);
|
||||||
@ -66,7 +66,7 @@ protected:
|
|||||||
SetConsoleTextAttribute(out_handle_, orig_attribs); //reset to orig colors
|
SetConsoleTextAttribute(out_handle_, orig_attribs); //reset to orig colors
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void _flush() override
|
void _flush() override
|
||||||
{
|
{
|
||||||
// windows console always flushed?
|
// windows console always flushed?
|
||||||
}
|
}
|
||||||
@ -92,30 +92,30 @@ private:
|
|||||||
//
|
//
|
||||||
// windows color console to stdout
|
// windows color console to stdout
|
||||||
//
|
//
|
||||||
template<class Mutex>
|
template <class Mutex>
|
||||||
class wincolor_stdout_sink: public wincolor_sink<Mutex>
|
class wincolor_stdout_sink : public wincolor_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wincolor_stdout_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_OUTPUT_HANDLE))
|
wincolor_stdout_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_OUTPUT_HANDLE))
|
||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef wincolor_stdout_sink<std::mutex> wincolor_stdout_sink_mt;
|
using wincolor_stdout_sink_mt = wincolor_stdout_sink<std::mutex>;
|
||||||
typedef wincolor_stdout_sink<details::null_mutex> wincolor_stdout_sink_st;
|
using wincolor_stdout_sink_st = wincolor_stdout_sink<details::null_mutex>;
|
||||||
|
|
||||||
//
|
//
|
||||||
// windows color console to stderr
|
// windows color console to stderr
|
||||||
//
|
//
|
||||||
template<class Mutex>
|
template <class Mutex>
|
||||||
class wincolor_stderr_sink: public wincolor_sink<Mutex>
|
class wincolor_stderr_sink : public wincolor_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wincolor_stderr_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_ERROR_HANDLE))
|
wincolor_stderr_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_ERROR_HANDLE))
|
||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef wincolor_stderr_sink<std::mutex> wincolor_stderr_sink_mt;
|
using wincolor_stderr_sink_mt = wincolor_stderr_sink<std::mutex>;
|
||||||
typedef wincolor_stderr_sink<details::null_mutex> wincolor_stderr_sink_st;
|
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::null_mutex>;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,11 +17,11 @@ namespace sinks
|
|||||||
/*
|
/*
|
||||||
* Windows debug sink (logging using OutputDebugStringA, synonym for msvc_sink)
|
* Windows debug sink (logging using OutputDebugStringA, synonym for msvc_sink)
|
||||||
*/
|
*/
|
||||||
template<class Mutex>
|
template <class Mutex>
|
||||||
using windebug_sink = msvc_sink<Mutex>;
|
using windebug_sink = msvc_sink<Mutex>;
|
||||||
|
|
||||||
typedef msvc_sink_mt windebug_sink_mt;
|
using windebug_sink_mt = msvc_sink_mt;
|
||||||
typedef msvc_sink_st windebug_sink_st;
|
using windebug_sink_st = msvc_sink_st;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ void flush_on(level::level_enum log_level);
|
|||||||
//
|
//
|
||||||
// Set global error handler
|
// Set global error handler
|
||||||
//
|
//
|
||||||
void set_error_handler(log_err_handler);
|
void set_error_handler(log_err_handler handler);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Turn on async mode (off by default) and set the queue size for each async_logger.
|
// Turn on async mode (off by default) and set the queue size for each async_logger.
|
||||||
@ -129,7 +129,7 @@ std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_b
|
|||||||
// Example:
|
// Example:
|
||||||
// spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename");
|
// spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename");
|
||||||
template <typename Sink, typename... Args>
|
template <typename Sink, typename... Args>
|
||||||
std::shared_ptr<spdlog::logger> create(const std::string& logger_name, Args...);
|
std::shared_ptr<spdlog::logger> create(const std::string& logger_name, Args... args);
|
||||||
|
|
||||||
// Create and register an async logger with a single sink
|
// Create and register an async logger with a single sink
|
||||||
std::shared_ptr<logger> create_async(const std::string& logger_name, const sink_ptr& sink, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function<void()>& worker_teardown_cb = nullptr);
|
std::shared_ptr<logger> create_async(const std::string& logger_name, const sink_ptr& sink, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function<void()>& worker_teardown_cb = nullptr);
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
*/
|
*/
|
||||||
#include "includes.h"
|
#include "includes.h"
|
||||||
|
|
||||||
using namespace spdlog::details;
|
using spdlog::details::log_msg;
|
||||||
|
using spdlog::details::file_helper;
|
||||||
|
|
||||||
static const std::string target_filename = "logs/file_helper_test.txt";
|
static const std::string target_filename = "logs/file_helper_test.txt";
|
||||||
|
|
||||||
@ -15,7 +16,6 @@ static void write_with_helper(file_helper &helper, size_t howmany)
|
|||||||
helper.flush();
|
helper.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("file_helper_filename", "[file_helper::filename()]]")
|
TEST_CASE("file_helper_filename", "[file_helper::filename()]]")
|
||||||
{
|
{
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
@ -25,8 +25,6 @@ TEST_CASE("file_helper_filename", "[file_helper::filename()]]")
|
|||||||
REQUIRE(helper.filename() == target_filename);
|
REQUIRE(helper.filename() == target_filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("file_helper_size", "[file_helper::size()]]")
|
TEST_CASE("file_helper_size", "[file_helper::size()]]")
|
||||||
{
|
{
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
@ -40,7 +38,6 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]")
|
|||||||
REQUIRE(get_filesize(target_filename) == expected_size);
|
REQUIRE(get_filesize(target_filename) == expected_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]")
|
TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]")
|
||||||
{
|
{
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
@ -73,8 +70,6 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
|
|||||||
REQUIRE(helper.size() == expected_size);
|
REQUIRE(helper.size() == expected_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void test_split_ext(const char* fname, const char* expect_base, const char* expect_ext)
|
static void test_split_ext(const char* fname, const char* expect_base, const char* expect_ext)
|
||||||
{
|
{
|
||||||
spdlog::filename_t filename(fname);
|
spdlog::filename_t filename(fname);
|
||||||
@ -91,7 +86,6 @@ static void test_split_ext(const char* fname, const char* expect_base, const cha
|
|||||||
REQUIRE(ext == expected_ext);
|
REQUIRE(ext == expected_ext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("file_helper_split_by_extenstion", "[file_helper::split_by_extenstion()]]")
|
TEST_CASE("file_helper_split_by_extenstion", "[file_helper::split_by_extenstion()]]")
|
||||||
{
|
{
|
||||||
test_split_ext("mylog.txt", "mylog", ".txt");
|
test_split_ext("mylog.txt", "mylog", ".txt");
|
||||||
@ -113,6 +107,3 @@ TEST_CASE("file_helper_split_by_extenstion", "[file_helper::split_by_extenstion(
|
|||||||
test_split_ext(".", ".", "");
|
test_split_ext(".", ".", "");
|
||||||
test_split_ext("..txt", ".", ".txt");
|
test_split_ext("..txt", ".", ".txt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
#include "includes.h"
|
#include "includes.h"
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
@ -16,11 +15,6 @@ std::string log_info(const T& what, spdlog::level::level_enum logger_level = spd
|
|||||||
return oss.str().substr(0, oss.str().length() - strlen(spdlog::details::os::default_eol));
|
return oss.str().substr(0, oss.str().length() - strlen(spdlog::details::os::default_eol));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("basic_logging ", "[basic_logging]")
|
TEST_CASE("basic_logging ", "[basic_logging]")
|
||||||
{
|
{
|
||||||
//const char
|
//const char
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "includes.h"
|
#include "includes.h"
|
||||||
|
|
||||||
// log to str and return it
|
// log to str and return it
|
||||||
static std::string log_to_str(const std::string& msg, std::shared_ptr<spdlog::formatter> formatter = nullptr)
|
static std::string log_to_str(const std::string& msg, const std::shared_ptr<spdlog::formatter>& formatter = nullptr)
|
||||||
{
|
{
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||||
@ -11,6 +11,7 @@ static std::string log_to_str(const std::string& msg, std::shared_ptr<spdlog::fo
|
|||||||
oss_logger.info(msg);
|
oss_logger.info(msg);
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("custom eol", "[pattern_formatter]")
|
TEST_CASE("custom eol", "[pattern_formatter]")
|
||||||
{
|
{
|
||||||
std::string msg = "Hello custom eol test";
|
std::string msg = "Hello custom eol test";
|
||||||
@ -38,7 +39,6 @@ TEST_CASE("level", "[pattern_formatter]")
|
|||||||
REQUIRE(log_to_str("Some message", formatter) == "[info] Some message\n");
|
REQUIRE(log_to_str("Some message", formatter) == "[info] Some message\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("short level", "[pattern_formatter]")
|
TEST_CASE("short level", "[pattern_formatter]")
|
||||||
{
|
{
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("[%L] %v", spdlog::pattern_time_type::local, "\n");
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("[%L] %v", spdlog::pattern_time_type::local, "\n");
|
||||||
@ -51,19 +51,11 @@ TEST_CASE("name", "[pattern_formatter]")
|
|||||||
REQUIRE(log_to_str("Some message", formatter) == "[pattern_tester] Some message\n");
|
REQUIRE(log_to_str("Some message", formatter) == "[pattern_tester] Some message\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
|
TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
|
||||||
{
|
{
|
||||||
using namespace::std::chrono;
|
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("%D %v", spdlog::pattern_time_type::local, "\n");
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%D %v", spdlog::pattern_time_type::local, "\n");
|
||||||
auto now_tm = spdlog::details::os::localtime();
|
auto now_tm = spdlog::details::os::localtime();
|
||||||
std::stringstream oss;
|
std::stringstream oss;
|
||||||
oss << std::setfill('0') << std::setw(2) << now_tm.tm_mon + 1 << "/" << now_tm.tm_mday << "/" << (now_tm.tm_year + 1900) % 1000 << " Some message\n";
|
oss << std::setfill('0') << std::setw(2) << now_tm.tm_mon + 1 << "/" << now_tm.tm_mday << "/" << (now_tm.tm_year + 1900) % 1000 << " Some message\n";
|
||||||
REQUIRE(log_to_str("Some message", formatter) == oss.str());
|
REQUIRE(log_to_str("Some message", formatter) == oss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user