15#include <source_location>
30 enum class Level : uint8_t {
46 void AppLog(std::unique_ptr<Log> log);
63 std::string_view filename;
65 std::string_view function;
70 Entry(
Log& logger, Level lvl, SourceInfo src);
72 void operator()(std::string_view msg)
const;
74 template<
class... Args>
75 void operator()(std::format_string<Args...> fmt, Args&&... args)
const {
76 nl(fmt, std::forward<Args>(args)...);
79 void nl(std::string_view msg)
const;
81 template<
class... Args>
82 void nl(std::format_string<Args...> fmt, Args&&... args)
const
83 requires(
sizeof...(Args) > 0)
85 nlf(fmt, std::forward<Args>(args)...);
88 template<
class... Args>
89 void nlf(std::format_string<Args...> fmt, Args&&... args)
const {
90 if (!_logger->enabled(_level)) {
93 _logger->log(_level, _source, std::format(fmt, std::forward<Args>(args)...));
96 void raw(std::string_view msg)
const;
98 template<
class... Args>
99 void raw(std::format_string<Args...> fmt, Args&&... args)
const
100 requires(
sizeof...(Args) > 0)
102 rawf(fmt, std::forward<Args>(args)...);
105 template<
class... Args>
106 void rawf(std::format_string<Args...> fmt, Args&&... args)
const {
107 if (!_logger->enabled(_level)) {
110 _logger->write(_level, _source, std::format(fmt, std::forward<Args>(args)...));
130 std::unique_ptr<LogSink> sink,
131 Level
level = DEFAULT_LEVEL,
136 std::vector<std::unique_ptr<LogSink>>
sinks,
137 Level
level = DEFAULT_LEVEL,
140 Log(
const Log& other) =
delete;
141 Log& operator=(
const Log& other) =
delete;
144 Log& operator=(
Log&&) =
delete;
151 const std::string&
name()
const;
154 const std::vector<std::unique_ptr<LogSink>>&
sinks()
const;
175 void info(
const std::string& msg);
178 void warn(
const std::string& msg);
191 void log(Level
level,
const SourceInfo& sourceInfo,
const std::string& msg);
193 Entry
trace(std::source_location where = std::source_location::current());
194 Entry
debug(std::source_location where = std::source_location::current());
195 Entry
info(std::source_location where = std::source_location::current());
196 Entry
warn(std::source_location where = std::source_location::current());
197 Entry
error(std::source_location where = std::source_location::current());
198 Entry
fatal(std::source_location where = std::source_location::current());
200 void write(Level
level,
const SourceInfo& sourceInfo, std::string_view msg);
201 void write(Level
level, std::string_view msg);
206 static std::string_view Basename(std::string_view p);
207 static std::string_view ShortFunction(std::string_view s,
int keepScopes = 2);
208 static SourceInfo MakeSourceInfo(
const std::source_location& where);
216 void log(Level
level,
const std::string& msg);
217 void dispatch(Level
level, std::string& output);
219 bool enabled(Level lvl)
const;
223 static constexpr Level DEFAULT_LEVEL = Level::Debug;
224 static constexpr Level DEFAULT_FLUSH_LEVEL = Level::Warn;
229 std::vector<std::unique_ptr<LogSink>> _sinks;
252 inline Log::Entry t(std::source_location where = std::source_location::current()) {
253 return MainLog().
trace(where);
257 inline Log::Entry d(std::source_location where = std::source_location::current()) {
258 return MainLog().
debug(where);
262 inline Log::Entry i(std::source_location where = std::source_location::current()) {
263 return MainLog().
info(where);
267 inline Log::Entry w(std::source_location where = std::source_location::current()) {
268 return MainLog().
warn(where);
272 inline Log::Entry e(std::source_location where = std::source_location::current()) {
273 return MainLog().
error(where);
277 inline Log::Entry f(std::source_location where = std::source_location::current()) {
278 return MainLog().
fatal(where);
302 inline Log::Entry t(std::source_location where = std::source_location::current()) {
303 return AppLog().
trace(where);
307 inline Log::Entry d(std::source_location where = std::source_location::current()) {
308 return AppLog().
debug(where);
312 inline Log::Entry i(std::source_location where = std::source_location::current()) {
313 return AppLog().
info(where);
317 inline Log::Entry w(std::source_location where = std::source_location::current()) {
318 return AppLog().
warn(where);
322 inline Log::Entry e(std::source_location where = std::source_location::current()) {
323 return AppLog().
error(where);
327 inline Log::Entry f(std::source_location where = std::source_location::current()) {
328 return AppLog().
fatal(where);
Dispatches severity-filtered log messages to one or more LogSink objects.
Log(const std::string &name, std::unique_ptr< LogSink > sink, Level level=DEFAULT_LEVEL, Level flushLevel=DEFAULT_FLUSH_LEVEL)
Creates a named Log that takes ownership of sink.
void fatal(const std::string &msg)
Emits msg at Fatal severity when enabled.
Level level() const
Returns the minimum severity currently emitted by the Log.
Log()
Creates an unnamed Log with default thresholds and no sinks.
void debug(const std::string &msg)
Emits msg at Debug severity when enabled.
void level(Level level)
Sets the minimum emitted severity; Level::Off disables message emission.
void error(const std::string &msg)
Emits msg at Error severity when enabled.
const std::vector< std::unique_ptr< LogSink > > & sinks() const
Returns the sinks owned by the Log.
Log(const std::string &name, std::vector< std::unique_ptr< LogSink > > sinks, Level level=DEFAULT_LEVEL, Level flushLevel=DEFAULT_FLUSH_LEVEL)
Creates a named Log that takes ownership of sinks.
void trace(const std::string &msg)
Emits msg at Trace severity when enabled.
void flushLevel(Level flushLevel)
Sets the severity threshold that triggers an automatic sink flush.
const std::string & name() const
Returns the name included in formatted log output.
void flush()
Flushes every owned sink.
Level flushLevel() const
Returns the severity threshold that triggers an automatic sink flush.
void warn(const std::string &msg)
Emits msg at Warn severity when enabled.
void info(const std::string &msg)
Emits msg at Info severity when enabled.