7#include "delayed_format.hpp"
8#include "format_check.hpp"
9#include "../container/module.hpp"
10#include "../time/module.hpp"
11#include "../utility/utility.hpp"
12#include "../concurrency/concurrency.hpp"
13#include "../console/console.hpp"
14#include "../macros.hpp"
28namespace hi {
inline namespace v1 {
31class log_message_base {
33 hi_force_inline log_message_base()
noexcept =
default;
34 virtual ~log_message_base() =
default;
36 [[nodiscard]]
virtual std::string format()
const noexcept = 0;
41class log_message :
public log_message_base {
43 static_assert(std::popcount(std::to_underlying(Level)) == 1);
46 constexpr static char const *log_level_name =
47 Level == global_state_type::log_fatal ?
"fatal" :
48 Level == global_state_type::log_error ?
"error" :
49 Level == global_state_type::log_warning ?
"warning" :
50 Level == global_state_type::log_info ?
"info" :
51 Level == global_state_type::log_debug ?
"debug" :
52 Level == global_state_type::log_trace ?
"trace" :
53 Level == global_state_type::log_audit ?
"audit" :
54 Level == global_state_type::log_statistics ?
"stats" :
55 "<unknown log level>";
58 log_message(log_message
const&)
noexcept =
default;
59 log_message& operator=(log_message
const&)
noexcept =
default;
61 template<
typename... Args>
62 hi_force_inline log_message(Args&&...args) noexcept :
63 _time_stamp(time_stamp_count::inplace_with_thread_id{}), _what(
std::forward<Args>(args)...)
69 hilet utc_time_point = time_stamp_utc::make(_time_stamp);
70 hilet sys_time_point = std::chrono::clock_cast<std::chrono::system_clock>(utc_time_point);
73 hilet
cpu_id = _time_stamp.cpu_id();
74 hilet thread_id = _time_stamp.thread_id();
77 if constexpr (to_bool(Level & global_state_type::log_statistics)) {
78 return std::format(
"{} {}({}) {:5} {}\n", local_time_point, thread_name,
cpu_id, log_level_name, _what());
80 auto source_filename = std::filesystem::path{
static_cast<std::string_view
>(SourcePath)}.filename().generic_string();
82 "{} {}({}) {:5} {} ({}:{})\n",
95 return std::make_unique<log_message>(*
this);
99 time_stamp_count _time_stamp;
100 delayed_format<Fmt, Values...> _what;
115 hi_force_inline
void add(Args&&...args)
noexcept
117 static_assert(std::popcount(std::to_underlying(Level)) == 1);
119 hilet state =
global_state.load(std::memory_order::relaxed);
120 if (not to_bool(state & Level)) {
134 if (to_bool(Level & global_state_type::log_fatal) or not to_bool(state & global_state_type::log_is_running)) {
137 [[unlikely]]
flush();
152 hilet lock = std::scoped_lock(_mutex);
154 wrote_message = _fifo.take_one([©_of_message](
auto& message) {
155 copy_of_message = message.make_unique_copy();
160 hi_assert_not_null(copy_of_message);
161 write(copy_of_message->format());
163 }
while (wrote_message);
176 return hi::start_subsystem(global_state_type::log_is_running, log::subsystem_init, log::subsystem_deinit);
190 wfree_fifo<detail::log_message_base, 64> _fifo;
191 mutable unfair_mutex _mutex;
204 static inline std::jthread _log_thread;
210 inline static void log_thread_main(std::stop_token stop_token)
noexcept;
214 inline static void subsystem_deinit() noexcept;
221 static
bool subsystem_init() noexcept
223 _log_thread = std::jthread(log_thread_main);
228inline log log_global;
232inline void log::subsystem_deinit() noexcept
235 if (_log_thread.joinable()) {
236 _log_thread.request_stop();
std::string get_thread_name(thread_id id) noexcept
Get the thread name of a thread id.
Definition thread_intf.hpp:63
T::value_type start_subsystem(T &check_variable, typename T::value_type off_value, typename T::value_type(*init_function)(), void(*deinit_function)())
Start a sub-system.
Definition subsystem.hpp:116
std::atomic< global_state_type > global_state
The global state of the hikogui framework.
Definition global_state.hpp:201
bool global_state_disable(global_state_type subsystem, std::memory_order order=std::memory_order::seq_cst) noexcept
Disable a subsystem.
Definition global_state.hpp:247
global_state_type
The flag-type used for global state.
Definition global_state.hpp:30
void set_log_level(global_state_type log_level) noexcept
Set the logging level.
Definition global_state.hpp:230
void stop_subsystem(void(*deinit_function)())
Stop a sub-system.
Definition subsystem.hpp:202
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
hi_export void print(std::format_string< Args... > fmt, Args &&... args) noexcept
Output text to the console.
std::chrono::time_zone const & cached_current_zone() noexcept
Cached current time zone.
Definition time_zone.hpp:39
static bool start_subsystem(global_state_type log_level=global_state_type::log_level_default)
Start the logger system.
Definition log.hpp:173
hi_force_inline void add(Args &&...args) noexcept
Log a message.
Definition log.hpp:115
static void stop_subsystem()
Stop the logger system.
Definition log.hpp:182
hi_no_inline void flush() noexcept
Flush all messages from the log_queue directly from this thread.
Definition log.hpp:145
A string which may be used as a none-type template parameter.
Definition fixed_string.hpp:41