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

View File

@ -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<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);
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);
return spdlog::lite::logger(std::move(logger_impl));
}

View File

@ -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");
}

View File

@ -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));
}

View File

@ -42,7 +42,7 @@ enum class level
class logger
{
public:
explicit logger(std::shared_ptr<spdlog::logger> impl);
explicit logger(std::shared_ptr<spdlog::logger> 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