YARP
Yet Another Robot Platform
LogComponent.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_OS_LOGCOMPONENT_H
7 #define YARP_OS_LOGCOMPONENT_H
8 
9 #include <yarp/os/api.h>
10 #include <yarp/os/Log.h>
11 
12 #include <atomic>
13 
14 namespace yarp {
15 namespace os {
16 
18 {
19 public:
22 
23  LogComponent(const char* name,
24  LogType minimumPrintLevel = yarp::os::Log::minimumPrintLevel(),
25  LogType minimumForwardLevel = yarp::os::Log::minimumForwardLevel(),
26  LogCallback printCallback = yarp::os::Log::printCallback(),
27  LogCallback forwardCallback = yarp::os::Log::forwardCallback());
28 
29  LogComponent(const LogComponent&) = delete;
30  LogComponent(LogComponent&&) noexcept = delete;
31  LogComponent& operator=(const LogComponent&) = delete;
32  LogComponent& operator=(LogComponent&&) noexcept = delete;
33 
34  ~LogComponent() = default;
35 
36  LogCallback printCallback(yarp::os::Log::LogType t) const;
37  LogCallback forwardCallback(yarp::os::Log::LogType t) const;
38 
39  constexpr const char* name() const { return m_name; }
40  constexpr operator bool() const { return m_name != nullptr; }
41 
42 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 
44 #ifndef YARP_NO_DEPRECATED // Since YARP 3.4
45  // These methods are currently used to support setVerbose and setQuiet
46  // around the code, they should not be used in new code
47  void setMinimumPrintLevel(LogType minimumPrintLevel);
48  void setMinimumForwardLevel(LogType minimumForwardLevel);
49 #endif // YARP_NO_DEPRECATED
50 
51 private:
52  const char* m_name { nullptr };
53 
54  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_tracePrintEnabled;
55  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_debugPrintEnabled;
56  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_infoPrintEnabled;
57  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_warningPrintEnabled;
58  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_errorPrintEnabled;
59  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_fatalPrintEnabled;
60  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_traceForwardEnabled;
61  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_debugForwardEnabled;
62  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_infoForwardEnabled;
63  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_warningForwardEnabled;
64  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_errorForwardEnabled;
65  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) m_fatalForwardEnabled;
66 
67  LogCallback m_printCallback { nullptr };
68  LogCallback m_forwardCallback { nullptr };
69 
70 #endif // DOXYGEN_SHOULD_SKIP_THIS
71 
72 };
73 
74 #define YARP_DECLARE_LOG_COMPONENT(name) \
75  extern const yarp::os::LogComponent& name();
76 
77 #define YARP_LOG_COMPONENT(name, ...) \
78  const yarp::os::LogComponent& name() \
79  { \
80  static const yarp::os::LogComponent component(__VA_ARGS__); \
81  return component; \
82  }
83 
84 #ifndef NDEBUG
85 # define yCTrace(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, nullptr, component()).trace(__VA_ARGS__)
86 # define yCTraceOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_ONCE_CALLBACK, component()).trace(__VA_ARGS__)
87 # define yCTraceThreadOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADONCE_CALLBACK, component()).trace(__VA_ARGS__)
88 # define yCTraceThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THROTTLE_CALLBACK(period), component()).trace(__VA_ARGS__)
89 # define yCTraceThreadThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADTHROTTLE_CALLBACK(period), component()).trace(__VA_ARGS__)
90 # define yCTraceExternalTime(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, nullptr, component()).trace(__VA_ARGS__)
91 # define yCTraceExternalTimeOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_ONCE_CALLBACK, component()).trace(__VA_ARGS__)
92 # define yCTraceExternalTimeThreadOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADONCE_CALLBACK, component()).trace(__VA_ARGS__)
93 # define yCTraceExternalTimeThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THROTTLE_CALLBACK(period), component()).trace(__VA_ARGS__)
94 # define yCTraceExternalTimeThreadThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADTHROTTLE_CALLBACK(period), component()).trace(__VA_ARGS__)
95 #else
96 # define yCTrace(component, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
97 # define yCTraceOnce(component, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
98 # define yCTraceThreadOnce(component, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
99 # define yCTraceThrottle(component, period, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
100 # define yCTraceThreadThrottle(component, period, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
101 # define yCTraceExternalTime(component, externaltime, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
102 # define yCTraceExternalTimeOnce(component, externaltime, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
103 # define yCTraceExternalTimeThreadOnce(component, externaltime, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
104 # define yCTraceExternalTimeThrottle(component, externaltime, period, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
105 # define yCTraceExternalTimeThreadThrottle(component, externaltime, period, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
106 #endif
107 
108 #ifndef YARP_NO_DEBUG_OUTPUT
109 # define yCDebug(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, nullptr, component()).debug(__VA_ARGS__)
110 # define yCDebugOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_ONCE_CALLBACK, component()).debug(__VA_ARGS__)
111 # define yCDebugThreadOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADONCE_CALLBACK, component()).debug(__VA_ARGS__)
112 # define yCDebugThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THROTTLE_CALLBACK(period), component()).debug(__VA_ARGS__)
113 # define yCDebugThreadThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADTHROTTLE_CALLBACK(period), component()).debug(__VA_ARGS__)
114 # define yCDebugExternalTime(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, nullptr, component()).debug(__VA_ARGS__)
115 # define yCDebugExternalTimeOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_ONCE_CALLBACK, component()).debug(__VA_ARGS__)
116 # define yCDebugExternalTimeThreadOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADONCE_CALLBACK, component()).debug(__VA_ARGS__)
117 # define yCDebugExternalTimeThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THROTTLE_CALLBACK(period), component()).debug(__VA_ARGS__)
118 # define yCDebugExternalTimeThreadThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADTHROTTLE_CALLBACK(period), component()).debug(__VA_ARGS__)
119 #else
120 # define yCDebug(component, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
121 # define yCDebugOnce(component, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
122 # define yCDebugThreadOnce(component, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
123 # define yCDebugThrottle(component, period, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
124 # define yCDebugThreadThrottle(component, period, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
125 # define yCDebugExternalTime(component, externaltime, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
126 # define yCDebugExternalTimeOnce(component, externaltime, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
127 # define yCDebugExternalTimeThreadOnce(component, externaltime, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
128 # define yCDebugExternalTimeThrottle(component, externaltime, period, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
129 # define yCDebugExternalTimeThreadThrottle(component, externaltime, period, ...) YARP_UNUSED(component()); yarp::os::Log::nolog(__VA_ARGS__)
130 #endif
131 
132 #define yCInfo(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, nullptr, component()).info(__VA_ARGS__)
133 #define yCInfoOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_ONCE_CALLBACK, component()).info(__VA_ARGS__)
134 #define yCInfoThreadOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADONCE_CALLBACK, component()).info(__VA_ARGS__)
135 #define yCInfoThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THROTTLE_CALLBACK(period), component()).info(__VA_ARGS__)
136 #define yCInfoThreadThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADTHROTTLE_CALLBACK(period), component()).info(__VA_ARGS__)
137 #define yCInfoExternalTime(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, nullptr, component()).info(__VA_ARGS__)
138 #define yCInfoExternalTimeOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_ONCE_CALLBACK, component()).info(__VA_ARGS__)
139 #define yCInfoExternalTimeThreadOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADONCE_CALLBACK, component()).info(__VA_ARGS__)
140 #define yCInfoExternalTimeThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THROTTLE_CALLBACK(period), component()).info(__VA_ARGS__)
141 #define yCInfoExternalTimeThreadThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADTHROTTLE_CALLBACK(period), component()).info(__VA_ARGS__)
142 
143 #define yCWarning(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, nullptr, component()).warning(__VA_ARGS__)
144 #define yCWarningOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_ONCE_CALLBACK, component()).warning(__VA_ARGS__)
145 #define yCWarningThreadOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADONCE_CALLBACK, component()).warning(__VA_ARGS__)
146 #define yCWarningThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THROTTLE_CALLBACK(period), component()).warning(__VA_ARGS__)
147 #define yCWarningThreadThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADTHROTTLE_CALLBACK(period), component()).warning(__VA_ARGS__)
148 #define yCWarningExternalTime(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, nullptr, component()).warning(__VA_ARGS__)
149 #define yCWarningExternalTimeOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_ONCE_CALLBACK, component()).warning(__VA_ARGS__)
150 #define yCWarningExternalTimeThreadOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADONCE_CALLBACK, component()).warning(__VA_ARGS__)
151 #define yCWarningExternalTimeThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THROTTLE_CALLBACK(period), component()).warning(__VA_ARGS__)
152 #define yCWarningExternalTimeThreadThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADTHROTTLE_CALLBACK(period), component()).warning(__VA_ARGS__)
153 
154 #define yCError(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, nullptr, component()).error(__VA_ARGS__)
155 #define yCErrorOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_ONCE_CALLBACK, component()).error(__VA_ARGS__)
156 #define yCErrorThreadOnce(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADONCE_CALLBACK, component()).error(__VA_ARGS__)
157 #define yCErrorThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THROTTLE_CALLBACK(period), component()).error(__VA_ARGS__)
158 #define yCErrorThreadThrottle(component, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, YARP_THREADTHROTTLE_CALLBACK(period), component()).error(__VA_ARGS__)
159 #define yCErrorExternalTime(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, nullptr, component()).error(__VA_ARGS__)
160 #define yCErrorExternalTimeOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_ONCE_CALLBACK, component()).error(__VA_ARGS__)
161 #define yCErrorExternalTimeThreadOnce(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADONCE_CALLBACK, component()).error(__VA_ARGS__)
162 #define yCErrorExternalTimeThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THROTTLE_CALLBACK(period), component()).error(__VA_ARGS__)
163 #define yCErrorExternalTimeThreadThrottle(component, externaltime, period, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, YARP_THREADTHROTTLE_CALLBACK(period), component()).error(__VA_ARGS__)
164 
165 #define yCFatal(component, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, nullptr, component()).fatal(__VA_ARGS__)
166 #define yCFatalExternalTime(component, externaltime, ...) yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__, externaltime, nullptr, component()).fatal(__VA_ARGS__)
167 
168 #ifndef NDEBUG
169 # define yCAssert(component, x) \
170  if (!(x)) { \
171  yCFatal(component, "Assertion failure at %s:%d (%s)", __FILE__, __LINE__, #x); \
172  }
173 # define yCAssertExternalTime(component, externaltime, x) \
174  if (!(x)) { \
175  yCFatalExternalTime(component, externaltime, "Assertion failure at %s:%d (%s)", __FILE__, __LINE__, #x); \
176  }
177 #else
178 # define yCAssert(component, x) { YARP_UNUSED(component()); }
179 # define yCAssertExternalTime(component, externaltime, x) { YARP_UNUSED(component()); YARP_UNUSED(externaltime); }
180 #endif
181 
182 
183 } // namespace yarp
184 } // namespace os
185 
186 #endif // YARP_OS_LOGCOMPONENT_H
float t
LogComponent(LogComponent &&) noexcept=delete
LogComponent(const LogComponent &)=delete
yarp::os::Log::LogCallback LogCallback
Definition: LogComponent.h:20
static LogCallback printCallback()
Get current print callback.
Definition: Log.cpp:849
static LogType minimumPrintLevel()
Get current minimum print level.
Definition: Log.cpp:802
static LogCallback forwardCallback()
Get current forward callback (or nullptr if forwarding is not enabled)
Definition: Log.cpp:871
static LogType minimumForwardLevel()
Get current minimum forward level (or LogTypeReserved if forwarding is not enabled)
Definition: Log.cpp:822
void(*)(yarp::os::Log::LogType type, const char *msg, const char *file, const unsigned int line, const char *func, double systemtime, double networktime, double externaltime, const char *comp_name) LogCallback
Definition: Log.h:105
yarp::os::Log::LogCallback LogCallback
Definition: LogComponent.cpp:9
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:336
#define YARP_os_API
Definition: api.h:18