Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
Debugging of code

Description


Functionality to debug/trace source code.

This system allows the programmer to insert traces inside portions of code (routines, methods, etc.) locally, i.e., activating/deactivating it independently in each portion. Also, all traces inside a given file can be deactivated globally if desired.

Usage:

Example:

  \code
  "something.cpp":

  ...

  // This define has no effect in production mode:
  #define ELE_PRE_TRACED

  ...

  void afunction(void)
  {
  ELE_CODE_TRACE_ON; // This statement does not insert code here.

      // This trace may insert extra code even if ELE_CODE_TRACE_OFF:
      ELE_CODE_TRACE({RTTextWith::kTime},
                     "beginning with var = " << myvar);

      std::cout << "This routine is being traced..." << std::endl;

      // This particular trace has been deactivated, and does not
      // generate any code.
      ELE_CODE_UNTRACE({RTTextWith::kTime, RTTextWith::kThread},
                       "an old trace");
  }

  ...
  \endcode
Note
- In production mode (ELE_MAKE_PRODUCTION defined), all .cpp with ELE_PRE_TRACED defined will ignore those ELE_PRE_TRACED and will contain no tracing code at all.
- Not defining ELE_PRE_TRACED guarantees no trace code is generated in that .cpp, but setting ELE_CODE_TRACE_OFF locally may produce some code (e.g., previously to C++17; see the ELE_CODE_TRACE documentation). Anyway, ELE_CODE_UNTRACE never generates code, and the code enclosed in ELE_CODE_TRACE_IF / ENDIF will always be there although it may not execute, unless the compiler get rid of code guarded by an if (false).
- The piece of code being locally traced must be scoped, i.e., if it is not a routine, it must be enclosed within {}.
- ELE_CODE_TRACE_ON/OFF cannot be used more than once inside a piece of code.
- RTTextWith::kTime uses std::chrono::steady_clock as a clock and always shows time in microseconds.
See also
Errors
Author
Juan-Antonio Fernandez-Madrigal. http://jafma.net
Date
2018-2019
Collaboration diagram for Debugging of code:

UTILITIES FOR SOURCE CODE INSPECTION IN RUN-TIME

constexpr char elementa::base::kHiddenPlaceMsg [] = "place hidden in production"
 Constant text hiding runtime place when the program is in production. More...
 
 elementa::base::ELE_CLASS_ENUM (RTTextWith, kTime, kThread)
 Flags for selecting what to include in runtime text in addition to the place. More...
 
std::string elementa::base::runtime_src_place (const std::string &place, const RTTextWithEnum::Combination &flags={}, const std::string &placepreff="Place:", const std::string &timepreff="Time:", const std::string &threadpreff="Thread:")
 Return a string with info about a place, time and thread during execution. More...
 
#define ELE_PRE_FILELONGNAME   std::string{__FILE__}
 Long name of the current source file, as a std::string. More...
 
#define ELE_PRE_FILEBASENAME
 Base name of current src file (computed in runtime), as a std::string. More...
 
#define ELE_CODE_PLACE
 Produces a std::string with the place of source where the macro is placed. More...
 
#define ELE_CODE_LONGPLACE
 Produces a string with the place of source code where the macro is placed. More...
 

TRACING SYSTEM

#define ELE_CODE_TRACE_ON
 Place this inside local scope (e.g., routine) to activate traces there. More...
 
#define ELE_CODE_TRACE_OFF
 Place this inside local scope (e.g., routine) to deactivate traces there. More...
 
#define ELE_CODE_TRACE_IF   if (false) {
 Place this before a part of code that should be executed in debug only. More...
 
#define ELE_CODE_TRACE_ELSE   } else {
 Place this between if and endif. More...
 
#define ELE_CODE_TRACE_ENDIF   }
 Place this after a part of code that should be executed in debug only. More...
 
#define ELE_PRE_TRACE_PREFFIX   "--- TR : "
 Preffix added to any trace. More...
 
#define ELE_CODE_TRACE(flags, ...)   {}
 
#define ELE_CODE_TRACEPOINT   {}
 
#define ELE_CODE_TRACENUDE(...)   {}
 
#define ELE_CODE_PAUSE(flags, ...)   {}
 
#define ELE_CODE_UNTRACE(flags, ...)
 Macro to use instead of an existing ELE_CODE_TRACE to deactivate that trace. More...
 
