From 1d72edcc4f9f37f3ebafe32639f7aa1454a16115 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 16:39:32 +0200 Subject: [PATCH] clang-format --- include/spdlog/common.h | 5 +- include/spdlog/sinks/rotating_file_sink.h | 4 +- include/spdlog/sinks/systemd_sink.h | 48 ++-- lite-example/create_lite.cpp | 8 +- lite-example/example.cpp | 5 +- lite/logger.cpp | 8 +- lite/logger.h | 294 +++++++++++----------- tests/test_file_logging.cpp | 2 +- 8 files changed, 183 insertions(+), 191 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index dd9a4785..71273d30 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -128,7 +128,10 @@ enum level_enum static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES; #if !defined(SPDLOG_SHORT_LEVEL_NAMES) -#define SPDLOG_SHORT_LEVEL_NAMES {"T", "D", "I", "W", "E", "C", "O"} +#define SPDLOG_SHORT_LEVEL_NAMES \ + { \ + "T", "D", "I", "W", "E", "C", "O" \ + } #endif static const char *short_level_names[] SPDLOG_SHORT_LEVEL_NAMES; diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index ae0f70f6..35f33a4d 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -31,7 +31,7 @@ template class rotating_file_sink final : public base_sink { public: - rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open=false) + rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false) : base_filename_(std::move(base_filename)) , max_size_(max_size) , max_files_(max_files) @@ -150,7 +150,7 @@ using rotating_file_sink_st = rotating_file_sink; template inline std::shared_ptr rotating_logger_mt( - const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open=false) + const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false) { return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open); } diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index fa9efd83..be143194 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -18,22 +18,24 @@ namespace spdlog { namespace sinks { -inline int syslog_level(level::level_enum l) { - switch(l) { - case level::off: - case level::trace: - case level::debug: - return LOG_DEBUG; - case level::info: - return LOG_INFO; - case level::warn: - return LOG_WARNING; - case level::err: - return LOG_ERR; - case level::critical: - return LOG_CRIT; - default: - throw std::invalid_argument("systemd_sink.h syslog_level()"); +inline int syslog_level(level::level_enum l) +{ + switch (l) + { + case level::off: + case level::trace: + case level::debug: + return LOG_DEBUG; + case level::info: + return LOG_INFO; + case level::warn: + return LOG_WARNING; + case level::err: + return LOG_ERR; + case level::critical: + return LOG_CRIT; + default: + throw std::invalid_argument("systemd_sink.h syslog_level()"); } } @@ -57,13 +59,7 @@ public: protected: void sink_it_(const details::log_msg &msg) override { - if( sd_journal_print( - syslog_level(msg.level), - "%.*s", - static_cast(msg.payload.size()), - msg.payload.data() - ) - ) + if (sd_journal_print(syslog_level(msg.level), "%.*s", static_cast(msg.payload.size()), msg.payload.data())) throw spdlog_ex("Failed writing to systemd"); } @@ -76,15 +72,13 @@ using systemd_sink_st = systemd_sink; // Create and register a syslog logger template -inline std::shared_ptr systemd_logger_mt( - const std::string &logger_name) +inline std::shared_ptr systemd_logger_mt(const std::string &logger_name) { return Factory::template create(logger_name); } template -inline std::shared_ptr systemd_logger_st( - const std::string &logger_name) +inline std::shared_ptr systemd_logger_st(const std::string &logger_name) { return Factory::template create(logger_name); } diff --git a/lite-example/create_lite.cpp b/lite-example/create_lite.cpp index aa375d4e..5345237f 100644 --- a/lite-example/create_lite.cpp +++ b/lite-example/create_lite.cpp @@ -1,15 +1,13 @@ #include "logger.h" #include "spdlog/spdlog.h" - -spdlog::lite::logger spdlog::create_lite(void* ctx) +spdlog::lite::logger spdlog::create_lite(void *ctx) { - if(ctx) { + if (ctx) + { //.. } auto logger_impl = spdlog::default_logger(); logger_impl->set_level(spdlog::level::trace); return spdlog::lite::logger(logger_impl); } - - diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 929e75dd..376d9a68 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -3,9 +3,8 @@ int main() { - //auto l = spdlog::create_lite(); - //spdlog::lite::info("HELLO info {}", 123); + // auto l = spdlog::create_lite(); + // spdlog::lite::info("HELLO info {}", 123); SPDLITE_TRACE("SOME MACRO {}", 123); SPDLITE_INFO("SOME MACRO {}", "HHHHH"); - } \ No newline at end of file diff --git a/lite/logger.cpp b/lite/logger.cpp index 39841553..0cbe766f 100644 --- a/lite/logger.cpp +++ b/lite/logger.cpp @@ -2,9 +2,9 @@ #include "spdlog/spdlog.h" #include "spdlog/logger.h" -static spdlog::level::level_enum to_spdlog_level(spdlog::lite::level level) +static spdlog::level::level_enum to_spdlog_level(spdlog::lite::level level) { - return static_cast(level); + return static_cast(level); } static spdlog::lite::level to_lite_level(spdlog::level::level_enum level) @@ -20,7 +20,7 @@ spdlog::lite::logger::logger(std::shared_ptr impl) bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NOEXCEPT { auto spd_level = to_spdlog_level(level); - return impl_->should_log(spd_level);//TODO level + return impl_->should_log(spd_level); // TODO level } void spdlog::lite::logger::log_formatted_(spdlog::lite::level level, const fmt::memory_buffer &formatted) @@ -29,7 +29,6 @@ void spdlog::lite::logger::log_formatted_(spdlog::lite::level level, const fmt:: impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); } - void spdlog::lite::logger::log_formatted_src(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted) { auto spd_level = to_spdlog_level(lvl); @@ -75,7 +74,6 @@ void spdlog::lite::logger::set_pattern(std::string pattern) impl_->set_pattern(std::move(pattern)); } - spdlog::lite::logger &spdlog::lite::default_logger() { static spdlog::lite::logger s_default(spdlog::default_logger()); diff --git a/lite/logger.h b/lite/logger.h index 7246411c..b55a7159 100644 --- a/lite/logger.h +++ b/lite/logger.h @@ -21,8 +21,8 @@ #define SPDLITE_LEVEL_CRITICAL 5 #define SPDLITE_LEVEL_OFF 6 -#define SPDLITE_LOGGER_CALL(logger, level, ...) \ - if (logger.should_log(level)) \ +#define SPDLITE_LOGGER_CALL(logger, level, ...) \ + if (logger.should_log(level)) \ logger.log(level, __VA_ARGS__) #if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_TRACE @@ -73,169 +73,169 @@ #define SPDLITE_CRITICAL(...) (void)0 #endif - - namespace spdlog { - class logger; +class logger; - namespace lite { - enum class level{ - trace = SPDLITE_LEVEL_TRACE, - debug =SPDLITE_LEVEL_DEBUG, - info = SPDLITE_LEVEL_INFO, - warn = SPDLITE_LEVEL_WARN, - err = SPDLITE_LEVEL_ERROR, - critical = SPDLITE_LEVEL_CRITICAL, - off = SPDLITE_LEVEL_OFF - }; +namespace lite { +enum class level +{ + trace = SPDLITE_LEVEL_TRACE, + debug = SPDLITE_LEVEL_DEBUG, + info = SPDLITE_LEVEL_INFO, + warn = SPDLITE_LEVEL_WARN, + err = SPDLITE_LEVEL_ERROR, + critical = SPDLITE_LEVEL_CRITICAL, + off = SPDLITE_LEVEL_OFF +}; - struct src_loc { - const char *filename; - int line; - const char* funcname; - }; +struct src_loc +{ + const char *filename; + int line; + const char *funcname; +}; - class logger { - public: - logger() = default; +class logger +{ +public: + logger() = default; - logger(std::shared_ptr impl); - logger(const logger&) = default; - logger(logger&&) = default; - logger& operator=(const logger&) = default; + logger(std::shared_ptr impl); + logger(const logger &) = default; + logger(logger &&) = default; + logger &operator=(const logger &) = default; - ~logger() = default; + ~logger() = default; - bool should_log(spdlog::lite::level lvl) const noexcept; + bool should_log(spdlog::lite::level lvl) const noexcept; - template - void log(spdlog::lite::level lvl, const char *fmt, const Args &... args) { - if (!should_log(lvl)) { - return; - } - fmt::memory_buffer formatted_buf; - fmt::format_to(formatted_buf, fmt, args...); - log_formatted_(lvl, formatted_buf); - } - - - template - void log(const spdlog::lite::src_loc& src, spdlog::lite::level lvl, const char *fmt, const Args &... args) { - if (!should_log(lvl)) { - return; - } - fmt::memory_buffer formatted_buf; - fmt::format_to(formatted_buf, fmt, args...); - log_formatted_src(src, lvl, formatted_buf); - } - - - template - void trace(const char *fmt, const Args &... args) - { - log(spdlog::lite::level::trace, fmt, args...); - } - - template - void trace(const char* source_file, int source_line, const char* source_func, const char *fmt, const Args &... args) - { - spdlog::lite::src_loc src{source_file, source_line, source_func}; - log(src, spdlog::lite::level::trace, fmt, args...); - } - - template - void debug(const char *fmt, const Args &... args) - { - log(spdlog::lite::level::debug, fmt, args...); - } - - template - void info(const char *fmt, const Args &... args) - { - log(spdlog::lite::level::info, fmt, args...); - } - - template - void warn(const char *fmt, const Args &... args) - { - log(spdlog::lite::level::warn, fmt, args...); - } - - template - void error(const char *fmt, const Args &... args) - { - log(spdlog::lite::level::err, fmt, args...); - } - - template - void critical(const char *fmt, const Args &... args) - { - log(spdlog::lite::level::critical, fmt, args...); - } - - std::string name() const; - - // level - void set_level(lite::level level); - lite::level get_level() const; - - // flush - void flush(); - void flush_on(spdlog::lite::level log_level); - spdlog::lite::level flush_level() const; - - // pattern - void set_pattern(std::string pattern); - - protected: - std::shared_ptr impl_; - void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted); - void log_formatted_src(const spdlog::lite::src_loc& src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted); - - }; - spdlog::lite::logger& default_logger(); - - - template - void trace(const char *fmt, const Args &... args) + template + void log(spdlog::lite::level lvl, const char *fmt, const Args &... args) + { + if (!should_log(lvl)) { - default_logger().trace(fmt, args...); + return; } - template - void debug(const char *fmt, const Args &... args) + fmt::memory_buffer formatted_buf; + fmt::format_to(formatted_buf, fmt, args...); + log_formatted_(lvl, formatted_buf); + } + + template + void log(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const char *fmt, const Args &... args) + { + if (!should_log(lvl)) { - default_logger().debug(fmt, args...); + return; } + fmt::memory_buffer formatted_buf; + fmt::format_to(formatted_buf, fmt, args...); + log_formatted_src(src, lvl, formatted_buf); + } - template - void info(const char *fmt, const Args &... args) - { - default_logger().info(fmt, args...); - } + template + void trace(const char *fmt, const Args &... args) + { + log(spdlog::lite::level::trace, fmt, args...); + } - template - void warn(const char *fmt, const Args &... args) - { - default_logger().warn(fmt, args...); - } + template + void trace(const char *source_file, int source_line, const char *source_func, const char *fmt, const Args &... args) + { + spdlog::lite::src_loc src{source_file, source_line, source_func}; + log(src, spdlog::lite::level::trace, fmt, args...); + } - template - void error(const char *fmt, const Args &... args) - { - default_logger().error(fmt, args...); - } + template + void debug(const char *fmt, const Args &... args) + { + log(spdlog::lite::level::debug, fmt, args...); + } - template - void critical(const char *fmt, const Args &... args) - { - default_logger().critical(fmt, args...); - } + template + void info(const char *fmt, const Args &... args) + { + log(spdlog::lite::level::info, fmt, args...); + } + template + void warn(const char *fmt, const Args &... args) + { + log(spdlog::lite::level::warn, fmt, args...); + } - } // namespace lite + template + void error(const char *fmt, const Args &... args) + { + log(spdlog::lite::level::err, fmt, args...); + } - // factory to create lite logger - // implement it in a dedicated compilation unit for fast compiles - spdlog::lite::logger create_lite(void* ctx = nullptr); + template + void critical(const char *fmt, const Args &... args) + { + log(spdlog::lite::level::critical, fmt, args...); + } + + std::string name() const; + + // level + void set_level(lite::level level); + lite::level get_level() const; + + // flush + void flush(); + void flush_on(spdlog::lite::level log_level); + spdlog::lite::level flush_level() const; + + // pattern + void set_pattern(std::string pattern); + +protected: + std::shared_ptr impl_; + void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted); + void log_formatted_src(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted); +}; +spdlog::lite::logger &default_logger(); + +template +void trace(const char *fmt, const Args &... args) +{ + default_logger().trace(fmt, args...); +} +template +void debug(const char *fmt, const Args &... args) +{ + default_logger().debug(fmt, args...); +} + +template +void info(const char *fmt, const Args &... args) +{ + default_logger().info(fmt, args...); +} + +template +void warn(const char *fmt, const Args &... args) +{ + default_logger().warn(fmt, args...); +} + +template +void error(const char *fmt, const Args &... args) +{ + default_logger().error(fmt, args...); +} + +template +void critical(const char *fmt, const Args &... args) +{ + default_logger().critical(fmt, args...); +} + +} // namespace lite + +// factory to create lite logger +// implement it in a dedicated compilation unit for fast compiles +spdlog::lite::logger create_lite(void *ctx = nullptr); } // namespace spdlog \ No newline at end of file diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index dd0c8999..4de3a742 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -69,7 +69,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]") logger->info("Test message {}", i); } // drop causes the logger destructor to be called, which is required so the - // next logger can rename the first output file. + // next logger can rename the first output file. spdlog::drop(logger->name()); }