stub_analytics_plugin  1.6
Network Optix Video Analytics SDK
Classes | Macros | Typedefs | Functions
debug.h File Reference
#include <iostream>
#include <cstdint>
#include <functional>
#include <sstream>
#include <memory>
#include <nx/kit/utils.h>

Go to the source code of this file.

Classes

class  nx::kit::debug::detail::Timer
 
class  nx::kit::debug::detail::Fps
 

Macros

#define NX_KIT_API   /*empty*/
 
#define NX_DEBUG_INI   ini().
 
#define NX_DEBUG_ENABLE_OUTPUT   NX_DEBUG_INI enableOutput
 
#define NX_PRINT_PREFIX   nx::kit::debug::detail::printPrefix(__FILE__)
 
#define NX_DEBUG_STREAM   *nx::kit::debug::stream()
 
#define NX_DEBUG_ENDL   << std::endl
 
#define NX_PRINT
 
#define NX_OUTPUT
 
#define NX_KIT_ASSERT(...)
 
#define LL
 
#define NX_PRINT_VALUE(VALUE)   NX_PRINT << "####### " #VALUE ": " << nx::kit::utils::toString((VALUE))
 
#define NX_PRINT_HEX_DUMP(CAPTION, BYTES, SIZE)
 
#define NX_DEBUG_ENABLE_TIME   NX_DEBUG_INI enableTime
 
#define NX_TIME_BEGIN(TAG)
 
#define NX_TIME_MARK(TAG, MARK)
 
#define NX_TIME_END(TAG)
 
#define NX_DEBUG_ENABLE_FPS   NX_DEBUG_INI enableFps
 
#define NX_FPS(TAG, ...)
 
#define NX_KIT_DEBUG_DETAIL_MSVC_EXPAND(ARG)   ARG
 
#define NX_KIT_DEBUG_DETAIL_GET_3RD_ARG(ARG1, ARG2, ARG3, ...)   ARG3
 
#define NX_KIT_DEBUG_DETAIL_ASSERT1(CONDITION)   NX_KIT_DEBUG_DETAIL_ASSERT(CONDITION, "")
 
#define NX_KIT_DEBUG_DETAIL_ASSERT2(CONDITION, MESSAGE)   NX_KIT_DEBUG_DETAIL_ASSERT(CONDITION, MESSAGE)
 
#define NX_KIT_DEBUG_DETAIL_ASSERT(CONDITION, MESSAGE)
 
#define NX_KIT_DEBUG_DETAIL_CONCAT(X, Y)   NX_KIT_DEBUG_DETAIL_CONCAT2(X, Y)
 
#define NX_KIT_DEBUG_DETAIL_CONCAT2(X, Y)   X##Y
 
#define NX_KIT_DEBUG_DETAIL_PRINT_FUNC   [&](const char* message) { NX_PRINT << message; }
 
#define NX_KIT_DEBUG_DETAIL_THREAD_ID()   ""
 

Typedefs

typedef std::function< void(const char *)> nx::kit::debug::detail::PrintFunc
 

Functions

char nx::kit::debug::pathSeparator ()
 
size_t nx::kit::debug::commonPrefixSize (const std::string &s1, const std::string &s2)
 
const char * nx::kit::debug::relativeSrcFilename (const char *file)
 
std::string nx::kit::debug::fileBaseNameWithoutExt (const char *file)
 
std::ostream *& nx::kit::debug::stream ()
 
std::string nx::kit::debug::detail::printPrefix (const char *file)
 
void nx::kit::debug::detail::assertionFailed (PrintFunc printFunc, const char *conditionStr, const std::string &message, const char *file, int line)
 
void nx::kit::debug::detail::printHexDump (PrintFunc printFunc, const char *caption, const char *const bytes, int size)
 

Detailed Description

Utilities for debugging: measuring execution time and FPS, working with strings, logging values and messages.

This unit can be compiled in the context of any C++ project.

Macro Definition Documentation

◆ LL

