Skip to content

Commit edf727b

Browse files
committed
Refactor: modernize code with C++17 features and style updates
- Replace macros with `constexpr` and `inline` variables for level names - Add `noexcept` to getters and improve `const` correctness - Standardize pointer alignment and modernize type aliases - General cleanup for better C++17/20 compatibility
1 parent 309204d commit edf727b

Some content is hidden

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

43 files changed

+447
-377
lines changed

include/spdlog/async_logger-inl.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
3232

3333
// send the log message to the thread pool
3434
SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){
35-
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
35+
SPDLOG_TRY{if (const auto pool_ptr = thread_pool_.lock()){
3636
pool_ptr -> post_log(shared_from_this(), msg, overflow_policy_);
3737
}
3838
else {
@@ -44,7 +44,7 @@ SPDLOG_LOGGER_CATCH(msg.source)
4444

4545
// send flush request to the thread pool
4646
SPDLOG_INLINE void spdlog::async_logger::flush_(){
47-
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
47+
SPDLOG_TRY{if (const auto pool_ptr = thread_pool_.lock()){
4848
pool_ptr -> post_flush(shared_from_this(), overflow_policy_);
4949
}
5050
else {
@@ -58,7 +58,7 @@ SPDLOG_LOGGER_CATCH(source_loc())
5858
// backend functions - called from the thread pool to do the actual job
5959
//
6060
SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) {
61-
for (auto &sink : sinks_) {
61+
for (const auto &sink : sinks_) {
6262
if (sink->should_log(msg.level)) {
6363
SPDLOG_TRY { sink->log(msg); }
6464
SPDLOG_LOGGER_CATCH(msg.source)
@@ -71,13 +71,14 @@ SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg
7171
}
7272

7373
SPDLOG_INLINE void spdlog::async_logger::backend_flush_() {
74-
for (auto &sink : sinks_) {
74+
for (const auto &sink : sinks_) {
7575
SPDLOG_TRY { sink->flush(); }
7676
SPDLOG_LOGGER_CATCH(source_loc())
7777
}
7878
}
7979

80-
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name) {
80+
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(
81+
std::string new_name) const {
8182
auto cloned = std::make_shared<spdlog::async_logger>(*this);
8283
cloned->name_ = std::move(new_name);
8384
return cloned;

include/spdlog/async_logger.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
#include <spdlog/logger.h>
1818

19+
#include <cstdint>
20+
#include <memory>
21+
1922
namespace spdlog {
2023

2124
// Async overflow policy - block by default.
22-
enum class async_overflow_policy {
25+
enum class async_overflow_policy : std::uint8_t {
2326
block, // Block until message can be enqueued
2427
overrun_oldest, // Discard oldest message in the queue if full when trying to
2528
// add new item.
@@ -55,7 +58,7 @@ class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_
5558
std::weak_ptr<details::thread_pool> tp,
5659
async_overflow_policy overflow_policy = async_overflow_policy::block);
5760

58-
std::shared_ptr<logger> clone(std::string new_name) override;
61+
std::shared_ptr<logger> clone(std::string new_name) const override;
5962

6063
protected:
6164
void sink_it_(const details::log_msg &msg) override;

include/spdlog/common-inl.h

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,25 @@
88
#endif
99

1010
#include <algorithm>
11+
#include <cassert>
1112
#include <iterator>
1213

1314
namespace spdlog {
1415
namespace level {
1516

16-
#if __cplusplus >= 201703L
17-
constexpr
17+
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string& name) SPDLOG_NOEXCEPT {
18+
#if __cplusplus >= 202002L
19+
if (const auto level_iter{std::ranges::find(SPDLOG_LEVEL_NAMES, name)};
20+
level_iter != std::end(SPDLOG_LEVEL_NAMES)) {
21+
return static_cast<level::level_enum>(
22+
std::ranges::distance(begin(SPDLOG_LEVEL_NAMES), level_iter));
23+
}
24+
#else
25+
const auto level_iter{find(begin(level::SPDLOG_LEVEL_NAMES), end(SPDLOG_LEVEL_NAMES), name)};
26+
if (level_iter != std::end(SPDLOG_LEVEL_NAMES)) {
27+
return static_cast<level::level_enum>(std::distance(begin(SPDLOG_LEVEL_NAMES), level_iter));
28+
}
1829
#endif
19-
static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES;
20-
21-
static const char *short_level_names[] SPDLOG_SHORT_LEVEL_NAMES;
22-
23-
SPDLOG_INLINE const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT {
24-
return level_string_views[l];
25-
}
26-
27-
SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT {
28-
return short_level_names[l];
29-
}
30-
31-
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT {
32-
auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name);
33-
if (it != std::end(level_string_views))
34-
return static_cast<level::level_enum>(std::distance(std::begin(level_string_views), it));
3530

3631
// check also for "warn" and "err" before giving up..
3732
if (name == "warn") {
@@ -47,7 +42,7 @@ SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG
4742
SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg)
4843
: msg_(std::move(msg)) {}
4944

50-
SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) {
45+
SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string& msg, int last_errno) {
5146
#ifdef SPDLOG_USE_STD_FORMAT
5247
msg_ = std::system_error(std::error_code(last_errno, std::generic_category()), msg).what();
5348
#else
@@ -57,9 +52,9 @@ SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) {
5752
#endif
5853
}
5954

60-
SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT { return msg_.c_str(); }
55+
SPDLOG_INLINE const char* spdlog_ex::what() const SPDLOG_NOEXCEPT { return msg_.c_str(); }
6156

62-
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno) {
57+
SPDLOG_INLINE void throw_spdlog_ex(const std::string& msg, int last_errno) {
6358
SPDLOG_THROW(spdlog_ex(msg, last_errno));
6459
}
6560

include/spdlog/common.h

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <spdlog/details/null_mutex.h>
77
#include <spdlog/tweakme.h>
88

9+
#include <array>
910
#include <atomic>
1011
#include <chrono>
1112
#include <cstdio>
@@ -93,6 +94,12 @@
9394
#define SPDLOG_DEPRECATED
9495
#endif
9596

97+
#if __cplusplus >= 201703L
98+
#define SPDLOG_NODISCARD [[nodiscard]]
99+
#else
100+
#define SPDLOG_NODISCARD
101+
#endif
102+
96103
// disable thread local on msvc 2013
97104
#ifndef SPDLOG_NO_TLS
98105
#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
@@ -101,7 +108,7 @@
101108
#endif
102109

103110
#ifndef SPDLOG_FUNCTION
104-
#define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
111+
#define SPDLOG_FUNCTION static_cast<const char*>(__FUNCTION__)
105112
#endif
106113

107114
#ifdef SPDLOG_NO_EXCEPTIONS
@@ -115,8 +122,8 @@
115122
#else
116123
#define SPDLOG_TRY try
117124
#define SPDLOG_THROW(ex) throw(ex)
118-
#define SPDLOG_CATCH_STD \
119-
catch (const std::exception &) { \
125+
#define SPDLOG_CATCH_STD \
126+
catch (const std::exception&) { \
120127
}
121128
#endif
122129

@@ -141,7 +148,7 @@ using filename_t = std::string;
141148
using log_clock = std::chrono::system_clock;
142149
using sink_ptr = std::shared_ptr<sinks::sink>;
143150
using sinks_init_list = std::initializer_list<sink_ptr>;
144-
using err_handler = std::function<void(const std::string &err_msg)>;
151+
using err_handler = std::function<void(const std::string& err_msg)>;
145152
#ifdef SPDLOG_USE_STD_FORMAT
146153
namespace fmt_lib = std;
147154

@@ -253,32 +260,61 @@ enum level_enum : int {
253260
n_levels
254261
};
255262

256-
#define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5)
257-
#define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5)
258-
#define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4)
259-
#define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7)
260-
#define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5)
261-
#define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8)
262-
#define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)
263+
#if __cplusplus >= 201703L
264+
SPDLOG_INLINE SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_TRACE{"trace", 5};
265+
SPDLOG_INLINE SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_DEBUG{"debug", 5};
266+
SPDLOG_INLINE SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_INFO{"info", 4};
267+
SPDLOG_INLINE SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_WARNING{"warning", 7};
268+
SPDLOG_INLINE SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_ERROR{"error", 5};
269+
SPDLOG_INLINE SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_CRITICAL{"critical", 8};
270+
SPDLOG_INLINE SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_OFF{"off", 3};
271+
#else
272+
SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_TRACE{"trace", 5};
273+
SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_DEBUG{"debug", 5};
274+
SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_INFO{"info", 4};
275+
SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_WARNING{"warning", 7};
276+
SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_ERROR{"error", 5};
277+
SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_CRITICAL{"critical", 8};
278+
SPDLOG_CONSTEXPR spdlog::string_view_t SPDLOG_LEVEL_NAME_OFF{"off", 3};
279+
#endif
263280

264281
#if !defined(SPDLOG_LEVEL_NAMES)
265-
#define SPDLOG_LEVEL_NAMES \
266-
{ \
267-
SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, \
268-
SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, SPDLOG_LEVEL_NAME_CRITICAL, \
269-
SPDLOG_LEVEL_NAME_OFF \
270-
}
282+
using level_names_t = std::array<spdlog::string_view_t, n_levels>;
283+
284+
using level_names_value_t = level_names_t::value_type;
285+
286+
#if __cplusplus >= 201703L
287+
SPDLOG_INLINE
288+
#endif
289+
SPDLOG_CONSTEXPR level_names_t SPDLOG_LEVEL_NAMES{
290+
SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO,
291+
SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, SPDLOG_LEVEL_NAME_CRITICAL,
292+
SPDLOG_LEVEL_NAME_OFF};
271293
#endif
272294

273295
#if !defined(SPDLOG_SHORT_LEVEL_NAMES)
296+
using short_level_names_t = std::array<const char*, n_levels>;
297+
298+
using level_short_names_value_t = short_level_names_t::value_type;
299+
300+
#if __cplusplus >= 201703L
301+
SPDLOG_INLINE
302+
#endif
303+
SPDLOG_CONSTEXPR short_level_names_t SPDLOG_SHORT_LEVEL_NAMES{"T", "D", "I", "W", "E", "C", "O"};
274304

275-
#define SPDLOG_SHORT_LEVEL_NAMES \
276-
{ "T", "D", "I", "W", "E", "C", "O" }
277305
#endif
278306

279-
SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
280-
SPDLOG_API const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
281-
SPDLOG_API spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;
307+
SPDLOG_API SPDLOG_NODISCARD SPDLOG_INLINE SPDLOG_CONSTEXPR const level_names_value_t& to_string_view(
308+
spdlog::level::level_enum l) SPDLOG_NOEXCEPT {
309+
return SPDLOG_LEVEL_NAMES[l];
310+
}
311+
312+
SPDLOG_API SPDLOG_NODISCARD SPDLOG_INLINE SPDLOG_CONSTEXPR level_short_names_value_t
313+
to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT {
314+
return SPDLOG_SHORT_LEVEL_NAMES[l];
315+
}
316+
317+
SPDLOG_API spdlog::level::level_enum from_str(const std::string& name) SPDLOG_NOEXCEPT;
282318

283319
} // namespace level
284320

@@ -302,47 +338,41 @@ enum class pattern_time_type {
302338
class SPDLOG_API spdlog_ex : public std::exception {
303339
public:
304340
explicit spdlog_ex(std::string msg);
305-
spdlog_ex(const std::string &msg, int last_errno);
306-
const char *what() const SPDLOG_NOEXCEPT override;
341+
spdlog_ex(const std::string& msg, int last_errno);
342+
const char* what() const SPDLOG_NOEXCEPT override;
307343

308344
private:
309345
std::string msg_;
310346
};
311347

312-
[[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno);
348+
[[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string& msg, int last_errno);
313349
[[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg);
314350

315351
struct source_loc {
316352
SPDLOG_CONSTEXPR source_loc() = default;
317-
SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
353+
SPDLOG_CONSTEXPR source_loc(const char* filename_in, int line_in, const char* funcname_in)
318354
: filename{filename_in},
319355
line{line_in},
320356
funcname{funcname_in} {}
321357

322358
SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT { return line <= 0; }
323-
const char *filename{nullptr};
359+
const char* filename{nullptr};
324360
int line{0};
325-
const char *funcname{nullptr};
361+
const char* funcname{nullptr};
326362
};
327363

328364
struct file_event_handlers {
329-
file_event_handlers()
330-
: before_open(nullptr),
331-
after_open(nullptr),
332-
before_close(nullptr),
333-
after_close(nullptr) {}
334-
335-
std::function<void(const filename_t &filename)> before_open;
336-
std::function<void(const filename_t &filename, std::FILE *file_stream)> after_open;
337-
std::function<void(const filename_t &filename, std::FILE *file_stream)> before_close;
338-
std::function<void(const filename_t &filename)> after_close;
365+
std::function<void(const filename_t& filename)> before_open{nullptr};
366+
std::function<void(const filename_t& filename, std::FILE* file_stream)> after_open{nullptr};
367+
std::function<void(const filename_t& filename, std::FILE* file_stream)> before_close{nullptr};
368+
std::function<void(const filename_t& filename)> after_close{nullptr};
339369
};
340370

341371
namespace details {
342372

343373
// to_string_view
344374

345-
SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(const memory_buf_t &buf)
375+
SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(const memory_buf_t& buf)
346376
SPDLOG_NOEXCEPT {
347377
return spdlog::string_view_t{buf.data(), buf.size()};
348378
}
@@ -353,7 +383,7 @@ SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(spdlog::string_view_t
353383
}
354384

355385
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
356-
SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf)
386+
SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(const wmemory_buf_t& buf)
357387
SPDLOG_NOEXCEPT {
358388
return spdlog::wstring_view_t{buf.data(), buf.size()};
359389
}
@@ -381,7 +411,7 @@ template <bool B, class T = void>
381411
using enable_if_t = typename std::enable_if<B, T>::type;
382412

383413
template <typename T, typename... Args>
384-
std::unique_ptr<T> make_unique(Args &&...args) {
414+
std::unique_ptr<T> make_unique(Args&&... args) {
385415
static_assert(!std::is_array<T>::value, "arrays not supported");
386416
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
387417
}

include/spdlog/details/backtracer-inl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ SPDLOG_INLINE bool backtracer::empty() const {
5151
}
5252

5353
// pop all items in the q and apply the given fun on each of them.
54-
SPDLOG_INLINE void backtracer::foreach_pop(std::function<void(const details::log_msg &)> fun) {
54+
SPDLOG_INLINE void backtracer::foreach_pop(
55+
const std::function<void(const details::log_msg &)> &fun) {
5556
std::lock_guard<std::mutex> lock{mutex_};
5657
while (!messages_.empty()) {
5758
auto &front_msg = messages_.front();

include/spdlog/details/backtracer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SPDLOG_API backtracer {
3434
bool empty() const;
3535

3636
// pop all items in the q and apply the given fun on each of them.
37-
void foreach_pop(std::function<void(const details::log_msg &)> fun);
37+
void foreach_pop(const std::function<void(const details::log_msg &)> &fun);
3838
};
3939

4040
} // namespace details

0 commit comments

Comments
 (0)