136 entry.
wall_time = std::chrono::steady_clock::now();
141 [[nodiscard]] std::string
Format(
bool include_context =
true)
const {
142 std::ostringstream oss;
143 oss <<
"[" << std::fixed << std::setprecision(3) <<
sim_time <<
"] ";
144 oss <<
"[" << GetLevelString(
level) <<
"] ";
146 if (include_context &&
context.IsSet()) {
147 oss <<
"[" <<
context.FullPath() <<
"] ";
156 std::ostringstream oss;
160 oss << std::fixed << std::setprecision(3) <<
sim_time;
164 oss << console.
Colorize(
"[" + std::string(GetLevelString(
level)) +
"]",
165 GetLevelColor(
level))
178 [[nodiscard]]
static const char *GetLevelString(
LogLevel level) {
198 [[nodiscard]]
static const char *GetLevelColor(
LogLevel level) {
246 const std::string &type =
"")
247 : previous_(current_context_) {
248 current_context_.entity = entity;
249 current_context_.component = component;
250 current_context_.type = type;
267 static inline thread_local LogContext current_context_;
292 using Sink = std::function<void(
const std::vector<LogEntry> &)>;
311 : service_(service), previous_mode_(service.immediate_mode_) {
312 service_.SetImmediateMode(
false);
316 service_.FlushAndClear();
317 service_.SetImmediateMode(previous_mode_);
338 void Reserve(std::size_t capacity) { entries_.reserve(capacity); }
352 void Log(
LogLevel level,
double sim_time, std::string_view message) {
358 if (level < min_level_) {
364 std::lock_guard<std::mutex> lock(mutex_);
373 entries_.push_back(std::move(entry));
375 if (immediate_mode_) {
376 FlushEntry(entries_.back());
392 void Flush(
bool sort_by_time =
false) {
393 std::lock_guard<std::mutex> lock(mutex_);
395 if (entries_.empty()) {
400 std::sort(entries_.begin(), entries_.end(), [](
const LogEntry &a,
const LogEntry &b) {
401 return a.wall_time < b.wall_time;
405 for (
const auto &[sink, min_level] : sinks_) {
407 std::vector<LogEntry> filtered;
408 filtered.reserve(entries_.size());
409 for (
const auto &entry : entries_) {
410 if (entry.level >= min_level) {
411 filtered.push_back(entry);
414 if (!filtered.empty()) {
423 std::lock_guard<std::mutex> lock(mutex_);
429 std::lock_guard<std::mutex> lock(mutex_);
436 std::lock_guard<std::mutex> lock(mutex_);
437 return entries_.size();
441 std::lock_guard<std::mutex> lock(mutex_);
442 return !entries_.empty();
446 std::lock_guard<std::mutex> lock(mutex_);
452 std::lock_guard<std::mutex> lock(mutex_);
453 return error_count_ > 0;
457 std::lock_guard<std::mutex> lock(mutex_);
458 return fatal_count_ > 0;
462 std::lock_guard<std::mutex> lock(mutex_);
467 std::lock_guard<std::mutex> lock(mutex_);
473 std::lock_guard<std::mutex> lock(mutex_);
481 std::lock_guard<std::mutex> lock(mutex_);
482 std::vector<LogEntry> result;
483 for (
const auto &entry : entries_) {
484 if (entry.level == level) {
485 result.push_back(entry);
492 std::lock_guard<std::mutex> lock(mutex_);
493 std::vector<LogEntry> result;
494 for (
const auto &entry : entries_) {
495 if (entry.context.entity == entity) {
496 result.push_back(entry);
503 std::lock_guard<std::mutex> lock(mutex_);
504 std::vector<LogEntry> result;
505 for (
const auto &entry : entries_) {
506 if (entry.context.FullPath() == path) {
507 result.push_back(entry);
514 std::vector<LogEntry> entries_;
515 std::vector<std::pair<Sink, LogLevel>> sinks_;
517 bool immediate_mode_ =
true;
519 std::size_t error_count_ = 0;
520 std::size_t fatal_count_ = 0;
522 mutable std::mutex mutex_;
524 void FlushEntry(
const LogEntry &entry) {
525 for (
const auto &[sink, min_level] : sinks_) {
526 if (entry.
level >= min_level) {
548#define ICARUS_LOG_TRACE(sim_time, msg) ::icarus::GetLogService().Trace(sim_time, msg)
550#define ICARUS_LOG_DEBUG(sim_time, msg) ::icarus::GetLogService().Debug(sim_time, msg)
552#define ICARUS_LOG_INFO(sim_time, msg) ::icarus::GetLogService().Info(sim_time, msg)
554#define ICARUS_LOG_EVENT(sim_time, msg) ::icarus::GetLogService().Event(sim_time, msg)
556#define ICARUS_LOG_WARN(sim_time, msg) ::icarus::GetLogService().Warning(sim_time, msg)
558#define ICARUS_LOG_ERROR(sim_time, msg) ::icarus::GetLogService().Error(sim_time, msg)
560#define ICARUS_LOG_FATAL(sim_time, msg) ::icarus::GetLogService().Fatal(sim_time, msg)
Console abstraction with ANSI color support.
Core type definitions, concepts, and configuration for Icarus.
Console output with color and formatting support.
Definition Console.hpp:111
std::string Colorize(std::string_view text, const char *color) const
Apply color if enabled.
Definition Console.hpp:177
ScopedContext(const std::string &entity, const std::string &component, const std::string &type="")
Definition LogService.hpp:245
ScopedContext(const ScopedContext &)=delete
ScopedContext(ScopedContext &&)=delete
ScopedContext & operator=(const ScopedContext &)=delete
~ScopedContext()
Definition LogService.hpp:253
ScopedContext & operator=(ScopedContext &&)=delete
Thread-local log context manager.
Definition LogService.hpp:229
static const LogContext & GetContext()
Get current context (called internally by LogBuffer::Log).
Definition LogService.hpp:238
static void SetContext(const LogContext &ctx)
Set context for current thread (called by Simulator before Step).
Definition LogService.hpp:232
static void ClearContext()
Clear context (called by Simulator after Step).
Definition LogService.hpp:235
~BufferedScope()
Definition LogService.hpp:315
BufferedScope(BufferedScope &&)=delete
BufferedScope(LogService &service)
Definition LogService.hpp:310
BufferedScope & operator=(const BufferedScope &)=delete
BufferedScope & operator=(BufferedScope &&)=delete
BufferedScope(const BufferedScope &)=delete
Unified logging service for Icarus.
Definition LogService.hpp:289
void Log(LogLevel level, double sim_time, std::string_view message, const LogContext &ctx)
Log with explicit context (bypasses thread-local).
Definition LogService.hpp:357
std::vector< LogEntry > GetEntriesForEntity(std::string_view entity) const
Definition LogService.hpp:491
void Fatal(double t, std::string_view msg)
Definition LogService.hpp:387
void Event(double t, std::string_view msg)
Definition LogService.hpp:384
bool HasErrors() const
Check if any errors were logged (in buffer or flushed).
Definition LogService.hpp:451
std::size_t ErrorCount() const
Definition LogService.hpp:461
void Warning(double t, std::string_view msg)
Definition LogService.hpp:385
void Error(double t, std::string_view msg)
Definition LogService.hpp:386
void SetMinLevel(LogLevel level)
Set minimum level (below this = dropped).
Definition LogService.hpp:334
std::size_t FatalCount() const
Definition LogService.hpp:466
void Flush(bool sort_by_time=false)
Flush buffer to all sinks.
Definition LogService.hpp:392
LogLevel GetMinLevel() const
Definition LogService.hpp:335
void Reserve(std::size_t capacity)
Set buffer capacity (pre-allocate for performance).
Definition LogService.hpp:338
void ClearSinks()
Clear all sinks.
Definition LogService.hpp:347
std::function< void(const std::vector< LogEntry > &)> Sink
Sink callback type: receives batch of entries to output.
Definition LogService.hpp:292
void Trace(double t, std::string_view msg)
Definition LogService.hpp:381
void Clear()
Clear buffer without flushing (discard pending logs).
Definition LogService.hpp:428
bool IsImmediateMode() const
Definition LogService.hpp:300
void ResetErrorCounts()
Reset error counts (call at start of new run).
Definition LogService.hpp:472
void AddSink(Sink sink)
Add an output sink.
Definition LogService.hpp:341
void Debug(double t, std::string_view msg)
Definition LogService.hpp:382
std::size_t PendingCount() const
Definition LogService.hpp:435
bool HasPending() const
Definition LogService.hpp:440
std::vector< LogEntry > GetEntriesAtLevel(LogLevel level) const
Definition LogService.hpp:480
std::vector< LogEntry > GetPending() const
Definition LogService.hpp:445
void FlushAndClear(bool sort_by_time=false)
Flush and clear buffer.
Definition LogService.hpp:421
void Log(LogLevel level, double sim_time, std::string_view message)
Log a message (uses current thread-local context).
Definition LogService.hpp:352
void SetImmediateMode(bool immediate)
Enable immediate flush after each log (for lifecycle phases).
Definition LogService.hpp:299
void AddSink(Sink sink, LogLevel min_level)
Add a sink that only receives entries at or above a level.
Definition LogService.hpp:344
void Info(double t, std::string_view msg)
Definition LogService.hpp:383
bool HasFatalErrors() const
Definition LogService.hpp:456
std::vector< LogEntry > GetEntriesForComponent(std::string_view path) const
Definition LogService.hpp:502
Definition AggregationTypes.hpp:13
std::string MakeFullPath(const std::string &entity, const std::string &name)
Build a full path from entity and name.
Definition CoreTypes.hpp:166
LogLevel
Log severity levels.
Definition Console.hpp:35
@ Warning
Potential issues.
Definition Console.hpp:40
@ Info
Normal operation.
Definition Console.hpp:38
@ Fatal
Unrecoverable errors.
Definition Console.hpp:42
@ Error
Recoverable errors.
Definition Console.hpp:41
@ Event
Simulation events (phase changes, etc.).
Definition Console.hpp:39
@ Debug
Debugging info.
Definition Console.hpp:37
@ Trace
Most verbose, internal debugging.
Definition Console.hpp:36
LogService & GetLogService()
Global log service singleton.
Definition LogService.hpp:536
static constexpr const char * BgRed
Definition Console.hpp:68
static constexpr const char * Cyan
Definition Console.hpp:63
static constexpr const char * Gray
Definition Console.hpp:65
static constexpr const char * Red
Definition Console.hpp:58
static constexpr const char * Dim
Definition Console.hpp:55
static constexpr const char * White
Definition Console.hpp:64
static constexpr const char * Green
Definition Console.hpp:59
static constexpr const char * Yellow
Definition Console.hpp:60
Logging configuration.
Definition LogService.hpp:35
static LogConfig Default()
Create default config.
Definition LogService.hpp:57
bool profiling_enabled
Definition LogService.hpp:48
bool file_rotate
Definition LogService.hpp:44
bool quiet_mode
Suppress all but errors.
Definition LogService.hpp:49
LogLevel console_level
Definition LogService.hpp:37
std::size_t max_file_size_mb
Definition LogService.hpp:45
bool progress_enabled
Definition LogService.hpp:38
std::string telemetry_path
Definition LogService.hpp:53
static LogConfig Verbose()
Create verbose config (all output).
Definition LogService.hpp:72
bool telemetry_enabled
Definition LogService.hpp:52
std::vector< std::string > telemetry_signals
Which signals to record.
Definition LogService.hpp:54
static LogConfig Quiet()
Create quiet config (errors only).
Definition LogService.hpp:63
LogLevel file_level
Definition LogService.hpp:43
bool file_enabled
Definition LogService.hpp:41
static LogConfig WithProfiling()
Create profiling config.
Definition LogService.hpp:81
std::string file_path
Definition LogService.hpp:42
Immutable log context - set by component/entity during execution.
Definition LogService.hpp:98
std::string type
Component type (e.g., "LiquidEngine").
Definition LogService.hpp:101
std::string FullPath() const
Full path: "entity.component" or just "component" if no entity.
Definition LogService.hpp:104
std::string component
Component name (e.g., "merlinEngine2").
Definition LogService.hpp:100
std::string entity
Entity name (e.g., "falcon9_1").
Definition LogService.hpp:99
bool IsSet() const
Check if context is set.
Definition LogService.hpp:107
A single log entry with full context.
Definition LogService.hpp:119
LogLevel level
Severity level.
Definition LogService.hpp:120
std::string message
Log message.
Definition LogService.hpp:122
LogContext context
Entity/component context.
Definition LogService.hpp:123
double sim_time
Simulation time when logged.
Definition LogService.hpp:121
std::chrono::steady_clock::time_point wall_time
Wall clock time for ordering logs from concurrent sources.
Definition LogService.hpp:126
std::string Format(bool include_context=true) const
Format for output: "[sim_time] [LEVEL] [entity.component] message".
Definition LogService.hpp:141
static LogEntry Create(LogLevel level, double sim_time, std::string_view message, const LogContext &ctx)
Create a log entry with current context.
Definition LogService.hpp:129
std::string FormatColored(const Console &console) const
Format with colors (for terminal).
Definition LogService.hpp:155