24#include <fmt/format.h>
68 void report(
const LogLevel l, std::string_view caller, std::string_view msg)
const {
69 m_action(l, caller, msg);
74 return [](
const LogLevel l, std::string_view caller, std::string_view msg) {
76 std::lock_guard<std::mutex> lock(m);
77 fmt::print(
"{} [{}] {}\n",
logLevelName(l), caller, msg);
82 "Default log level for the LogSvc"};
93 : m_mylevel{l}, m_caller{caller}, m_logger{
LogSvc::instance()} {}
96 m_logger.report(m_mylevel, m_caller, this->str());
105 const std::string m_caller;
116 : m_caller{caller}, m_level{level}, m_threshold{
threshold} {}
120 if (m_impl && !m_impl->buffer.str().empty()) {
126 if (m_level >= m_threshold) {
127 std::lock_guard<std::mutex> lock{m_mutex};
128 os() << std::forward<Arg>(streamable);
138 if (m_level >= m_threshold) {
139 std::lock_guard<std::mutex> lock{m_mutex};
146 if (m_level >= m_threshold) {
147 std::lock_guard<std::mutex> lock{m_mutex};
158 Impl(
const LogLevel level, std::string_view caller) : buffer{level, caller}, os{&buffer} {}
164 m_impl = std::make_unique<Impl>(m_level, m_caller);
170 std::string_view m_caller;
171 std::unique_ptr<Impl> m_impl;
181 : m_caller{caller}, m_level{threshold}, m_logger{
LogSvc::instance()} {}
202 void critical(std::string_view msg)
const { report<LogLevel::kCritical>(msg); }
203 void error(std::string_view msg)
const { report<LogLevel::kError>(msg); }
204 void warning(std::string_view msg)
const { report<LogLevel::kWarning>(msg); }
205 void info(std::string_view msg)
const { report<LogLevel::kInfo>(msg); }
206 void debug(std::string_view msg)
const { report<LogLevel::kDebug>(msg); }
207 void trace(std::string_view msg)
const { report<LogLevel::kTrace>(msg); }
209 template <
typename ...T>
constexpr void critical(fmt::format_string<T...> fmt, T&&... args)
const {
212 template <
typename ...T>
constexpr void error(fmt::format_string<T...> fmt, T&&... args)
const {
215 template <
typename ...T>
constexpr void warning(fmt::format_string<T...> fmt, T&&... args)
const {
218 template <
typename ...T>
constexpr void info(fmt::format_string<T...> fmt, T&&... args)
const {
221 template <
typename ...T>
constexpr void debug(fmt::format_string<T...> fmt, T&&... args)
const {
224 template <
typename ...T>
constexpr void trace(fmt::format_string<T...> fmt, T&&... args)
const {
238 template <
class ErrorType = Error>
void raise(std::string_view msg)
const {
240 throw ErrorType(msg);
244 static std::ostream&
endmsg(std::ostream& os) {
249 template <
LogLevel l,
typename ...T>
250 constexpr void report_fmt(fmt::format_string<T...> fmt, T&&... args)
const {
252 m_logger.report(l, m_caller, fmt::format(fmt, std::forward<
decltype(args)>(args)...));
257 template <LogLevel l>
void report(std::string_view msg)
const {
259 m_logger.
report(l, m_caller, msg);
262 detail::LoggerStream stream(
const LogLevel l)
const {
return {m_caller, l, m_level}; }
264 const std::string m_caller;
267 const LogSvc& m_logger;
278#define ALGORITHMS_DEFINE_LOGGED_SERVICE(className) \
280 className() : LoggedService<className>(#className) {} \
283 friend class Service<className>; \
284 className(const className&) = delete; \
285 void operator=(const className&) = delete; \
286 constexpr static const char* kName = #className;
void report(const LogLevel l, std::string_view caller, std::string_view msg) const
LogLevel defaultLevel() const
void defaultLevel(const LogLevel l)
std::function< void(LogLevel, std::string_view, std::string_view)> LogAction
LoggedService(std::string_view name)
bool aboveDebugThreshold() const
bool aboveWarningThreshold() const
void debug(std::string_view msg) const
void critical(std::string_view msg) const
LoggerMixin(std::string_view caller, const LogLevel threshold=LogSvc::instance().defaultLevel())
detail::LoggerStream error() const
static std::ostream & endmsg(std::ostream &os)
bool aboveInfoThreshold() const
detail::LoggerStream critical() const
void warning(std::string_view msg) const
detail::LoggerStream warning() const
constexpr void report_fmt(fmt::format_string< T... > fmt, T &&... args) const
detail::LoggerStream info() const
LoggerMixin & operator=(const LoggerMixin &)=delete
detail::LoggerStream debug() const
constexpr void debug(fmt::format_string< T... > fmt, T &&... args) const
constexpr void info(fmt::format_string< T... > fmt, T &&... args) const
constexpr void warning(fmt::format_string< T... > fmt, T &&... args) const
void level(const LogLevel threshold)
void trace(std::string_view msg) const
void error(std::string_view msg) const
void raise(std::string_view msg) const
detail::LoggerStream trace() const
LoggerMixin(const LoggerMixin &)=delete
constexpr void critical(fmt::format_string< T... > fmt, T &&... args) const
constexpr void error(fmt::format_string< T... > fmt, T &&... args) const
bool aboveTraceThreshold() const
constexpr void trace(fmt::format_string< T... > fmt, T &&... args) const
bool aboveCriticalThreshold() const
void info(std::string_view msg) const
bool aboveErrorThreshold() const
std::string_view name() const
Service(std::string_view name)
static LogSvc & instance()
LoggerBuffer(const LogLevel l, std::string_view caller)
LoggerStream(std::string_view caller, const LogLevel level, const LogLevel threshold=LogSvc::instance().defaultLevel())
LogLevel threshold() const
std::ios_base &(std::ios_base &) IOManipType2
LoggerStream(const LoggerStream &)=delete
LoggerStream & operator<<(Arg &&streamable)
LoggerStream & operator<<(IOManipType1 *f)
LoggerStream & operator<<(IOManipType2 *f)
std::ostream &(std::ostream &) IOManipType1
void threshold(const LogLevel th)
typename upcast_type< std::remove_cvref_t< T > >::type upcast_type_t
constexpr std::string_view logLevelName(LogLevel level)
#define ALGORITHMS_DEFINE_SERVICE(className)