Skip to content

Commit 21b15d3

Browse files
author
Grok Compression
committed
spdlog: update
1 parent 22c32e3 commit 21b15d3

Some content is hidden

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

53 files changed

+7361
-6918
lines changed

src/include/spdlog/async.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ inline void init_thread_pool(size_t q_size,
8989
}
9090

9191
inline void init_thread_pool(size_t q_size, size_t thread_count) {
92-
init_thread_pool(
93-
q_size, thread_count, [] {}, [] {});
92+
init_thread_pool(q_size, thread_count, [] {}, [] {});
9493
}
9594

9695
// get the global thread pool.

src/include/spdlog/async_logger-inl.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
3333
// send the log message to the thread pool
3434
SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){
3535
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
36-
pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
36+
pool_ptr -> post_log(shared_from_this(), msg, overflow_policy_);
3737
}
3838
else {
3939
throw_spdlog_ex("async log: thread pool doesn't exist anymore");
@@ -43,15 +43,13 @@ SPDLOG_LOGGER_CATCH(msg.source)
4343
}
4444

4545
// send flush request to the thread pool
46-
SPDLOG_INLINE void spdlog::async_logger::flush_(){SPDLOG_TRY{auto pool_ptr = thread_pool_.lock();
47-
if (!pool_ptr) {
46+
SPDLOG_INLINE void spdlog::async_logger::flush_(){
47+
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
48+
pool_ptr -> post_flush(shared_from_this(), overflow_policy_);
49+
}
50+
else {
4851
throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
4952
}
50-
51-
std::future<void> future = pool_ptr->post_flush(shared_from_this(), overflow_policy_);
52-
// Wait for the flush operation to complete.
53-
// This might throw exception if the flush message get dropped because of overflow.
54-
future.get();
5553
}
5654
SPDLOG_LOGGER_CATCH(source_loc())
5755
}

src/include/spdlog/cfg/env.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
namespace spdlog {
2727
namespace cfg {
28-
inline void load_env_levels() {
29-
auto env_val = details::os::getenv("SPDLOG_LEVEL");
28+
inline void load_env_levels(const char* var = "SPDLOG_LEVEL") {
29+
auto env_val = details::os::getenv(var);
3030
if (!env_val.empty()) {
3131
helpers::load_levels(env_val);
3232
}

src/include/spdlog/common.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,7 @@ SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(spdlog::wstring_view
364364
}
365365
#endif
366366

367-
#ifndef SPDLOG_USE_STD_FORMAT
368-
template <typename T, typename... Args>
369-
inline fmt::basic_string_view<T> to_string_view(fmt::basic_format_string<T, Args...> fmt) {
370-
return fmt;
371-
}
372-
#elif __cpp_lib_format >= 202207L
367+
#if defined(SPDLOG_USE_STD_FORMAT) && __cpp_lib_format >= 202207L
373368
template <typename T, typename... Args>
374369
SPDLOG_CONSTEXPR_FUNC std::basic_string_view<T> to_string_view(
375370
std::basic_format_string<T, Args...> fmt) SPDLOG_NOEXCEPT {

src/include/spdlog/details/file_helper-inl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf) {
101101
if (fd_ == nullptr) return;
102102
size_t msg_size = buf.size();
103103
auto data = buf.data();
104-
if (std::fwrite(data, 1, msg_size, fd_) != msg_size) {
104+
105+
if (!details::os::fwrite_bytes(data, msg_size, fd_)) {
105106
throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno);
106107
}
107108
}

src/include/spdlog/details/mpmc_blocking_q.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,19 @@ class mpmc_blocking_queue {
148148
#endif
149149

150150
size_t overrun_counter() {
151-
std::unique_lock<std::mutex> lock(queue_mutex_);
151+
std::lock_guard<std::mutex> lock(queue_mutex_);
152152
return q_.overrun_counter();
153153
}
154154

155155
size_t discard_counter() { return discard_counter_.load(std::memory_order_relaxed); }
156156

157157
size_t size() {
158-
std::unique_lock<std::mutex> lock(queue_mutex_);
158+
std::lock_guard<std::mutex> lock(queue_mutex_);
159159
return q_.size();
160160
}
161161

162162
void reset_overrun_counter() {
163-
std::unique_lock<std::mutex> lock(queue_mutex_);
163+
std::lock_guard<std::mutex> lock(queue_mutex_);
164164
q_.reset_overrun_counter();
165165
}
166166

src/include/spdlog/details/os-inl.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,10 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
265265
return offset;
266266
#else
267267

268-
#if defined(sun) || defined(__sun) || defined(_AIX) || \
269-
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
270-
(!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
268+
#if defined(sun) || defined(__sun) || defined(_AIX) || \
269+
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
270+
(!defined(__APPLE__) && !defined(_BSD_SOURCE) && !defined(_GNU_SOURCE) && \
271+
(!defined(_POSIX_VERSION) || (_POSIX_VERSION < 202405L)))
271272
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
272273
struct helper {
273274
static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(),
@@ -482,13 +483,12 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
482483
}
483484

484485
// find the size to allocate for the result buffer
485-
int result_size =
486-
::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
486+
int result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0);
487487

488488
if (result_size > 0) {
489489
target.resize(result_size);
490-
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size,
491-
target.data(), result_size);
490+
result_size =
491+
::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), result_size);
492492
if (result_size > 0) {
493493
assert(result_size == target.size());
494494
return;
@@ -589,6 +589,18 @@ SPDLOG_INLINE bool fsync(FILE *fp) {
589589
#endif
590590
}
591591

592+
// Do non-locking fwrite if possible by the os or use the regular locking fwrite
593+
// Return true on success.
594+
SPDLOG_INLINE bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) {
595+
#if defined(_WIN32) && defined(SPDLOG_FWRITE_UNLOCKED)
596+
return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes;
597+
#elif defined(SPDLOG_FWRITE_UNLOCKED)
598+
return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes;
599+
#else
600+
return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes;
601+
#endif
602+
}
603+
592604
} // namespace os
593605
} // namespace details
594606
} // namespace spdlog

src/include/spdlog/details/os.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ SPDLOG_API std::string getenv(const char *field);
114114
// Return true on success.
115115
SPDLOG_API bool fsync(FILE *fp);
116116

117+
// Do non-locking fwrite if possible by the os or use the regular locking fwrite
118+
// Return true on success.
119+
SPDLOG_API bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp);
120+
117121
} // namespace os
118122
} // namespace details
119123
} // namespace spdlog

