From 4449bf6f8366c94d0b05c4df27e3d98b72c2c07d Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 23 Mar 2019 23:13:38 +0200 Subject: [PATCH] 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();