Skip to content

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
gabime committed Jan 17, 2025
1 parent b9f0243 commit 47fe6ef
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 76 deletions.
8 changes: 4 additions & 4 deletions bench/async_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//
// bench.cpp : spdlog benchmarks
//
#include <algorithm>
#include <atomic>
#include <iostream>
#include <fstream>
#include <iostream>
#include <locale>
#include <memory>
#include <string>
#include <thread>
#include <locale>
#include <algorithm>

#include "spdlog/sinks/async_sink.h"
#include "spdlog/sinks/basic_file_sink.h"
Expand Down Expand Up @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) {
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink));
bench_mt(howmany, std::move(logger), threads);
}
//verify_file(filename, howmany); // in separate scope to ensure logger is destroyed and all logs were written
// verify_file(filename, howmany); // in separate scope to ensure logger is destroyed and all logs were written
}
spdlog::info("");
spdlog::info("*********************************");
Expand Down
9 changes: 6 additions & 3 deletions bench/latency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ int main(int argc, char *argv[]) {
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();

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

// daily st
Expand All @@ -142,7 +143,8 @@ int main(int argc, char *argv[]) {
benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime();

// rotating mt
auto rotating_mt = spdlog::create<rotating_file_sink_mt>("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files);
auto rotating_mt =
spdlog::create<rotating_file_sink_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();

// daily mt
Expand All @@ -151,7 +153,8 @@ int main(int argc, char *argv[]) {
}
using spdlog::sinks::async_sink;
async_sink::config config;
config.queue_size = 3 * 1024 * 1024;;
config.queue_size = 3 * 1024 * 1024;
;
config.sinks.push_back(std::make_shared<null_sink_st>());
config.policy = async_sink::overflow_policy::overrun_oldest;
auto async_logger = std::make_shared<spdlog::logger>("async_logger", std::make_shared<async_sink>(config));
Expand Down
2 changes: 1 addition & 1 deletion example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void stdout_logger_example() {
// Create color multithreading logger.
auto console = spdlog::create<stdout_color_sink_mt>("console");
// or for stderr:
//auto console = spdlog::create<stderr_color_sink_mt>("console");
// auto console = spdlog::create<stderr_color_sink_mt>("console");
}

#include "spdlog/sinks/basic_file_sink.h"
Expand Down
2 changes: 1 addition & 1 deletion include/spdlog/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
#include <array>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <exception>
#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
#include <string_view>
#include <cstdint>

#include "./source_loc.h"
#include "fmt/base.h"
Expand Down
8 changes: 3 additions & 5 deletions include/spdlog/details/async_log_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@
#pragma once

#include <cstdint>

#include "./log_msg.h"

namespace spdlog {
namespace details {


// Extend log_msg with internal buffer to store its payload.
// This is needed since log_msg holds string_views that points to stack data.

class SPDLOG_API async_log_msg : public log_msg {
public:
enum class type:std::uint8_t { log, flush, terminate };
enum class type : std::uint8_t { log, flush, terminate };
async_log_msg() = default;
explicit async_log_msg(type type);
async_log_msg(type type, const log_msg &orig_msg);

~async_log_msg() = default;
async_log_msg(const async_log_msg &other);
async_log_msg(async_log_msg &&other) noexcept;
async_log_msg &operator=(const async_log_msg &other);
async_log_msg &operator=(async_log_msg &&other) noexcept;
[[nodiscard]] type message_type() const { return msg_type_; }

type message_type() const {return msg_type_;}
private:
type msg_type_{type::log};
memory_buf_t buffer_;
Expand Down
8 changes: 5 additions & 3 deletions include/spdlog/details/err_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

#pragma once

#include <string>
#include <chrono>
#include <exception>
#include <mutex>
#include <string>

#include "spdlog/common.h"

// by default, prints the error to stderr, at max rate of 1/sec thread safe
Expand All @@ -16,6 +17,7 @@ class SPDLOG_API err_helper {
err_handler custom_err_handler_;
std::chrono::steady_clock::time_point last_report_time_;
mutable std::mutex mutex_;

public:
err_helper() = default;
~err_helper() = default;
Expand All @@ -25,5 +27,5 @@ class SPDLOG_API err_helper {
void handle_unknown_ex(const std::string& origin, const source_loc& loc) noexcept;
void set_err_handler(err_handler handler);
};
} // namespace details
} // namespace spdlog
} // namespace details
} // namespace spdlog
2 changes: 1 addition & 1 deletion include/spdlog/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SPDLOG_API logger {
logger(std::string name, sinks_init_list sinks)
: logger(std::move(name), sinks.begin(), sinks.end()) {}

logger(const logger &other) ;
logger(const logger &other);
logger(logger &&other) noexcept;

~logger() = default;
Expand Down
3 changes: 1 addition & 2 deletions include/spdlog/sinks/async_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ class SPDLOG_API async_sink final : public sink {
return std::make_shared<async_sink>(cfg);
}


private:
using async_log_msg = details::async_log_msg;
using queue_t = details::mpmc_blocking_queue<async_log_msg>;

void enqueue_message_(details::async_log_msg &&msg) const;
void backend_loop_();
void backend_log_(const details::log_msg &msg) ;
void backend_log_(const details::log_msg &msg);
void backend_flush_();

config config_;
Expand Down
4 changes: 2 additions & 2 deletions include/spdlog/sinks/mongo_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>

#include <mutex>
#include "../details/null_mutex.h"

#include "../common.h"
#include "../details/log_msg.h"
#include "../details/null_mutex.h"
#include "./base_sink.h"

namespace spdlog {
Expand Down
1 change: 1 addition & 0 deletions include/spdlog/sinks/msvc_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <mutex>

#include "../details/null_mutex.h"
#include "./base_sink.h"

Expand Down
4 changes: 3 additions & 1 deletion include/spdlog/sinks/ringbuffer_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class ringbuffer_sink final : public base_sink<Mutex> {
}

protected:
void sink_it_(const details::log_msg &msg) override { q_.push_back(details::async_log_msg{details::async_log_msg::type::log, msg}); }
void sink_it_(const details::log_msg &msg) override {
q_.push_back(details::async_log_msg{details::async_log_msg::type::log, msg});
}
void flush_() override {}

private:
Expand Down
4 changes: 2 additions & 2 deletions include/spdlog/sinks/sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class SPDLOG_API sink {
virtual void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) = 0;

void set_level(level level) { level_.store(level, std::memory_order_relaxed); }
level log_level() const { return level_.load(std::memory_order_relaxed);}
bool should_log(level msg_level) const {return msg_level >= level_.load(std::memory_order_relaxed);}
level log_level() const { return level_.load(std::memory_order_relaxed); }
bool should_log(level msg_level) const { return msg_level >= level_.load(std::memory_order_relaxed); }

protected:
// sink log level - default is all
Expand Down
4 changes: 2 additions & 2 deletions include/spdlog/sinks/syslog_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <syslog.h>

#include <array>
#include <string>
#include <mutex>
#include <string>

namespace spdlog {
namespace sinks {
Expand All @@ -19,7 +19,7 @@ namespace sinks {
template <typename Mutex>
class syslog_sink final : public base_sink<Mutex> {
public:
syslog_sink(std::string ident = "", int syslog_option = 0, int syslog_facility = LOG_USER, bool enable_formatting=false)
syslog_sink(std::string ident = "", int syslog_option = 0, int syslog_facility = LOG_USER, bool enable_formatting = false)
: enable_formatting_{enable_formatting},
syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
/* spdlog::level::debug */ LOG_DEBUG,
Expand Down
11 changes: 7 additions & 4 deletions src/details/async_log_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace spdlog {
namespace details {


async_log_msg::async_log_msg(const type type)
: msg_type_{type} {}

Expand All @@ -15,21 +14,25 @@ async_log_msg::async_log_msg(const type type)
// are compiler generated const chars* (__FILE__, __LINE__, __FUNCTION__)
// if you pass custom strings to source location, make sure they outlive the async_log_msg
async_log_msg::async_log_msg(const type type, const log_msg &orig_msg)
: log_msg{orig_msg}, msg_type_(type) {
: log_msg{orig_msg},
msg_type_(type) {
buffer_.append(logger_name);
buffer_.append(payload);
update_string_views();
}

async_log_msg::async_log_msg(const async_log_msg &other)
: log_msg{other}, msg_type_{other.msg_type_} {
: log_msg{other},
msg_type_{other.msg_type_} {
buffer_.append(logger_name);
buffer_.append(payload);
update_string_views();
}

async_log_msg::async_log_msg(async_log_msg &&other) noexcept
: log_msg{other}, msg_type_{other.msg_type_}, buffer_{std::move(other.buffer_)} {
: log_msg{other},
msg_type_{other.msg_type_},
buffer_{std::move(other.buffer_)} {
update_string_views();
}

Expand Down
1 change: 0 additions & 1 deletion src/sinks/ansicolor_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ template <typename Mutex>
ansicolor_stderr_sink<Mutex>::ansicolor_stderr_sink(color_mode mode)
: ansicolor_sink<Mutex>(stderr, mode) {}


} // namespace sinks
} // namespace spdlog

Expand Down
20 changes: 8 additions & 12 deletions src/sinks/async_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,21 @@ async_sink::async_sink(config async_config)
});
}


async_sink::~async_sink() {
try {
q_->enqueue(async_log_msg(async_log_msg::type::terminate));
worker_thread_.join();
} catch (...) {
terminate_worker_ = true; // as last resort, stop the worker thread using terminate_worker_ flag.
#ifndef NDEBUG
printf("Exception in ~async_sink()\n");
#endif
terminate_worker_ = true; // as last resort, stop the worker thread using terminate_worker_ flag.
#ifndef NDEBUG
printf("Exception in ~async_sink()\n");
#endif
}
}

void async_sink::log(const details::log_msg &msg) {
enqueue_message_(async_log_msg(async_log_msg::type::log, msg));
}
void async_sink::log(const details::log_msg &msg) { enqueue_message_(async_log_msg(async_log_msg::type::log, msg)); }

void async_sink::flush() {
enqueue_message_(details::async_log_msg(async_log_msg::type::flush));
}
void async_sink::flush() { enqueue_message_(details::async_log_msg(async_log_msg::type::flush)); }

void async_sink::set_pattern(const std::string &pattern) { set_formatter(std::make_unique<pattern_formatter>(pattern)); }

Expand Down Expand Up @@ -83,7 +78,8 @@ bool async_sink::wait_all(const std::chrono::milliseconds timeout) const {
}

void async_sink::wait_all() const {
while (!wait_all(std::chrono::milliseconds(10))) { /* empty */ }
while (!wait_all(std::chrono::milliseconds(10))) { /* empty */
}
}

size_t async_sink::get_overrun_counter() const { return q_->overrun_counter(); }
Expand Down
5 changes: 3 additions & 2 deletions src/sinks/base_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <memory>
#include <mutex>

#include "spdlog/common.h"
#include "spdlog/pattern_formatter.h"

Expand Down Expand Up @@ -53,8 +54,8 @@ void base_sink<Mutex>::set_formatter_(std::unique_ptr<formatter> sink_formatter)
formatter_ = std::move(sink_formatter);
}

} // namespace sinks
} // namespace spdlog
} // namespace sinks
} // namespace spdlog

// template instantiations
#include "spdlog/details/null_mutex.h"
Expand Down
5 changes: 3 additions & 2 deletions src/sinks/basic_file_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT)

#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/common.h"

#include <mutex>

#include "spdlog/common.h"

namespace spdlog {
namespace sinks {

Expand Down Expand Up @@ -34,7 +36,6 @@ void basic_file_sink<Mutex>::flush_() {
} // namespace sinks
} // namespace spdlog


// template instantiations
#include "spdlog/details/null_mutex.h"
template class SPDLOG_API spdlog::sinks::basic_file_sink<std::mutex>;
Expand Down
2 changes: 1 addition & 1 deletion src/sinks/wincolor_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// clang-format on
#include "spdlog/sinks/wincolor_sink.h"

#include "spdlog/common.h"

namespace spdlog {
Expand Down Expand Up @@ -131,7 +132,6 @@ template <typename Mutex>
wincolor_stderr_sink<Mutex>::wincolor_stderr_sink(color_mode mode)
: wincolor_sink<Mutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) {}


} // namespace sinks
} // namespace spdlog

Expand Down
10 changes: 3 additions & 7 deletions src/spdlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@

namespace spdlog {


#ifndef SPDLOG_DISABLE_GLOBAL_LOGGER
static std::shared_ptr<logger> s_logger = std::make_shared<logger>("", std::make_shared<sinks::stdout_color_sink_mt>());
static std::shared_ptr<logger> s_logger = std::make_shared<logger>("", std::make_shared<sinks::stdout_color_sink_mt>());
#else
static std::short_ptr<logger> s_logger = nullptr;
static std::short_ptr<logger> s_logger = nullptr;
#endif


std::shared_ptr<logger> global_logger() { return s_logger; }

void set_global_logger(std::shared_ptr<logger> global_logger) { s_logger = std::move(global_logger); }

logger *global_logger_raw() noexcept {
return s_logger.get();
}
logger *global_logger_raw() noexcept { return s_logger.get(); }

void set_formatter(std::unique_ptr<formatter> formatter) { global_logger()->set_formatter(std::move(formatter)); }

Expand Down
Loading

0 comments on commit 47fe6ef

Please sign in to comment.