Skip to content

Commit

Permalink
Merge branch 'gabime:v1.x' into v1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxwellGengYF authored Sep 21, 2024
2 parents fee6148 + 2c76e61 commit be90323
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ if(SPDLOG_FMT_EXTERNAL OR SPDLOG_FMT_EXTERNAL_HO)
target_compile_definitions(spdlog PUBLIC SPDLOG_FMT_EXTERNAL)
target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_FMT_EXTERNAL)

# use external fmt-header-nly
# use external fmt-header-only
if(SPDLOG_FMT_EXTERNAL_HO)
target_link_libraries(spdlog PUBLIC fmt::fmt-header-only)
target_link_libraries(spdlog_header_only INTERFACE fmt::fmt-header-only)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ see example [CMakeLists.txt](https://github.com/gabime/spdlog/blob/v1.x/example/
* Arch Linux: `pacman -S spdlog`
* openSUSE: `sudo zypper in spdlog-devel`
* vcpkg: `vcpkg install spdlog`
* conan: `spdlog/[>=1.4.1]`
* conan: `conan install --requires=spdlog/[*]`
* conda: `conda install -c conda-forge spdlog`
* build2: ```depends: spdlog ^1.8.2```

Expand Down
9 changes: 8 additions & 1 deletion example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ void replace_default_logger_example() {
// Mapped Diagnostic Context (MDC) is a map that stores key-value pairs (string values) in thread local storage.
// Each thread maintains its own MDC, which loggers use to append diagnostic information to log outputs.
// Note: it is not supported in asynchronous mode due to its reliance on thread-local storage.
#include "spdlog/mdc.h"

#ifndef SPDLOG_NO_TLS
#include "spdlog/mdc.h"
void mdc_example()
{
spdlog::mdc::put("key1", "value1");
Expand All @@ -391,3 +393,8 @@ void mdc_example()
spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [%&] %v");
spdlog::info("Some log message with context");
}
#else
void mdc_example() {
// if TLS feature is disabled
}
#endif
6 changes: 3 additions & 3 deletions include/spdlog/details/mpmc_blocking_q.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ class mpmc_blocking_queue {
#endif

size_t overrun_counter() {
std::unique_lock<std::mutex> lock(queue_mutex_);
std::lock_guard<std::mutex> lock(queue_mutex_);
return q_.overrun_counter();
}

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

size_t size() {
std::unique_lock<std::mutex> lock(queue_mutex_);
std::lock_guard<std::mutex> lock(queue_mutex_);
return q_.size();
}

void reset_overrun_counter() {
std::unique_lock<std::mutex> lock(queue_mutex_);
std::lock_guard<std::mutex> lock(queue_mutex_);
q_.reset_overrun_counter();
}

Expand Down
4 changes: 4 additions & 0 deletions include/spdlog/mdc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#pragma once

#if defined(SPDLOG_NO_TLS)
#error "This header requires thread local storage support, but SPDLOG_NO_TLS is defined."
#endif

#include <map>
#include <string>

Expand Down
18 changes: 16 additions & 2 deletions include/spdlog/pattern_formatter-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
#include <spdlog/details/fmt_helper.h>
#include <spdlog/details/log_msg.h>
#include <spdlog/details/os.h>
#include <spdlog/mdc.h>

#ifndef SPDLOG_NO_TLS
#include <spdlog/mdc.h>
#endif

#include <spdlog/fmt/fmt.h>
#include <spdlog/formatter.h>

Expand Down Expand Up @@ -176,7 +180,7 @@ class A_formatter : public flag_formatter {

// Abbreviated month
static const std::array<const char *, 12> months{
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"}};
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}};

template <typename ScopedPadder>
class b_formatter final : public flag_formatter {
Expand Down Expand Up @@ -786,6 +790,7 @@ class elapsed_formatter final : public flag_formatter {

// Class for formatting Mapped Diagnostic Context (MDC) in log messages.
// Example: [logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message
#ifndef SPDLOG_NO_TLS
template <typename ScopedPadder>
class mdc_formatter : public flag_formatter {
public:
Expand Down Expand Up @@ -824,6 +829,7 @@ class mdc_formatter : public flag_formatter {
}
}
};
#endif

// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
Expand Down Expand Up @@ -901,6 +907,7 @@ class full_formatter final : public flag_formatter {
dest.push_back(' ');
}

#ifndef SPDLOG_NO_TLS
// add mdc if present
auto &mdc_map = mdc::get_context();
if (!mdc_map.empty()) {
Expand All @@ -909,14 +916,19 @@ class full_formatter final : public flag_formatter {
dest.push_back(']');
dest.push_back(' ');
}
#endif
// fmt_helper::append_string_view(msg.msg(), dest);
fmt_helper::append_string_view(msg.payload, dest);
}

private:
std::chrono::seconds cache_timestamp_{0};
memory_buf_t cached_datetime_;

#ifndef SPDLOG_NO_TLS
mdc_formatter<null_scoped_padder> mdc_formatter_{padding_info{}};
#endif

};

} // namespace details
Expand Down Expand Up @@ -1211,9 +1223,11 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
padding));
break;

