19#include "fastmm/core/config_macros.hpp"
22#include "fastmm/core/fixed_string.hpp"
23#include "fastmm/core/spsc_ring.hpp"
77using LogRing = SpscRing<LogRecord, kLogRingSize>;
92 return static_cast<std::uint8_t
>(lvl) >=
g_log_level.load(std::memory_order_relaxed);
129 [[nodiscard]]
bool reopen();
132 [[nodiscard]]
bool running() const noexcept {
return running_.load(std::memory_order_acquire); }
134 [[nodiscard]]
bool to_file() const noexcept {
return to_file_.load(std::memory_order_acquire); }
136 [[nodiscard]] std::uint64_t
dropped() const noexcept {
137 return dropped_.load(std::memory_order_relaxed);
139 void note_drop() noexcept { dropped_.fetch_add(1, std::memory_order_relaxed); }
140 [[nodiscard]] std::uint64_t
written() const noexcept {
141 return written_.load(std::memory_order_relaxed);
150 std::size_t drain_all();
155 Impl* impl_ =
nullptr;
156 std::atomic<bool> running_{
false};
157 std::atomic<bool> to_file_{
false};
158 std::atomic<std::uint64_t> dropped_{0};
159 std::atomic<std::uint64_t> written_{0};
164inline thread_local LogRing* t_log_ring =
nullptr;
165inline thread_local std::uint32_t t_log_tid = 0;
166inline thread_local std::uint32_t t_log_slot = 0;
167inline thread_local bool t_log_thread_exiting =
false;
168inline thread_local const SimClock* t_log_clock =
nullptr;
169inline thread_local std::uint32_t t_log_run = 0;
173constexpr bool log_compiled(
LogLevel l)
noexcept {
174 const int v =
static_cast<int>(l);
175 const int floor = FASTMM_MIN_LOG_LEVEL;
180concept EnumWithToString = std::is_enum_v<E> &&
requires(E e) {
181 {
to_string(e) } -> std::convertible_to<std::string_view>;
184concept StringLike = std::convertible_to<const T&, std::string_view>;
186struct IsSecret : std::false_type {};
188struct IsSecret<
Secret<T>> : std::true_type {};
190struct IsFixed : std::false_type {};
192struct IsFixed<Fixed<Tag>> : std::true_type {};
196struct LogFmtTypeImpl {
197 using D = std::remove_cvref_t<T>;
198 using type = std::conditional_t<
199 std::is_same_v<D, bool>,
202 std::is_same_v<D, char>,
205 std::is_integral_v<D>,
206 std::conditional_t<std::is_signed_v<D>, std::int64_t, std::uint64_t>,
208 std::is_floating_point_v<D>,
211 IsFixed<D>::value || IsSecret<D>::value || StringLike<D> ||
214 std::conditional_t<std::is_enum_v<D>,
216 std::conditional_t<std::is_same_v<D, Timestamp> ||
217 std::is_same_v<D, Duration>,
220 static_assert(!std::is_void_v<type>,
"unsupported log argument type");
223using LogFmtType =
typename LogFmtTypeImpl<T>::type;
230 FASTMM_FORCE_INLINE
bool put_scalar(
LogArgType t, std::uint64_t bits)
noexcept {
231 if (end - p < 9)
return false;
232 *p++ =
static_cast<std::uint8_t
>(t);
233 std::memcpy(p, &bits, 8);
238 FASTMM_FORCE_INLINE
bool put_str(
LogArgType t, std::string_view s)
noexcept {
239 const std::size_t room =
static_cast<std::size_t
>(end - p);
240 if (room < 2)
return false;
242 if (len > room - 2) len = room - 2;
243 *p++ =
static_cast<std::uint8_t
>(t);
244 *p++ =
static_cast<std::uint8_t
>(len);
245 if (len != 0) std::memcpy(p, s.data(), len);
246 if (len < s.size() && len >= 3) std::memcpy(p + len - 3,
"...", 3);
254FASTMM_FORCE_INLINE
bool pack_arg(ArgPacker& pk,
const T& v)
noexcept {
255 using D = std::remove_cvref_t<T>;
256 if constexpr (std::is_same_v<D, bool>) {
258 }
else if constexpr (std::is_same_v<D, char>) {
260 }
else if constexpr (std::is_integral_v<D>) {
261 if constexpr (std::is_signed_v<D>) {
263 static_cast<std::uint64_t
>(
static_cast<std::int64_t
>(v)));
267 }
else if constexpr (std::is_floating_point_v<D>) {
268 const double d =
static_cast<double>(v);
269 std::uint64_t bits = 0;
270 std::memcpy(&bits, &d, 8);
272 }
else if constexpr (std::is_same_v<D, Price>) {
274 }
else if constexpr (std::is_same_v<D, Qty>) {
276 }
else if constexpr (std::is_same_v<D, Notional>) {
278 }
else if constexpr (IsSecret<D>::value) {
280 }
else if constexpr (EnumWithToString<D>) {
282 }
else if constexpr (std::is_enum_v<D>) {
284 static_cast<std::uint64_t
>(
285 static_cast<std::int64_t
>(
static_cast<std::underlying_type_t<D>
>(v))));
286 }
else if constexpr (std::is_same_v<D, Timestamp> || std::is_same_v<D, Duration>) {
287 return pk.put_scalar(
LogArgType::Int,
static_cast<std::uint64_t
>(v.ns));
288 }
else if constexpr (StringLike<D>) {
291 static_assert(
sizeof(D) == 0,
"unsupported log argument type");
296template <
class... Args>
297FASTMM_FORCE_INLINE
void log_emit(
const LogDescriptor& d,
const Args&... args)
noexcept {
299 if (FASTMM_UNLIKELY(ring ==
nullptr)) {
301 if (ring ==
nullptr) {
306 LogRecord* r = ring->try_reserve();
307 if (FASTMM_UNLIKELY(r ==
nullptr)) {
312 r->ts_ns = t_log_clock !=
nullptr ? t_log_clock->
now().
ns :
wall_now().
ns;
315 ArgPacker pk{r->args, r->args +
sizeof(r->args)};
316 (
static_cast<void>(pack_arg(pk, args)), ...);
318 r->used =
static_cast<std::uint8_t
>(pk.p - r->args);
331 detail::t_log_clock = &clock;
346 LogRunScope() noexcept : prev_(detail::t_log_run) {}
347 explicit LogRunScope(std::size_t index) noexcept : prev_(detail::t_log_run) {
348 detail::t_log_run =
static_cast<std::uint32_t
>(index + 1);
361 detail::t_log_clock = &clock;
362 detail::t_log_run =
static_cast<std::uint32_t
>(index + 1);
369#define FASTMM_LOG_IMPL(lvl, fmt_str, ...) \
371 if constexpr (::fastmm::detail::log_compiled(lvl)) { \
372 if (::fastmm::log_enabled(lvl)) { \
373 static constexpr ::fastmm::LogDescriptor fastmm_log_desc_{ \
374 fmt_str, __FILE__, __LINE__, lvl}; \
375 [&](const auto&... fastmm_a_) { \
377 ::fmt::format_string<::fastmm::detail::LogFmtType<decltype(fastmm_a_)>...>( \
379 ::fastmm::detail::log_emit(fastmm_log_desc_, fastmm_a_...); \
385#define FASTMM_LOG_TRACE(fmt_str, ...) \
386 FASTMM_LOG_IMPL(::fastmm::LogLevel::Trace, fmt_str __VA_OPT__(, ) __VA_ARGS__)
387#define FASTMM_LOG_DEBUG(fmt_str, ...) \
388 FASTMM_LOG_IMPL(::fastmm::LogLevel::Debug, fmt_str __VA_OPT__(, ) __VA_ARGS__)
389#define FASTMM_LOG_INFO(fmt_str, ...) \
390 FASTMM_LOG_IMPL(::fastmm::LogLevel::Info, fmt_str __VA_OPT__(, ) __VA_ARGS__)
391#define FASTMM_LOG_WARN(fmt_str, ...) \
392 FASTMM_LOG_IMPL(::fastmm::LogLevel::Warn, fmt_str __VA_OPT__(, ) __VA_ARGS__)
393#define FASTMM_LOG_ERROR(fmt_str, ...) \
394 FASTMM_LOG_IMPL(::fastmm::LogLevel::Error, fmt_str __VA_OPT__(, ) __VA_ARGS__)
While one is alive, the lines the thread logs carry the time of clock instead of the wall clock: a ba...
Definition log.hpp:328
LogClockScope() noexcept
Keeps the thread's clock, and restores it at the end (log_switch_run() changes it).
Definition log.hpp:331
~LogClockScope()
Definition log.hpp:335
LogClockScope & operator=(const LogClockScope &)=delete
While one is alive, the lines the thread logs name run index of a sweep ("\#index",...
Definition log.hpp:345
~LogRunScope()
Definition log.hpp:352
LogRunScope & operator=(const LogRunScope &)=delete
LogRunScope() noexcept
Keeps the thread's run, and restores it at the end (log_switch_run() changes it).
Definition log.hpp:348
Process-wide registry of per-thread rings + the sink thread. Implemented in log_sink....
Definition log.hpp:98
static Logger & instance() noexcept
LogLevel level() const noexcept
Definition log.hpp:103
std::size_t thread_slots() const noexcept
Number of rings allocated so far; reused rings are not counted again.
void start(std::FILE *out=stderr, LogLevel mirror_level=LogLevel::Warn)
Sink control.
bool start_file(const std::string &path, LogLevel mirror_level=LogLevel::Warn)
Opens path for appending and starts the sink on it.
void request_reopen() noexcept
Log rotation: the sink drains what was logged so far into the file it has open, then opens the path a...
void detach_current_thread() noexcept
Returns the calling thread's ring to the pool.
std::uint64_t written() const noexcept
Definition log.hpp:142
void stop()
drains everything then joins the sink thread (and closes a start_file() file)
std::uint64_t dropped() const noexcept
Definition log.hpp:138
void set_level(LogLevel lvl) noexcept
Definition log.hpp:102
void flush()
Drains all rings and fflush()es. Works with or without a running sink thread.
void note_drop() noexcept
Definition log.hpp:141
bool running() const noexcept
Definition log.hpp:134
LogRing * attach_current_thread()
Gives the calling thread a ring: a drained ring released by an exited thread if one exists,...
bool to_file() const noexcept
The sink writes to a stream other than stderr (a log file).
Definition log.hpp:136
static void format_record(const LogRecord &r, std::string &out)
Formats one record into out (appends). Public for tests/tools.
Deterministic clock for sim/backtest/replay. 1 cycle == 1 ns.
Definition time.hpp:338
Timestamp now() const noexcept
Definition time.hpp:342
Fixed<Tag>: int64 fixed point with a global 1e-8 scale (ADR-0001).
Definition strategy.hpp:23
SpscRing< LogRecord, kLogRingSize > LogRing
Definition log.hpp:79
LogLevel
Definition enums.hpp:688
Secret< std::decay_t< T > > secret(T &&v)
Definition log.hpp:87
constexpr std::size_t kLogRecordBytes
Definition log.hpp:62
Timestamp wall_now() noexcept
CLOCK_REALTIME via vDSO (~20 ns). Implemented in src/core/time.cpp.
std::atomic< std::uint8_t > g_log_level
Global level (runtime); FASTMM_MIN_LOG_LEVEL is the compile-time floor.
Definition log.hpp:92
FASTMM_FORCE_INLINE bool log_enabled(LogLevel lvl) noexcept
Definition log.hpp:93
constexpr std::size_t kLogMaxStrBytes
Definition log.hpp:63
LogArgType
Definition log.hpp:49
constexpr std::size_t kLogRingSize
records per thread (2 MiB)
Definition log.hpp:64
constexpr std::string_view to_string(ParamType t) noexcept
Definition params.hpp:63
void log_switch_run(const SimClock &clock, std::size_t index) noexcept
Within a LogClockScope and a LogRunScope, the thread's lines from now on carry the time of clock and ...
Definition log.hpp:362
constexpr std::size_t kLogMaxThreads
Definition log.hpp:65
Strongly typed identifiers.
LogLevel level
Definition log.hpp:46
const char * file
Definition log.hpp:44
const char * fmt
Definition log.hpp:43
std::uint32_t line
Definition log.hpp:45
std::uint32_t run
LogRunScope: 1 + the run's index in its sweep, 0 outside one.
Definition log.hpp:71
std::uint8_t args[kLogRecordBytes - 28]
Definition log.hpp:75
std::int64_t ts_ns
Definition log.hpp:69
std::uint32_t tid
Definition log.hpp:70
std::uint8_t used
Definition log.hpp:73
std::uint8_t pad_[2]
Definition log.hpp:74
const LogDescriptor * desc
Definition log.hpp:68
std::uint8_t nargs
Definition log.hpp:72
Wrap a value to have it rendered as "***".
Definition log.hpp:83
T value
Definition log.hpp:84
std::int64_t ns
Definition time.hpp:46