This commit is contained in:
gabime 2019-03-29 14:38:40 +03:00
parent 99a5484dfb
commit c7535a91a6
4 changed files with 30 additions and 43 deletions

View File

@ -6,7 +6,7 @@
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
// example of creating lite logger with console and file sink // 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); UNUSED(ctx);
std::shared_ptr<spdlog::logger> logger_impl; std::shared_ptr<spdlog::logger> logger_impl;
@ -17,7 +17,7 @@ spdlog::lite::logger spdlog::create_lite(void *ctx)
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt > ("log.txt", true); auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt > ("log.txt", true);
file_sink ->set_level(spdlog::level::info); file_sink ->set_level(spdlog::level::info);
logger_impl = std::make_shared<spdlog::logger>("my-logger", spdlog::sinks_init_list{console_sink, file_sink}); logger_impl = std::make_unique<spdlog::logger>("my-logger", spdlog::sinks_init_list{console_sink, file_sink});
logger_impl->set_level(spdlog::level::debug); logger_impl->set_level(spdlog::level::debug);
return spdlog::lite::logger(std::move(logger_impl)); return spdlog::lite::logger(std::move(logger_impl));
} }

View File

@ -2,15 +2,18 @@
int main() int main()
{ {
auto l = spdlog::create_lite(); auto l = spdlog::lite::create_logger();
l.set_level(spdlog::lite::level::trace); 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.info_printf("Hello %d", 12346);
l.warn_printf("Hello %f", 12346.5656); l.warn_printf("Hello %f", 12346.5656);
l.warn("Hello {}", "LITE :) "); l.warn("Hello {}", "LITE :) ");
auto l2 = l.clone("logger2"); auto l2 = l.clone("logger2");
l2.debug("HELLO"); l2.debug("HELLO");
auto l3 = std::move(l);
l3.warn("HELLO FROM L3");
} }

View File

@ -87,18 +87,18 @@ void spdlog::lite::logger::critical_printf(const char *format, ...)
va_end(args); 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); auto spd_level = to_spdlog_level(level);
impl_->set_level(spd_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()); return to_lite_level(impl_->level());
} }
std::string spdlog::lite::logger::name() const std::string spdlog::lite::logger::name() const noexcept
{ {
return impl_->name(); return impl_->name();
} }
@ -114,13 +114,13 @@ void spdlog::lite::logger::flush_on(spdlog::lite::level level)
impl_->flush_on(spd_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()); return to_lite_level(impl_->flush_level());
} }
// pattern // 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)); impl_->set_pattern(std::move(pattern));
} }

View File

@ -42,7 +42,7 @@ enum class level
class logger class logger
{ {
public: public:
explicit logger(std::shared_ptr<spdlog::logger> impl); explicit logger(std::shared_ptr<spdlog::logger> impl);
logger(const logger &) = default; logger(const logger &) = default;
logger(logger &&) = default; logger(logger &&) = default;
logger &operator=(const logger &) = default; logger &operator=(const logger &) = default;
@ -65,6 +65,8 @@ public:
// log string view // log string view
void log(lite::level lvl, const string_view_t &sv); 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); void log_printf(lite::level lvl, const char *format, va_list args);
// //
@ -98,10 +100,8 @@ public:
} }
void debug_printf(const char *format, ...); void debug_printf(const char *format, ...);
// // info
// info
//
void info(const char *msg) void info(const char *msg)
{ {
log(lite::level::info, string_view_t(msg)); log(lite::level::info, string_view_t(msg));
@ -115,9 +115,7 @@ public:
void info_printf(const char *format, ...); void info_printf(const char *format, ...);
//
// warn // warn
//
void warn(const char *msg) void warn(const char *msg)
{ {
log(lite::level::warn, string_view_t(msg)); log(lite::level::warn, string_view_t(msg));
@ -131,9 +129,7 @@ public:
void warn_printf(const char *format, ...); void warn_printf(const char *format, ...);
//
// error // error
//
void error(const char *msg) void error(const char *msg)
{ {
log(lite::level::err, string_view_t(msg)); log(lite::level::err, string_view_t(msg));
@ -147,9 +143,7 @@ public:
void error_printf(const char *format, ...); void error_printf(const char *format, ...);
//
// critical // critical
//
void critical(const char *msg) void critical(const char *msg)
{ {
log(lite::level::critical, string_view_t(msg)); log(lite::level::critical, string_view_t(msg));
@ -163,28 +157,18 @@ public:
void critical_printf(const char *format, ...); void critical_printf(const char *format, ...);
// // setters/getters
// setters/getters void set_level(lite::level level) noexcept;
// void set_pattern(std::string pattern) noexcept;
std::string name() const; lite::level level() const noexcept;
void set_level(lite::level level); std::string name() const noexcept;
lite::level get_level() const; lite::level flush_level() const noexcept;
//
// flush // flush
//
void flush(); void flush();
void flush_on(lite::level log_level); void flush_on(lite::level log_level);
lite::level flush_level() const;
//
// set pattern
//
void set_pattern(std::string pattern);
//
//clone with new name //clone with new name
//
spdlog::lite::logger clone(std::string logger_name); spdlog::lite::logger clone(std::string logger_name);
protected: protected:
@ -230,10 +214,10 @@ void critical(const char *fmt, const Args &... args)
default_logger().critical(fmt, 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 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