25Log::Sinks Log::sinks_;
26std::mutex Log::sinks_mutex_;
27u32 Log::nextSinkId_ = 1;
35 info(
"ESEngine Log initialized");
39 info(
"ESEngine Log shutdown");
50const char* Log::levelToString(
LogLevel level) {
58 default:
return "UNKNOWN";
63 std::lock_guard lock(sinks_mutex_);
64 u32 id = nextSinkId_++;
65 sinks_.emplace_back(
id, std::make_shared<LogSink>(std::move(
sink)));
70 std::lock_guard lock(sinks_mutex_);
72 std::remove_if(sinks_.begin(), sinks_.end(),
73 [sinkId](
const auto& pair) { return pair.first == sinkId; }),
77void Log::emit(
LogLevel level,
const std::string& message) {
79 line.reserve(message.size() + 10);
81 line += levelToString(level);
86 notifySinks(level, message);
89void Log::appendSigned(std::string& out,
i64 value) {
91 auto res = std::to_chars(buf, buf +
sizeof buf, value);
92 out.append(buf, res.ptr);
95void Log::appendUnsigned(std::string& out,
u64 value) {
97 auto res = std::to_chars(buf, buf +
sizeof buf, value);
98 out.append(buf, res.ptr);
101void Log::appendFloat(std::string& out,
double value,
int precision) {
104 int n = precision < 0 ? std::snprintf(buf,
sizeof buf,
"%g", value)
105 : std::snprintf(buf, sizeof buf,
"%.*f", precision > 99 ? 99 : precision, value);
106 if (n > 0) out.append(buf,
static_cast<usize>(n <
static_cast<int>(
sizeof buf) ? n :
static_cast<int>(
sizeof buf) - 1));
109void Log::appendPointer(std::string& out,
const void* value) {
111 int n = std::snprintf(buf,
sizeof buf,
"%p", value);
112 if (n > 0) out.append(buf,
static_cast<usize>(n));
115int Log::precisionOf(
const char* spec,
const char* end) {
116 if (spec == end || spec[0] !=
':' || end[-1] !=
'f')
return -1;
117 const char* dot = std::find(spec + 1, end,
'.');
118 if (dot == end)
return -1;
122 const char* begin = dot + 1;
123 auto res = std::from_chars(begin, end - 1, precision);
124 return res.ptr != begin ? precision : -1;
127void Log::notifySinks(
LogLevel level,
const std::string& message) {
128 auto now = std::chrono::system_clock::now();
129 auto duration = now.time_since_epoch();
130 auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
132 LogEntry entry{level, message,
static_cast<u64>(millis)};
136 std::lock_guard lock(sinks_mutex_);
139 for (
const auto& [
id,
sink] : snapshot) {
Logging utilities for ESEngine.
static void info(const char *fmt, Args &&... args)
Logs an info message.
static void init()
Initializes the logging system.
static void removeSink(u32 sinkId)
Unregisters a log sink.
static void shutdown()
Shuts down the logging system.
static u32 addSink(LogSink sink)
Registers a log sink to receive log entries.
static void setLevel(LogLevel level)
Sets the minimum log level.
static LogLevel getLevel()
Gets the current minimum log level.
std::uint64_t u64
Unsigned 64-bit integer.
std::size_t usize
Size type for indexing and sizes.
std::uint32_t u32
Unsigned 32-bit integer.
std::int64_t i64
Signed 64-bit integer.
std::function< void(const LogEntry &)> LogSink
LogLevel
Logging severity levels.
@ Info
General information.
@ Fatal
Fatal errors (application cannot continue).
@ Debug
Debug information.
@ Trace
Detailed debugging information.
auto sink(SignalType &signal)
Create a sink for a signal.