#define ELE_CODE_UNTRACEPOINT
 Macro to use instead of an existing ELE_CODE_TRACEPOINT to deactivate it. More...
 
#define ELE_CODE_UNPAUSE(flags, ...)
 Macro to use instead of an existing ELE_CODE_PAUSE to deactivate that trace. More...
 
#define ELE_CODE_UNTRACE_IF   if (false) {
 Macro to use instead of an existing ELE_CODE_TRACE_IF to deactivate it. More...
 
#define ELE_CODE_UNTRACE_ELSE   } else {
 Macro to use instead of an existing ELE_CODE_TRACE_ELSE to deactivate it. More...
 
#define ELE_CODE_UNTRACE_ENDIF   }
 Macro to use instead of an existing ELE_CODE_TRACE_ENDIF to deactivate it. More...
 

Macro Definition Documentation

◆ ELE_PRE_FILELONGNAME

#define ELE_PRE_FILELONGNAME   std::string{__FILE__}

#include <elementa/base/debugging.h>

Long name of the current source file, as a std::string.

Definition at line 171 of file debugging.h.

◆ ELE_PRE_FILEBASENAME

#define ELE_PRE_FILEBASENAME

#include <elementa/base/debugging.h>

Value:
(std::string{__FILE__}.rfind('/') >= \
std::string{__FILE__}.size()-1 ? \
std::string{__FILE__} : \
std::string{__FILE__}.substr(\
std::string{__FILE__}.rfind('/')+1) )

Base name of current src file (computed in runtime), as a std::string.

Definition at line 174 of file debugging.h.

◆ ELE_CODE_PLACE

#define ELE_CODE_PLACE

#include <elementa/base/debugging.h>

Value:
(ELE_PRE_FILEBASENAME + ":" + std::string{__func__} + \
":" + std::to_string(__LINE__) )
#define ELE_PRE_FILEBASENAME
Base name of current src file (computed in runtime), as a std::string.
Definition: debugging.h:174

Produces a std::string with the place of source where the macro is placed.

This is the short version, producing just the basename of the file.

Note
- The .cpp where this macro is used must have a #define ELE_PRE_TRACED at the beginning.
- If we are in production mode, this hides the actual info, showing instead kHiddenPlaceMsg.
- Cannot be something different from a macro since it has to capture information from the very code of the user, not from inside some function.

Definition at line 194 of file debugging.h.

◆ ELE_CODE_LONGPLACE

#define ELE_CODE_LONGPLACE

#include <elementa/base/debugging.h>

Value:
(ELE_PRE_FILELONGNAME + ":" +std::string{__func__} +\
":" + std::to_string(__LINE__) )
#define ELE_PRE_FILELONGNAME
Long name of the current source file, as a std::string.
Definition: debugging.h:171

Produces a string with the place of source code where the macro is placed.

This is the long-text version, producing the entire path of the file.

Definition at line 199 of file debugging.h.

◆ ELE_CODE_TRACE_ON

#define ELE_CODE_TRACE_ON

#include <elementa/base/debugging.h>

Place this inside local scope (e.g., routine) to activate traces there.

This does not occupy room in memory; it is absorbed during compilation

Definition at line 279 of file debugging.h.

◆ ELE_CODE_TRACE_OFF

#define ELE_CODE_TRACE_OFF

#include <elementa/base/debugging.h>

Place this inside local scope (e.g., routine) to deactivate traces there.

This does not occupy room in memory; it is absorbed during compilation

Definition at line 283 of file debugging.h.

◆ ELE_CODE_TRACE_IF

#define ELE_CODE_TRACE_IF   if (false) {

#include <elementa/base/debugging.h>

Place this before a part of code that should be executed in debug only.

When ELE_CODE_TRACE_OFF is set in the scope of this, the code between ELE_CODE_TRACE_IF and ELE_CODE_TRACE_ENDIF will still be there, but never executed.

Definition at line 289 of file debugging.h.

◆ ELE_CODE_TRACE_ELSE

#define ELE_CODE_TRACE_ELSE   } else {

#include <elementa/base/debugging.h>

Place this between if and endif.

Definition at line 292 of file debugging.h.

◆ ELE_CODE_TRACE_ENDIF

#define ELE_CODE_TRACE_ENDIF   }

#include <elementa/base/debugging.h>

Place this after a part of code that should be executed in debug only.

