Skip to content

Commit

Permalink
V2.x no reg (#3285)
Browse files Browse the repository at this point in the history
Removed registry
  • Loading branch information
gabime authored Dec 6, 2024
1 parent ed6919b commit 166843f
Show file tree
Hide file tree
Showing 52 changed files with 308 additions and 1,336 deletions.
15 changes: 5 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ endif()

option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" OFF)
option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" OFF)
option(SPDLOG_DISABLE_DEFAULT_LOGGER "Disable default logger creation" OFF)
option(SPDLOG_DISABLE_GLOBAL_LOGGER "Disable global logger creation" OFF)
option(SPDLOG_NO_TLS "Disable thread local storage" OFF)

# clang-tidy
Expand Down Expand Up @@ -150,9 +150,6 @@ set(SPDLOG_HEADERS
"include/spdlog/spdlog.h"
"include/spdlog/stopwatch.h"
"include/spdlog/version.h"
"include/spdlog/cfg/argv.h"
"include/spdlog/cfg/env.h"
"include/spdlog/cfg/helpers.h"
"include/spdlog/details/circular_q.h"
"include/spdlog/details/file_helper.h"
"include/spdlog/details/fmt_helper.h"
Expand All @@ -162,7 +159,7 @@ set(SPDLOG_HEADERS
"include/spdlog/details/null_mutex.h"
"include/spdlog/details/os.h"
"include/spdlog/details/periodic_worker.h"
"include/spdlog/details/registry.h"
"include/spdlog/details/context.h"
"include/spdlog/details/synchronous_factory.h"
"include/spdlog/details/thread_pool.h"
"include/spdlog/fmt/bin_to_hex.h"
Expand Down Expand Up @@ -197,13 +194,11 @@ set(SPDLOG_SRCS
"src/logger.cpp"
"src/pattern_formatter.cpp"
"src/spdlog.cpp"
"src/cfg/helpers.cpp"
"src/details/file_helper.cpp"
"src/details/os_filesystem.cpp"
"src/details/log_msg.cpp"
"src/details/log_msg_buffer.cpp"
"src/details/periodic_worker.cpp"
"src/details/registry.cpp"
"src/details/context.cpp"
"src/details/thread_pool.cpp"
"src/sinks/base_sink.cpp"
"src/sinks/basic_file_sink.cpp"
Expand Down Expand Up @@ -309,11 +304,11 @@ endif()
# ---------------------------------------------------------------------------------------
# spdlog private defines according to the options
# ---------------------------------------------------------------------------------------
foreach(SPDLOG_OPTION
foreach(SPDLOG_OPTION
SPDLOG_CLOCK_COARSE
SPDLOG_PREVENT_CHILD_FD
SPDLOG_NO_THREAD_ID
SPDLOG_DISABLE_DEFAULT_LOGGER
SPDLOG_DISABLE_GLOBAL_LOGGER
SPDLOG_NO_TLS
SPDLOG_FWRITE_UNLOCKED)
if(${SPDLOG_OPTION})
Expand Down
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,6 @@ void binary_example()

```

---
#### Mapped diagnostic context (MDC)
MDC is a map of contextual information that can be used to enrich log messages.
It is a map of key-value pairs that can be set on a per-thread basis since it is stored in thread-local storage.
```c++
#include "spdlog/spdlog.h"
#include "spdlog/mdc"
...
spdlog::mdc::put("key1", "value1");
spdlog::mdc::put("key2", "value2");
```
---
#### Logger with multi sinks - each with a different format and log level
```c++
Expand Down Expand Up @@ -410,10 +398,10 @@ void file_events_example()
---
#### Replace the Default Logger
```c++
void replace_default_logger_example()
void replace_global_logger_example()
{
auto new_logger = spdlog::basic_logger_mt("new_default_logger", "logs/new-default-log.txt", true);
spdlog::set_default_logger(new_logger);
auto new_logger = spdlog::basic_logger_mt("new_global_logger", "logs/new-default-log.txt", true);
spdlog::set_global_logger(new_logger);
spdlog::info("new logger log message");
}
```
Expand Down
2 changes: 1 addition & 1 deletion bench/async_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,5 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> logger, int thread_co

auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count();
spdlog::info("Elapsed: {} secs\t {:L}/sec", delta_d, int(howmany / delta_d));
spdlog::info("Elapsed: {} secs\t {:L}/sec", delta_d, static_cast<int>(howmany / delta_d));
}
21 changes: 9 additions & 12 deletions bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ void bench_single_threaded(int iters) {
}

int main(int argc, char *argv[]) {
spdlog::set_automatic_registration(false);
spdlog::default_logger()->set_pattern("[%^%l%$] %v");
spdlog::global_logger()->set_pattern("[%^%l%$] %v");
int iters = 250000;
size_t threads = 4;
try {
Expand Down Expand Up @@ -117,8 +116,7 @@ void bench(int howmany, std::shared_ptr<spdlog::logger> log) {
auto delta_d = duration_cast<duration<double>>(delta).count();

spdlog::info(spdlog::fmt_lib::format(std::locale("en_US.UTF-8"), "{:<30} Elapsed: {:0.2f} secs {:>16L}/sec", log->name(),
delta_d, size_t(howmany / delta_d)));
spdlog::drop(log->name());
delta_d, static_cast<size_t>(howmany / delta_d)));
}

void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, size_t thread_count) {
Expand All @@ -144,8 +142,7 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, size_t thread_co
auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count();
spdlog::info(spdlog::fmt_lib::format(std::locale("en_US.UTF-8"), "{:<30} Elapsed: {:0.2f} secs {:>16L}/sec", log->name(),
delta_d, size_t(howmany / delta_d)));
spdlog::drop(log->name());
delta_d, static_cast<size_t>(howmany / delta_d)));
}

/*
Expand All @@ -155,8 +152,8 @@ void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log)
using std::chrono::duration;
using std::chrono::duration_cast;
auto orig_default = spdlog::default_logger();
spdlog::set_default_logger(log);
auto orig_default = spdlog::global_logger();
spdlog::set_global_logger(log);
auto start = high_resolution_clock::now();
for (auto i = 0; i < howmany; ++i)
{
Expand All @@ -166,7 +163,7 @@ void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log)
auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count();
spdlog::drop(log->name());
spdlog::set_default_logger(std::move(orig_default));
spdlog::set_global_logger(std::move(orig_default));
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16}/sec", log->name(), delta_d, int(howmany /
delta_d));
}
Expand All @@ -184,8 +181,8 @@ non dapibus eros. Donec fringilla dui sed " "augue pretium, nec scelerisque est
convallis, sem nec blandit maximus, nisi turpis ornare " "nisl, sit amet volutpat neque massa eu
odio. Maecenas malesuada quam ex, posuere congue nibh turpis duis.";
auto orig_default = spdlog::default_logger();
spdlog::set_default_logger(log);
auto orig_default = spdlog::global_logger();
spdlog::set_global_logger(log);
auto start = high_resolution_clock::now();
for (auto i = 0; i < howmany; ++i)
{
Expand All @@ -195,7 +192,7 @@ odio. Maecenas malesuada quam ex, posuere congue nibh turpis duis.";
auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count();
spdlog::drop(log->name());
spdlog::set_default_logger(std::move(orig_default));
spdlog::set_global_logger(std::move(orig_default));
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16}/sec", log->name(), delta_d, int(howmany /
delta_d));
}
Expand Down
52 changes: 2 additions & 50 deletions bench/latency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logge
}

void bench_global_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
spdlog::set_default_logger(std::move(logger));
spdlog::set_global_logger(std::move(logger));
int i = 0;
for (auto _ : state) {
spdlog::info("Hello logger: msg number {}...............", ++i);
Expand All @@ -57,7 +57,7 @@ void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logge
}

void bench_disabled_macro_global_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
spdlog::set_default_logger(std::move(logger));
spdlog::set_global_logger(std::move(logger));
int i = 0;
benchmark::DoNotOptimize(i); // prevent unused warnings
benchmark::DoNotOptimize(logger); // prevent unused warnings
Expand All @@ -71,11 +71,9 @@ void bench_disabled_macro_global_logger(benchmark::State &state, std::shared_ptr
void bench_dev_null() {
auto dev_null_st = spdlog::basic_logger_st("/dev/null_st", "/dev/null");
benchmark::RegisterBenchmark("/dev/null_st", bench_logger, std::move(dev_null_st))->UseRealTime();
spdlog::drop("/dev/null_st");

auto dev_null_mt = spdlog::basic_logger_mt("/dev/null_mt", "/dev/null");
benchmark::RegisterBenchmark("/dev/null_mt", bench_logger, std::move(dev_null_mt))->UseRealTime();
spdlog::drop("/dev/null_mt");
}

#endif // __linux__
Expand All @@ -84,7 +82,6 @@ void bench_dev_null() {
// for this test we create multiple null loggers and then call spdlog::get() on one of them multiple times
// create multiple null loggers and return name of the one to test
static std::string prepare_null_loggers() {
spdlog::drop_all();
const std::string some_logger_name = "Some logger name";
const int null_logger_count = 9;
for (int i = 0; i < null_logger_count; i++) {
Expand All @@ -93,41 +90,6 @@ static std::string prepare_null_loggers() {
return some_logger_name + std::to_string(null_logger_count / 2);
}

// benchmark spdlog::get() with const char*
void bench_get_logger_const_char(benchmark::State &state) {
std::string str_name = prepare_null_loggers();
const char *name = str_name.c_str();
for (auto _ : state) {
auto rv = spdlog::get(name);
if (rv == nullptr) {
state.SkipWithError("get() returned nullptr");
}
}
}

// benchmark spdlog::get() with std::string_view
void bench_get_logger_sv(benchmark::State &state) {
auto str_name = prepare_null_loggers();
auto sv_name = std::string_view{str_name};
for (auto _ : state) {
auto rv = spdlog::get(sv_name);
if (rv == nullptr) {
state.SkipWithError("get() returned nullptr");
}
}
}

// benchmark spdlog::get() with std::string
void bench_get_logger_string(benchmark::State &state) {
auto str_name = prepare_null_loggers();
for (auto _ : state) {
auto rv = spdlog::get(str_name);
if (rv == nullptr) {
state.SkipWithError("get() returned nullptr");
}
}
}

int main(int argc, char *argv[]) {
using spdlog::sinks::null_sink_mt;
using spdlog::sinks::null_sink_st;
Expand Down Expand Up @@ -159,17 +121,14 @@ int main(int argc, char *argv[]) {
// basic_st
auto basic_st = spdlog::basic_logger_st("basic_st", "latency_logs/basic_st.log", true);
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();
spdlog::drop("basic_st");

// rotating st
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files);
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime();
spdlog::drop("rotating_st");

// daily st
auto daily_st = spdlog::daily_logger_mt("daily_st", "latency_logs/daily_st.log");
benchmark::RegisterBenchmark("daily_st", bench_logger, std::move(daily_st))->UseRealTime();
spdlog::drop("daily_st");

//
// Multi threaded bench, 10 loggers using same logger concurrently
Expand All @@ -180,17 +139,14 @@ int main(int argc, char *argv[]) {
// basic_mt
auto basic_mt = spdlog::basic_logger_mt("basic_mt", "latency_logs/basic_mt.log", true);
benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime();
spdlog::drop("basic_mt");

// rotating mt
auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files);
benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))->Threads(n_threads)->UseRealTime();
spdlog::drop("rotating_mt");

// daily mt
auto daily_mt = spdlog::daily_logger_mt("daily_mt", "latency_logs/daily_mt.log");
benchmark::RegisterBenchmark("daily_mt", bench_logger, std::move(daily_mt))->Threads(n_threads)->UseRealTime();
spdlog::drop("daily_mt");
}

// async
Expand All @@ -200,10 +156,6 @@ int main(int argc, char *argv[]) {
spdlog::async_overflow_policy::overrun_oldest);
benchmark::RegisterBenchmark("async_logger", bench_logger, async_logger)->Threads(n_threads)->UseRealTime();

benchmark::RegisterBenchmark("spdlog::get(const char* name)", bench_get_logger_const_char);
benchmark::RegisterBenchmark("spdlog::get(std::string_view name)", bench_get_logger_sv);
benchmark::RegisterBenchmark("spdlog::get(const std::string &name)", bench_get_logger_string);

benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
}
Loading

0 comments on commit 166843f

Please sign in to comment.