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