Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
10#if defined(MOMENTUM_WITH_XR_LOGGER)
11
16#ifndef DEFAULT_LOG_CHANNEL
18#define DEFAULT_LOG_CHANNEL MOMENTUM_LOG_CHANNEL
19#endif
20
21#include <logging/Log.h>
22
26#define MT_LOGT(...) XR_LOGT(__VA_ARGS__)
27#define MT_LOGD(...) XR_LOGD(__VA_ARGS__)
28#define MT_LOGI(...) XR_LOGI(__VA_ARGS__)
29#define MT_LOGW(...) XR_LOGW(__VA_ARGS__)
30#define MT_LOGE(...) XR_LOGE(__VA_ARGS__)
32
35#define MT_LOGT_IF(condition, ...) XR_LOGT_IF(condition, __VA_ARGS__)
36#define MT_LOGD_IF(condition, ...) XR_LOGD_IF(condition, __VA_ARGS__)
37#define MT_LOGI_IF(condition, ...) XR_LOGI_IF(condition, __VA_ARGS__)
38#define MT_LOGW_IF(condition, ...) XR_LOGW_IF(condition, __VA_ARGS__)
39#define MT_LOGE_IF(condition, ...) XR_LOGE_IF(condition, __VA_ARGS__)
41
44#define MT_LOGT_ONCE(...) XR_LOGT_ONCE(__VA_ARGS__)
45#define MT_LOGD_ONCE(...) XR_LOGD_ONCE(__VA_ARGS__)
46#define MT_LOGI_ONCE(...) XR_LOGI_ONCE(__VA_ARGS__)
47#define MT_LOGW_ONCE(...) XR_LOGW_ONCE(__VA_ARGS__)
48#define MT_LOGE_ONCE(...) XR_LOGE_ONCE(__VA_ARGS__)
50
53#define MT_LOGT_ONCE_IF(condition, ...) XR_LOGT_ONCE_IF(condition, __VA_ARGS__)
54#define MT_LOGD_ONCE_IF(condition, ...) XR_LOGD_ONCE_IF(condition, __VA_ARGS__)
55#define MT_LOGI_ONCE_IF(condition, ...) XR_LOGI_ONCE_IF(condition, __VA_ARGS__)
56#define MT_LOGW_ONCE_IF(condition, ...) XR_LOGW_ONCE_IF(condition, __VA_ARGS__)
57#define MT_LOGE_ONCE_IF(condition, ...) XR_LOGE_ONCE_IF(condition, __VA_ARGS__)
59
60#elif defined(MOMENTUM_WITH_SPDLOG)
61
62#include <spdlog/spdlog.h>
63
64#include <atomic>
65
69#define MT_LOGT(...) ::spdlog::trace(__VA_ARGS__)
70#define MT_LOGD(...) ::spdlog::debug(__VA_ARGS__)
71#define MT_LOGI(...) ::spdlog::info(__VA_ARGS__)
72#define MT_LOGW(...) ::spdlog::warn(__VA_ARGS__)
73#define MT_LOGE(...) ::spdlog::error(__VA_ARGS__)
75
78#define MT_LOGT_IF(condition, ...) \
79 do { \
80 if (condition) { \
81 MT_LOGT(__VA_ARGS__); \
82 } \
83 } while (0)
84#define MT_LOGD_IF(condition, ...) \
85 do { \
86 if (condition) { \
87 MT_LOGD(__VA_ARGS__); \
88 } \
89 } while (0)
90#define MT_LOGI_IF(condition, ...) \
91 do { \
92 if (condition) { \
93 MT_LOGI(__VA_ARGS__); \
94 } \
95 } while (0)
96#define MT_LOGW_IF(condition, ...) \
97 do { \
98 if (condition) { \
99 MT_LOGW(__VA_ARGS__); \
100 } \
101 } while (0)
102#define MT_LOGE_IF(condition, ...) \
103 do { \
104 if (condition) { \
105 MT_LOGE(__VA_ARGS__); \
106 } \
107 } while (0)
109
115#define MT_DETAIL_RUN_ONCE(runcode) \
116 do { \
117 static std::atomic<bool> codeRan(false); \
118 if (!codeRan) { \
119 bool expected = false; \
120 if (codeRan.compare_exchange_strong(expected, true)) { \
121 runcode; \
122 } \
123 } \
124 } while (0)
125
128#define MT_LOGT_ONCE(...) MT_DETAIL_RUN_ONCE(MT_LOGT(__VA_ARGS__))
129#define MT_LOGD_ONCE(...) MT_DETAIL_RUN_ONCE(MT_LOGD(__VA_ARGS__))
130#define MT_LOGI_ONCE(...) MT_DETAIL_RUN_ONCE(MT_LOGI(__VA_ARGS__))
131#define MT_LOGW_ONCE(...) MT_DETAIL_RUN_ONCE(MT_LOGW(__VA_ARGS__))
132#define MT_LOGE_ONCE(...) MT_DETAIL_RUN_ONCE(MT_LOGE(__VA_ARGS__))
134
140#define MT_LOGT_ONCE_IF(condition, ...) \
141 do { \
142 if (condition) { \
143 MT_LOGT_ONCE(__VA_ARGS__); \
144 } \
145 } while (0)
146#define MT_LOGD_ONCE_IF(condition, ...) \
147 do { \
148 if (condition) { \
149 MT_LOGD_ONCE(__VA_ARGS__); \
150 } \
151 } while (0)
152#define MT_LOGI_ONCE_IF(condition, ...) \
153 do { \
154 if (condition) { \
155 MT_LOGI_ONCE(__VA_ARGS__); \
156 } \
157 } while (0)
158#define MT_LOGW_ONCE_IF(condition, ...) \
159 do { \
160 if (condition) { \
161 MT_LOGW_ONCE(__VA_ARGS__); \
162 } \
163 } while (0)
164#define MT_LOGE_ONCE_IF(condition, ...) \
165 do { \
166 if (condition) { \
167 MT_LOGE_ONCE(__VA_ARGS__); \
168 } \
169 } while (0)
171
172#else
173
174// Fallback: no logging backend is configured. All MT_LOG* macros expand to nothing, so call-site
175// arguments are not evaluated. Callers must not rely on side effects in log arguments.
176
177#define MT_LOGT(...)
178#define MT_LOGD(...)
179#define MT_LOGI(...)
180#define MT_LOGW(...)
181#define MT_LOGE(...)
182
183#define MT_LOGT_IF(condition, ...)
184#define MT_LOGD_IF(condition, ...)
185#define MT_LOGI_IF(condition, ...)
186#define MT_LOGW_IF(condition, ...)
187#define MT_LOGE_IF(condition, ...)
188
189#define MT_LOGT_ONCE(...)
190#define MT_LOGD_ONCE(...)
191#define MT_LOGI_ONCE(...)
192#define MT_LOGW_ONCE(...)
193#define MT_LOGE_ONCE(...)
194
195#define MT_LOGT_ONCE_IF(condition, ...)
196#define MT_LOGD_ONCE_IF(condition, ...)
197#define MT_LOGI_ONCE_IF(condition, ...)
198#define MT_LOGW_ONCE_IF(condition, ...)
199#define MT_LOGE_ONCE_IF(condition, ...)
200
201#endif
202
204
205#include <map>
206#include <string>
207
208namespace momentum {
209
220
224[[nodiscard]] std::map<std::string, LogLevel> logLevelMap();
225
230void setLogLevel(LogLevel level);
231
236void setLogLevel(const std::string& levelStr);
237
240[[nodiscard]] LogLevel getLogLevel();
241
242} // namespace momentum
LogLevel
Logging levels in ascending order of verbosity.
Definition log.h:212
void setLogLevel(LogLevel level)
Sets the global log threshold; messages below level are filtered out.
Definition log.cpp:114
std::map< std::string, LogLevel > logLevelMap()
Returns the canonical mapping from human-readable level names ("Disabled", "Error",...
Definition log.cpp:104
LogLevel getLogLevel()
Returns the current global log threshold.
Definition log.cpp:129