Skip to content

Commit 76a0eb3

Browse files
committed
update SPDlog librabry
1 parent 86d5a39 commit 76a0eb3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+12477
-12284
lines changed

spdlog/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ THE SOFTWARE.
2222

2323
-- NOTE: Third party dependency used by this software --
2424
This software depends on the fmt lib (MIT License),
25-
and users must comply to its license: https://github.com/fmtlib/fmt/blob/master/LICENSE.rst
25+
and users must comply to its license: https://raw.githubusercontent.com/fmtlib/fmt/master/LICENSE
2626

spdlog/async.h

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#include <spdlog/details/registry.h>
1919
#include <spdlog/details/thread_pool.h>
2020

21+
#include <functional>
2122
#include <memory>
2223
#include <mutex>
23-
#include <functional>
2424

2525
namespace spdlog {
2626

@@ -31,27 +31,25 @@ static const size_t default_async_q_size = 8192;
3131
// async logger factory - creates async loggers backed with thread pool.
3232
// if a global thread pool doesn't already exist, create it with default queue
3333
// size of 8192 items and single thread.
34-
template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
35-
struct async_factory_impl
36-
{
37-
template<typename Sink, typename... SinkArgs>
38-
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args)
39-
{
34+
template <async_overflow_policy OverflowPolicy = async_overflow_policy::block>
35+
struct async_factory_impl {
36+
template <typename Sink, typename... SinkArgs>
37+
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args) {
4038
auto &registry_inst = details::registry::instance();
4139

4240
// create global thread pool if not already exists..
4341

4442
auto &mutex = registry_inst.tp_mutex();
4543
std::lock_guard<std::recursive_mutex> tp_lock(mutex);
4644
auto tp = registry_inst.get_tp();
47-
if (tp == nullptr)
48-
{
45+
if (tp == nullptr) {
4946
tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1U);
5047
registry_inst.set_tp(tp);
5148
}
5249

5350
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
54-
auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
51+
auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink),
52+
std::move(tp), OverflowPolicy);
5553
registry_inst.initialize_logger(new_logger);
5654
return new_logger;
5755
}
@@ -60,40 +58,42 @@ struct async_factory_impl
6058
using async_factory = async_factory_impl<async_overflow_policy::block>;
6159
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
6260

63-
template<typename Sink, typename... SinkArgs>
64-
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&...sink_args)
65-
{
66-
return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
61+
template <typename Sink, typename... SinkArgs>
62+
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name,
63+
SinkArgs &&...sink_args) {
64+
return async_factory::create<Sink>(std::move(logger_name),
65+
std::forward<SinkArgs>(sink_args)...);
6766
}
6867

69-
template<typename Sink, typename... SinkArgs>
70-
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&...sink_args)
71-
{
72-
return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
68+
template <typename Sink, typename... SinkArgs>
69+
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name,
70+
SinkArgs &&...sink_args) {
71+
return async_factory_nonblock::create<Sink>(std::move(logger_name),
72+
std::forward<SinkArgs>(sink_args)...);
7373
}
7474

7575
// set global thread pool.
76-
inline void init_thread_pool(
77-
size_t q_size, size_t thread_count, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
78-
{
79-
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start, on_thread_stop);
76+
inline void init_thread_pool(size_t q_size,
77+
size_t thread_count,
78+
std::function<void()> on_thread_start,
79+
std::function<void()> on_thread_stop) {
80+
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start,
81+
on_thread_stop);
8082
details::registry::instance().set_tp(std::move(tp));
8183
}
8284

