From 414ff25564d85f06c668133f205c922bf9456d28 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 13:31:57 +0200 Subject: [PATCH 01/24] lite wip --- CMakeLists.txt | 7 ++++ lite-example/CMakeLists.txt | 13 ++++++++ lite-example/create_lite.cpp | 15 +++++++++ lite-example/example.cpp | 7 ++++ lite/CMakeLists.txt | 6 ++++ lite/logger.cpp | 21 ++++++++++++ lite/logger.h | 64 ++++++++++++++++++++++++++++++++++++ lite/spd_types.h | 24 ++++++++++++++ 8 files changed, 157 insertions(+) create mode 100644 lite-example/CMakeLists.txt create mode 100644 lite-example/create_lite.cpp create mode 100644 lite-example/example.cpp create mode 100644 lite/CMakeLists.txt create mode 100644 lite/logger.cpp create mode 100644 lite/logger.h create mode 100644 lite/spd_types.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f0951fc..c4093403 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,8 @@ option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/ option(SPDLOG_BUILD_TESTS "Build tests" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF) option(SPDLOG_INSTALL "Generate the install target." ${SPDLOG_MASTER_PROJECT}) +option(SPDLOG_BUILD_LITE "Build spdlog lite" ${SPDLOG_MASTER_PROJECT}) + if(SPDLOG_FMT_EXTERNAL AND NOT TARGET fmt::fmt) find_package(fmt REQUIRED CONFIG) @@ -86,6 +88,11 @@ if(SPDLOG_BUILD_BENCH) add_subdirectory(bench) endif() +if(SPDLOG_BUILD_LITE) + add_subdirectory(lite) + add_subdirectory(lite-example) +endif() + #--------------------------------------------------------------------------------------- # Install/export targets and files #--------------------------------------------------------------------------------------- diff --git a/lite-example/CMakeLists.txt b/lite-example/CMakeLists.txt new file mode 100644 index 00000000..d152ecd1 --- /dev/null +++ b/lite-example/CMakeLists.txt @@ -0,0 +1,13 @@ +project(spdlog-lite-example CXX) + +find_package(Threads REQUIRED) + +set(LITE_SOURCES example.cpp create_lite.cpp) + +add_executable(${PROJECT_NAME} ${LITE_SOURCES}) + +include_directories(../lite) +target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads) +target_link_libraries(${PROJECT_NAME} PRIVATE spdlog_lite) + + diff --git a/lite-example/create_lite.cpp b/lite-example/create_lite.cpp new file mode 100644 index 00000000..aa375d4e --- /dev/null +++ b/lite-example/create_lite.cpp @@ -0,0 +1,15 @@ +#include "logger.h" +#include "spdlog/spdlog.h" + + +spdlog::lite::logger spdlog::create_lite(void* 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 new file mode 100644 index 00000000..a1a78eb6 --- /dev/null +++ b/lite-example/example.cpp @@ -0,0 +1,7 @@ +#include "logger.h" + +int main() +{ + auto l = spdlog::create_lite(); + l.trace("HELLO {}!!!", "lite"); +} \ No newline at end of file diff --git a/lite/CMakeLists.txt b/lite/CMakeLists.txt new file mode 100644 index 00000000..3ed30be7 --- /dev/null +++ b/lite/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.1) +project(spdlog_lite) + +add_library(spdlog_lite logger.cpp) + +target_link_libraries(spdlog_lite spdlog::spdlog) diff --git a/lite/logger.cpp b/lite/logger.cpp new file mode 100644 index 00000000..6098df84 --- /dev/null +++ b/lite/logger.cpp @@ -0,0 +1,21 @@ +#include "logger.h" +#include "spdlog/spdlog.h" +#include "spdlog/logger.h" + +spdlog::lite::logger::logger(std::shared_ptr impl) +{ + impl_ = std::move(impl); +} + + +bool spdlog::lite::logger::should_log(spdlog::lite::level lvl) const SPDLOG_NOEXCEPT +{ + auto spd_level = static_cast(lvl); + return impl_->should_log(spd_level);//TODO level +} + +void spdlog::lite::logger::log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted) +{ + auto spd_level = static_cast(lvl); + impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); //TODO and source_loc +} \ No newline at end of file diff --git a/lite/logger.h b/lite/logger.h new file mode 100644 index 00000000..6cc60b09 --- /dev/null +++ b/lite/logger.h @@ -0,0 +1,64 @@ +// +// Created by gabi on 3/16/19. +// + +#ifndef SPDLOG_LIB_LOGGER_H +#define SPDLOG_LIB_LOGGER_H + +#include +#include "spd_types.h" +#include "spdlog/fmt/fmt.h" + + +namespace spdlog { + class logger; + + namespace lite { + class logger { + public: + logger() = default; + + logger(std::shared_ptr impl); + logger(const logger&) = default; + logger(logger&&) = default; + logger& operator=(const logger&) = default; + + ~logger() = default; + + 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(spdlog::lite::level lvl, const char *fmt, const Args &... args) +// { +// log(lvl, fmt, args...); +// } + + template + void trace(const char *fmt, const Args &... args) { + log(spdlog::lite::level::trace, fmt, args...); + } + + protected: + std::shared_ptr impl_; + + void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted); + + }; + + } // 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 +#endif //SPDLOG_LIB_LOGGER_H diff --git a/lite/spd_types.h b/lite/spd_types.h new file mode 100644 index 00000000..94be9b34 --- /dev/null +++ b/lite/spd_types.h @@ -0,0 +1,24 @@ +// +// Copyright(c) 2019 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +// core types, forward declarations and defines used by spdlog + +#pragma once + +namespace spdlog +{ +namespace lite +{ + enum class level{ + trace, + debug, + info, + warning, + error, + critical, + off + + }; +}} \ No newline at end of file From ee502aed491e31a0e3b2bd14e7c59ec21d0403a4 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 16:15:58 +0200 Subject: [PATCH 02/24] wip lite --- example/example.cpp | 240 ++------------------------------------- lite-example/example.cpp | 4 +- lite/logger.cpp | 76 +++++++++++-- lite/logger.h | 139 ++++++++++++++++++++--- lite/spd_types.h | 24 ---- 5 files changed, 205 insertions(+), 278 deletions(-) delete mode 100644 lite/spd_types.h diff --git a/example/example.cpp b/example/example.cpp index 345f8bd1..0c3b8395 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -1,235 +1,11 @@ -// -// Copyright(c) 2015 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// -// -// spdlog usage example -// -// - -#include - -void stdout_logger_example(); -void basic_example(); -void rotating_example(); -void daily_example(); -void async_example(); -void binary_example(); -void trace_example(); -void multi_sink_example(); -void user_defined_example(); -void err_handler_example(); -void syslog_example(); -void clone_example(); - #include "spdlog/spdlog.h" -int main(int, char *[]) +int main() { - spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH); - spdlog::warn("Easy padding in numbers like {:08d}", 12); - spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); - spdlog::info("Support for floats {:03.2f}", 1.23456); - spdlog::info("Positional args are {1} {0}..", "too", "supported"); - spdlog::info("{:>8} aligned, {:<8} aligned", "right", "left"); - - // Runtime log levels - spdlog::set_level(spdlog::level::info); // Set global log level to info - spdlog::debug("This message should not be displayed!"); - spdlog::set_level(spdlog::level::trace); // Set specific logger's log level - spdlog::debug("This message should be displayed.."); - - // Customize msg format for all loggers - spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v"); - spdlog::info("This an info message with custom format"); - spdlog::set_pattern("%+"); // back to default format - - try - { - stdout_logger_example(); - basic_example(); - rotating_example(); - daily_example(); - clone_example(); - async_example(); - binary_example(); - multi_sink_example(); - user_defined_example(); - err_handler_example(); - trace_example(); - - // Flush all *registered* loggers using a worker thread every 3 seconds. - // note: registered loggers *must* be thread safe for this to work correctly! - spdlog::flush_every(std::chrono::seconds(3)); - - // Apply some function on all registered loggers - spdlog::apply_all([&](std::shared_ptr l) { l->info("End of example."); }); - - // Release all spdlog resources, and drop all loggers in the registry. - // This is optional (only mandatory if using windows + async log). - spdlog::shutdown(); - } - - // Exceptions will only be thrown upon failed logger or sink construction (not during logging). - catch (const spdlog::spdlog_ex &ex) - { - std::printf("Log initialization failed: %s\n", ex.what()); - return 1; - } -} - -#include "spdlog/sinks/stdout_color_sinks.h" -// or #include "spdlog/sinks/stdout_sinks.h" if no colors needed. -void stdout_logger_example() -{ - // Create color multi threaded logger. - auto console = spdlog::stdout_color_mt("console"); - // or for stderr: - // auto console = spdlog::stderr_color_mt("error-logger"); -} - -#include "spdlog/sinks/basic_file_sink.h" -void basic_example() -{ - // Create basic file logger (not rotated). - auto my_logger = spdlog::basic_logger_mt("file_logger", "logs/basic-log.txt"); -} - -#include "spdlog/sinks/rotating_file_sink.h" -void rotating_example() -{ - // Create a file rotating logger with 5mb size max and 3 rotated files. - auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); -} - -#include "spdlog/sinks/daily_file_sink.h" -void daily_example() -{ - // Create a daily logger - a new file is created every day on 2:30am. - auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); -} - -// Clone a logger and give it new name. -// Useful for creating component/subsystem loggers from some "root" logger. -void clone_example() -{ - auto network_logger = spdlog::default_logger()->clone("network"); - network_logger->info("Logging network stuff.."); -} - -#include "spdlog/async.h" -void async_example() -{ - // Default thread pool settings can be modified *before* creating the async logger: - // spdlog::init_thread_pool(32768, 1); // queue with max 32k items 1 backing thread. - auto async_file = spdlog::basic_logger_mt("async_file_logger", "logs/async_log.txt"); - // alternatively: - // auto async_file = spdlog::create_async("async_file_logger", "logs/async_log.txt"); - - for (int i = 1; i < 101; ++i) - { - async_file->info("Async message #{}", i); - } -} - -// Log binary data as hex. -// Many types of std::container types can be used. -// Iterator ranges are supported too. -// Format flags: -// {:X} - print in uppercase. -// {:s} - don't separate each byte with space. -// {:p} - don't print the position on each line start. -// {:n} - don't split the output to lines. - -#include "spdlog/fmt/bin_to_hex.h" -void binary_example() -{ - std::vector buf; - for (int i = 0; i < 80; i++) - { - buf.push_back(static_cast(i & 0xff)); - } - spdlog::info("Binary example: {}", spdlog::to_hex(buf)); - spdlog::info("Another binary example:{:n}", spdlog::to_hex(std::begin(buf), std::begin(buf) + 10)); - // more examples: - // logger->info("uppercase: {:X}", spdlog::to_hex(buf)); - // logger->info("uppercase, no delimiters: {:Xs}", spdlog::to_hex(buf)); - // logger->info("uppercase, no delimiters, no position info: {:Xsp}", spdlog::to_hex(buf)); -} - -// Compile time log levels. -// define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE) -void trace_example() -{ - // trace from default logger - SPDLOG_TRACE("Some trace message.. {} ,{}", 1, 3.23); - // debug from default logger - SPDLOG_DEBUG("Some debug message.. {} ,{}", 1, 3.23); - - // trace from logger object - auto logger = spdlog::get("file_logger"); - SPDLOG_LOGGER_TRACE(logger, "another trace message"); -} - -// A logger with multiple sinks (stdout and file) - each with a different format and log level. -void multi_sink_example() -{ - auto console_sink = std::make_shared(); - console_sink->set_level(spdlog::level::warn); - console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v"); - - auto file_sink = std::make_shared("logs/multisink.txt", true); - file_sink->set_level(spdlog::level::trace); - - spdlog::logger logger("multi_sink", {console_sink, file_sink}); - logger.set_level(spdlog::level::debug); - logger.warn("this should appear in both console and file"); - logger.info("this message should not appear in the console, only in the file"); -} - -// User defined types logging by implementing operator<< -#include "spdlog/fmt/ostr.h" // must be included -struct my_type -{ - int i; - template - friend OStream &operator<<(OStream &os, const my_type &c) - { - return os << "[my_type i=" << c.i << "]"; - } -}; - -void user_defined_example() -{ - spdlog::info("user defined type: {}", my_type{14}); -} - -// Custom error handler. Will be triggered on log failure. -void err_handler_example() -{ - // can be set globally or per logger(logger->set_error_handler(..)) - spdlog::set_error_handler([](const std::string &msg) { printf("*** Custom log error handler: %s ***\n", msg.c_str()); }); -} - -// syslog example (linux/osx/freebsd) -#ifndef _WIN32 -#include "spdlog/sinks/syslog_sink.h" -void syslog_example() -{ - std::string ident = "spdlog-example"; - auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID); - syslog_logger->warn("This is warning that will end up in syslog."); -} -#endif - -// Android example. -#if defined(__ANDROID__) -#include "spdlog/sinks/android_sink.h" -void android_example() -{ - std::string tag = "spdlog-android"; - auto android_logger = spdlog::android_logger_mt("android", tag); - android_logger->critical("Use \"adb shell logcat\" to view this message."); -} - -#endif + auto l = spdlog::default_logger(); + l->trace("HELLO {}!!!", "lite"); + l->debug("HELLO {}!!!", "lite"); + l->info("HELLO {}!!!", "lite"); + l->warn("HELLO {}!!!", "lite"); + l->critical("HELLO s{}!!!", "lite"); +} \ No newline at end of file diff --git a/lite-example/example.cpp b/lite-example/example.cpp index a1a78eb6..05d772aa 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -2,6 +2,6 @@ int main() { - auto l = spdlog::create_lite(); - l.trace("HELLO {}!!!", "lite"); + //auto l = spdlog::create_lite(); + spdlog::lite::info("HELLO info {}", 123); } \ No newline at end of file diff --git a/lite/logger.cpp b/lite/logger.cpp index 6098df84..39841553 100644 --- a/lite/logger.cpp +++ b/lite/logger.cpp @@ -2,20 +2,82 @@ #include "spdlog/spdlog.h" #include "spdlog/logger.h" +static spdlog::level::level_enum to_spdlog_level(spdlog::lite::level level) +{ + return static_cast(level); +} + +static spdlog::lite::level to_lite_level(spdlog::level::level_enum level) +{ + return static_cast(level); +} + spdlog::lite::logger::logger(std::shared_ptr impl) { impl_ = std::move(impl); } - -bool spdlog::lite::logger::should_log(spdlog::lite::level lvl) const SPDLOG_NOEXCEPT +bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NOEXCEPT { - auto spd_level = static_cast(lvl); + auto spd_level = to_spdlog_level(level); return impl_->should_log(spd_level);//TODO level } -void spdlog::lite::logger::log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted) +void spdlog::lite::logger::log_formatted_(spdlog::lite::level level, const fmt::memory_buffer &formatted) { - auto spd_level = static_cast(lvl); - impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); //TODO and source_loc -} \ No newline at end of file + auto spd_level = to_spdlog_level(level); + 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); + spdlog::source_loc source_loc{src.filename, src.line, src.funcname}; + impl_->log(source_loc, spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); +} + +void spdlog::lite::logger::set_level(spdlog::lite::level level) +{ + auto spd_level = to_spdlog_level(level); + impl_->set_level(spd_level); +} + +spdlog::lite::level spdlog::lite::logger::get_level() const +{ + return to_lite_level(impl_->level()); +} + +std::string spdlog::lite::logger::name() const +{ + return impl_->name(); +} + +void spdlog::lite::logger::flush() +{ + impl_->flush(); +} + +void spdlog::lite::logger::flush_on(spdlog::lite::level level) +{ + auto spd_level = to_spdlog_level(level); + impl_->flush_on(spd_level); +} + +spdlog::lite::level spdlog::lite::logger::flush_level() const +{ + return to_lite_level(impl_->flush_level()); +} + +// pattern +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()); + return s_default; +} diff --git a/lite/logger.h b/lite/logger.h index 6cc60b09..b762b3f5 100644 --- a/lite/logger.h +++ b/lite/logger.h @@ -1,19 +1,36 @@ // // Created by gabi on 3/16/19. // - -#ifndef SPDLOG_LIB_LOGGER_H -#define SPDLOG_LIB_LOGGER_H +#pragma once #include -#include "spd_types.h" +#include #include "spdlog/fmt/fmt.h" +//#define SPDLITE_LOGGER_INFO(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::info, __VA_ARGS__) +//#define SPDLITE_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__) + namespace spdlog { class logger; namespace lite { + enum class level{ + trace, + debug, + info, + warn, + err, + critical, + off + }; + + struct src_loc { + const char *filename; + int line; + const char* funcname; + }; + class logger { public: logger() = default; @@ -37,28 +54,124 @@ namespace spdlog { log_formatted_(lvl, formatted_buf); } -// template -// void log(spdlog::lite::level lvl, const char *fmt, const Args &... args) -// { -// log(lvl, fmt, args...); -// } template - void trace(const char *fmt, const Args &... args) { + 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) + { + 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 -#endif //SPDLOG_LIB_LOGGER_H + +} // namespace spdlog \ No newline at end of file diff --git a/lite/spd_types.h b/lite/spd_types.h deleted file mode 100644 index 94be9b34..00000000 --- a/lite/spd_types.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// Copyright(c) 2019 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -// core types, forward declarations and defines used by spdlog - -#pragma once - -namespace spdlog -{ -namespace lite -{ - enum class level{ - trace, - debug, - info, - warning, - error, - critical, - off - - }; -}} \ No newline at end of file From c29b7d22d96999dcb696a16a5d1dfc804defd10a Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 16:39:05 +0200 Subject: [PATCH 03/24] wip lite --- lite-example/example.cpp | 6 ++- lite/logger.h | 86 +++++++++++++++++++++++++++++++++++----- 2 files changed, 80 insertions(+), 12 deletions(-) diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 05d772aa..929e75dd 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -1,7 +1,11 @@ +#define SPDLITE_ACTIVE_LEVEL SPDLITE_LEVEL_INFO #include "logger.h" int main() { //auto l = spdlog::create_lite(); - spdlog::lite::info("HELLO info {}", 123); + //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.h b/lite/logger.h index b762b3f5..7246411c 100644 --- a/lite/logger.h +++ b/lite/logger.h @@ -1,28 +1,92 @@ // -// Created by gabi on 3/16/19. -// +// Copyright(c) 2015-present Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + #pragma once #include #include #include "spdlog/fmt/fmt.h" +// +// enable/disable log calls at compile time according to global level. +// +// define SPDLITE_ACTIVE_LEVEL to one of those (before including lite.h): + +#define SPDLITE_LEVEL_TRACE 0 +#define SPDLITE_LEVEL_DEBUG 1 +#define SPDLITE_LEVEL_INFO 2 +#define SPDLITE_LEVEL_WARN 3 +#define SPDLITE_LEVEL_ERROR 4 +#define SPDLITE_LEVEL_CRITICAL 5 +#define SPDLITE_LEVEL_OFF 6 + +#define SPDLITE_LOGGER_CALL(logger, level, ...) \ + if (logger.should_log(level)) \ + logger.log(level, __VA_ARGS__) + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_TRACE +#define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::trace, __VA_ARGS__) +#define SPDLITE_TRACE(...) SPDLITE_LOGGER_TRACE(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_TRACE(logger, ...) (void)0 +#define SPDLITE_TRACE(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_DEBUG +#define SPDLITE_LOGGER_DEBUG(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::debug, __VA_ARGS__) +#define SPDLITE_DEBUG(...) SPDLITE_LOGGER_DEBUG(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_DEBUG(logger, ...) (void)0 +#define SPDLITE_DEBUG(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_INFO +#define SPDLITE_LOGGER_INFO(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::info, __VA_ARGS__) +#define SPDLITE_INFO(...) SPDLITE_LOGGER_INFO(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_INFO(logger, ...) (void)0 +#define SPDLITE_INFO(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_WARN +#define SPDLITE_LOGGER_WARN(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::warn, __VA_ARGS__) +#define SPDLITE_WARN(...) SPDLITE_LOGGER_WARN(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_WARN(logger, ...) (void)0 +#define SPDLITE_WARN(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_ERROR +#define SPDLITE_LOGGER_ERROR(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::err, __VA_ARGS__) +#define SPDLITE_ERROR(...) SPDLITE_LOGGER_ERROR(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_ERROR(logger, ...) (void)0 +#define SPDLITE_ERROR(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_CRITICAL +#define SPDLITE_LOGGER_CRITICAL(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::critical, __VA_ARGS__) +#define SPDLITE_CRITICAL(...) SPDLITE_LOGGER_CRITICAL(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_CRITICAL(logger, ...) (void)0 +#define SPDLITE_CRITICAL(...) (void)0 +#endif + -//#define SPDLITE_LOGGER_INFO(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::info, __VA_ARGS__) -//#define SPDLITE_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__) namespace spdlog { class logger; namespace lite { enum class level{ - trace, - debug, - info, - warn, - err, - critical, - off + 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 { From 1d72edcc4f9f37f3ebafe32639f7aa1454a16115 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 16:39:32 +0200 Subject: [PATCH 04/24] 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()); } From 3dee10772dfe6aa196336cc1932375c062054710 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 17:25:50 +0200 Subject: [PATCH 05/24] renamed --- lite-example/create_lite.cpp | 2 +- lite-example/example.cpp | 2 +- lite/CMakeLists.txt | 2 +- lite/{logger.cpp => spdlite.cpp} | 2 +- lite/{logger.h => spdlite.h} | 0 5 files changed, 4 insertions(+), 4 deletions(-) rename lite/{logger.cpp => spdlite.cpp} (99%) rename lite/{logger.h => spdlite.h} (100%) diff --git a/lite-example/create_lite.cpp b/lite-example/create_lite.cpp index 5345237f..beea64ba 100644 --- a/lite-example/create_lite.cpp +++ b/lite-example/create_lite.cpp @@ -1,4 +1,4 @@ -#include "logger.h" +#include "spdlite.h" #include "spdlog/spdlog.h" spdlog::lite::logger spdlog::create_lite(void *ctx) diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 376d9a68..82ddef93 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -1,5 +1,5 @@ #define SPDLITE_ACTIVE_LEVEL SPDLITE_LEVEL_INFO -#include "logger.h" +#include "spdlite.h" int main() { diff --git a/lite/CMakeLists.txt b/lite/CMakeLists.txt index 3ed30be7..ee8a4aab 100644 --- a/lite/CMakeLists.txt +++ b/lite/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.1) project(spdlog_lite) -add_library(spdlog_lite logger.cpp) +add_library(spdlog_lite spdlite.cpp) target_link_libraries(spdlog_lite spdlog::spdlog) diff --git a/lite/logger.cpp b/lite/spdlite.cpp similarity index 99% rename from lite/logger.cpp rename to lite/spdlite.cpp index 0cbe766f..043bd1c8 100644 --- a/lite/logger.cpp +++ b/lite/spdlite.cpp @@ -1,4 +1,4 @@ -#include "logger.h" +#include "spdlite.h" #include "spdlog/spdlog.h" #include "spdlog/logger.h" diff --git a/lite/logger.h b/lite/spdlite.h similarity index 100% rename from lite/logger.h rename to lite/spdlite.h From 0e977d66c1186e19704682ac2d45327c3823b062 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 19:23:37 +0200 Subject: [PATCH 06/24] Dont check level in macros (redundant) --- include/spdlog/spdlog.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 20ff24be..2fadfb45 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -311,8 +311,7 @@ inline void critical(const wchar_t *fmt, const Args &... args) // SPDLOG_LEVEL_OFF // -#define SPDLOG_LOGGER_CALL(logger, level, ...) \ - if (logger->should_log(level)) \ +#define SPDLOG_LOGGER_CALL(logger, level, ...) \ logger->log(spdlog::source_loc{SPDLOG_FILE_BASENAME(__FILE__), __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE From 4c240edf946e0b5887dd0cc0b6f12b907fb27072 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 19:34:50 +0200 Subject: [PATCH 07/24] wip lite --- .clang-tidy | 2 +- clang_tidy.sh | 3 +- example/example.cpp | 12 ++++---- include/spdlog/spdlog.h | 2 +- lite-example/create_lite.cpp | 12 ++++++-- lite-example/example.cpp | 10 +++---- lite/spdlite.cpp | 15 +++++----- lite/spdlite.h | 55 +++++++++++++++++------------------- 8 files changed, 58 insertions(+), 53 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 309c7e94..9079dd9a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,6 +1,6 @@ Checks: 'modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*' WarningsAsErrors: '' -HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h' +HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h|spdlite.h' AnalyzeTemporaryDtors: false FormatStyle: none diff --git a/clang_tidy.sh b/clang_tidy.sh index 6e043e27..4fb8644b 100755 --- a/clang_tidy.sh +++ b/clang_tidy.sh @@ -1,2 +1,3 @@ #!/bin/bash -clang-tidy example/example.cpp -- -I ./include +clang-tidy example/example.cpp -- -I ./include +clang-tidy lite-example/example.cpp -- -I./include -I./lite diff --git a/example/example.cpp b/example/example.cpp index 0c3b8395..c0971620 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -1,11 +1,11 @@ +#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE #include "spdlog/spdlog.h" int main() { - auto l = spdlog::default_logger(); - l->trace("HELLO {}!!!", "lite"); - l->debug("HELLO {}!!!", "lite"); - l->info("HELLO {}!!!", "lite"); - l->warn("HELLO {}!!!", "lite"); - l->critical("HELLO s{}!!!", "lite"); + // auto l = spdlog::create_lite(); + // spdlog::lite::info("HELLO info {}", 123); + + SPDLOG_TRACE("SOME MACRO {}", 123); + SPDLOG_INFO("SOME MACRO {}", "HHHHH"); } \ No newline at end of file diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 2fadfb45..995e93f4 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -311,7 +311,7 @@ inline void critical(const wchar_t *fmt, const Args &... args) // SPDLOG_LEVEL_OFF // -#define SPDLOG_LOGGER_CALL(logger, level, ...) \ +#define SPDLOG_LOGGER_CALL(logger, level, ...) \ logger->log(spdlog::source_loc{SPDLOG_FILE_BASENAME(__FILE__), __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE diff --git a/lite-example/create_lite.cpp b/lite-example/create_lite.cpp index beea64ba..a0e5e389 100644 --- a/lite-example/create_lite.cpp +++ b/lite-example/create_lite.cpp @@ -1,5 +1,9 @@ #include "spdlite.h" #include "spdlog/spdlog.h" +#include "spdlog/sinks/basic_file_sink.h" +#include "spdlog/sinks/stdout_color_sinks.h" + + spdlog::lite::logger spdlog::create_lite(void *ctx) { @@ -7,7 +11,9 @@ spdlog::lite::logger spdlog::create_lite(void *ctx) { //.. } - auto logger_impl = spdlog::default_logger(); - logger_impl->set_level(spdlog::level::trace); - return spdlog::lite::logger(logger_impl); + auto logger_impl = spdlog::stdout_color_mt("mylogger"); + auto file_sink = std::make_shared("file.txt", true); + logger_impl->sinks().push_back(file_sink); + logger_impl->set_level(spdlog::level::debug); + return spdlog::lite::logger(std::move(logger_impl)); } diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 82ddef93..87cbc895 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -1,10 +1,10 @@ -#define SPDLITE_ACTIVE_LEVEL SPDLITE_LEVEL_INFO +#define SPDLITE_ACTIVE_LEVEL SPDLITE_LEVEL_TRACE #include "spdlite.h" int main() { - // auto l = spdlog::create_lite(); - // spdlog::lite::info("HELLO info {}", 123); - SPDLITE_TRACE("SOME MACRO {}", 123); - SPDLITE_INFO("SOME MACRO {}", "HHHHH"); + //auto l = spdlog::create_lite(); + //l.info("Hello spdlog {}", "lite"); + SPDLITE_TRACE("HELLO TRACE"); + } \ No newline at end of file diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 043bd1c8..53f98fe9 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -20,22 +20,23 @@ 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 avoid the call using local level member? } -void spdlog::lite::logger::log_formatted_(spdlog::lite::level level, const fmt::memory_buffer &formatted) -{ - auto spd_level = to_spdlog_level(level); - 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) + +void spdlog::lite::logger::log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted) { auto spd_level = to_spdlog_level(lvl); spdlog::source_loc source_loc{src.filename, src.line, src.funcname}; impl_->log(source_loc, spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); } +void spdlog::lite::logger::log_formatted_(spdlog::lite::level level, const fmt::memory_buffer &formatted) +{ + log_formatted_(src_loc{}, level, formatted); +} + void spdlog::lite::logger::set_level(spdlog::lite::level level) { auto spd_level = to_spdlog_level(level); diff --git a/lite/spdlite.h b/lite/spdlite.h index b55a7159..c1ec48ba 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -21,12 +21,16 @@ #define SPDLITE_LEVEL_CRITICAL 5 #define SPDLITE_LEVEL_OFF 6 -#define SPDLITE_LOGGER_CALL(logger, level, ...) \ - if (logger.should_log(level)) \ - logger.log(level, __VA_ARGS__) + +#define SPDLITE_LOGGER_CALL(logger, level, ...) logger.log(level, __VA_ARGS__) + +//with source info +#define SPDLITE_LOGGER_CALL2(logger, level, ...) \ + logger.log(spdlog::lite::src_loc{__FILE__, __LINE__, __FUNCTION__}, level, __VA_ARGS__) + #if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_TRACE -#define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::trace, __VA_ARGS__) +#define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL2(logger, spdlog::lite::level::trace, __VA_ARGS__) #define SPDLITE_TRACE(...) SPDLITE_LOGGER_TRACE(spdlog::lite::default_logger(), __VA_ARGS__) #else #define SPDLITE_LOGGER_TRACE(logger, ...) (void)0 @@ -90,9 +94,13 @@ enum class level struct src_loc { - const char *filename; - int line; - const char *funcname; + src_loc() = default; + src_loc(const char *filename, int line, const char *funcname): + filename(filename), line(line), funcname(funcname){} + + const char *filename = nullptr; + int line = 0; + const char *funcname = nullptr; }; class logger @@ -100,7 +108,7 @@ class logger public: logger() = default; - logger(std::shared_ptr impl); + explicit logger(std::shared_ptr impl); logger(const logger &) = default; logger(logger &&) = default; logger &operator=(const logger &) = default; @@ -109,18 +117,6 @@ public: 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) { @@ -130,7 +126,13 @@ public: } fmt::memory_buffer formatted_buf; fmt::format_to(formatted_buf, fmt, args...); - log_formatted_src(src, lvl, formatted_buf); + log_formatted_(src, lvl, formatted_buf); + } + + template + void log(spdlog::lite::level lvl, const char *fmt, const Args &... args) + { + log(spdlog::lite::src_loc{}, lvl, fmt, args...); } template @@ -139,13 +141,7 @@ public: 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) { @@ -193,8 +189,9 @@ public: 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); + void log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted); }; + spdlog::lite::logger &default_logger(); template From 569c62e5286d61c0e65c4ce76cc2e067ddac4202 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 20:06:49 +0200 Subject: [PATCH 08/24] support for const char* messages --- lite/spdlite.cpp | 13 +++++++++--- lite/spdlite.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 53f98fe9..1e32a570 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -24,7 +24,6 @@ bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NO } - void spdlog::lite::logger::log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted) { auto spd_level = to_spdlog_level(lvl); @@ -32,11 +31,19 @@ void spdlog::lite::logger::log_formatted_(const spdlog::lite::src_loc &src, spdl impl_->log(source_loc, spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); } -void spdlog::lite::logger::log_formatted_(spdlog::lite::level level, const fmt::memory_buffer &formatted) +void spdlog::lite::logger::log_string_view_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const string_view_t &sv) { - log_formatted_(src_loc{}, level, formatted); + auto spd_level = to_spdlog_level(lvl); + spdlog::source_loc source_loc{src.filename, src.line, src.funcname}; + impl_->log(source_loc, spd_level, sv); } +void spdlog::lite::logger::log_string_view_(spdlog::lite::level lvl, const string_view_t &sv) +{ + log_string_view_(spdlog::lite::src_loc{}, lvl, sv); +} + + void spdlog::lite::logger::set_level(spdlog::lite::level level) { auto spd_level = to_spdlog_level(level); diff --git a/lite/spdlite.h b/lite/spdlite.h index c1ec48ba..f596cf31 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -77,10 +77,22 @@ #define SPDLITE_CRITICAL(...) (void)0 #endif + + + + namespace spdlog { class logger; namespace lite { + +// string_view type - either std::string_view or fmt::string_view (pre c++17) +#if defined(FMT_USE_STD_STRING_VIEW) + using string_view_t = std::string_view; +#else + using string_view_t = fmt::string_view; +#endif + enum class level { trace = SPDLITE_LEVEL_TRACE, @@ -135,42 +147,74 @@ public: log(spdlog::lite::src_loc{}, lvl, fmt, args...); } + template void trace(const char *fmt, const Args &... args) { log(spdlog::lite::level::trace, fmt, args...); } - + void trace(const char* msg) + { + log_string_view_(spdlog::lite::level::trace, msg); + } + + template void debug(const char *fmt, const Args &... args) { log(spdlog::lite::level::debug, fmt, args...); } + void debug(const char* msg) + { + log_string_view_(spdlog::lite::level::debug, msg); + } + template void info(const char *fmt, const Args &... args) { log(spdlog::lite::level::info, fmt, args...); } + void info(const char* msg) + { + log_string_view_(spdlog::lite::level::info, msg); + } + + template void warn(const char *fmt, const Args &... args) { log(spdlog::lite::level::warn, fmt, args...); } + void warn(const char* msg) + { + log_string_view_(spdlog::lite::level::warn, msg); + } + template void error(const char *fmt, const Args &... args) { log(spdlog::lite::level::err, fmt, args...); } + void error(const char* msg) + { + log_string_view_(spdlog::lite::level::err, msg); + } + template void critical(const char *fmt, const Args &... args) { log(spdlog::lite::level::critical, fmt, args...); } + void critical(const char* msg) + { + log_string_view_(spdlog::lite::level::critical, msg); + } + std::string name() const; @@ -188,8 +232,10 @@ public: protected: std::shared_ptr impl_; - void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted); void log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted); + void log_string_view_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const string_view_t &sv); + void log_string_view_(spdlog::lite::level lvl, const string_view_t &sv); + }; spdlog::lite::logger &default_logger(); @@ -199,6 +245,7 @@ void trace(const char *fmt, const Args &... args) { default_logger().trace(fmt, args...); } + template void debug(const char *fmt, const Args &... args) { From 4449bf6f8366c94d0b05c4df27e3d98b72c2c07d Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 23:13:38 +0200 Subject: [PATCH 09/24] wip lite --- lite-example/create_lite.cpp | 5 ++- lite-example/example.cpp | 11 +++--- lite/spdlite.cpp | 2 -- lite/spdlite.h | 69 ++++++++++++++++-------------------- 4 files changed, 39 insertions(+), 48 deletions(-) diff --git a/lite-example/create_lite.cpp b/lite-example/create_lite.cpp index a0e5e389..1a6d6bfe 100644 --- a/lite-example/create_lite.cpp +++ b/lite-example/create_lite.cpp @@ -3,8 +3,6 @@ #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/stdout_color_sinks.h" - - spdlog::lite::logger spdlog::create_lite(void *ctx) { if (ctx) @@ -13,7 +11,8 @@ spdlog::lite::logger spdlog::create_lite(void *ctx) } auto logger_impl = spdlog::stdout_color_mt("mylogger"); auto file_sink = std::make_shared("file.txt", true); + file_sink->set_level(spdlog::level::info); logger_impl->sinks().push_back(file_sink); - logger_impl->set_level(spdlog::level::debug); + logger_impl->set_level(spdlog::level::trace); return spdlog::lite::logger(std::move(logger_impl)); } diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 87cbc895..60942ebe 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -1,10 +1,11 @@ -#define SPDLITE_ACTIVE_LEVEL SPDLITE_LEVEL_TRACE #include "spdlite.h" int main() { - //auto l = spdlog::create_lite(); - //l.info("Hello spdlog {}", "lite"); - SPDLITE_TRACE("HELLO TRACE"); - + using namespace spdlog; + auto l = spdlog::create_lite(); + l.set_level(spdlog::lite::level::trace); + lite::default_logger().set_level(l.get_level()); + lite::trace("hello"); + lite::trace("hello {}", std::string("again")); } \ No newline at end of file diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 1e32a570..6b4f4f87 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -23,7 +23,6 @@ bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NO return impl_->should_log(spd_level); // TODO avoid the call using local level member? } - void spdlog::lite::logger::log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted) { auto spd_level = to_spdlog_level(lvl); @@ -43,7 +42,6 @@ void spdlog::lite::logger::log_string_view_(spdlog::lite::level lvl, const strin log_string_view_(spdlog::lite::src_loc{}, lvl, sv); } - void spdlog::lite::logger::set_level(spdlog::lite::level level) { auto spd_level = to_spdlog_level(level); diff --git a/lite/spdlite.h b/lite/spdlite.h index f596cf31..77656959 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -21,13 +21,10 @@ #define SPDLITE_LEVEL_CRITICAL 5 #define SPDLITE_LEVEL_OFF 6 +#define SPDLITE_LOGGER_CALL(logger, level, ...) logger.log(level, __VA_ARGS__) -#define SPDLITE_LOGGER_CALL(logger, level, ...) logger.log(level, __VA_ARGS__) - -//with source info -#define SPDLITE_LOGGER_CALL2(logger, level, ...) \ - logger.log(spdlog::lite::src_loc{__FILE__, __LINE__, __FUNCTION__}, level, __VA_ARGS__) - +// with source info +#define SPDLITE_LOGGER_CALL2(logger, level, ...) logger.log(spdlog::lite::src_loc{__FILE__, __LINE__, __FUNCTION__}, level, __VA_ARGS__) #if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_TRACE #define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL2(logger, spdlog::lite::level::trace, __VA_ARGS__) @@ -77,10 +74,6 @@ #define SPDLITE_CRITICAL(...) (void)0 #endif - - - - namespace spdlog { class logger; @@ -88,9 +81,9 @@ namespace lite { // string_view type - either std::string_view or fmt::string_view (pre c++17) #if defined(FMT_USE_STD_STRING_VIEW) - using string_view_t = std::string_view; +using string_view_t = std::string_view; #else - using string_view_t = fmt::string_view; +using string_view_t = fmt::string_view; #endif enum class level @@ -107,8 +100,12 @@ enum class level struct src_loc { src_loc() = default; - src_loc(const char *filename, int line, const char *funcname): - filename(filename), line(line), funcname(funcname){} + src_loc(const char *filename, int line, const char *funcname) + : filename(filename) + , line(line) + , funcname(funcname) + { + } const char *filename = nullptr; int line = 0; @@ -147,6 +144,11 @@ public: log(spdlog::lite::src_loc{}, lvl, fmt, args...); } + // trace + void trace(const char *msg) + { + log_string_view_(spdlog::lite::level::trace, msg); + } template void trace(const char *fmt, const Args &... args) @@ -154,21 +156,22 @@ public: log(spdlog::lite::level::trace, fmt, args...); } - void trace(const char* msg) + // debug + void debug(const char *msg) { - log_string_view_(spdlog::lite::level::trace, msg); + log_string_view_(spdlog::lite::level::debug, msg); } - template void debug(const char *fmt, const Args &... args) { log(spdlog::lite::level::debug, fmt, args...); } - void debug(const char* msg) + // info + void info(const char *msg) { - log_string_view_(spdlog::lite::level::debug, msg); + log_string_view_(spdlog::lite::level::info, msg); } template @@ -177,21 +180,22 @@ public: log(spdlog::lite::level::info, fmt, args...); } - void info(const char* msg) + // warn + void warn(const char *msg) { - log_string_view_(spdlog::lite::level::info, msg); + log_string_view_(spdlog::lite::level::warn, msg); } - template void warn(const char *fmt, const Args &... args) { log(spdlog::lite::level::warn, fmt, args...); } - void warn(const char* msg) + // error + void error(const char *msg) { - log_string_view_(spdlog::lite::level::warn, msg); + log_string_view_(spdlog::lite::level::err, msg); } template @@ -200,9 +204,10 @@ public: log(spdlog::lite::level::err, fmt, args...); } - void error(const char* msg) + // critical + void critical(const char *msg) { - log_string_view_(spdlog::lite::level::err, msg); + log_string_view_(spdlog::lite::level::critical, msg); } template @@ -210,24 +215,13 @@ public: { log(spdlog::lite::level::critical, fmt, args...); } - void critical(const char* msg) - { - log_string_view_(spdlog::lite::level::critical, msg); - } - 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: @@ -235,7 +229,6 @@ protected: void log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted); void log_string_view_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const string_view_t &sv); void log_string_view_(spdlog::lite::level lvl, const string_view_t &sv); - }; spdlog::lite::logger &default_logger(); From 621e0a833055cbd785095ce67c01ced44f5d0d14 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 00:30:08 +0200 Subject: [PATCH 10/24] added printf syntax support for even faster compilation! --- example/example.cpp | 9 ++--- lite-example/example.cpp | 8 +++-- lite/spdlite.cpp | 74 +++++++++++++++++++++++++++++++++----- lite/spdlite.h | 76 ++++++++++++++++++++++++---------------- 4 files changed, 121 insertions(+), 46 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index c0971620..8384906d 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -3,9 +3,10 @@ int main() { - // auto l = spdlog::create_lite(); - // spdlog::lite::info("HELLO info {}", 123); + using namespace spdlog; + auto l = spdlog::default_logger(); + l->set_level(spdlog::level::trace); + + l->trace("hello123123213 "); - SPDLOG_TRACE("SOME MACRO {}", 123); - SPDLOG_INFO("SOME MACRO {}", "HHHHH"); } \ No newline at end of file diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 60942ebe..34bb3ac4 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -5,7 +5,9 @@ int main() using namespace spdlog; auto l = spdlog::create_lite(); l.set_level(spdlog::lite::level::trace); - lite::default_logger().set_level(l.get_level()); - lite::trace("hello"); - lite::trace("hello {}", std::string("again")); + + + l.trace_f("Hello %s ","GABI"); + l.info_f("Hello %d", 12346); + } \ No newline at end of file diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 6b4f4f87..2bcdfa1b 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -23,23 +23,71 @@ bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NO return impl_->should_log(spd_level); // TODO avoid the call using local level member? } -void spdlog::lite::logger::log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted) +void spdlog::lite::logger::log(spdlog::lite::level lvl, const string_view_t &sv) { auto spd_level = to_spdlog_level(lvl); - spdlog::source_loc source_loc{src.filename, src.line, src.funcname}; - impl_->log(source_loc, spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); + impl_->log(spd_level, sv); } -void spdlog::lite::logger::log_string_view_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const string_view_t &sv) + +void spdlog::lite::logger::log_printf(spdlog::lite::level lvl, const char* format, va_list args) { - auto spd_level = to_spdlog_level(lvl); - spdlog::source_loc source_loc{src.filename, src.line, src.funcname}; - impl_->log(source_loc, spd_level, sv); + char buffer[500]; + auto size = vsnprintf (buffer, sizeof(buffer),format, args); + if(size < 0) + { + size = snprintf(buffer, sizeof(buffer), "invalid format (%s)", format); + } + log(lvl, string_view_t{buffer, static_cast(size)}); } -void spdlog::lite::logger::log_string_view_(spdlog::lite::level lvl, const string_view_t &sv) + +void spdlog::lite::logger::trace_f(const char *format, ...) { - log_string_view_(spdlog::lite::src_loc{}, lvl, sv); + va_list args; + va_start (args, format); + log_printf(lite::level::trace, format, args); + va_end (args); +} + +void spdlog::lite::logger::debug_f(const char *format, ...) +{ + va_list args; + va_start (args, format); + log_printf(lite::level::debug, format, args); + va_end (args); +} + +void spdlog::lite::logger::info_f(const char *format, ...) +{ + va_list args; + va_start (args, format); + log_printf(lite::level::info, format, args); + va_end (args); +} + +void spdlog::lite::logger::warn_f(const char *format, ...) +{ + va_list args; + va_start (args, format); + log_printf(lite::level::warn, format, args); + va_end (args); +} + +void spdlog::lite::logger::error_f(const char *format, ...) +{ + va_list args; + va_start (args, format); + log_printf(lite::level::err, format, args); + va_end (args); +} + +void spdlog::lite::logger::critical_f(const char *format, ...) +{ + va_list args; + va_start (args, format); + log_printf(lite::level::critical, format, args); + va_end (args); } void spdlog::lite::logger::set_level(spdlog::lite::level level) @@ -80,8 +128,16 @@ void spdlog::lite::logger::set_pattern(std::string pattern) impl_->set_pattern(std::move(pattern)); } +void spdlog::lite::logger::log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted) +{ + auto spd_level = to_spdlog_level(lvl); + impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); +} + spdlog::lite::logger &spdlog::lite::default_logger() { static spdlog::lite::logger s_default(spdlog::default_logger()); return s_default; } + + diff --git a/lite/spdlite.h b/lite/spdlite.h index 77656959..51f4ed6f 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -97,20 +97,6 @@ enum class level off = SPDLITE_LEVEL_OFF }; -struct src_loc -{ - src_loc() = default; - src_loc(const char *filename, int line, const char *funcname) - : filename(filename) - , line(line) - , funcname(funcname) - { - } - - const char *filename = nullptr; - int line = 0; - const char *funcname = nullptr; -}; class logger { @@ -127,7 +113,7 @@ public: bool should_log(spdlog::lite::level lvl) const noexcept; template - void log(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const char *fmt, const Args &... args) + void log(spdlog::lite::level lvl, const char *fmt, const Args &... args) { if (!should_log(lvl)) { @@ -135,19 +121,20 @@ public: } fmt::memory_buffer formatted_buf; fmt::format_to(formatted_buf, fmt, args...); - log_formatted_(src, lvl, formatted_buf); + log_formatted_(lvl, formatted_buf); } - template - void log(spdlog::lite::level lvl, const char *fmt, const Args &... args) - { - log(spdlog::lite::src_loc{}, lvl, fmt, args...); - } + // log string view + void log(spdlog::lite::level lvl, const string_view_t &sv); + void log_printf(spdlog::lite::level lvl, const char* format, va_list args); + + // // trace + // void trace(const char *msg) { - log_string_view_(spdlog::lite::level::trace, msg); + log(spdlog::lite::level::trace, string_view_t(msg)); } template @@ -156,10 +143,14 @@ public: log(spdlog::lite::level::trace, fmt, args...); } + void trace_f(const char *printf_format, ...); + + // // debug + // void debug(const char *msg) { - log_string_view_(spdlog::lite::level::debug, msg); + log(spdlog::lite::level::debug, string_view_t(msg)); } template @@ -168,10 +159,14 @@ public: log(spdlog::lite::level::debug, fmt, args...); } + void debug_f(const char *printf_format, ...); + + // // info + // void info(const char *msg) { - log_string_view_(spdlog::lite::level::info, msg); + log(spdlog::lite::level::info, string_view_t(msg)); } template @@ -180,10 +175,14 @@ public: log(spdlog::lite::level::info, fmt, args...); } + void info_f(const char *printf_format, ...); + + // // warn + // void warn(const char *msg) { - log_string_view_(spdlog::lite::level::warn, msg); + log(spdlog::lite::level::warn, string_view_t(msg)); } template @@ -192,22 +191,31 @@ public: log(spdlog::lite::level::warn, fmt, args...); } + void warn_f(const char *printf_format, ...); + + // // error + // void error(const char *msg) { - log_string_view_(spdlog::lite::level::err, msg); + log(spdlog::lite::level::err, string_view_t(msg)); } + template void error(const char *fmt, const Args &... args) { log(spdlog::lite::level::err, fmt, args...); } + void error_f(const char *printf_format, ...); + + // // critical + // void critical(const char *msg) { - log_string_view_(spdlog::lite::level::critical, msg); + log(spdlog::lite::level::critical, string_view_t(msg)); } template @@ -216,9 +224,18 @@ public: log(spdlog::lite::level::critical, fmt, args...); } + void critical_f(const char *printf_format, ...); + + // + // setters/getters + // std::string name() const; 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; @@ -226,9 +243,8 @@ public: protected: std::shared_ptr impl_; - void log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted); - void log_string_view_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const string_view_t &sv); - void log_string_view_(spdlog::lite::level lvl, const string_view_t &sv); + void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted); + }; spdlog::lite::logger &default_logger(); From 680f19a4244975cbb1be2a4890612588024ac3c7 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 00:31:55 +0200 Subject: [PATCH 11/24] Removed source logging --- lite/spdlite.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lite/spdlite.h b/lite/spdlite.h index 51f4ed6f..2793f5c1 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -23,11 +23,8 @@ #define SPDLITE_LOGGER_CALL(logger, level, ...) logger.log(level, __VA_ARGS__) -// with source info -#define SPDLITE_LOGGER_CALL2(logger, level, ...) logger.log(spdlog::lite::src_loc{__FILE__, __LINE__, __FUNCTION__}, level, __VA_ARGS__) - #if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_TRACE -#define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL2(logger, spdlog::lite::level::trace, __VA_ARGS__) +#define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::trace, __VA_ARGS__) #define SPDLITE_TRACE(...) SPDLITE_LOGGER_TRACE(spdlog::lite::default_logger(), __VA_ARGS__) #else #define SPDLITE_LOGGER_TRACE(logger, ...) (void)0 From b1c90f08cc534430d62ca94da5f5175497e50af5 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 00:38:00 +0200 Subject: [PATCH 12/24] moved macros to seprate headers --- lite-example/example.cpp | 4 +-- lite/spdlite.cpp | 1 - lite/spdlite.h | 78 ++++------------------------------------ lite/spdlite_macros.h | 67 ++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 74 deletions(-) create mode 100644 lite/spdlite_macros.h diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 34bb3ac4..f45eeb9e 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -6,8 +6,8 @@ int main() auto l = spdlog::create_lite(); l.set_level(spdlog::lite::level::trace); - l.trace_f("Hello %s ","GABI"); l.info_f("Hello %d", 12346); - + l.warn_f("Hello %f", 12346.5656); + l.warn("Hello {}", 12346.5656); } \ No newline at end of file diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 2bcdfa1b..8b7ace31 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -1,6 +1,5 @@ #include "spdlite.h" #include "spdlog/spdlog.h" -#include "spdlog/logger.h" static spdlog::level::level_enum to_spdlog_level(spdlog::lite::level level) { diff --git a/lite/spdlite.h b/lite/spdlite.h index 2793f5c1..39579002 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -8,69 +8,6 @@ #include #include "spdlog/fmt/fmt.h" -// -// enable/disable log calls at compile time according to global level. -// -// define SPDLITE_ACTIVE_LEVEL to one of those (before including lite.h): - -#define SPDLITE_LEVEL_TRACE 0 -#define SPDLITE_LEVEL_DEBUG 1 -#define SPDLITE_LEVEL_INFO 2 -#define SPDLITE_LEVEL_WARN 3 -#define SPDLITE_LEVEL_ERROR 4 -#define SPDLITE_LEVEL_CRITICAL 5 -#define SPDLITE_LEVEL_OFF 6 - -#define SPDLITE_LOGGER_CALL(logger, level, ...) logger.log(level, __VA_ARGS__) - -#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_TRACE -#define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::trace, __VA_ARGS__) -#define SPDLITE_TRACE(...) SPDLITE_LOGGER_TRACE(spdlog::lite::default_logger(), __VA_ARGS__) -#else -#define SPDLITE_LOGGER_TRACE(logger, ...) (void)0 -#define SPDLITE_TRACE(...) (void)0 -#endif - -#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_DEBUG -#define SPDLITE_LOGGER_DEBUG(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::debug, __VA_ARGS__) -#define SPDLITE_DEBUG(...) SPDLITE_LOGGER_DEBUG(spdlog::lite::default_logger(), __VA_ARGS__) -#else -#define SPDLITE_LOGGER_DEBUG(logger, ...) (void)0 -#define SPDLITE_DEBUG(...) (void)0 -#endif - -#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_INFO -#define SPDLITE_LOGGER_INFO(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::info, __VA_ARGS__) -#define SPDLITE_INFO(...) SPDLITE_LOGGER_INFO(spdlog::lite::default_logger(), __VA_ARGS__) -#else -#define SPDLITE_LOGGER_INFO(logger, ...) (void)0 -#define SPDLITE_INFO(...) (void)0 -#endif - -#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_WARN -#define SPDLITE_LOGGER_WARN(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::warn, __VA_ARGS__) -#define SPDLITE_WARN(...) SPDLITE_LOGGER_WARN(spdlog::lite::default_logger(), __VA_ARGS__) -#else -#define SPDLITE_LOGGER_WARN(logger, ...) (void)0 -#define SPDLITE_WARN(...) (void)0 -#endif - -#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_ERROR -#define SPDLITE_LOGGER_ERROR(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::err, __VA_ARGS__) -#define SPDLITE_ERROR(...) SPDLITE_LOGGER_ERROR(spdlog::lite::default_logger(), __VA_ARGS__) -#else -#define SPDLITE_LOGGER_ERROR(logger, ...) (void)0 -#define SPDLITE_ERROR(...) (void)0 -#endif - -#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_CRITICAL -#define SPDLITE_LOGGER_CRITICAL(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::critical, __VA_ARGS__) -#define SPDLITE_CRITICAL(...) SPDLITE_LOGGER_CRITICAL(spdlog::lite::default_logger(), __VA_ARGS__) -#else -#define SPDLITE_LOGGER_CRITICAL(logger, ...) (void)0 -#define SPDLITE_CRITICAL(...) (void)0 -#endif - namespace spdlog { class logger; @@ -85,13 +22,13 @@ using string_view_t = fmt::string_view; 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 + trace, + debug, + info, + warn, + err, + critical, + off }; @@ -121,7 +58,6 @@ public: log_formatted_(lvl, formatted_buf); } - // log string view void log(spdlog::lite::level lvl, const string_view_t &sv); void log_printf(spdlog::lite::level lvl, const char* format, va_list args); diff --git a/lite/spdlite_macros.h b/lite/spdlite_macros.h new file mode 100644 index 00000000..8d9eb4f8 --- /dev/null +++ b/lite/spdlite_macros.h @@ -0,0 +1,67 @@ +// +// Created by gabi on 3/24/19. +// + +#pragma once +// +// enable/disable log calls at compile time according to global level. +// +// define SPDLITE_ACTIVE_LEVEL to one of those (before including lite.h): + +#define SPDLITE_LEVEL_TRACE 0 +#define SPDLITE_LEVEL_DEBUG 1 +#define SPDLITE_LEVEL_INFO 2 +#define SPDLITE_LEVEL_WARN 3 +#define SPDLITE_LEVEL_ERROR 4 +#define SPDLITE_LEVEL_CRITICAL 5 +#define SPDLITE_LEVEL_OFF 6 + +#define SPDLITE_LOGGER_CALL(logger, level, ...) logger.log(level, __VA_ARGS__) + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_TRACE +#define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::trace, __VA_ARGS__) +#define SPDLITE_TRACE(...) SPDLITE_LOGGER_TRACE(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_TRACE(logger, ...) (void)0 +#define SPDLITE_TRACE(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_DEBUG +#define SPDLITE_LOGGER_DEBUG(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::debug, __VA_ARGS__) +#define SPDLITE_DEBUG(...) SPDLITE_LOGGER_DEBUG(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_DEBUG(logger, ...) (void)0 +#define SPDLITE_DEBUG(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_INFO +#define SPDLITE_LOGGER_INFO(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::info, __VA_ARGS__) +#define SPDLITE_INFO(...) SPDLITE_LOGGER_INFO(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_INFO(logger, ...) (void)0 +#define SPDLITE_INFO(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_WARN +#define SPDLITE_LOGGER_WARN(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::warn, __VA_ARGS__) +#define SPDLITE_WARN(...) SPDLITE_LOGGER_WARN(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_WARN(logger, ...) (void)0 +#define SPDLITE_WARN(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_ERROR +#define SPDLITE_LOGGER_ERROR(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::err, __VA_ARGS__) +#define SPDLITE_ERROR(...) SPDLITE_LOGGER_ERROR(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_ERROR(logger, ...) (void)0 +#define SPDLITE_ERROR(...) (void)0 +#endif + +#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_CRITICAL +#define SPDLITE_LOGGER_CRITICAL(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::critical, __VA_ARGS__) +#define SPDLITE_CRITICAL(...) SPDLITE_LOGGER_CRITICAL(spdlog::lite::default_logger(), __VA_ARGS__) +#else +#define SPDLITE_LOGGER_CRITICAL(logger, ...) (void)0 +#define SPDLITE_CRITICAL(...) (void)0 +#endif From 934cc892eb159c1b12f536dadb1436b5e9e331a9 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 00:40:27 +0200 Subject: [PATCH 13/24] Fixed examples --- example/example.cpp | 237 +++++++++++++++++++++++++++++++++++++-- lite-example/example.cpp | 1 - 2 files changed, 230 insertions(+), 8 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 8384906d..88b0b415 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -1,12 +1,235 @@ -#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE +// +// Copyright(c) 2015 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// +// +// spdlog usage example +// +// + +#include + +void stdout_logger_example(); +void basic_example(); +void rotating_example(); +void daily_example(); +void async_example(); +void binary_example(); +void trace_example(); +void multi_sink_example(); +void user_defined_example(); +void err_handler_example(); +void syslog_example(); +void clone_example(); + #include "spdlog/spdlog.h" -int main() +int main(int, char *[]) { - using namespace spdlog; - auto l = spdlog::default_logger(); - l->set_level(spdlog::level::trace); + spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH); + spdlog::warn("Easy padding in numbers like {:08d}", 12); + spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); + spdlog::info("Support for floats {:03.2f}", 1.23456); + spdlog::info("Positional args are {1} {0}..", "too", "supported"); + spdlog::info("{:>8} aligned, {:<8} aligned", "right", "left"); - l->trace("hello123123213 "); + // Runtime log levels + spdlog::set_level(spdlog::level::info); // Set global log level to info + spdlog::debug("This message should not be displayed!"); + spdlog::set_level(spdlog::level::trace); // Set specific logger's log level + spdlog::debug("This message should be displayed.."); -} \ No newline at end of file + // Customize msg format for all loggers + spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v"); + spdlog::info("This an info message with custom format"); + spdlog::set_pattern("%+"); // back to default format + + try + { + stdout_logger_example(); + basic_example(); + rotating_example(); + daily_example(); + clone_example(); + async_example(); + binary_example(); + multi_sink_example(); + user_defined_example(); + err_handler_example(); + trace_example(); + + // Flush all *registered* loggers using a worker thread every 3 seconds. + // note: registered loggers *must* be thread safe for this to work correctly! + spdlog::flush_every(std::chrono::seconds(3)); + + // Apply some function on all registered loggers + spdlog::apply_all([&](std::shared_ptr l) { l->info("End of example."); }); + + // Release all spdlog resources, and drop all loggers in the registry. + // This is optional (only mandatory if using windows + async log). + spdlog::shutdown(); + } + + // Exceptions will only be thrown upon failed logger or sink construction (not during logging). + catch (const spdlog::spdlog_ex &ex) + { + std::printf("Log initialization failed: %s\n", ex.what()); + return 1; + } +} + +#include "spdlog/sinks/stdout_color_sinks.h" +// or #include "spdlog/sinks/stdout_sinks.h" if no colors needed. +void stdout_logger_example() +{ + // Create color multi threaded logger. + auto console = spdlog::stdout_color_mt("console"); + // or for stderr: + // auto console = spdlog::stderr_color_mt("error-logger"); +} + +#include "spdlog/sinks/basic_file_sink.h" +void basic_example() +{ + // Create basic file logger (not rotated). + auto my_logger = spdlog::basic_logger_mt("file_logger", "logs/basic-log.txt"); +} + +#include "spdlog/sinks/rotating_file_sink.h" +void rotating_example() +{ + // Create a file rotating logger with 5mb size max and 3 rotated files. + auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); +} + +#include "spdlog/sinks/daily_file_sink.h" +void daily_example() +{ + // Create a daily logger - a new file is created every day on 2:30am. + auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); +} + +// Clone a logger and give it new name. +// Useful for creating component/subsystem loggers from some "root" logger. +void clone_example() +{ + auto network_logger = spdlog::default_logger()->clone("network"); + network_logger->info("Logging network stuff.."); +} + +#include "spdlog/async.h" +void async_example() +{ + // Default thread pool settings can be modified *before* creating the async logger: + // spdlog::init_thread_pool(32768, 1); // queue with max 32k items 1 backing thread. + auto async_file = spdlog::basic_logger_mt("async_file_logger", "logs/async_log.txt"); + // alternatively: + // auto async_file = spdlog::create_async("async_file_logger", "logs/async_log.txt"); + + for (int i = 1; i < 101; ++i) + { + async_file->info("Async message #{}", i); + } +} + +// Log binary data as hex. +// Many types of std::container types can be used. +// Iterator ranges are supported too. +// Format flags: +// {:X} - print in uppercase. +// {:s} - don't separate each byte with space. +// {:p} - don't print the position on each line start. +// {:n} - don't split the output to lines. + +#include "spdlog/fmt/bin_to_hex.h" +void binary_example() +{ + std::vector buf; + for (int i = 0; i < 80; i++) + { + buf.push_back(static_cast(i & 0xff)); + } + spdlog::info("Binary example: {}", spdlog::to_hex(buf)); + spdlog::info("Another binary example:{:n}", spdlog::to_hex(std::begin(buf), std::begin(buf) + 10)); + // more examples: + // logger->info("uppercase: {:X}", spdlog::to_hex(buf)); + // logger->info("uppercase, no delimiters: {:Xs}", spdlog::to_hex(buf)); + // logger->info("uppercase, no delimiters, no position info: {:Xsp}", spdlog::to_hex(buf)); +} + +// Compile time log levels. +// define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE) +void trace_example() +{ + // trace from default logger + SPDLOG_TRACE("Some trace message.. {} ,{}", 1, 3.23); + // debug from default logger + SPDLOG_DEBUG("Some debug message.. {} ,{}", 1, 3.23); + + // trace from logger object + auto logger = spdlog::get("file_logger"); + SPDLOG_LOGGER_TRACE(logger, "another trace message"); +} + +// A logger with multiple sinks (stdout and file) - each with a different format and log level. +void multi_sink_example() +{ + auto console_sink = std::make_shared(); + console_sink->set_level(spdlog::level::warn); + console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v"); + + auto file_sink = std::make_shared("logs/multisink.txt", true); + file_sink->set_level(spdlog::level::trace); + + spdlog::logger logger("multi_sink", {console_sink, file_sink}); + logger.set_level(spdlog::level::debug); + logger.warn("this should appear in both console and file"); + logger.info("this message should not appear in the console, only in the file"); +} + +// User defined types logging by implementing operator<< +#include "spdlog/fmt/ostr.h" // must be included +struct my_type +{ + int i; + template + friend OStream &operator<<(OStream &os, const my_type &c) + { + return os << "[my_type i=" << c.i << "]"; + } +}; + +void user_defined_example() +{ + spdlog::info("user defined type: {}", my_type{14}); +} + +// Custom error handler. Will be triggered on log failure. +void err_handler_example() +{ + // can be set globally or per logger(logger->set_error_handler(..)) + spdlog::set_error_handler([](const std::string &msg) { printf("*** Custom log error handler: %s ***\n", msg.c_str()); }); +} + +// syslog example (linux/osx/freebsd) +#ifndef _WIN32 +#include "spdlog/sinks/syslog_sink.h" +void syslog_example() +{ + std::string ident = "spdlog-example"; + auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID); + syslog_logger->warn("This is warning that will end up in syslog."); +} +#endif + +// Android example. +#if defined(__ANDROID__) +#include "spdlog/sinks/android_sink.h" +void android_example() +{ + std::string tag = "spdlog-android"; + auto android_logger = spdlog::android_logger_mt("android", tag); + android_logger->critical("Use \"adb shell logcat\" to view this message."); +} + +#endif diff --git a/lite-example/example.cpp b/lite-example/example.cpp index f45eeb9e..d0ce4562 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -2,7 +2,6 @@ int main() { - using namespace spdlog; auto l = spdlog::create_lite(); l.set_level(spdlog::lite::level::trace); From bac1e4a850a6e5c83e8217ff1f632b0eb180419f Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 00:41:22 +0200 Subject: [PATCH 14/24] clang-format --- example/example.cpp | 2 +- lite-example/example.cpp | 2 +- lite/spdlite.cpp | 34 +++++++++++++++------------------- lite/spdlite.h | 15 ++++++--------- 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 88b0b415..345f8bd1 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -70,7 +70,7 @@ int main(int, char *[]) spdlog::shutdown(); } - // Exceptions will only be thrown upon failed logger or sink construction (not during logging). + // Exceptions will only be thrown upon failed logger or sink construction (not during logging). catch (const spdlog::spdlog_ex &ex) { std::printf("Log initialization failed: %s\n", ex.what()); diff --git a/lite-example/example.cpp b/lite-example/example.cpp index d0ce4562..e4185131 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -5,7 +5,7 @@ int main() auto l = spdlog::create_lite(); l.set_level(spdlog::lite::level::trace); - l.trace_f("Hello %s ","GABI"); + l.trace_f("Hello %s ", "GABI"); l.info_f("Hello %d", 12346); l.warn_f("Hello %f", 12346.5656); l.warn("Hello {}", 12346.5656); diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 8b7ace31..80c0388b 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -28,65 +28,63 @@ void spdlog::lite::logger::log(spdlog::lite::level lvl, const string_view_t &sv) impl_->log(spd_level, sv); } - -void spdlog::lite::logger::log_printf(spdlog::lite::level lvl, const char* format, va_list args) +void spdlog::lite::logger::log_printf(spdlog::lite::level lvl, const char *format, va_list args) { char buffer[500]; - auto size = vsnprintf (buffer, sizeof(buffer),format, args); - if(size < 0) + auto size = vsnprintf(buffer, sizeof(buffer), format, args); + if (size < 0) { size = snprintf(buffer, sizeof(buffer), "invalid format (%s)", format); } log(lvl, string_view_t{buffer, static_cast(size)}); } - void spdlog::lite::logger::trace_f(const char *format, ...) { va_list args; - va_start (args, format); + va_start(args, format); log_printf(lite::level::trace, format, args); - va_end (args); + va_end(args); } void spdlog::lite::logger::debug_f(const char *format, ...) { va_list args; - va_start (args, format); + va_start(args, format); log_printf(lite::level::debug, format, args); - va_end (args); + va_end(args); } void spdlog::lite::logger::info_f(const char *format, ...) { va_list args; - va_start (args, format); + va_start(args, format); log_printf(lite::level::info, format, args); - va_end (args); + va_end(args); } void spdlog::lite::logger::warn_f(const char *format, ...) { va_list args; - va_start (args, format); + va_start(args, format); log_printf(lite::level::warn, format, args); - va_end (args); + va_end(args); } void spdlog::lite::logger::error_f(const char *format, ...) { va_list args; - va_start (args, format); + va_start(args, format); log_printf(lite::level::err, format, args); - va_end (args); + va_end(args); } void spdlog::lite::logger::critical_f(const char *format, ...) { va_list args; - va_start (args, format); + va_start(args, format); log_printf(lite::level::critical, format, args); - va_end (args); + va_end(args); } void spdlog::lite::logger::set_level(spdlog::lite::level level) @@ -138,5 +136,3 @@ spdlog::lite::logger &spdlog::lite::default_logger() static spdlog::lite::logger s_default(spdlog::default_logger()); return s_default; } - - diff --git a/lite/spdlite.h b/lite/spdlite.h index 39579002..ddfc4b27 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -31,7 +31,6 @@ enum class level off }; - class logger { public: @@ -60,7 +59,7 @@ public: // log string view void log(spdlog::lite::level lvl, const string_view_t &sv); - void log_printf(spdlog::lite::level lvl, const char* format, va_list args); + void log_printf(spdlog::lite::level lvl, const char *format, va_list args); // // trace @@ -76,7 +75,7 @@ public: log(spdlog::lite::level::trace, fmt, args...); } - void trace_f(const char *printf_format, ...); + void trace_f(const char *printf_format, ...); // // debug @@ -92,7 +91,7 @@ public: log(spdlog::lite::level::debug, fmt, args...); } - void debug_f(const char *printf_format, ...); + void debug_f(const char *printf_format, ...); // // info @@ -124,7 +123,7 @@ public: log(spdlog::lite::level::warn, fmt, args...); } - void warn_f(const char *printf_format, ...); + void warn_f(const char *printf_format, ...); // // error @@ -134,14 +133,13 @@ public: log(spdlog::lite::level::err, string_view_t(msg)); } - template void error(const char *fmt, const Args &... args) { log(spdlog::lite::level::err, fmt, args...); } - void error_f(const char *printf_format, ...); + void error_f(const char *printf_format, ...); // // critical @@ -157,7 +155,7 @@ public: log(spdlog::lite::level::critical, fmt, args...); } - void critical_f(const char *printf_format, ...); + void critical_f(const char *printf_format, ...); // // setters/getters @@ -177,7 +175,6 @@ public: protected: std::shared_ptr impl_; void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted); - }; spdlog::lite::logger &default_logger(); From 509a503761e8f108f452cdf04416222071fd579f Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 01:18:05 +0200 Subject: [PATCH 15/24] update example --- lite-example/create_lite.cpp | 23 ++++++++++++-------- lite-example/example.cpp | 2 +- lite/spdlite.h | 42 ++++++++++++++++++++---------------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/lite-example/create_lite.cpp b/lite-example/create_lite.cpp index 1a6d6bfe..74892d64 100644 --- a/lite-example/create_lite.cpp +++ b/lite-example/create_lite.cpp @@ -3,16 +3,21 @@ #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/stdout_color_sinks.h" +#define UNUSED(x) (void)(x) + +// example of creating lite logger with console and file sink spdlog::lite::logger spdlog::create_lite(void *ctx) { - if (ctx) - { - //.. - } - auto logger_impl = spdlog::stdout_color_mt("mylogger"); - auto file_sink = std::make_shared("file.txt", true); - file_sink->set_level(spdlog::level::info); - logger_impl->sinks().push_back(file_sink); - logger_impl->set_level(spdlog::level::trace); + UNUSED(ctx); + std::shared_ptr logger_impl; + + auto console_sink = std::make_shared (); + console_sink->set_level(spdlog::level::debug); + + auto file_sink = std::make_shared ("log.txt", true); + file_sink ->set_level(spdlog::level::info); + + logger_impl = std::make_shared("my-logger", spdlog::sinks_init_list{console_sink, file_sink}); + logger_impl->set_level(spdlog::level::debug); return spdlog::lite::logger(std::move(logger_impl)); } diff --git a/lite-example/example.cpp b/lite-example/example.cpp index e4185131..bffd0a2f 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -2,7 +2,7 @@ int main() { - auto l = spdlog::create_lite(); + auto l = spdlog::create_lite((void*)"async"); l.set_level(spdlog::lite::level::trace); l.trace_f("Hello %s ", "GABI"); diff --git a/lite/spdlite.h b/lite/spdlite.h index ddfc4b27..4429b50d 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -43,10 +43,10 @@ public: ~logger() = default; - bool should_log(spdlog::lite::level lvl) const noexcept; + bool should_log(lite::level lvl) const noexcept; template - void log(spdlog::lite::level lvl, const char *fmt, const Args &... args) + void log(lite::level lvl, const char *fmt, const Args &... args) { if (!should_log(lvl)) { @@ -58,21 +58,21 @@ public: } // log string view - void log(spdlog::lite::level lvl, const string_view_t &sv); - void log_printf(spdlog::lite::level lvl, const char *format, va_list args); + void log(lite::level lvl, const string_view_t &sv); + void log_printf(lite::level lvl, const char *format, va_list args); // // trace // void trace(const char *msg) { - log(spdlog::lite::level::trace, string_view_t(msg)); + log(lite::level::trace, string_view_t(msg)); } template void trace(const char *fmt, const Args &... args) { - log(spdlog::lite::level::trace, fmt, args...); + log(lite::level::trace, fmt, args...); } void trace_f(const char *printf_format, ...); @@ -82,13 +82,13 @@ public: // void debug(const char *msg) { - log(spdlog::lite::level::debug, string_view_t(msg)); + log(lite::level::debug, string_view_t(msg)); } template void debug(const char *fmt, const Args &... args) { - log(spdlog::lite::level::debug, fmt, args...); + log(lite::level::debug, fmt, args...); } void debug_f(const char *printf_format, ...); @@ -98,13 +98,13 @@ public: // void info(const char *msg) { - log(spdlog::lite::level::info, string_view_t(msg)); + log(lite::level::info, string_view_t(msg)); } template void info(const char *fmt, const Args &... args) { - log(spdlog::lite::level::info, fmt, args...); + log(lite::level::info, fmt, args...); } void info_f(const char *printf_format, ...); @@ -114,13 +114,13 @@ public: // void warn(const char *msg) { - log(spdlog::lite::level::warn, string_view_t(msg)); + log(lite::level::warn, string_view_t(msg)); } template void warn(const char *fmt, const Args &... args) { - log(spdlog::lite::level::warn, fmt, args...); + log(lite::level::warn, fmt, args...); } void warn_f(const char *printf_format, ...); @@ -130,13 +130,13 @@ public: // void error(const char *msg) { - log(spdlog::lite::level::err, string_view_t(msg)); + log(lite::level::err, string_view_t(msg)); } template void error(const char *fmt, const Args &... args) { - log(spdlog::lite::level::err, fmt, args...); + log(lite::level::err, fmt, args...); } void error_f(const char *printf_format, ...); @@ -146,13 +146,13 @@ public: // void critical(const char *msg) { - log(spdlog::lite::level::critical, string_view_t(msg)); + log(lite::level::critical, string_view_t(msg)); } template void critical(const char *fmt, const Args &... args) { - log(spdlog::lite::level::critical, fmt, args...); + log(lite::level::critical, fmt, args...); } void critical_f(const char *printf_format, ...); @@ -168,13 +168,17 @@ public: // flush // void flush(); - void flush_on(spdlog::lite::level log_level); - spdlog::lite::level flush_level() const; + void flush_on(lite::level log_level); + lite::level flush_level() const; + + // + // set 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_(lite::level lvl, const fmt::memory_buffer &formatted); }; spdlog::lite::logger &default_logger(); From 722943000e0a23d14175f3e6d08905e3b3dbdecf Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 01:21:07 +0200 Subject: [PATCH 16/24] renamed printf --- lite-example/example.cpp | 6 +++--- lite/spdlite.cpp | 12 ++++++------ lite/spdlite.h | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lite-example/example.cpp b/lite-example/example.cpp index bffd0a2f..1a08bcc7 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -5,8 +5,8 @@ int main() auto l = spdlog::create_lite((void*)"async"); l.set_level(spdlog::lite::level::trace); - l.trace_f("Hello %s ", "GABI"); - l.info_f("Hello %d", 12346); - l.warn_f("Hello %f", 12346.5656); + l.trace_printf("Hello %s ", "GABI"); + l.info_printf("Hello %d", 12346); + l.warn_printf("Hello %f", 12346.5656); l.warn("Hello {}", 12346.5656); } \ No newline at end of file diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 80c0388b..e3af7bb7 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -39,7 +39,7 @@ void spdlog::lite::logger::log_printf(spdlog::lite::level lvl, const char *forma log(lvl, string_view_t{buffer, static_cast(size)}); } -void spdlog::lite::logger::trace_f(const char *format, ...) +void spdlog::lite::logger::trace_printf(const char *format, ...) { va_list args; va_start(args, format); @@ -47,7 +47,7 @@ void spdlog::lite::logger::trace_f(const char *format, ...) va_end(args); } -void spdlog::lite::logger::debug_f(const char *format, ...) +void spdlog::lite::logger::debug_printf(const char *format, ...) { va_list args; va_start(args, format); @@ -55,7 +55,7 @@ void spdlog::lite::logger::debug_f(const char *format, ...) va_end(args); } -void spdlog::lite::logger::info_f(const char *format, ...) +void spdlog::lite::logger::info_printf(const char *format, ...) { va_list args; va_start(args, format); @@ -63,7 +63,7 @@ void spdlog::lite::logger::info_f(const char *format, ...) va_end(args); } -void spdlog::lite::logger::warn_f(const char *format, ...) +void spdlog::lite::logger::warn_printf(const char *format, ...) { va_list args; va_start(args, format); @@ -71,7 +71,7 @@ void spdlog::lite::logger::warn_f(const char *format, ...) va_end(args); } -void spdlog::lite::logger::error_f(const char *format, ...) +void spdlog::lite::logger::error_printf(const char *format, ...) { va_list args; va_start(args, format); @@ -79,7 +79,7 @@ void spdlog::lite::logger::error_f(const char *format, ...) va_end(args); } -void spdlog::lite::logger::critical_f(const char *format, ...) +void spdlog::lite::logger::critical_printf(const char *format, ...) { va_list args; va_start(args, format); diff --git a/lite/spdlite.h b/lite/spdlite.h index 4429b50d..5642df79 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -75,7 +75,7 @@ public: log(lite::level::trace, fmt, args...); } - void trace_f(const char *printf_format, ...); + void trace_printf(const char *format, ...); // // debug @@ -91,7 +91,7 @@ public: log(lite::level::debug, fmt, args...); } - void debug_f(const char *printf_format, ...); + void debug_printf(const char *format, ...); // // info @@ -107,7 +107,7 @@ public: log(lite::level::info, fmt, args...); } - void info_f(const char *printf_format, ...); + void info_printf(const char *format, ...); // // warn @@ -123,7 +123,7 @@ public: log(lite::level::warn, fmt, args...); } - void warn_f(const char *printf_format, ...); + void warn_printf(const char *format, ...); // // error @@ -139,7 +139,7 @@ public: log(lite::level::err, fmt, args...); } - void error_f(const char *printf_format, ...); + void error_printf(const char *format, ...); // // critical @@ -155,7 +155,7 @@ public: log(lite::level::critical, fmt, args...); } - void critical_f(const char *printf_format, ...); + void critical_printf(const char *format, ...); // // setters/getters From 221ce33eb5e37411e67c61494e9728677cc93605 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 01:23:14 +0200 Subject: [PATCH 17/24] renamed printf --- lite-example/example.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 1a08bcc7..b89ddc09 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -2,11 +2,11 @@ int main() { - auto l = spdlog::create_lite((void*)"async"); + auto l = spdlog::create_lite(); l.set_level(spdlog::lite::level::trace); l.trace_printf("Hello %s ", "GABI"); l.info_printf("Hello %d", 12346); l.warn_printf("Hello %f", 12346.5656); - l.warn("Hello {}", 12346.5656); + l.warn("Hello {}", "LITE :) "); } \ No newline at end of file From 240f13a0a68eaff0dfaf7875f28563ed8f65b376 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 01:27:05 +0200 Subject: [PATCH 18/24] Renamed source file --- lite-example/CMakeLists.txt | 2 +- lite-example/{create_lite.cpp => create_logger.cpp} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename lite-example/{create_lite.cpp => create_logger.cpp} (100%) diff --git a/lite-example/CMakeLists.txt b/lite-example/CMakeLists.txt index d152ecd1..5f309a6b 100644 --- a/lite-example/CMakeLists.txt +++ b/lite-example/CMakeLists.txt @@ -2,7 +2,7 @@ project(spdlog-lite-example CXX) find_package(Threads REQUIRED) -set(LITE_SOURCES example.cpp create_lite.cpp) +set(LITE_SOURCES example.cpp create_logger.cpp) add_executable(${PROJECT_NAME} ${LITE_SOURCES}) diff --git a/lite-example/create_lite.cpp b/lite-example/create_logger.cpp similarity index 100% rename from lite-example/create_lite.cpp rename to lite-example/create_logger.cpp From 9e5d1b3ba5a9a632ccfde286f6e2793e4980f873 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 01:34:21 +0200 Subject: [PATCH 19/24] Removed default ctor from lite logger --- lite/spdlite.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/lite/spdlite.h b/lite/spdlite.h index 5642df79..e969678a 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -34,8 +34,6 @@ enum class level class logger { public: - logger() = default; - explicit logger(std::shared_ptr impl); logger(const logger &) = default; logger(logger &&) = default; From 4ccca079a56102060006761c0f59c8f6c7dd9234 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 01:40:20 +0200 Subject: [PATCH 20/24] clone support in lite logger --- lite-example/example.cpp | 4 ++++ lite/spdlite.cpp | 5 +++++ lite/spdlite.h | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/lite-example/example.cpp b/lite-example/example.cpp index b89ddc09..34771028 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -9,4 +9,8 @@ int main() l.info_printf("Hello %d", 12346); l.warn_printf("Hello %f", 12346.5656); l.warn("Hello {}", "LITE :) "); + + auto l2 = l.clone("logger2"); + l2.debug("HELLO"); + } \ No newline at end of file diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index e3af7bb7..71a1aacb 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -125,6 +125,11 @@ void spdlog::lite::logger::set_pattern(std::string pattern) impl_->set_pattern(std::move(pattern)); } +spdlog::lite::logger spdlog::lite::logger::clone(std::string logger_name) +{ + return spdlog::lite::logger(impl_->clone(std::move(logger_name))); +} + void spdlog::lite::logger::log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted) { auto spd_level = to_spdlog_level(lvl); diff --git a/lite/spdlite.h b/lite/spdlite.h index e969678a..36a7b7af 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -174,6 +174,11 @@ public: // void set_pattern(std::string pattern); + // + //clone with new name + // + spdlog::lite::logger clone(std::string logger_name); + protected: std::shared_ptr impl_; void log_formatted_(lite::level lvl, const fmt::memory_buffer &formatted); From 4a00590a1bd4f77adc7faa1396c0fb75b0de138a Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 01:44:40 +0200 Subject: [PATCH 21/24] comment --- lite/spdlite.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lite/spdlite.h b/lite/spdlite.h index 36a7b7af..425a762c 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -4,6 +4,10 @@ #pragma once +// lite logger - a lite wrapper around spdlog::logger shared_ptr pimpl +// main purpose is much faster compile time and very cheap copy and move. +// also supports printf format for even faster compile times (by avoiding variadic templates) + #include #include #include "spdlog/fmt/fmt.h" From bf307e24c5d919131595939796145175890dceae Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 24 Mar 2019 01:50:56 +0200 Subject: [PATCH 22/24] comment --- lite/spdlite.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lite/spdlite.h b/lite/spdlite.h index 425a762c..bb9972ee 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -4,9 +4,13 @@ #pragma once -// lite logger - a lite wrapper around spdlog::logger shared_ptr pimpl -// main purpose is much faster compile time and very cheap copy and move. -// also supports printf format for even faster compile times (by avoiding variadic templates) +// lite logger - a pimpl around spdlog::logger shared_ptr: +// much faster compile times. +// can be used as lib or separate compilation unit. +// very cheap copy and move. +// supports printf format for even faster compile (by avoiding variadic templates). +// +// see lite-example/ for usage. #include #include From 4a07ce5faef5ae32448003468288b323a423c6a7 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 29 Mar 2019 13:56:44 +0300 Subject: [PATCH 23/24] added header to cmake --- lite/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lite/CMakeLists.txt b/lite/CMakeLists.txt index ee8a4aab..f0769a0d 100644 --- a/lite/CMakeLists.txt +++ b/lite/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.1) project(spdlog_lite) -add_library(spdlog_lite spdlite.cpp) +add_library(spdlog_lite spdlite.cpp spdlite.h) target_link_libraries(spdlog_lite spdlog::spdlog) From bb88a74f92f45bdf1c2518972403b443145e77c2 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 29 Mar 2019 14:38:40 +0300 Subject: [PATCH 24/24] wip lite --- lite-example/create_logger.cpp | 4 +-- lite-example/example.cpp | 9 ++++-- lite/spdlite.cpp | 10 +++---- lite/spdlite.h | 50 ++++++++++++---------------------- 4 files changed, 30 insertions(+), 43 deletions(-) diff --git a/lite-example/create_logger.cpp b/lite-example/create_logger.cpp index 74892d64..07608139 100644 --- a/lite-example/create_logger.cpp +++ b/lite-example/create_logger.cpp @@ -6,7 +6,7 @@ #define UNUSED(x) (void)(x) // example of creating lite logger with console and file sink -spdlog::lite::logger spdlog::create_lite(void *ctx) +spdlog::lite::logger spdlog::lite::create_logger(void *ctx) { UNUSED(ctx); std::shared_ptr logger_impl; @@ -17,7 +17,7 @@ spdlog::lite::logger spdlog::create_lite(void *ctx) auto file_sink = std::make_shared ("log.txt", true); file_sink ->set_level(spdlog::level::info); - logger_impl = std::make_shared("my-logger", spdlog::sinks_init_list{console_sink, file_sink}); + logger_impl = std::make_unique("my-logger", spdlog::sinks_init_list{console_sink, file_sink}); logger_impl->set_level(spdlog::level::debug); return spdlog::lite::logger(std::move(logger_impl)); } diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 34771028..3668a400 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -2,15 +2,18 @@ int main() { - auto l = spdlog::create_lite(); + auto l = spdlog::lite::create_logger(); l.set_level(spdlog::lite::level::trace); - l.trace_printf("Hello %s ", "GABI"); + l.trace_printf("Hello %s ", "GABI"); l.info_printf("Hello %d", 12346); l.warn_printf("Hello %f", 12346.5656); l.warn("Hello {}", "LITE :) "); - auto l2 = l.clone("logger2"); + auto l2 = l.clone("logger2"); l2.debug("HELLO"); + auto l3 = std::move(l); + l3.warn("HELLO FROM L3"); + } \ No newline at end of file diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 71a1aacb..f3557543 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -87,18 +87,18 @@ void spdlog::lite::logger::critical_printf(const char *format, ...) va_end(args); } -void spdlog::lite::logger::set_level(spdlog::lite::level level) +void spdlog::lite::logger::set_level(spdlog::lite::level level) noexcept { auto spd_level = to_spdlog_level(level); impl_->set_level(spd_level); } -spdlog::lite::level spdlog::lite::logger::get_level() const +spdlog::lite::level spdlog::lite::logger::level() const noexcept { return to_lite_level(impl_->level()); } -std::string spdlog::lite::logger::name() const +std::string spdlog::lite::logger::name() const noexcept { return impl_->name(); } @@ -114,13 +114,13 @@ void spdlog::lite::logger::flush_on(spdlog::lite::level level) impl_->flush_on(spd_level); } -spdlog::lite::level spdlog::lite::logger::flush_level() const +spdlog::lite::level spdlog::lite::logger::flush_level() const noexcept { return to_lite_level(impl_->flush_level()); } // pattern -void spdlog::lite::logger::set_pattern(std::string pattern) +void spdlog::lite::logger::set_pattern(std::string pattern) noexcept { impl_->set_pattern(std::move(pattern)); } diff --git a/lite/spdlite.h b/lite/spdlite.h index bb9972ee..636dbdab 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -42,7 +42,7 @@ enum class level class logger { public: - explicit logger(std::shared_ptr impl); + explicit logger(std::shared_ptr impl); logger(const logger &) = default; logger(logger &&) = default; logger &operator=(const logger &) = default; @@ -65,6 +65,8 @@ public: // log string view void log(lite::level lvl, const string_view_t &sv); + + // log using printf format void log_printf(lite::level lvl, const char *format, va_list args); // @@ -98,10 +100,8 @@ public: } void debug_printf(const char *format, ...); - - // - // info - // + + // info void info(const char *msg) { log(lite::level::info, string_view_t(msg)); @@ -115,9 +115,7 @@ public: void info_printf(const char *format, ...); - // // warn - // void warn(const char *msg) { log(lite::level::warn, string_view_t(msg)); @@ -131,9 +129,7 @@ public: void warn_printf(const char *format, ...); - // // error - // void error(const char *msg) { log(lite::level::err, string_view_t(msg)); @@ -147,9 +143,7 @@ public: void error_printf(const char *format, ...); - // // critical - // void critical(const char *msg) { log(lite::level::critical, string_view_t(msg)); @@ -163,28 +157,18 @@ public: void critical_printf(const char *format, ...); - // - // setters/getters - // - std::string name() const; - void set_level(lite::level level); - lite::level get_level() const; + // setters/getters + void set_level(lite::level level) noexcept; + void set_pattern(std::string pattern) noexcept; + lite::level level() const noexcept; + std::string name() const noexcept; + lite::level flush_level() const noexcept; - // // flush - // void flush(); void flush_on(lite::level log_level); - lite::level flush_level() const; - - // - // set pattern - // - void set_pattern(std::string pattern); - - // + //clone with new name - // spdlog::lite::logger clone(std::string logger_name); protected: @@ -230,10 +214,10 @@ void critical(const char *fmt, const Args &... args) default_logger().critical(fmt, args...); } +// user implemented factory to create lite logger +// implement it in a seperated and dedicated compilation unit for fast compiles. +logger create_logger(void *ctx = nullptr); + } // namespace lite +} // namespace spdlog -// 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