#define LL
Value:
NX_PRINT << "####### LL line " << __LINE__ \
<< NX_KIT_DEBUG_DETAIL_THREAD_ID() \
<< ", file " << nx::kit::debug::relativeSrcFilename(__FILE__);
#define NX_PRINT
Definition: debug.h:92

Log execution of a line - to use, write LL at the beginning of the line.

◆ NX_DEBUG_ENABLE_FPS

#define NX_DEBUG_ENABLE_FPS   NX_DEBUG_INI enableFps

Redefine if needed. Will be suffixed with Fps TAG values.

◆ NX_DEBUG_ENABLE_OUTPUT

#define NX_DEBUG_ENABLE_OUTPUT   NX_DEBUG_INI enableOutput

Redefine if needed.

◆ NX_DEBUG_ENABLE_TIME

#define NX_DEBUG_ENABLE_TIME   NX_DEBUG_INI enableTime

Redefine if needed.

◆ NX_DEBUG_ENDL

#define NX_DEBUG_ENDL   << std::endl

Redefine if needed; used after NX_DEBUG_STREAM by other macros.

◆ NX_DEBUG_INI

#define NX_DEBUG_INI   ini().

Define if needed to redirect NX_PRINT... to qDebug(). If needed, redefine to form a prefix for NX_DEBUG_ENABLE... macros.

◆ NX_DEBUG_STREAM

#define NX_DEBUG_STREAM   *nx::kit::debug::stream()

Redefine if needed; used for all output by other macros.

◆ NX_FPS

#define NX_FPS (   TAG,
  ... 
)
Value:
do \
{ \
if (NX_KIT_DEBUG_DETAIL_CONCAT(NX_DEBUG_ENABLE_FPS, TAG)) \
{ \
NX_KIT_DEBUG_DETAIL_PRINT_FUNC, #TAG); \
fps.mark(__VA_ARGS__); \
} \
} while (0)
Definition: debug.h:256
#define NX_DEBUG_ENABLE_FPS
Definition: debug.h:183

Measure and print with NX_PRINT each period between executions of the point of call; do nothing if !NX_DEBUG_ENABLE_FPS##TAG.

◆ NX_KIT_ASSERT

#define NX_KIT_ASSERT (   ...)
Value:
__VA_ARGS__, NX_KIT_DEBUG_DETAIL_ASSERT2, NX_KIT_DEBUG_DETAIL_ASSERT1, \
/* Helps to generate a reasonable compiler error. */ args_required)(__VA_ARGS__))
#define NX_KIT_DEBUG_DETAIL_MSVC_EXPAND(ARG)
Definition: debug.h:207
#define NX_KIT_DEBUG_DETAIL_GET_3RD_ARG(ARG1, ARG2, ARG3,...)
Definition: debug.h:210

If the condition is false, log the failure with NX_PRINT, and in debug build (i.e. NDEBUG is not defined), crash the process to let a dump/core be generated.

ATTENTION: Unlike std library assert(), the condition is checked even in Release build to log the failure.

◆ NX_KIT_DEBUG_DETAIL_ASSERT

#define NX_KIT_DEBUG_DETAIL_ASSERT (   CONDITION,
  MESSAGE 
)
Value:
do \
{ \
if (!(CONDITION)) \
{ \
nx::kit::debug::detail::assertionFailed( \
NX_KIT_DEBUG_DETAIL_PRINT_FUNC, #CONDITION, (MESSAGE), __FILE__, __LINE__); \
} \
} while (0)

◆ NX_KIT_DEBUG_DETAIL_GET_3RD_ARG

#define NX_KIT_DEBUG_DETAIL_GET_3RD_ARG (   ARG1,
  ARG2,
  ARG3,
  ... 
)    ARG3

Needed to make an "overloaded" macro which can accept either one or two args.

◆ NX_KIT_DEBUG_DETAIL_MSVC_EXPAND

#define NX_KIT_DEBUG_DETAIL_MSVC_EXPAND (   ARG)    ARG

Needed as a workaround for an MSVC issue: if VA_ARGS is used as an argument to another macro, it forms a single macro argument even if contains commas.

◆ NX_OUTPUT