83-
inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start)
84-
{
85+
inline void init_thread_pool(size_t q_size,
86+
size_t thread_count,
87+
std::function<void()> on_thread_start) {
8588
init_thread_pool(q_size, thread_count, on_thread_start, [] {});
8689
}
8790

88-
inline void init_thread_pool(size_t q_size, size_t thread_count)
89-
{
90-
init_thread_pool(
91-
q_size, thread_count, [] {}, [] {});
91+
inline void init_thread_pool(size_t q_size, size_t thread_count) {
92+
init_thread_pool(q_size, thread_count, [] {}, [] {});
9293
}
9394

9495
// get the global thread pool.
95-
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
96-
{
96+
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() {
9797
return details::registry::instance().get_tp();
9898
}
99-
} // namespace spdlog
99+
} // namespace spdlog

spdlog/async_logger-inl.h

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,38 @@
44
#pragma once
55

66
#ifndef SPDLOG_HEADER_ONLY
7-
# include <spdlog/async_logger.h>
7+
#include <spdlog/async_logger.h>
88
#endif
99

10-
#include <spdlog/sinks/sink.h>
1110
#include <spdlog/details/thread_pool.h>
11+
#include <spdlog/sinks/sink.h>
1212

1313
#include <memory>
1414
#include <string>
1515

16-
SPDLOG_INLINE spdlog::async_logger::async_logger(
17-
std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
18-
: async_logger(std::move(logger_name), sinks_list.begin(), sinks_list.end(), std::move(tp), overflow_policy)
19-
{}
16+
SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
17+
sinks_init_list sinks_list,
18+
std::weak_ptr<details::thread_pool> tp,
19+
async_overflow_policy overflow_policy)
20+
: async_logger(std::move(logger_name),
21+
sinks_list.begin(),
22+
sinks_list.end(),
23+
std::move(tp),
24+
overflow_policy) {}
2025

21-
SPDLOG_INLINE spdlog::async_logger::async_logger(
22-
std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
23-
: async_logger(std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy)
24-
{}
26+
SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
27+
sink_ptr single_sink,
28+
std::weak_ptr<details::thread_pool> tp,
29+
async_overflow_policy overflow_policy)
30+
: async_logger(
31+
std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {}
2532

2633
// send the log message to the thread pool
2734
SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){
28-
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
35+
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
36+
pool_ptr -> post_log(shared_from_this(), msg, overflow_policy_);
2937
}
30-
else
31-
{
38+
else {
3239
throw_spdlog_ex("async log: thread pool doesn't exist anymore");
3340
}
3441
}
@@ -37,10 +44,10 @@ SPDLOG_LOGGER_CATCH(msg.source)
3744

3845
// send flush request to the thread pool
3946
SPDLOG_INLINE void spdlog::async_logger::flush_(){
40-
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){pool_ptr->post_flush(shared_from_this(), overflow_policy_);
47+
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
48+
pool_ptr -> post_flush(shared_from_this(), overflow_policy_);
4149
}
42-
else
43-
{
50+
else {
4451
throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
4552
}
4653
}
@@ -50,40 +57,27 @@ SPDLOG_LOGGER_CATCH(source_loc())
5057
//
5158
// backend functions - called from the thread pool to do the actual job
5259
//
53-
SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg)
54-
{
55-
for (auto &sink : sinks_)
56-
{
57-
if (sink->should_log(msg.level))
58-
{
59-
SPDLOG_TRY
60-
{
61-
sink->log(msg);
62-
}
60+
SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) {
61+
for (auto &sink : sinks_) {
62+
if (sink->should_log(msg.level)) {
63+
SPDLOG_TRY { sink->log(msg); }
6364
SPDLOG_LOGGER_CATCH(msg.source)
6465
}
6566
}
6667

67-
if (should_flush_(msg))
68-
{
68+
if (should_flush_(msg)) {
6969
backend_flush_();
7070
}
7171
}
7272

73-
SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
74-
{
75-
for (auto &sink : sinks_)
76-
{
77-
SPDLOG_TRY
78-
{
79-
sink->flush();
80-
}
73+
SPDLOG_INLINE void spdlog::async_logger::backend_flush_() {
74+
for (auto &sink : sinks_) {
75+
SPDLOG_TRY { sink->flush(); }
8176
SPDLOG_LOGGER_CATCH(source_loc())
8277
}
8378
}
8479

85-
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
86-
{
80+
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name) {
8781
auto cloned = std::make_shared<spdlog::async_logger>(*this);
8882
cloned->name_ = std::move(new_name);
8983
return cloned;

spdlog/async_logger.h

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,41 @@
1919
namespace spdlog {
2020

2121
// Async overflow policy - block by default.
22-
enum class async_overflow_policy
23-
{
24-
block, // Block until message can be enqueued
25-
overrun_oldest // Discard oldest message in the queue if full when trying to
26-
// add new item.
22+
enum class async_overflow_policy {
23+
block, // Block until message can be enqueued
24+
overrun_oldest, // Discard oldest message in the queue if full when trying to
25+
// add new item.
26+
discard_new // Discard new message if the queue is full when trying to add new item.
2727
};
2828

2929
namespace details {
3030
class thread_pool;
3131
}
3232

33-
class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_logger>, public logger
34-
{
33+
class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_logger>,
34+
public logger {
3535
friend class details::thread_pool;
3636

3737
public:
38-
template<typename It>
39-
async_logger(std::string logger_name, It begin, It end, std::weak_ptr<details::thread_pool> tp,
40-
async_overflow_policy overflow_policy = async_overflow_policy::block)
41-
: logger(std::move(logger_name), begin, end)
42-
, thread_pool_(std::move(tp))
43-
, overflow_policy_(overflow_policy)
44-
{}
38+
template <typename It>
39+
async_logger(std::string logger_name,
40+
It begin,
41+
It end,
42+
std::weak_ptr<details::thread_pool> tp,
43+
async_overflow_policy overflow_policy = async_overflow_policy::block)
44+
: logger(std::move(logger_name), begin, end),
45+
thread_pool_(std::move(tp)),
46+
overflow_policy_(overflow_policy) {}
4547

46-
async_logger(std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp,
47-
async_overflow_policy overflow_policy = async_overflow_policy::block);
48+
async_logger(std::string logger_name,
49+
sinks_init_list sinks_list,
50+
std::weak_ptr<details::thread_pool> tp,
51+
async_overflow_policy overflow_policy = async_overflow_policy::block);
4852

49-
async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp,
50-
async_overflow_policy overflow_policy = async_overflow_policy::block);
53+
async_logger(std::string logger_name,
54+
sink_ptr single_sink,
55+
std::weak_ptr<details::thread_pool> tp,
56+
async_overflow_policy overflow_policy = async_overflow_policy::block);
5157

5258
std::shared_ptr<logger> clone(std::string new_name) override;
5359

@@ -61,8 +67,8 @@ class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_
6167
std::weak_ptr<details::thread_pool> thread_pool_;
6268
async_overflow_policy overflow_policy_;
6369
};
64-
} // namespace spdlog
70+
} // namespace spdlog
6571

6672
#ifdef SPDLOG_HEADER_ONLY
67-
# include "async_logger-inl.h"
73+
#include "async_logger-inl.h"
6874
#endif

spdlog/cfg/argv.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,20 @@ namespace spdlog {
2121
namespace cfg {
2222

2323
// search for SPDLOG_LEVEL= in the args and use it to init the levels
24-
inline void load_argv_levels(int argc, const char **argv)
25-
{
24+
inline void load_argv_levels(int argc, const char **argv) {
2625
const std::string spdlog_level_prefix = "SPDLOG_LEVEL=";
27-
for (int i = 1; i < argc; i++)
28-
{
26+
for (int i = 1; i < argc; i++) {
2927
std::string arg = argv[i];
30-
if (arg.find(spdlog_level_prefix) == 0)
31-
{
28+
if (arg.find(spdlog_level_prefix) == 0) {
3229
auto levels_string = arg.substr(spdlog_level_prefix.size());
3330
helpers::load_levels(levels_string);
3431
}
3532
}
3633
}
3734

38-
inline void load_argv_levels(int argc, char **argv)
39-
{
35+
inline void load_argv_levels(int argc, char **argv) {
4036
load_argv_levels(argc, const_cast<const char **>(argv));
4137
}
4238

43-
} // namespace cfg
44-
} // namespace spdlog
39+
} // namespace cfg
40+
} // namespace spdlog

spdlog/cfg/env.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#pragma once
55
#include <spdlog/cfg/helpers.h>
6-
#include <spdlog/details/registry.h>
76
#include <spdlog/details/os.h>
7+
#include <spdlog/details/registry.h>
88

99
//
1010
// Init levels and patterns from env variables SPDLOG_LEVEL
@@ -25,14 +25,12 @@
2525

2626
namespace spdlog {
2727
namespace cfg {
28-
inline void load_env_levels()
29-
{
30-
auto env_val = details::os::getenv("SPDLOG_LEVEL");
31-
if (!env_val.empty())
32-
{
28+
inline void load_env_levels(const char* var = "SPDLOG_LEVEL") {
29+
auto env_val = details::os::getenv(var);
30+
if (!env_val.empty()) {
3331
helpers::load_levels(env_val);
3432
}
3533
}
3634

37-
} // namespace cfg
38-
} // namespace spdlog
35+
} // namespace cfg
36+
} // namespace spdlog

0 commit comments

Comments
 (0)