#ifndef SPDLOG_NO_TLS // mdc formatter requires TLS support
case ('&'):
formatters_.push_back(details::make_unique<details::mdc_formatter<Padder>>(padding));
break;
#endif

default: // Unknown flag appears as is
auto unknown_flag = details::make_unique<details::aggregate_formatter>();
Expand Down
2 changes: 1 addition & 1 deletion include/spdlog/sinks/ansicolor_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ansicolor_sink : public sink {

void log(const details::log_msg &msg) override;
void flush() override;
void set_pattern(const std::string &pattern) final;
void set_pattern(const std::string &pattern) final override;
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override;

// Formatting codes
Expand Down
8 changes: 4 additions & 4 deletions include/spdlog/sinks/base_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class SPDLOG_API base_sink : public sink {
base_sink &operator=(const base_sink &) = delete;
base_sink &operator=(base_sink &&) = delete;

void log(const details::log_msg &msg) final;
void flush() final;
void set_pattern(const std::string &pattern) final;
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) final;
void log(const details::log_msg &msg) final override;
void flush() final override;
void set_pattern(const std::string &pattern) final override;
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) final override;

protected:
// sink formatter
Expand Down
3 changes: 3 additions & 0 deletions include/spdlog/sinks/daily_file_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ struct daily_filename_format_calculator {
* Rotating file sink based on date.
* If truncate != false , the created file will be truncated.
* If max_files > 0, retain only the last max_files and delete previous.
* If max_files > 0, retain only the last max_files and delete previous.
* Note that old log files from previous executions will not be deleted by this class,
* rotation and deletion is only applied while the program is running.
*/
template <typename Mutex, typename FileNameCalc = daily_filename_calculator>
class daily_file_sink final : public base_sink<Mutex> {
Expand Down
2 changes: 2 additions & 0 deletions include/spdlog/sinks/hourly_file_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct hourly_filename_calculator {
* Rotating file sink based on time.
* If truncate != false , the created file will be truncated.
* If max_files > 0, retain only the last max_files and delete previous.
* Note that old log files from previous executions will not be deleted by this class,
* rotation and deletion is only applied while the program is running.
*/
template <typename Mutex, typename FileNameCalc = hourly_filename_calculator>
class hourly_file_sink final : public base_sink<Mutex> {
Expand Down
6 changes: 5 additions & 1 deletion tests/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/details/fmt_helper.h"
#include "spdlog/mdc.h"

#ifndef SPDLOG_NO_TLS
#include "spdlog/mdc.h"
#endif

#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/null_sink.h"
Expand Down
2 changes: 2 additions & 0 deletions tests/test_pattern_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ TEST_CASE("override need_localtime", "[pattern_formatter]") {
}
}

#ifndef SPDLOG_NO_TLS
TEST_CASE("mdc formatter test-1", "[pattern_formatter]") {
spdlog::mdc::put("mdc_key_1", "mdc_value_1");
spdlog::mdc::put("mdc_key_2", "mdc_value_2");
Expand Down Expand Up @@ -628,3 +629,4 @@ TEST_CASE("mdc empty", "[pattern_formatter]") {

SECTION("Tear down") { spdlog::mdc::clear(); }
}
#endif

0 comments on commit be90323

Please sign in to comment.