#define NX_OUTPUT
Value:
/* << args... */ \
#define NX_PRINT
Definition: debug.h:92
#define NX_DEBUG_ENABLE_OUTPUT
Definition: debug.h:63

Print the args like NX_PRINT; do nothing if !NX_DEBUG_ENABLE_OUTPUT.

◆ NX_PRINT

#define NX_PRINT
Value:
/* << args... */ \
/* Allocate a temp value, which prints endl in its destructor, in the "<<" expression. */ \
( []() { struct Endl { ~Endl() { NX_DEBUG_STREAM NX_DEBUG_ENDL; } }; \
return std::make_shared<Endl>(); }() ) /*operator,*/, \
NX_DEBUG_STREAM << NX_PRINT_PREFIX
#define NX_DEBUG_STREAM
Definition: debug.h:73
#define NX_PRINT_PREFIX
Definition: debug.h:68
#define NX_DEBUG_ENDL
Definition: debug.h:78

Print the args to NX_DEBUG_STREAM, starting with NX_PRINT_PREFIX and ending with NX_DEBUG_ENDL. Redefine if needed.

◆ NX_PRINT_HEX_DUMP

#define NX_PRINT_HEX_DUMP (   CAPTION,
  BYTES,
  SIZE 
)
Value:
nx::kit::debug::detail::printHexDump( \
NX_KIT_DEBUG_DETAIL_PRINT_FUNC, (CAPTION), (const char*) (BYTES), (int) (SIZE))

Hex-dump binary data using NX_PRINT.

◆ NX_PRINT_PREFIX

#define NX_PRINT_PREFIX   nx::kit::debug::detail::printPrefix(__FILE__)

Redefine if needed; used as the first item in NX_PRINT and NX_OUTPUT.

◆ NX_PRINT_VALUE

#define NX_PRINT_VALUE (   VALUE)    NX_PRINT << "####### " #VALUE ": " << nx::kit::utils::toString((VALUE))

Print the expression text and its value via toString().

◆ NX_TIME_BEGIN

#define NX_TIME_BEGIN (   TAG)
Value:
(NX_DEBUG_ENABLE_TIME), NX_KIT_DEBUG_DETAIL_PRINT_FUNC, #TAG)
#define NX_DEBUG_ENABLE_TIME
Definition: debug.h:149
Definition: debug.h:243

Start measuring time - save current time in a variable; do nothing if !NX_DEBUG_ENABLE_TIME.

◆ NX_TIME_END

#define NX_TIME_END (   TAG)
Value:
do \
{ \
nxTimer_##TAG.finish(); \
} while (0)
#define NX_DEBUG_ENABLE_TIME
Definition: debug.h:149

Print the totals of this time measurement with NX_PRINT; do nothing if !NX_DEBUG_ENABLE_TIME.

◆ NX_TIME_MARK

#define NX_TIME_MARK (   TAG,
  MARK 
)
Value:
do \
{ \
nxTimer_##TAG.mark((MARK)); \
} while (0)
#define NX_DEBUG_ENABLE_TIME
Definition: debug.h:149

Measure the time in the intermediate point, to be printed at the end; do nothing if !NX_DEBUG_ENABLE_TIME. If called at least once, forces "ms" instead of "us" for the totals.

Function Documentation

◆ pathSeparator()

NX_KIT_API char nx::kit::debug::pathSeparator ( )
Returns
Separator in FILE - slash or backslash, or '\0' if unknown. Cached in static.

◆ printPrefix()

NX_KIT_API std::string nx::kit::debug::detail::printPrefix ( const char *  file)
Parameters
fileSupply FILE.

◆ relativeSrcFilename()

NX_KIT_API const char * nx::kit::debug::relativeSrcFilename ( const char *  file)

Extract part of the source code filename which follows the uninteresting common part.

Returns
Pointer to some char inside the given string.

◆ stream()

NX_KIT_API std::ostream *& nx::kit::debug::stream ( )

Allows to override the stream used by NX_PRINT via NX_DEBUG_STREAM, initially std::cerr.

Returns
Reference to the static variable which stores the stream pointer.