File size: 937 Bytes
3ce1a40 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include "core.h"
#include <chrono>
#include <mutex>
namespace unify {
uint64_t wall_clock_ns() {
static const auto start = std::chrono::steady_clock::now();
return uint64_t(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start).count());
}
static LogLevel g_min_level = LogLevel::Info;
static std::mutex g_log_mutex;
void set_log_level(LogLevel min_level) { g_min_level = min_level; }
void log_message(LogLevel level, const char* category, const char* fmt, ...) {
if (level < g_min_level) return;
static const char* names[] = {"DEBUG", "INFO", "WARN", "ERROR"};
char buf[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof buf, fmt, args);
va_end(args);
std::lock_guard<std::mutex> lock(g_log_mutex);
std::fprintf(level >= LogLevel::Warn ? stderr : stdout, "[%s] [%s] %s\n", names[int(level)], category, buf);
}
} // namespace unify
|