HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
log.hpp
1// Copyright Take Vos 2019-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "time_stamp_count.hpp"
8#include "time_stamp_utc.hpp"
9#include "wfree_fifo.hpp"
10#include "atomic.hpp"
11#include "meta.hpp"
12#include "source_location.hpp"
13#include "architecture.hpp"
14#include "delayed_format.hpp"
15#include "fixed_string.hpp"
16#include "subsystem.hpp"
17#include "global_state.hpp"
18#include "URL.hpp"
19#include "unfair_mutex.hpp"
20#include "debugger.hpp"
21#include <chrono>
22#include <format>
23#include <string>
24#include <string_view>
25#include <tuple>
26#include <mutex>
27#include <atomic>
28#include <memory>
29#include <thread>
30
31namespace hi::inline v1 {
32namespace detail {
33
35public:
36 hi_force_inline log_message_base() noexcept = default;
37 virtual ~log_message_base() = default;
38
39 [[nodiscard]] virtual std::string format() const noexcept = 0;
40 [[nodiscard]] virtual std::unique_ptr<log_message_base> make_unique_copy() const noexcept = 0;
41
42public:
43 static inline std::chrono::time_zone const *zone = nullptr;
44};
45
46template<global_state_type Level, basic_fixed_string SourceFile, int SourceLine, basic_fixed_string Fmt, typename... Values>
48public:
49 static_assert(std::popcount(to_underlying(Level)) == 1);
50 static_assert(std::is_same_v<decltype(SourceFile)::value_type, char>, "SourceFile must be a basic_fixed_string<char>");
51 static_assert(std::is_same_v<decltype(Fmt)::value_type, char>, "Fmt must be a basic_fixed_string<char>");
52
53 // clang-format off
54 static constexpr char const *log_level_name =
55 Level == global_state_type::log_fatal ? "fatal" :
56 Level == global_state_type::log_error ? "error" :
57 Level == global_state_type::log_warning ? "warning" :
58 Level == global_state_type::log_info ? "info" :
59 Level == global_state_type::log_debug ? "debug" :
60 Level == global_state_type::log_trace ? "trace" :
61 Level == global_state_type::log_audit ? "audit" :
62 Level == global_state_type::log_statistics ? "stats" :
63 "<unknown log level>";
64 // clang-format on
65
66 log_message(log_message const&) noexcept = default;
67 log_message& operator=(log_message const&) noexcept = default;
68
69 template<typename... Args>
70 hi_force_inline log_message(Args&&...args) noexcept :
71 _time_stamp(time_stamp_count::inplace_with_thread_id{}), _what(std::forward<Args>(args)...)
72 {
73 }
74
75 std::string format() const noexcept override
76 {
77 hilet utc_time_point = time_stamp_utc::make(_time_stamp);
78 hilet sys_time_point = std::chrono::clock_cast<std::chrono::system_clock>(utc_time_point);
79 hilet local_time_point = zone->to_local(sys_time_point);
80
81 hilet cpu_id = _time_stamp.cpu_id();
82 hilet thread_id = _time_stamp.thread_id();
83
84 if constexpr (to_bool(Level & global_state_type::log_statistics)) {
85 return std::format("{} {:2}:{:<10} {:5} {}\n", local_time_point, cpu_id, thread_id, log_level_name, _what());
86 } else {
87 return std::format(
88 "{} {:2}:{:<10} {:5} {} ({}:{})\n",
89 local_time_point,
90 cpu_id,
91 thread_id,
92 log_level_name,
93 _what(),
94 URL::urlFromPath(SourceFile).filename(),
95 SourceLine);
96 }
97 }
98
99 [[nodiscard]] std::unique_ptr<log_message_base> make_unique_copy() const noexcept override
100 {
101 return std::make_unique<log_message>(*this);
102 }
103
104private:
105 time_stamp_count _time_stamp;
106 delayed_format<Fmt, Values...> _what;
107};
108
109} // namespace detail
110
111class log {
112public:
121 template<global_state_type Level, basic_fixed_string SourceFile, int SourceLine, basic_fixed_string Fmt, typename... Args>
122 hi_force_inline void add(Args&&...args) noexcept
123 {
124 static_assert(std::popcount(to_underlying(Level)) == 1);
125
126 hilet state = global_state.load(std::memory_order::relaxed);
127 if (not to_bool(state & Level)) {
128 return;
129 }
130
131 // Add messages in the queue, block when full.
132 // * This reduces amount of instructions needed to be executed during logging.
133 // * Simplifies logged_fatal_message logic.
134 // * Will make sure everything gets logged.
135 // * Blocking is bad in a real time thread, so maybe count the number of times it is blocked.
136
137 // Emplace a message directly on the queue.
139 std::forward<Args>(args)...);
140
141 if (to_bool(Level & global_state_type::log_fatal) or not to_bool(state & global_state_type::log_is_running)) {
142 // If the logger did not start we will log in degraded mode and log from the current thread.
143 // On fatal error we also want to log from the current thread.
144 [[unlikely]] flush();
145 }
146 }
147
152 hi_no_inline void flush() noexcept;
153
161 static bool start_subsystem(global_state_type log_level = global_state_type::log_level_default)
162 {
163 set_log_level(log_level);
164 return hi::start_subsystem(global_state_type::log_is_running, log::subsystem_init, log::subsystem_deinit);
165 }
166
170 static void stop_subsystem()
171 {
172 return hi::stop_subsystem(log::subsystem_deinit);
173 }
174
175private:
179 mutable unfair_mutex _mutex;
180
185 void write(std::string const& str) const noexcept;
186
189 static inline std::jthread _log_thread;
190
193 static void log_thread_main(std::stop_token stop_token) noexcept;
194
197 static void subsystem_deinit() noexcept;
198
204 static bool subsystem_init() noexcept;
205};
206
207inline log log_global;
208
211[[nodiscard]] std::string get_last_error_message() noexcept;
212
213} // namespace hi::inline v1
214
215#define hi_log_debug(fmt, ...) ::hi::log_global.add<::hi::global_state_type::log_debug, __FILE__, __LINE__, fmt>(__VA_ARGS__)
216#define hi_log_info(fmt, ...) ::hi::log_global.add<::hi::global_state_type::log_info, __FILE__, __LINE__, fmt>(__VA_ARGS__)
217#define hi_log_statistics(fmt, ...) \
218 ::hi::log_global.add<::hi::global_state_type::log_statistics, __FILE__, __LINE__, fmt>(__VA_ARGS__)
219#define hi_log_trace(fmt, ...) ::hi::log_global.add<::hi::global_state_type::log_trace, __FILE__, __LINE__, fmt>(__VA_ARGS__)
220#define hi_log_audit(fmt, ...) ::hi::log_global.add<::hi::global_state_type::log_audit, __FILE__, __LINE__, fmt>(__VA_ARGS__)
221#define hi_log_warning(fmt, ...) ::hi::log_global.add<::hi::global_state_type::log_warning, __FILE__, __LINE__, fmt>(__VA_ARGS__)
222#define hi_log_error(fmt, ...) ::hi::log_global.add<::hi::global_state_type::log_error, __FILE__, __LINE__, fmt>(__VA_ARGS__)
223#define hi_log_fatal(fmt, ...) \
224 ::hi::log_global.add<::hi::global_state_type::log_fatal, __FILE__, __LINE__, fmt>(__VA_ARGS__); \
225 hi_debug_abort()
226
227#define hi_log_info_once(name, fmt, ...) \
228 do { \
229 if (++global_counter<name> == 1) { \
230 ::hi::log_global.add<::hi::global_state_type::log_info, __FILE__, __LINE__, fmt>(__VA_ARGS__); \
231 } \
232 } while (false)
233
234#define hi_log_error_once(name, fmt, ...) \
235 do { \
236 if (++global_counter<name> == 1) { \
237 ::hi::log_global.add<::hi::global_state_type::log_error, __FILE__, __LINE__, fmt>(__VA_ARGS__); \
238 } \
239 } while (false)
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
STL namespace.
Delayed formatting.
Definition delayed_format.hpp:19
example: ``` template<hi::basic_fixed_string Foo> class A { auto bar() { return std::string{Foo}; } }...
Definition fixed_string.hpp:34
Definition log.hpp:34
Definition log.hpp:47
Definition log.hpp:111
hi_no_inline void flush() noexcept
Flush all messages from the log_queue directly from this thread.
hi_force_inline void add(Args &&...args) noexcept
Log a message.
Definition log.hpp:122
static void stop_subsystem()
Stop the logger system.
Definition log.hpp:170
Since Window's 10 QueryPerformanceCounter() counts at only 10MHz which is too low to measure performa...
Definition time_stamp_count.hpp:29
Definition time_stamp_count.hpp:35
A wait-free multiple-producer/single-consumer fifo designed for absolute performance.
Definition wfree_fifo.hpp:30