129 template<
typename... Args>
130 static void trace(
const char* fmt, Args&&... args) {
140 template<
typename... Args>
141 static void debug(
const char* fmt, Args&&... args) {
151 template<
typename... Args>
152 static void info(
const char* fmt, Args&&... args) {
162 template<
typename... Args>
163 static void warn(
const char* fmt, Args&&... args) {
173 template<
typename... Args>
174 static void error(
const char* fmt, Args&&... args) {
184 template<
typename... Args>
185 static void fatal(
const char* fmt, Args&&... args) {
190 static std::atomic<LogLevel> level_;
191 using Sinks = std::vector<std::pair<u32, std::shared_ptr<LogSink>>>;
193 static std::mutex sinks_mutex_;
194 static u32 nextSinkId_;
196 template<
typename... Args>
197 static void log(
LogLevel level,
const char* fmt, Args&&... args) {
198 if (level < level_.load(std::memory_order_relaxed))
return;
201 formatTo(message, fmt, std::forward<Args>(args)...);
202 emit(level, message);
207 static void emit(
LogLevel level,
const std::string& message);
208 static void notifySinks(
LogLevel level,
const std::string& message);
209 static const char* levelToString(
LogLevel level);
212 static void appendSigned(std::string& out,
i64 value);
213 static void appendUnsigned(std::string& out,
u64 value);
214 static void appendFloat(std::string& out,
double value,
int precision);
215 static void appendPointer(std::string& out,
const void* value);
217 static int precisionOf(
const char* spec,
const char* end);
219 static void formatTo(std::string& out,
const char* fmt) {
223 template<
typename T,
typename... Args>
224 static void formatTo(std::string& out,
const char* fmt, T&& value, Args&&... args) {
227 const char* start = fmt + 1;
228 const char* end = start;
229 while (*end && *end !=
'}') ++end;
232 appendValue(out, value, precisionOf(start, end));
233 formatTo(out, end + 1, std::forward<Args>(args)...);
237 out.push_back(*fmt++);
242 static void appendValue(std::string& out,
const T& value,
int precision) {
243 using D = std::remove_cv_t<std::remove_reference_t<T>>;
244 if constexpr (std::is_same_v<D, bool>) {
245 out.push_back(value ?
'1' :
'0');
246 }
else if constexpr (std::is_same_v<D, char> || std::is_same_v<D, signed char> || std::is_same_v<D, unsigned char>) {
247 out.push_back(
static_cast<char>(value));
248 }
else if constexpr (std::is_enum_v<D>) {
249 appendValue(out,
static_cast<std::conditional_t<std::is_signed_v<std::underlying_type_t<D>
>,
i64,
u64>>(value), precision);
250 }
else if constexpr (std::is_integral_v<D>) {
251 if constexpr (std::is_signed_v<D>) appendSigned(out,
static_cast<i64>(value));
252 else appendUnsigned(out,
static_cast<u64>(value));
253 }
else if constexpr (std::is_floating_point_v<D>) {
254 appendFloat(out,
static_cast<double>(value), precision);
255 }
else if constexpr (std::is_same_v<D, const char*> || std::is_same_v<D, char*>) {
256 if (value) out += value;
257 }
else if constexpr (std::is_convertible_v<const D&, std::string_view>) {
258 out += std::string_view(value);
260 static_assert(std::is_pointer_v<D>,
"Log cannot format this argument type");
261 appendPointer(out,
static_cast<const void*
>(value));