-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctme_debug_logger.h
92 lines (78 loc) · 2.33 KB
/
ctme_debug_logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* CTME @link https://github.com/qoala101/ctme @endlink
* @author Volodymyr Hromakov (4y5t6r@gmail.com)
* @copyright Copyright (c) 2023, MIT License
*/
#ifndef VH_CTME_DEBUG_LOGGER_H_
#define VH_CTME_DEBUG_LOGGER_H_
#ifdef CTME_DEBUG
#include <iostream>
#include <map>
#include <string>
#include "ctme_concepts.h" // IWYU pragma: keep
namespace ctme::debug {
namespace detail {
auto GetAddress [[nodiscard]] (const auto &object) {
// NOLINTNEXTLINE(*-reinterpret-cast)
return reinterpret_cast<int64_t>(std::addressof(object));
}
} // namespace detail
/**
* @brief Used to compose and visualize the order of operations on matrices.
* Output example:
* Evaluating cell (0, 0):
* m1[0][2] * m2[2][0] +
* m1[0][1] * m2[1][0] +
* m1[0][0] * m2[0][0
*/
class Logger {
public:
/**
* @brief Gives the logger instance.
*/
static auto Instance [[nodiscard]] () -> auto & {
static auto instance = Logger{};
return instance;
}
/**
* @brief Matrix would have the specified name when logged.
* @remark Address of container is used by default.
*/
void RegisterMatName(const MatValues auto &mat, std::string_view name) {
mat_names_[detail::GetAddress(mat)] = name;
}
/**
* @brief Prints inputs.
*/
// NOLINTNEXTLINE(*-convert-member-functions-to-static)
auto Print [[nodiscard]] () const -> auto & { return std::cout; }
/**
* @brief Prints matrix cell being read.
*/
void PrintCell(const MatValues auto &mat, unsigned row_index,
unsigned column_index) const {
Print() << GetMatName(mat) << "[" << row_index << "][" << column_index
<< "]";
}
/**
* @brief Prints the cell being evaluated.
*/
void PrintEvaluatingCell(unsigned row_index, unsigned column_index) const {
Print() << "\n\nEvaluating cell (" << row_index << ", " << column_index
<< "):\n";
}
private:
auto GetMatName [[nodiscard]] (const MatValues auto &mat) const {
const auto address = detail::GetAddress(mat);
const auto registered_name = mat_names_.find(address);
if (registered_name == mat_names_.end()) {
return std::to_string(address);
}
return registered_name->second;
}
std::string message_{};
std::map<int64_t, std::string> mat_names_{};
};
} // namespace ctme::debug
#endif // CTME_DEBUG
#endif // VH_CTME_DEBUG_LOGGER_H_