This commit is contained in:
gabime 2019-03-23 23:13:38 +02:00
parent 569c62e528
commit 4449bf6f83
4 changed files with 39 additions and 48 deletions

View File

@ -3,8 +3,6 @@
#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/sinks/stdout_color_sinks.h"
spdlog::lite::logger spdlog::create_lite(void *ctx) spdlog::lite::logger spdlog::create_lite(void *ctx)
{ {
if (ctx) if (ctx)
@ -13,7 +11,8 @@ spdlog::lite::logger spdlog::create_lite(void *ctx)
} }
auto logger_impl = spdlog::stdout_color_mt("mylogger"); auto logger_impl = spdlog::stdout_color_mt("mylogger");
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("file.txt", true); auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("file.txt", true);
file_sink->set_level(spdlog::level::info);
logger_impl->sinks().push_back(file_sink); 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)); return spdlog::lite::logger(std::move(logger_impl));
} }

View File

@ -1,10 +1,11 @@
#define SPDLITE_ACTIVE_LEVEL SPDLITE_LEVEL_TRACE
#include "spdlite.h" #include "spdlite.h"
int main() int main()
{ {
//auto l = spdlog::create_lite(); using namespace spdlog;
//l.info("Hello spdlog {}", "lite"); auto l = spdlog::create_lite();
SPDLITE_TRACE("HELLO TRACE"); l.set_level(spdlog::lite::level::trace);
lite::default_logger().set_level(l.get_level());
lite::trace("hello");
lite::trace("hello {}", std::string("again"));
} }

View File

@ -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? 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_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted)
{ {
auto spd_level = to_spdlog_level(lvl); 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); log_string_view_(spdlog::lite::src_loc{}, lvl, sv);
} }
void spdlog::lite::logger::set_level(spdlog::lite::level level) void spdlog::lite::logger::set_level(spdlog::lite::level level)
{ {
auto spd_level = to_spdlog_level(level); auto spd_level = to_spdlog_level(level);

View File

@ -21,13 +21,10 @@
#define SPDLITE_LEVEL_CRITICAL 5 #define SPDLITE_LEVEL_CRITICAL 5
#define SPDLITE_LEVEL_OFF 6 #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 #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_CALL2(logger, spdlog::lite::level::trace, __VA_ARGS__)
@ -77,10 +74,6 @@
#define SPDLITE_CRITICAL(...) (void)0 #define SPDLITE_CRITICAL(...) (void)0
#endif #endif
namespace spdlog { namespace spdlog {
class logger; class logger;
@ -88,9 +81,9 @@ namespace lite {
// string_view type - either std::string_view or fmt::string_view (pre c++17) // string_view type - either std::string_view or fmt::string_view (pre c++17)
#if defined(FMT_USE_STD_STRING_VIEW) #if defined(FMT_USE_STD_STRING_VIEW)
using string_view_t = std::string_view; using string_view_t = std::string_view;
#else #else
using string_view_t = fmt::string_view; using string_view_t = fmt::string_view;
#endif #endif
enum class level enum class level
@ -107,8 +100,12 @@ enum class level
struct src_loc struct src_loc
{ {
src_loc() = default; src_loc() = default;
src_loc(const char *filename, int line, const char *funcname): src_loc(const char *filename, int line, const char *funcname)
filename(filename), line(line), funcname(funcname){} : filename(filename)
, line(line)
, funcname(funcname)
{
}
const char *filename = nullptr; const char *filename = nullptr;
int line = 0; int line = 0;
@ -147,6 +144,11 @@ public:
log(spdlog::lite::src_loc{}, lvl, fmt, args...); log(spdlog::lite::src_loc{}, lvl, fmt, args...);
} }
// trace
void trace(const char *msg)
{
log_string_view_(spdlog::lite::level::trace, msg);
}
template<typename... Args> template<typename... Args>
void trace(const char *fmt, const Args &... args) void trace(const char *fmt, const Args &... args)
@ -154,21 +156,22 @@ public:
log(spdlog::lite::level::trace, fmt, args...); 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<typename... Args> template<typename... Args>
void debug(const char *fmt, const Args &... args) void debug(const char *fmt, const Args &... args)
{ {
log(spdlog::lite::level::debug, fmt, 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<typename... Args> template<typename... Args>
@ -177,21 +180,22 @@ public:
log(spdlog::lite::level::info, fmt, args...); 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<typename... Args> template<typename... Args>
void warn(const char *fmt, const Args &... args) void warn(const char *fmt, const Args &... args)
{ {
log(spdlog::lite::level::warn, fmt, 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<typename... Args> template<typename... Args>
@ -200,9 +204,10 @@ public:
log(spdlog::lite::level::err, fmt, args...); 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<typename... Args> template<typename... Args>
@ -210,24 +215,13 @@ public:
{ {
log(spdlog::lite::level::critical, fmt, 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; std::string name() const;
// level
void set_level(lite::level level); void set_level(lite::level level);
lite::level get_level() const; lite::level get_level() const;
// flush
void flush(); void flush();
void flush_on(spdlog::lite::level log_level); void flush_on(spdlog::lite::level log_level);
spdlog::lite::level flush_level() const; spdlog::lite::level flush_level() const;
// pattern
void set_pattern(std::string pattern); void set_pattern(std::string pattern);
protected: 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_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_(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_string_view_(spdlog::lite::level lvl, const string_view_t &sv);
}; };
spdlog::lite::logger &default_logger(); spdlog::lite::logger &default_logger();