spdlog/include/spdlog/logger-inl.h

182 lines
4.1 KiB
C
Raw Normal View History

2019-06-03 17:09:16 -04:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2019-05-11 13:06:17 -04:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
2019-05-12 17:09:00 -04:00
#ifndef SPDLOG_HEADER_ONLY
#include "spdlog/logger.h"
#endif
2019-04-05 09:44:17 -04:00
#include "spdlog/sinks/sink.h"
#include "spdlog/details/pattern_formatter.h"
namespace spdlog {
2019-04-05 09:44:17 -04:00
// public methods
SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, const char *msg)
2019-04-05 09:44:17 -04:00
{
if (!should_log(lvl))
{
return;
}
try
{
details::log_msg log_msg(loc, string_view_t(name_), lvl, string_view_t(msg));
2019-04-05 09:44:17 -04:00
sink_it_(log_msg);
}
catch (const std::exception &ex)
{
err_handler_(ex.what());
}
catch (...)
{
err_handler_("Unknown exception in logger");
}
}
SPDLOG_INLINE void logger::log(level::level_enum lvl, const char *msg)
2019-04-05 09:44:17 -04:00
{
log(source_loc{}, lvl, msg);
}
SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const
2019-04-05 09:44:17 -04:00
{
return msg_level >= level_.load(std::memory_order_relaxed);
}
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
2019-04-05 09:44:17 -04:00
{
level_.store(log_level);
}
SPDLOG_INLINE level::level_enum logger::default_level()
2019-04-05 09:44:17 -04:00
{
return static_cast<level::level_enum>(SPDLOG_ACTIVE_LEVEL);
2019-04-05 09:44:17 -04:00
}
SPDLOG_INLINE level::level_enum logger::level() const
2019-04-05 09:44:17 -04:00
{
return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
2019-04-05 09:44:17 -04:00
}
SPDLOG_INLINE const std::string &logger::name() const
2019-04-05 09:44:17 -04:00
{
return name_;
}
// set formatting for the sinks in this logger.
// each sink will get a seperate instance of the formatter object.
SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
2019-04-05 09:44:17 -04:00
{
for (auto &sink : sinks_)
{
sink->set_formatter(f->clone());
}
}
SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
2019-04-05 09:44:17 -04:00
{
auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
2019-04-05 09:44:17 -04:00
set_formatter(std::move(new_formatter));
}
// flush functions
SPDLOG_INLINE void logger::flush()
2019-04-05 09:44:17 -04:00
{
try
{
flush_();
}
catch (const std::exception &ex)
{
err_handler_(ex.what());
}
catch (...)
{
err_handler_("Unknown exception in logger");
}
}
SPDLOG_INLINE void logger::flush_on(level::level_enum log_level)
2019-04-05 09:44:17 -04:00
{
flush_level_.store(log_level);
}
SPDLOG_INLINE level::level_enum logger::flush_level() const
2019-04-05 09:44:17 -04:00
{
return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
2019-04-05 09:44:17 -04:00
}
// sinks
SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const
2019-04-05 09:44:17 -04:00
{
return sinks_;
}
SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
2019-04-05 09:44:17 -04:00
{
return sinks_;
}
// error handler
SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
2019-04-05 09:44:17 -04:00
{
2019-04-05 16:05:46 -04:00
custom_err_handler_ = handler;
2019-04-05 09:44:17 -04:00
}
// create new logger with same sinks and configuration.
SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
2019-04-05 09:44:17 -04:00
{
auto cloned = std::make_shared<logger>(std::move(logger_name), sinks_.begin(), sinks_.end());
2019-04-05 09:44:17 -04:00
cloned->set_level(this->level());
cloned->flush_on(this->flush_level());
2019-04-05 16:05:46 -04:00
cloned->set_error_handler(this->custom_err_handler_);
2019-04-05 09:44:17 -04:00
return cloned;
}
// protected methods
SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg)
2019-04-05 09:44:17 -04:00
{
for (auto &sink : sinks_)
{
if (sink->should_log(msg.level))
{
sink->log(msg);
}
}
if (should_flush_(msg))
{
flush_();
}
}
SPDLOG_INLINE void logger::flush_()
2019-04-05 09:44:17 -04:00
{
for (auto &sink : sinks_)
{
sink->flush();
}
}
SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
2019-04-05 09:44:17 -04:00
{
auto flush_level = flush_level_.load(std::memory_order_relaxed);
return (msg.level >= flush_level) && (msg.level != level::off);
}
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
2019-04-26 19:34:50 -04:00
{
if (custom_err_handler_)
{
custom_err_handler_(msg);
}
2019-04-05 16:05:46 -04:00
else
2019-04-05 09:44:17 -04:00
{
auto tm_time = details::os::localtime();
2019-04-05 16:05:46 -04:00
char date_buf[64];
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
2019-04-05 09:44:17 -04:00
}
}
} // namespace spdlog