log.h
| File Path | engine/include/conjure/core/log.h |
| API ↗ | Errors and results, Logging and assertions |
Logging.
Structs
| File | Description |
|---|---|
log_message |
A formatted message delivered to sinks. Views are valid only during the write call. |
Classes
| File | Description |
|---|---|
callback_log_sink |
Forwards every message to a callable. Used by the editor console and by tests. The callable is invoked from the logging thread and must be thread-safe. |
console_log_sink |
Writes to stdout (below WARN) and stderr (WARN and above) with optional ANSI colours. |
file_log_sink |
Writes to a file, created (with parent directories) on construction: emptied first, or appended to when truncate is false. Every line is flushed as it is written, so the file holds each complete line written so far even when the process ends without a shutdown (a crash, a kill); only a line still being written then can be cut short. Writes go to the end of the file, but two sinks over one file can tear or overwrite each other's lines (on Windows the C runtime seeks to the end before each write): a file that several owners share in a process goes through log::hold_log_file. A flush per line costs one operating-system write per line. |
log_category |
A named channel with its own minimum level. Instances are global and register themselves so tools can list and adjust categories at runtime. |
log_sink |
Destination for log messages. The logger calls write() from any thread, possibly concurrently, and without holding its own lock, so a sink must synchronise itself and may safely log or add/remove sinks from inside write(). |
Enumerations
log_level
enum class log_level : u8 {
TRACE = 0,
DEBUG,
INFO,
WARN,
ERR, // "ERROR" is a Windows macro (wingdi.h); ERR keeps the enum usable everywhere
FATAL,
OFF
};
Forward Declarations
file_log_sink::state
Constants
| Name | Type | Value | Description |
|---|---|---|---|
LOG_TIME_LENGTH |
inline constexpr usize |
12 |
How many characters format_log_time writes: "HH:MM:SS.mmm". |
Variables
| Name | Type | Value | Description |
|---|---|---|---|
LOG_CORE |
extern CONJURE_ |
— | Category used by core itself and by code that has no better home. |
Functions
Public
format_log_line
format_log_line[[nodiscard]] CONJURE_API std::string format_log_line(
const log_message &message);Formats a message the way the console and file sinks do: "HH:MM:SS.mmm [LEVEL] [category] text".
format_log_time
format_log_time[[nodiscard]] CONJURE_API usize format_log_time(
std::chrono::system_clock::time_point timestamp,
std::span<char> out) noexcept;Writes the local wall-clock time of timestamp as format_log_line begins a line, "HH:MM:SS.mmm" (LOG_TIME_LENGTH characters, not terminated), into the front of out and returns how many it wrote: LOG_TIME_LENGTH, or 0 when out is shorter. A time before the epoch is taken to the millisecond below it; one the C library cannot convert to local time (the Windows runtime refuses a time before the epoch) is written as its UTC time of day. Allocates nothing, so a view that shows a message's time every frame (the editor's console) stays free of allocations.
log::add_sink
log::add_sinkCONJURE_API void add_sink(std::shared_ptr<log_sink> sink);Adds a sink. Sinks are shared so the caller may keep a reference for later removal.
log::categories
log::categories[[nodiscard]] CONJURE_API std::vector<log_category *> categories();Every registered category, in registration order.
log::clear_sinks
log::clear_sinksCONJURE_API void clear_sinks();Removes every sink. A log file held with hold_log_file stays open for its holders; the next hold_log_file of it installs it again.
log::console_enabled
log::console_enabled[[nodiscard]] CONJURE_API bool console_enabled() noexcept;log::find_category
log::find_category[[nodiscard]] CONJURE_API log_category *find_category(std::string_view name);Finds a category by name, or nullptr.
log::flush
log::flushCONJURE_API void flush();Flushes every sink.
log::global_level
log::global_level[[nodiscard]] CONJURE_API log_level global_level() noexcept;log::hold_log_file
log::hold_log_fileCONJURE_API std::shared_ptr<file_log_sink> hold_log_file(const std::filesystem::path &path);Takes a hold on the process's sink for the log file at path, installs it and returns it. Every holder of one file shares one file_log_sink, and a new sink is opened only once the previous one is no longer in use, so two sinks of hold_log_file do not write the file at the same time. One file is the same path once made absolute and normal or, once the file exists, the same file on disk (two spellings on a case-insensitive volume, a hard link, a symbolic link); two spellings of a file that does not exist yet are two files until it does. The file is emptied only the first time the process opens it; a hold after every hold was given back appends, so one process run leaves one file however often its holders come and go (each engine the editor starts holds --log-file for its own run). Every hold is given back with release_log_file. An empty path takes no hold and returns nullptr. A file that cannot be opened still yields a sink, whose is_open() is false and which drops messages while it is held; the next hold after every hold was given back tries to open the file again (and empties it if it opens for the first time).
log::is_enabled
log::is_enabled[[nodiscard]] CONJURE_API bool is_enabled(const log_category &category, log_level level) noexcept;True when a message at level for category would reach the sinks.
log::release_log_file
log::release_log_fileCONJURE_API void release_log_file(const std::shared_ptr<log_sink> &sink);Gives back one hold on the sink hold_log_file returned (by the sink, not by path, so a working directory that changed in between does not matter). The last hold removes the sink from the log and flushes it; the file closes once nothing keeps the sink any more (a copy the caller kept, a write in progress on another thread). A sink without a hold, and nullptr, are ignored.
log::remove_sink
log::remove_sinkCONJURE_API void remove_sink(const std::shared_ptr<log_sink> &sink);Removes a previously added sink. Unknown sinks are ignored.
log::set_category_level
log::set_category_levelCONJURE_API bool set_category_level(std::string_view name, log_level level);Sets a category's level by name. Returns false when the category does not exist.
log::set_console_enabled
log::set_console_enabledCONJURE_API void set_console_enabled(bool enabled);The console sink that exists until disabled. On by default so early messages are never lost; tests turn it off.
log::set_global_level
log::set_global_levelCONJURE_API void set_global_level(log_level level) noexcept;Messages below this level are dropped regardless of category.
log::write
log::writeCONJURE_API void write( const log_category &category, log_level level, const char *file, int line, std::string_view text );Delivers an already formatted message to every sink.
parse_log_level
parse_log_level[[nodiscard]] CONJURE_API std::optional<log_level> parse_log_level(
std::string_view name) noexcept;Parses a level name case-insensitively ("warn", "ERROR", "Off"). The names are the ones to_string returns, so OFF parses to log_level::OFF. An unknown or empty name returns std::nullopt.
to_string
to_string[[nodiscard]] CONJURE_API std::string_view to_string(
log_level level) noexcept;Returns the upper-case name of a level ("INFO").