Skip to content

Commit

Permalink
spdlog 1.14.1
Browse files Browse the repository at this point in the history
  • Loading branch information
eddelbuettel committed May 5, 2024
1 parent e383635 commit 1435e92
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 90 deletions.
2 changes: 1 addition & 1 deletion inst/include/spdlog/common.h
Expand Up @@ -81,7 +81,7 @@
#if FMT_USE_CONSTEXPR
#define SPDLOG_CONSTEXPR_FUNC FMT_CONSTEXPR
#else
#define SPDLOG_CONSTEXPR_FUNC inline
#define SPDLOG_CONSTEXPR_FUNC inline
#endif
#endif

Expand Down
2 changes: 1 addition & 1 deletion inst/include/spdlog/details/file_helper-inl.h
Expand Up @@ -98,7 +98,7 @@ SPDLOG_INLINE void file_helper::close() {
}

SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf) {
if(fd_ == nullptr) return;
if (fd_ == nullptr) return;
size_t msg_size = buf.size();
auto data = buf.data();
if (std::fwrite(data, 1, msg_size, fd_) != msg_size) {
Expand Down
9 changes: 9 additions & 0 deletions inst/include/spdlog/details/os-inl.h
Expand Up @@ -534,6 +534,15 @@ SPDLOG_INLINE bool create_dir(const filename_t &path) {
}

auto subdir = path.substr(0, token_pos);
#ifdef _WIN32
// if subdir is just a drive letter, add a slash e.g. "c:"=>"c:\",
// otherwise path_exists(subdir) returns false (issue #3079)
const bool is_drive = subdir.length() == 2 && subdir[1] == ':';
if (is_drive) {
subdir += '\\';
token_pos++;
}
#endif

if (!subdir.empty() && !path_exists(subdir) && !mkdir_(subdir)) {
return false; // return error if failed creating dir
Expand Down
27 changes: 1 addition & 26 deletions inst/include/spdlog/details/registry-inl.h
Expand Up @@ -84,31 +84,6 @@ SPDLOG_INLINE std::shared_ptr<logger> registry::get(const std::string &logger_na
return found == loggers_.end() ? nullptr : found->second;
}

#if __cplusplus >= 201703L // C++17
// if the map is small do a sequential search and avoid creating string for find(logger_name)
// otherwise use the standard find()
SPDLOG_INLINE std::shared_ptr<logger> registry::get(std::string_view logger_name) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
if (loggers_.size() <= 10) {
for (const auto &[key, val]: loggers_) {
if (logger_name == key) {
return val;
}
}
return nullptr;
}
// otherwise use the normal map lookup
else {
auto found = loggers_.find(std::string(logger_name));
return found == loggers_.end() ? nullptr : found->second;
}
}

SPDLOG_INLINE std::shared_ptr<logger> registry::get(const char *logger_name) {
return get(std::string_view(logger_name));
}
#endif

SPDLOG_INLINE std::shared_ptr<logger> registry::default_logger() {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
return default_logger_;
Expand All @@ -123,7 +98,7 @@ SPDLOG_INLINE logger *registry::get_default_raw() { return default_logger_.get()
// set default logger.
// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
SPDLOG_INLINE void registry::set_default_logger(std::shared_ptr<logger> new_default_logger) {
std::lock_guard<std::mutex> lock(logger_map_mutex_);
std::lock_guard<std::mutex> lock(logger_map_mutex_);
if (new_default_logger != nullptr) {
loggers_[new_default_logger->name()] = new_default_logger;
}
Expand Down
15 changes: 4 additions & 11 deletions inst/include/spdlog/details/registry.h
Expand Up @@ -18,10 +18,6 @@
#include <string>
#include <unordered_map>

#if __cplusplus >= 201703L // C++17
#include <string_view>
#endif

namespace spdlog {
class logger;

Expand All @@ -37,10 +33,6 @@ class SPDLOG_API registry {
void register_logger(std::shared_ptr<logger> new_logger);
void initialize_logger(std::shared_ptr<logger> new_logger);
std::shared_ptr<logger> get(const std::string &logger_name);
#if __cplusplus >= 201703L // C++17
std::shared_ptr<logger> get(std::string_view logger_name);
std::shared_ptr<logger> get(const char *logger_name);
#endif
std::shared_ptr<logger> default_logger();

// Return raw ptr to the default logger.
Expand All @@ -52,7 +44,8 @@ class SPDLOG_API registry {

// set default logger and add it to the registry if not registered already.
// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
// Note: Make sure to unregister it when no longer needed or before calling again with a new logger.
// Note: Make sure to unregister it when no longer needed or before calling again with a new
// logger.
void set_default_logger(std::shared_ptr<logger> new_default_logger);

void set_tp(std::shared_ptr<thread_pool> tp);
Expand All @@ -78,8 +71,8 @@ class SPDLOG_API registry {
}

std::unique_ptr<periodic_worker> &get_flusher() {
std::lock_guard<std::mutex> lock(flusher_mutex_);
return periodic_flusher_;
std::lock_guard<std::mutex> lock(flusher_mutex_);
return periodic_flusher_;
}

void set_error_handler(err_handler handler);
Expand Down
4 changes: 1 addition & 3 deletions inst/include/spdlog/details/thread_pool.h
Expand Up @@ -67,9 +67,7 @@ struct async_msg : log_msg_buffer {
worker_ptr{std::move(worker)},
flush_promise{} {}

async_msg(async_logger_ptr &&worker,
async_msg_type the_type,
std::promise<void> &&promise)
async_msg(async_logger_ptr &&worker, async_msg_type the_type, std::promise<void> &&promise)
: log_msg_buffer{},
msg_type{the_type},
worker_ptr{std::move(worker)},
Expand Down
2 changes: 1 addition & 1 deletion inst/include/spdlog/fmt/fmt.h
Expand Up @@ -20,7 +20,7 @@
#ifndef FMT_USE_WINDOWS_H
#define FMT_USE_WINDOWS_H 0
#endif

#include <spdlog/fmt/bundled/core.h>
#include <spdlog/fmt/bundled/format.h>

Expand Down
54 changes: 32 additions & 22 deletions inst/include/spdlog/mdc.h
@@ -1,36 +1,46 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)

#pragma once

#include <map>
#include <string>

#include <spdlog/common.h>

namespace spdlog {
class SPDLOG_API mdc {
public:
using mdc_map_t = std::map<std::string, std::string>;

static void put(const std::string &key, const std::string &value) {
get_context()[key] = value;
}
// MDC is a simple map of key->string values stored in thread local storage whose content will be printed by the loggers.
// Note: Not supported in async mode (thread local storage - so the async thread pool have different copy).
//
// Usage example:
// spdlog::mdc::put("mdc_key_1", "mdc_value_1");
// spdlog::info("Hello, {}", "World!"); // => [2024-04-26 02:08:05.040] [info] [mdc_key_1:mdc_value_1] Hello, World!

static std::string get(const std::string &key) {
auto &context = get_context();
auto it = context.find(key);
if (it != context.end()) {
return it->second;
}
return "";
namespace spdlog {
class SPDLOG_API mdc {
public:
using mdc_map_t = std::map<std::string, std::string>;

static void put(const std::string &key, const std::string &value) {
get_context()[key] = value;
}

static std::string get(const std::string &key) {
auto &context = get_context();
auto it = context.find(key);
if (it != context.end()) {
return it->second;
}
return "";
}

static void remove(const std::string &key) { get_context().erase(key); }
static void remove(const std::string &key) { get_context().erase(key); }

static void clear() { get_context().clear(); }
static void clear() { get_context().clear(); }

static mdc_map_t &get_context() {
static thread_local mdc_map_t context;
return context;
}
};
static mdc_map_t &get_context() {
static thread_local mdc_map_t context;
return context;
}
};

} // namespace spdlog
8 changes: 2 additions & 6 deletions inst/include/spdlog/pattern_formatter-inl.h
Expand Up @@ -790,7 +790,7 @@ template <typename ScopedPadder>
class mdc_formatter : public flag_formatter {
public:
explicit mdc_formatter(padding_info padinfo)
: flag_formatter(padinfo) {}
: flag_formatter(padinfo) {}

void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override {
auto &mdc_map = mdc::get_context();
Expand All @@ -802,7 +802,7 @@ class mdc_formatter : public flag_formatter {
}
}

void format_mdc(const mdc::mdc_map_t &mdc_map, memory_buf_t &dest){
void format_mdc(const mdc::mdc_map_t &mdc_map, memory_buf_t &dest) {
auto last_element = --mdc_map.end();
for (auto it = mdc_map.begin(); it != mdc_map.end(); ++it) {
auto &pair = *it;
Expand All @@ -825,8 +825,6 @@ class mdc_formatter : public flag_formatter {
}
};



// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
class full_formatter final : public flag_formatter {
Expand Down Expand Up @@ -921,8 +919,6 @@ class full_formatter final : public flag_formatter {
mdc_formatter<null_scoped_padder> mdc_formatter_{padding_info{}};
};



} // namespace details

SPDLOG_INLINE pattern_formatter::pattern_formatter(std::string pattern,
Expand Down
10 changes: 0 additions & 10 deletions inst/include/spdlog/spdlog-inl.h
Expand Up @@ -20,16 +20,6 @@ SPDLOG_INLINE std::shared_ptr<logger> get(const std::string &name) {
return details::registry::instance().get(name);
}

#if __cplusplus >= 201703L // C++17
SPDLOG_INLINE std::shared_ptr<logger> get(std::string_view name) {
return details::registry::instance().get(name);
}

SPDLOG_INLINE std::shared_ptr<logger> get(const char *name) {
return details::registry::instance().get(name);
}
#endif

SPDLOG_INLINE void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
details::registry::instance().set_formatter(std::move(formatter));
}
Expand Down
8 changes: 0 additions & 8 deletions inst/include/spdlog/spdlog.h
Expand Up @@ -20,10 +20,6 @@
#include <memory>
#include <string>

#if __cplusplus >= 201703L // C++17
#include <string_view>
#endif

namespace spdlog {

using default_factory = synchronous_factory;
Expand Down Expand Up @@ -54,10 +50,6 @@ SPDLOG_API void initialize_logger(std::shared_ptr<logger> logger);
// exist.
// example: spdlog::get("my_logger")->info("hello {}", "world");
SPDLOG_API std::shared_ptr<logger> get(const std::string &name);
#if __cplusplus >= 201703L // C++17
SPDLOG_API std::shared_ptr<logger> get(std::string_view name);
SPDLOG_API std::shared_ptr<logger> get(const char *name);
#endif

// Set global formatter. Each sink in each logger will get a clone of this object
SPDLOG_API void set_formatter(std::unique_ptr<spdlog::formatter> formatter);
Expand Down
2 changes: 1 addition & 1 deletion inst/include/spdlog/version.h
Expand Up @@ -5,7 +5,7 @@

#define SPDLOG_VER_MAJOR 1
#define SPDLOG_VER_MINOR 14
#define SPDLOG_VER_PATCH 0
#define SPDLOG_VER_PATCH 1

#define SPDLOG_TO_VERSION(major, minor, patch) (major * 10000 + minor * 100 + patch)
#define SPDLOG_VERSION SPDLOG_TO_VERSION(SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH)

0 comments on commit 1435e92

Please sign in to comment.