49template<global_state_type Level, fixed_string SourcePath,
int SourceLine, fixed_string Fmt,
typename... Values>
52 static_assert(std::popcount(to_underlying(Level)) == 1);
55 static constexpr char const *log_level_name =
56 Level == global_state_type::log_fatal ?
"fatal" :
57 Level == global_state_type::log_error ?
"error" :
58 Level == global_state_type::log_warning ?
"warning" :
59 Level == global_state_type::log_info ?
"info" :
60 Level == global_state_type::log_debug ?
"debug" :
61 Level == global_state_type::log_trace ?
"trace" :
62 Level == global_state_type::log_audit ?
"audit" :
63 Level == global_state_type::log_statistics ?
"stats" :
64 "<unknown log level>";
70 template<
typename... Args>
71 hi_force_inline
log_message(Args&&...args) noexcept :
72 _time_stamp(time_stamp_count::inplace_with_thread_id{}), _what(std::forward<Args>(args)...)
78 hilet utc_time_point = time_stamp_utc::make(_time_stamp);
79 hilet sys_time_point = std::chrono::clock_cast<std::chrono::system_clock>(utc_time_point);
80 hilet local_time_point = zone->to_local(sys_time_point);
82 hilet cpu_id = _time_stamp.cpu_id();
83 hilet thread_id = _time_stamp.thread_id();
84 hilet thread_name = get_thread_name(thread_id);
86 if constexpr (to_bool(Level & global_state_type::log_statistics)) {
87 return std::format(
"{} {}({}) {:5} {}\n", local_time_point, thread_name, cpu_id, log_level_name, _what());
89 auto source_filename = std::filesystem::path{
static_cast<std::string_view
>(SourcePath)}.filename().generic_string();
91 "{} {}({}) {:5} {} ({}:{})\n",
104 return std::make_unique<log_message>(*
this);
108 time_stamp_count _time_stamp;
109 delayed_format<Fmt, Values...> _what;
123 template<global_state_type Level, fixed_string SourcePath,
int SourceLine, fixed_string Fmt,
typename... Args>
124 hi_force_inline
void add(Args&&...args)
noexcept
126 static_assert(std::popcount(to_underlying(Level)) == 1);
128 hilet state = global_state.load(std::memory_order::relaxed);
129 if (not to_bool(state & Level)) {
141 std::forward<Args>(args)...);
143 if (to_bool(Level & global_state_type::log_fatal) or not to_bool(state & global_state_type::log_is_running)) {
146 [[unlikely]]
flush();
163 static
bool start_subsystem(global_state_type log_level = global_state_type::log_level_default)
165 set_log_level(log_level);
166 return hi::start_subsystem(global_state_type::log_is_running, log::subsystem_init, log::subsystem_deinit);
174 return hi::stop_subsystem(log::subsystem_deinit);
180 wfree_fifo<detail::log_message_base, 64> _fifo;
181 mutable unfair_mutex _mutex;
191 static inline std::jthread _log_thread;
195 static void log_thread_main(std::stop_token stop_token)
noexcept;
199 static void subsystem_deinit() noexcept;
206 static
bool subsystem_init() noexcept;