src/include/spdlog/details/registry-inl.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ SPDLOG_INLINE void registry::register_logger(std::shared_ptr<logger> new_logger)
5454
register_logger_(std::move(new_logger));
5555
}
5656

57+
SPDLOG_INLINE void registry::register_or_replace(std::shared_ptr<logger> new_logger) {
58+
std::lock_guard<std::mutex> lock(logger_map_mutex_);
59+
register_or_replace_(std::move(new_logger));
60+
}
61+
5762
SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logger) {
5863
std::lock_guard<std::mutex> lock(logger_map_mutex_);
5964
new_logger->set_formatter(formatter_->clone());
@@ -252,10 +257,14 @@ SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name) {
252257
}
253258

254259
SPDLOG_INLINE void registry::register_logger_(std::shared_ptr<logger> new_logger) {
255-
auto logger_name = new_logger->name();
260+
auto &logger_name = new_logger->name();
256261
throw_if_exists_(logger_name);
257262
loggers_[logger_name] = std::move(new_logger);
258263
}
259264

265+
SPDLOG_INLINE void registry::register_or_replace_(std::shared_ptr<logger> new_logger) {
266+
loggers_[new_logger->name()] = std::move(new_logger);
267+
}
268+
260269
} // namespace details
261270
} // namespace spdlog

src/include/spdlog/details/registry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class SPDLOG_API registry {
3131
registry &operator=(const registry &) = delete;
3232

3333
void register_logger(std::shared_ptr<logger> new_logger);
34+
void register_or_replace(std::shared_ptr<logger> new_logger);
3435
void initialize_logger(std::shared_ptr<logger> new_logger);
3536
std::shared_ptr<logger> get(const std::string &logger_name);
3637
std::shared_ptr<logger> default_logger();
@@ -105,6 +106,7 @@ class SPDLOG_API registry {
105106

106107
void throw_if_exists_(const std::string &logger_name);
107108
void register_logger_(std::shared_ptr<logger> new_logger);
109+
void register_or_replace_(std::shared_ptr<logger> new_logger);
108110
bool set_level_from_cfg_(logger *logger);
109111
std::mutex logger_map_mutex_, flusher_mutex_;
110112
std::recursive_mutex tp_mutex_;

0 commit comments

Comments
 (0)