stub_analytics_plugin  1.6
Network Optix Video Analytics SDK
debug.h
Go to the documentation of this file.
1 // Copyright 2018-present Network Optix, Inc.
2 #pragma once
3 
11 #include <iostream>
12 #include <cstdint>
13 #include <functional>
14 #include <sstream>
15 #include <memory>
16 
17 #include <nx/kit/utils.h>
18 
19 #if !defined(NX_KIT_API)
20  #define NX_KIT_API /*empty*/
21 #endif
22 
23 namespace nx {
24 namespace kit {
25 namespace debug {
26 
27 //-------------------------------------------------------------------------------------------------
28 // Tools
29 
31 NX_KIT_API char pathSeparator();
32 
33 NX_KIT_API size_t commonPrefixSize(const std::string& s1, const std::string& s2);
34 
39 NX_KIT_API const char* relativeSrcFilename(const char* file);
40 
41 NX_KIT_API std::string fileBaseNameWithoutExt(const char* file);
42 
43 //-------------------------------------------------------------------------------------------------
44 // Output
45 
47 #if defined(NX_PRINT_TO_QDEBUG)
48  #define NX_DEBUG_STREAM qDebug().nospace().noquote()
49  #define NX_DEBUG_ENDL /*empty*/
50  static inline QDebug operator<<(QDebug d, const std::string& s)
51  {
52  return d << QString::fromStdString(s);
53  }
54 #endif
55 
56 #if !defined(NX_DEBUG_INI)
57 
58  #define NX_DEBUG_INI ini().
59 #endif
60 
61 #if !defined(NX_DEBUG_ENABLE_OUTPUT)
62 
63  #define NX_DEBUG_ENABLE_OUTPUT NX_DEBUG_INI enableOutput
64 #endif
65 
66 #if !defined(NX_PRINT_PREFIX)
67 
68  #define NX_PRINT_PREFIX nx::kit::debug::detail::printPrefix(__FILE__)
69 #endif
70 
71 #if !defined(NX_DEBUG_STREAM)
72 
73  #define NX_DEBUG_STREAM *nx::kit::debug::stream()
74 #endif
75 
76 #if !defined(NX_DEBUG_ENDL)
77 
78  #define NX_DEBUG_ENDL << std::endl
79 #endif
80 
85 NX_KIT_API std::ostream*& stream();
86 
87 #if !defined(NX_PRINT)
88 
92  #define NX_PRINT /* << args... */ \
93  /* Allocate a temp value, which prints endl in its destructor, in the "<<" expression. */ \
94  ( []() { struct Endl { ~Endl() { NX_DEBUG_STREAM NX_DEBUG_ENDL; } }; \
95  return std::make_shared<Endl>(); }() ) /*operator,*/, \
96  NX_DEBUG_STREAM << NX_PRINT_PREFIX
97 #endif
98 
102 #define NX_OUTPUT /* << args... */ \
103  if (!(NX_DEBUG_ENABLE_OUTPUT)) {} else NX_PRINT
104 
105 //-------------------------------------------------------------------------------------------------
106 // Assertions
107 
115 #define NX_KIT_ASSERT(...) \
116  NX_KIT_DEBUG_DETAIL_MSVC_EXPAND(NX_KIT_DEBUG_DETAIL_GET_3RD_ARG( \
117  __VA_ARGS__, NX_KIT_DEBUG_DETAIL_ASSERT2, NX_KIT_DEBUG_DETAIL_ASSERT1, \
118  /* Helps to generate a reasonable compiler error. */ args_required)(__VA_ARGS__))
119 
120 //-------------------------------------------------------------------------------------------------
121 // Print info
122 
126 #define LL \
127  NX_PRINT << "####### LL line " << __LINE__ \
128  << NX_KIT_DEBUG_DETAIL_THREAD_ID() \
129  << ", file " << nx::kit::debug::relativeSrcFilename(__FILE__);
130 
134 #define NX_PRINT_VALUE(VALUE) \
135  NX_PRINT << "####### " #VALUE ": " << nx::kit::utils::toString((VALUE))
136 
140 #define NX_PRINT_HEX_DUMP(CAPTION, BYTES, SIZE) \
141  nx::kit::debug::detail::printHexDump( \
142  NX_KIT_DEBUG_DETAIL_PRINT_FUNC, (CAPTION), (const char*) (BYTES), (int) (SIZE))
143 
144 //-------------------------------------------------------------------------------------------------
145 // Time
146 
147 #if !defined(NX_DEBUG_ENABLE_TIME)
148 
149  #define NX_DEBUG_ENABLE_TIME NX_DEBUG_INI enableTime
150 #endif
151 
155 #define NX_TIME_BEGIN(TAG) \
156  nx::kit::debug::detail::Timer nxTimer_##TAG( \
157  (NX_DEBUG_ENABLE_TIME), NX_KIT_DEBUG_DETAIL_PRINT_FUNC, #TAG)
158 
163 #define NX_TIME_MARK(TAG, MARK) do \
164 { \
165  if (NX_DEBUG_ENABLE_TIME) \
166  nxTimer_##TAG.mark((MARK)); \
167 } while (0)
168 
172 #define NX_TIME_END(TAG) do \
173 { \
174  if (NX_DEBUG_ENABLE_TIME) \
175  nxTimer_##TAG.finish(); \
176 } while (0)
177 
178 //-------------------------------------------------------------------------------------------------
179 // Fps
180 
181 #if !defined(NX_DEBUG_ENABLE_FPS)
182 
183  #define NX_DEBUG_ENABLE_FPS NX_DEBUG_INI enableFps
184 #endif
185 
190 #define NX_FPS(TAG, /*OPTIONAL_MARK*/...) do \
191 { \
192  if (NX_KIT_DEBUG_DETAIL_CONCAT(NX_DEBUG_ENABLE_FPS, TAG)) \
193  { \
194  static nx::kit::debug::detail::Fps fps( \
195  NX_KIT_DEBUG_DETAIL_PRINT_FUNC, #TAG); \
196  fps.mark(__VA_ARGS__); \
197  } \
198 } while (0)
199 
200 //-------------------------------------------------------------------------------------------------
201 // Implementation
202 
207 #define NX_KIT_DEBUG_DETAIL_MSVC_EXPAND(ARG) ARG
208 
210 #define NX_KIT_DEBUG_DETAIL_GET_3RD_ARG(ARG1, ARG2, ARG3, ...) ARG3
211 
212 #define NX_KIT_DEBUG_DETAIL_ASSERT1(CONDITION) \
213  NX_KIT_DEBUG_DETAIL_ASSERT(CONDITION, "")
214 
215 #define NX_KIT_DEBUG_DETAIL_ASSERT2(CONDITION, MESSAGE) \
216  NX_KIT_DEBUG_DETAIL_ASSERT(CONDITION, MESSAGE)
217 
218 #define NX_KIT_DEBUG_DETAIL_ASSERT(CONDITION, MESSAGE) do \
219 { \
220  if (!(CONDITION)) \
221  { \
222  nx::kit::debug::detail::assertionFailed( \
223  NX_KIT_DEBUG_DETAIL_PRINT_FUNC, #CONDITION, (MESSAGE), __FILE__, __LINE__); \
224  } \
225 } while (0)
226 
227 #define NX_KIT_DEBUG_DETAIL_CONCAT(X, Y) NX_KIT_DEBUG_DETAIL_CONCAT2(X, Y)
228 #define NX_KIT_DEBUG_DETAIL_CONCAT2(X, Y) X##Y
229 
230 namespace detail {
231 
232 typedef std::function<void(const char*)> PrintFunc;
233 
234 #define NX_KIT_DEBUG_DETAIL_PRINT_FUNC [&](const char* message) { NX_PRINT << message; }
235 
237 NX_KIT_API std::string printPrefix(const char* file);
238 
239 NX_KIT_API void assertionFailed(
240  PrintFunc printFunc, const char* conditionStr, const std::string& message,
241  const char* file, int line);
242 
243 class NX_KIT_API Timer
244 {
245 public:
246  Timer(bool enabled, PrintFunc printFunc, const char* tag);
247  ~Timer();
248  void mark(const char* markStr);
249  void finish();
250 
251 private:
252  struct Impl;
253  Impl* const d;
254 };
255 
256 class NX_KIT_API Fps
257 {
258 public:
259  Fps(PrintFunc printFunc, const char* tag);
260  ~Fps();
261  void mark(const char* markStr = nullptr);
262 
263 private:
264  struct Impl;
265  Impl* const d;
266 };
267 
268 NX_KIT_API void printHexDump(
269  PrintFunc printFunc, const char* caption, const char* bytes, int size);
270 
271 } // namespace detail
272 
273 } // namespace debug
274 } // namespace kit
275 } // namespace nx
276 
277 #if defined(__linux__)
278  #include <pthread.h>
279  #define NX_KIT_DEBUG_DETAIL_THREAD_ID() \
280  nx::kit::utils::format(", thread %llx", (long long) pthread_self())
281 #elif defined(QT_CORE_LIB)
282  #include <QtCore/QThread>
283  #define NX_KIT_DEBUG_DETAIL_THREAD_ID() \
284  nx::kit::utils::format(", thread %llx", (long long) QThread::currentThreadId())
285 #else
286  // No threading libs available - do not print thread id.
287  #define NX_KIT_DEBUG_DETAIL_THREAD_ID() ""
288 #endif
Definition: debug.h:256
Definition: debug.cpp:286
Definition: debug.cpp:211
Definition: debug.cpp:13
Definition: debug.h:243