30 return [console = std::move(console)](
const std::vector<LogEntry> &entries) {
31 for (
const auto &entry : entries) {
33 std::cout << entry.FormatColored(console) <<
"\n";
35 std::cout << entry.Format() <<
"\n";
46 return [console = std::move(console)](
const std::vector<LogEntry> &entries) {
48 std::map<std::string, std::vector<const LogEntry *>> grouped;
49 for (
const auto &entry : entries) {
50 grouped[entry.context.entity].push_back(&entry);
54 for (
const auto &[entity, group_entries] : grouped) {
56 std::string header =
"─── " + (entity.empty() ?
"(no entity)" : entity) +
" ";
58 std::size_t remaining = (header.size() >= 80) ? 0 : (80 - header.size());
59 for (std::size_t i = 0; i < remaining; ++i) {
66 std::cout << header <<
"\n";
70 for (
const auto *entry : group_entries) {
72 std::cout << entry->FormatColored(console) <<
"\n";
74 std::cout << entry->Format() <<
"\n";
85 auto file = std::make_shared<std::ofstream>(path, std::ios::app);
86 return [file](
const std::vector<LogEntry> &entries) {
87 if (!file->is_open()) {
90 for (
const auto &entry : entries) {
91 *file << entry.Format() <<
"\n";
99 std::size_t max_files) {
101 std::string base_path;
102 std::size_t max_size;
103 std::size_t max_files;
104 std::size_t current_size = 0;
105 std::shared_ptr<std::ofstream> file;
108 auto state = std::make_shared<State>();
109 state->base_path = path;
110 state->max_size = max_size_bytes;
111 state->max_files = max_files;
112 state->file = std::make_shared<std::ofstream>(path, std::ios::app);
114 return [state](
const std::vector<LogEntry> &entries) {
115 if (!state->file->is_open()) {
119 for (
const auto &entry : entries) {
120 std::string line = entry.Format() +
"\n";
121 *state->file << line;
122 state->current_size += line.size();
125 if (state->current_size >= state->max_size) {
126 state->file->close();
129 for (std::size_t i = state->max_files - 1; i > 0; --i) {
130 std::string old_name = state->base_path +
"." + std::to_string(i);
131 std::string new_name = state->base_path +
"." + std::to_string(i + 1);
132 std::rename(old_name.c_str(), new_name.c_str());
134 std::rename(state->base_path.c_str(), (state->base_path +
".1").c_str());
137 state->file = std::make_shared<std::ofstream>(state->base_path, std::ios::app);
138 state->current_size = 0;
141 state->file->flush();
147 auto file = std::make_shared<std::ofstream>(path, std::ios::app);
148 return [file](
const std::vector<LogEntry> &entries) {
149 if (!file->is_open()) {
152 for (
const auto &entry : entries) {
154 *file <<
"{\"time\":" << entry.sim_time <<
",\"level\":\""
155 << GetLevelString(entry.level) <<
"\",\"entity\":\""
156 << EscapeJson(entry.context.entity) <<
"\",\"component\":\""
157 << EscapeJson(entry.context.component) <<
"\",\"message\":\""
158 << EscapeJson(entry.message) <<
"\"}\n";
166 return [](
const std::vector<LogEntry> & ) {
173 return [handler = std::move(handler)](
const std::vector<LogEntry> &entries) {
174 for (
const auto &entry : entries) {
181 [[nodiscard]]
static const char *GetLevelString(
LogLevel level) {
201 [[nodiscard]]
static std::string EscapeJson(
const std::string &s) {
203 result.reserve(s.size());
Console abstraction with ANSI color support.
Unified logging service for Icarus.
bool IsColorEnabled() const
Definition Console.hpp:120
std::string Colorize(std::string_view text, const char *color) const
Apply color if enabled.
Definition Console.hpp:177
std::function< void(const std::vector< LogEntry > &)> Sink
Sink callback type: receives batch of entries to output.
Definition LogService.hpp:292
Factory for common log sinks.
Definition LogSink.hpp:25
static LogService::Sink Null()
Null sink (for testing/benchmarking).
Definition LogSink.hpp:165
static LogService::Sink ConsoleGrouped(class Console console)
Definition LogSink.hpp:45
static LogService::Sink JsonLines(const std::string &path)
JSON Lines sink (for log aggregation).
Definition LogSink.hpp:146
static LogService::Sink Callback(std::function< void(const LogEntry &)> handler)
Callback sink (custom handling).
Definition LogSink.hpp:172
static LogService::Sink Console(class Console console)
Definition LogSink.hpp:29
static LogService::Sink RotatingFile(const std::string &path, std::size_t max_size_bytes, std::size_t max_files)
File sink with rotation.
Definition LogSink.hpp:98
static LogService::Sink File(const std::string &path)
File sink (plain text, no colors).
Definition LogSink.hpp:83
Definition AggregationTypes.hpp:13
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
static constexpr const char * Dim
Definition Console.hpp:55
A single log entry with full context.
Definition LogService.hpp:119