BetaTrader
A HFT Eco-System
Loading...
Searching...
No Matches
Runbook.h
Go to the documentation of this file.
1#pragma once
2
3#include "spdlog/fmt/fmt.h"
4#include "spdlog/spdlog.h" // needed for get_default()
5#include <stdexcept>
6#include <string>
7#include "logging/Logger.h"
8#include <string_view>
9
10namespace runbook {
12 public:
13 consteval ErrorDefinition(const std::string_view code,
14 const std::string_view what,
15 const std::string_view how)
16 : mCode(code), mWhatWentWrongString(what), mHowToFixString(how)
17 {}
18
19 [[nodiscard]] std::string_view getCode() const { return mCode; }
20 [[nodiscard]] std::string_view getWhat() const
21 {
23 }
24 [[nodiscard]] std::string_view getHow() const
25 {
26 return mHowToFixString;
27 }
28
29 private:
30 std::string_view mCode;
31 std::string_view mWhatWentWrongString;
32 std::string_view mHowToFixString;
33 };
34
38 template<typename... Args>
39 static std::string FormatRunbookLog(const ErrorDefinition& errorDef,
40 const std::string_view& fmt_str,
41 Args&&... args)
42 {
43 std::string user_msg = fmt::format(fmt::runtime(fmt_str),
44 std::forward<Args>(args)...);
45
46 std::string_view code = errorDef.getCode();
47 std::string_view what = errorDef.getWhat();
48 std::string_view how = errorDef.getHow();
49
50 return fmt::format("[{}] {}\n"
51 " [?] What Went Wrong: {}\n"
52 " [!] How to Fix: {}",
53 code, user_msg, what, how);
54 }
55} // namespace runbook
56
57// --- REDEFINE LOGGING MACROS ---
58
59#ifdef LOG_ERROR
60#undef LOG_ERROR
61#endif
62
63#ifdef LOG_CRITICAL
64#undef LOG_CRITICAL
65#endif
66
68#define LOG_ERROR(code, ...) \
69 do { \
70 if (spdlog::default_logger()->should_log(spdlog::level::err)) { \
71 spdlog::source_loc loc{__FILE__, __LINE__, __FUNCTION__}; \
72 std::string msg = runbook::FormatRunbookLog(code, __VA_ARGS__); \
73 spdlog::default_logger()->log(loc, spdlog::level::err, \
74 spdlog::string_view_t(msg)); \
75 } \
76 } \
77 while (0)
78
80#define LOG_CRITICAL(code, ...) \
81 do { \
82 if (spdlog::default_logger()->should_log(spdlog::level::critical)) { \
83 spdlog::source_loc loc{__FILE__, __LINE__, __FUNCTION__}; \
84 std::string msg = runbook::FormatRunbookLog(code, __VA_ARGS__); \
85 spdlog::default_logger()->log(loc, spdlog::level::critical, \
86 spdlog::string_view_t(msg)); \
87 } \
88 } \
89 while (0)
Definition Runbook.h:11
std::string_view mCode
Definition Runbook.h:30
std::string_view mWhatWentWrongString
Definition Runbook.h:31
consteval ErrorDefinition(const std::string_view code, const std::string_view what, const std::string_view how)
Definition Runbook.h:13
std::string_view getWhat() const
Definition Runbook.h:20
std::string_view mHowToFixString
Definition Runbook.h:32
std::string_view getCode() const
Definition Runbook.h:19
std::string_view getHow() const
Definition Runbook.h:24
Definition Runbook.h:10
static std::string FormatRunbookLog(const ErrorDefinition &errorDef, const std::string_view &fmt_str, Args &&... args)
Internal helper function to format the final log message.
Definition Runbook.h:39