33#include <source_location>
37#include <unordered_map>
41#include "iceberg/iceberg_export.h"
43#include "iceberg/result.h"
67 std::source_location location = std::source_location::current();
68 std::vector<LogAttribute> attributes;
90 std::source_location location = std::source_location::current())
91 : level_(level), location_(location) {}
95 message_ = std::move(message);
100 Builder&
Attribute(std::string key, std::string value) {
101 attributes_.push_back(
LogAttribute{.key = std::move(key), .value = std::move(value)});
107 location_ = location;
114 .message = std::move(message_),
115 .location = location_,
116 .attributes = std::move(attributes_)};
121 std::string message_;
123 std::source_location location_;
124 std::vector<LogAttribute> attributes_;
133inline constexpr std::string_view kPatternProperty =
"pattern";
156 const std::unordered_map<std::string, std::string>& properties) {
157 if (
auto it = properties.find(std::string(
kLevelProperty)); it != properties.end()) {
159 if (!parsed)
return std::unexpected(parsed.error());
178 virtual
void Flush() noexcept {}
181 virtual bool IsNoop()
const {
return false; }
184 static std::shared_ptr<Logger> Noop();
191ICEBERG_EXPORT std::shared_ptr<Logger> GetDefaultLogger();
199ICEBERG_EXPORT std::shared_ptr<Logger> GetCurrentLogger();
205ICEBERG_EXPORT
void SetDefaultLogger(std::shared_ptr<Logger> logger);
212ICEBERG_EXPORT
void SetDefaultLevel(LogLevel level);
237 explicit ScopedLogger(std::shared_ptr<Logger> logger)
noexcept;
246 std::shared_ptr<Logger> previous_;
284ICEBERG_EXPORT
const std::shared_ptr<Logger>& CurrentLogger() noexcept;
291 const
std::source_location& location,
std::
string&& message);
298ICEBERG_EXPORT
void EmitFormatError(
Logger& logger,
LogLevel level,
299 const
std::source_location& location) noexcept;
307template <typename... Args>
308std::
string VFormat(
std::string_view fmt, Args&&... args) {
309 auto store = std::make_format_args(args...);
310 return std::vformat(fmt, store);
319template <
typename... Args>
321 std::format_string<Args...> fmt;
322 std::source_location loc;
324 template <
typename T>
325 requires std::convertible_to<
const T&, std::format_string<Args...>>
326 consteval FmtWithLoc(
328 const T& s, std::source_location loc = std::source_location::current())
329 : fmt(s), loc(loc) {}
336template <
typename... Args>
337void FormatAndEmit(Logger& logger, LogLevel level,
const std::source_location& loc,
338 std::format_string<Args...> fmt, Args&&... args)
noexcept {
339 if (!logger.ShouldLog(level))
return;
341 Emit(logger, level, loc, std::format(fmt, std::forward<Args>(args)...));
343 EmitFormatError(logger, level, loc);
353template <
typename... Args>
354void Log(
LogLevel level, internal::FmtWithLoc<std::type_identity_t<Args>...> fmt,
355 Args&&... args)
noexcept {
356 const std::shared_ptr<Logger>& logger = internal::CurrentLogger();
358 internal::FormatAndEmit(*logger, level, fmt.loc, fmt.fmt,
359 std::forward<Args>(args)...);
366template <
typename... Args>
368 internal::FmtWithLoc<std::type_identity_t<Args>...> fmt,
369 Args&&... args)
noexcept {
370 internal::FormatAndEmit(logger, level, fmt.loc, fmt.fmt, std::forward<Args>(args)...);
Pluggable logging sink.
Definition logger.h:145
virtual void Log(LogMessage &&message) noexcept=0
Emit one (already-formatted) record, taking ownership. Must not throw.
virtual LogLevel level() const noexcept=0
Return the minimum level this logger emits.
virtual void SetLevel(LogLevel level) noexcept=0
Set the minimum level this logger emits.
virtual bool ShouldLog(LogLevel level) const noexcept=0
Cheap check whether a record at level would be emitted.
virtual bool IsNoop() const
Return true if this logger is a no-op.
Definition logger.h:181
virtual Status Initialize(const std::unordered_map< std::string, std::string > &properties)
Property-based setup, called by Loggers::Load() before first use.
Definition logger.h:155
Bind a logger for the current thread until this object leaves scope.
Definition logger.h:235
Severity levels for the logging system.
Result< LogLevel > LogLevelFromString(std::string_view s)
Parse a LogLevel from a string (case-insensitive).
Definition log_level.h:77
LogLevel
Logging severity level, ordered from most to least verbose.
Definition log_level.h:38
void Log(LogLevel level, internal::FmtWithLoc< std::type_identity_t< Args >... > fmt, Args &&... args) noexcept
Log to the process-default logger, std::format style. Formats only if the level is enabled; never thr...
Definition logger.h:354
constexpr std::string_view kLevelProperty
Well-known Logger::Initialize() property keys.
Definition logger.h:132
A structured key/value attribute attached to a log record.
Definition logger.h:52
A single log record handed to a Logger.
Definition logger.h:64
Builder & Location(std::source_location location)
Override the record's source location (defaults to the build site).
Definition logger.h:106
LogMessage Build()
Materialize the LogMessage, moving the accumulated state out.
Definition logger.h:112
Builder & Attribute(std::string key, std::string value)
Append a structured key/value attribute.
Definition logger.h:100
Builder & Message(std::string message)
Set the already-formatted message text.
Definition logger.h:94