Definition at line 295 of file debugging.h.

◆ ELE_PRE_TRACE_PREFFIX

#define ELE_PRE_TRACE_PREFFIX   "--- TR : "

#include <elementa/base/debugging.h>

Preffix added to any trace.

Definition at line 307 of file debugging.h.

◆ ELE_CODE_TRACE

#define ELE_CODE_TRACE (   flags,
  ... 
)    {}

#include <elementa/base/debugging.h>

Definition at line 426 of file debugging.h.

◆ ELE_CODE_TRACEPOINT

#define ELE_CODE_TRACEPOINT   {}

#include <elementa/base/debugging.h>

Definition at line 428 of file debugging.h.

◆ ELE_CODE_TRACENUDE

#define ELE_CODE_TRACENUDE (   ...)    {}

#include <elementa/base/debugging.h>

Definition at line 430 of file debugging.h.

◆ ELE_CODE_PAUSE

#define ELE_CODE_PAUSE (   flags,
  ... 
)    {}

#include <elementa/base/debugging.h>

Definition at line 432 of file debugging.h.

◆ ELE_CODE_UNTRACE

#define ELE_CODE_UNTRACE (   flags,
  ... 
)

#include <elementa/base/debugging.h>

Macro to use instead of an existing ELE_CODE_TRACE to deactivate that trace.

Definition at line 437 of file debugging.h.

◆ ELE_CODE_UNTRACEPOINT

#define ELE_CODE_UNTRACEPOINT

#include <elementa/base/debugging.h>

Macro to use instead of an existing ELE_CODE_TRACEPOINT to deactivate it.

Definition at line 440 of file debugging.h.

◆ ELE_CODE_UNPAUSE

#define ELE_CODE_UNPAUSE (   flags,
  ... 
)

#include <elementa/base/debugging.h>

Macro to use instead of an existing ELE_CODE_PAUSE to deactivate that trace.

Definition at line 443 of file debugging.h.

◆ ELE_CODE_UNTRACE_IF

#define ELE_CODE_UNTRACE_IF   if (false) {

#include <elementa/base/debugging.h>

Macro to use instead of an existing ELE_CODE_TRACE_IF to deactivate it.

The associated ELE_CODE_TRACE_ELSE and ELE_CODE_TRACE_ENDIF must not be modified.

Definition at line 448 of file debugging.h.

◆ ELE_CODE_UNTRACE_ELSE

#define ELE_CODE_UNTRACE_ELSE   } else {

#include <elementa/base/debugging.h>

Macro to use instead of an existing ELE_CODE_TRACE_ELSE to deactivate it.

Definition at line 451 of file debugging.h.

◆ ELE_CODE_UNTRACE_ENDIF

#define ELE_CODE_UNTRACE_ENDIF   }

#include <elementa/base/debugging.h>

Macro to use instead of an existing ELE_CODE_TRACE_ENDIF to deactivate it.

Definition at line 454 of file debugging.h.

Function Documentation

◆ ELE_CLASS_ENUM()

elementa::base::ELE_CLASS_ENUM ( RTTextWith  ,
kTime  ,
kThread   
)

#include <elementa/base/debugging.h>

Flags for selecting what to include in runtime text in addition to the place.

  • kTime Include the current run-time time in microseconds
  • kThread Include the current run-time thread

◆ runtime_src_place()

std::string elementa::base::runtime_src_place ( const std::string &  place,
const RTTextWithEnum::Combination &  flags = {},
const std::string &  placepreff = "Place:",
const std::string &  timepreff = "Time:",
const std::string &  threadpreff = "Thread:" 
)

#include <elementa/base/debugging.h>

Return a string with info about a place, time and thread during execution.

It fills with nothing those parts that are given empty. Usage:

    \code
    runtime_src_place("plc" (use here ELE_CODE_PLACE, ELE_CODE_LONGPLACE or
                             any location string you wish),
                      {...flags of info to include...},
                      [  "Preffix of the place part of the result",
                         "Preffix of the time part of the result",
                         "Preffix of the thread part of the result" ]
                     );
    \endcode

Variable Documentation

◆ kHiddenPlaceMsg

constexpr char elementa::base::kHiddenPlaceMsg[] = "place hidden in production"
constexpr

#include <elementa/base/debugging.h>

Constant text hiding runtime place when the program is in production.

Definition at line 158 of file debugging.h.