Program Listing for File macros.h

Return to documentation for file (rcsw/er/macros.h)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "rcsw/er/er.h"
#include "rcsw/rcsw.h"
#include "rcsw/common/compilers.h"

/*******************************************************************************
 * Macros when ER is enabled for FATAL events:
 *
 * - No plugin is used. When reporting FATAL events only, this is
 *   frequently in production and/or using a full-featured ER plugin is too
 *   costly in terms of execution time or space.
 *
 * - Debug printing macros enabled.
 ******************************************************************************/

#if (RCSW_ERL >= RCSW_ERL_FATAL)

#define RCSW_ER_INIT(...) RCSW_ER_PLUGIN_INIT(__VA_ARGS__)

#define RCSW_ER_DEINIT(...) RCSW_ER_PLUGIN_DEINIT(__VA_ARGS__)

#define DPRINTF(...) PRINTF(__VA_ARGS__)

#define DPRINT_TOK(tok) DPRINTF(STR(tok) ": %d/0x%x\n", (int)(tok), (int)(tok));

#define DPRINT_TOKD(tok) DPRINTF(STR(tok) ": %d\n", (int)(tok));

#define DPRINT_TOKX(tok) DPRINTF(STR(tok) ": 0x%x\n", (int)(tok));

#define DPRINT_TOKF(tok) DPRINTF(STR(tok) ": %.8f\n", (float)(tok));

#endif /* RCSW_ERL >= RCSW_ERL_FATAL */

#if (RCSW_ERL == RCSW_ERL_FATAL)

#define ER_FATAL(msg, ...)                                              \
    {                                                                   \
      DPRINTF(RCSW_ER_MODNAME " [FATAL]: " msg, ## __VA_ARGS__);        \
    }                                                                   \

#define RCSW_ER_MODULE_INIT(...)

#elif (RCSW_ERL > RCSW_ERL_FATAL)

#define ER_FATAL(...)  ER_FATAL_IMPL(RCSW_ER_PLUGIN_HANDLE(RCSW_ER_MODID,   \
                                                           RCSW_ER_MODNAME), \
                                 __VA_ARGS__)

/* \cond INTERNAL */
#define ER_FATAL_IMPL(handle, ...) {                                    \
    if (RCSW_ER_PLUGIN_LVL_CHECK(handle, RCSW_ERL_FATAL)) {             \
      ER_REPORT(FATAL, handle, __VA_ARGS__)                             \
    }                                                                   \
  }
/* \endcond */
#endif

/*******************************************************************************
 * Macros when ER is enabled for severity level >= ERROR:
 *
 * - The configured plugin is used. Only [FATAL,ERROR] events are compiled in;
 *   others are discarded.
 *
 * - Debug printing macros enabled.
 ******************************************************************************/
#if (RCSW_ERL >= RCSW_ERL_ERROR)
#define ER_ERR(...)  ER_ERR_IMPL(RCSW_ER_PLUGIN_HANDLE(RCSW_ER_MODID,   \
                                                       RCSW_ER_MODNAME), \
                                 __VA_ARGS__)

/* \cond INTERNAL */
#define ER_ERR_IMPL(handle, ...) {                                      \
    if (RCSW_ER_PLUGIN_LVL_CHECK(handle, ERROR)) {       \
      ER_REPORT(ERROR, handle, __VA_ARGS__)                             \
    }                                                                   \
  }
/* \endcond */

#if !defined(RCSW_ER_MODNAME)
#define RCSW_ER_MODNAME __FILE_NAME__
#endif

#if !defined(RCSW_ER_MODID)
#define RCSW_ER_MODID (-1)
#endif

#define RCSW_ER_MODULE_INIT(...)                        \
  RCSW_ER_PLUGIN_INSMOD(RCSW_ER_MODID, RCSW_ER_MODNAME)

#define RCSW_ER_INSMOD(ID, NAME) RCSW_ER_PLUGIN_INSMOD(ID, NAME)

#endif /* RCSW_ERL >= RCSW_ERL_ERROR */

/*******************************************************************************
 * Macros when ER is enabled for severity level >= WARN:
 *
 * - The configured plugin is used. Only [FATAL,ERROR,WARN] events are compiled
 *   in; others are discarded.
 *
 * - Debug printing macros enabled.
 ******************************************************************************/
#if (RCSW_ERL >= RCSW_ERL_WARN)

#define ER_WARN(...)  ER_WARN_IMPL(RCSW_ER_PLUGIN_HANDLE(RCSW_ER_MODID, \
                                                         RCSW_ER_MODNAME), \
                                   __VA_ARGS__)


/* \cond INTERNAL */
#define ER_WARN_IMPL(handle, ...) {                                     \
    if (RCSW_ER_PLUGIN_LVL_CHECK(handle, WARN)) {        \
      ER_REPORT(WARN, handle, ## __VA_ARGS__)                           \
    }                                                                   \
  }
/* \endcond */
#endif /* RCSW_ERL >= RCSW_ERL_WARN */

/*******************************************************************************
 * Macros when ER is enabled for severity level >= INFO:
 *
 * - The configured plugin is used. Only [FATAL,ERROR,WARN,INFO] events are
 *   compiled in; others are discarded.
 *
 * - Debug printing macros enabled.
 ******************************************************************************/
#if (RCSW_ERL >= RCSW_ERL_INFO)

#define ER_INFO(...)  ER_INFO_IMPL(RCSW_ER_PLUGIN_HANDLE(RCSW_ER_MODID, \
                                                         RCSW_ER_MODNAME), \
                                   __VA_ARGS__)

/* \cond INTERNAL */
#define ER_INFO_IMPL(handle, ...) {                                     \
    if (RCSW_ER_PLUGIN_LVL_CHECK(handle, INFO)) {        \
      ER_REPORT(INFO, handle, ## __VA_ARGS__)                           \
    }                                                                   \
  }
/* \endcond */
#endif /* RCSW_ERL >= RCSW_ERL_INFO */

/*******************************************************************************
 * Macros when ER is enabled for severity level >= DEBUG:
 *
 * - The configured plugin is used. Only [FATAL,ERROR,WARN,INFO,DEBUG] events
 *   are compiled in; others are discarded.
 *
 * - Debug printing macros enabled.
 ******************************************************************************/
#if (RCSW_ERL >= RCSW_ERL_DEBUG)

#define ER_DEBUG(...)  ER_DEBUG_IMPL(RCSW_ER_PLUGIN_HANDLE(RCSW_ER_MODID, \
                                                         RCSW_ER_MODNAME), \
                                   __VA_ARGS__)

/* \cond INTERNAL */
#define ER_DEBUG_IMPL(handle, ...) {                                     \
    if (RCSW_ER_PLUGIN_LVL_CHECK(handle, DEBUG)) {        \
      ER_REPORT(DEBUG, handle, ## __VA_ARGS__)                           \
    }                                                                   \
  }
/* \endcond */
#endif /* RCSW_ERL >= RCSW_ERL_DEBUG */

/*******************************************************************************
 * Macros when ER is enabled for severity level >= TRACE:
 *
 * - The configured plugin is used. Only [FATAL,ERROR,WARN,INFO,DEBUG,TRACE]
 *   events are compiled in; others are discarded.
 *
 * - Debug printing macros enabled.
 ******************************************************************************/
#if (RCSW_ERL >= RCSW_ERL_TRACE)

#define ER_TRACE(...)  ER_TRACE_IMPL(RCSW_ER_PLUGIN_HANDLE(RCSW_ER_MODID, \
                                                         RCSW_ER_MODNAME), \
                                   __VA_ARGS__)

/* \cond INTERNAL */
#define ER_TRACE_IMPL(handle, ...) {                                     \
    if (RCSW_ER_PLUGIN_LVL_CHECK(handle, TRACE)) {        \
      ER_REPORT(TRACE, handle, ## __VA_ARGS__)                           \
    }                                                                   \
  }
/* \endcond */
#endif /* RCSW_ERL >= RCSW_ERL_TRACE */


/*******************************************************************************
 * General ER macros for when ER is != NONE.
 ******************************************************************************/
#if RCSW_ERL != RCSW_ERL_NONE

#define ER_REPORT(lvl, handle, msg, ...)        \
  {                                             \
    RCSW_ER_PLUGIN_REPORT(lvl,                  \
                          handle,               \
                          RCSW_ER_MODID,        \
                          RCSW_ER_MODNAME,      \
                          msg "\r\n",           \
                          ## __VA_ARGS__)       \
  }

#endif /* (RCSW_ERL != RCSW_ERL_NONE) */

/*******************************************************************************
 * General ER macros independent of level
 ******************************************************************************/
#define PRINTF(...) RCSW_ER_PLUGIN_PRINTF(__VA_ARGS__)

/*
 * Don't define the macro to be nothing, as that can leave tons of "unused
 * variable" warnings in the code for variables which are only used in
 * asserts. The sizeof() trick here does *NOT* actually evaluate the
 * condition--only the size of whatever it returns. The variables are "used",
 * making the compiler happy, but ultimately removed by the optimizer.
 */
#define ER_ASSERT(cond, msg, ...)               \
  do {                                          \
    (void)sizeof((cond));                       \
    if (RCSW_UNLIKELY(!(cond))) {               \
      ER_FATAL( msg, ##__VA_ARGS__);            \
      assert(cond);                             \
    }                                           \
  } while (0);

#define ER_CONDW(cond, msg, ...)                \
  {                                             \
    if (RCSW_LIKELY((cond))) {                  \
      ER_WARN(msg, ##__VA_ARGS__);              \
    }                                           \
  }

#define ER_CONDI(cond, msg, ...)                \
  {                                             \
    if (RCSW_LIKELY((cond))) {                  \
      ER_INFO(msg, ##__VA_ARGS__);              \
    }                                           \
  }

#define ER_CONDD(cond, msg, ...)                \
  {                                             \
    if (RCSW_LIKELY((cond))) {                  \
      ER_DEBUG(msg, ##__VA_ARGS__);             \
    }                                           \
  }



#define ER_FATAL_SENTINEL(msg, ...)             \
  {                                             \
    ER_FATAL(msg, ##__VA_ARGS__);               \
    abort();                                    \
  }


#define ER_CHECK(cond, msg, ...)                \
  {                                             \
    if (RCSW_UNLIKELY(!(cond))) {               \
      ER_ERR(msg, ##__VA_ARGS__);               \
      goto error;                               \
    }                                           \
  }

#define ER_SENTINEL(msg, ...)                   \
  {                                             \
    ER_ERR(msg, ##__VA_ARGS__);                 \
    goto error;                                 \
  }

/*******************************************************************************
 * Macro Cleanup
 *
 * Depending on compile-time level, one or more of these macros may be
 * undefined, so make sure everything is defined so things build cleanly.
 ******************************************************************************/
#ifndef ER_FATAL
#define ER_FATAL(...)
#endif

#ifndef ER_ERR
#define ER_ERR(...)
#endif

#ifndef ER_WARN
#define ER_WARN(...)
#endif

#ifndef ER_INFO
#define ER_INFO(...)
#endif

#ifndef ER_DEBUG
#define ER_DEBUG(...)
#endif

#ifndef ER_TRACE
#define ER_TRACE(...)
#endif

#ifndef RCSW_ER_MODULE_INIT
#define RCSW_ER_MODULE_INIT(...)
#endif

#ifndef RCSW_ER_INIT
#define RCSW_ER_INIT(...)
#endif

#ifndef RCSW_ER_DEINIT
#define RCSW_ER_DEINIT(...)
#endif

#ifndef RCSW_ER_INSMOD
#define RCSW_ER_INSMOD(...)
#endif

#ifndef ER_REPORT
#define ER_REPORT(...)
#endif

#ifndef DPRINTF
#define DPRINTF(...)
#endif

#ifndef DPRINT_TOK
#define DPRINT_TOK(...)
#endif

#ifndef DPRINT_TOKD
#define DPRINT_TOKD(...)
#endif

#ifndef DPRINT_TOKX
#define DPRINT_TOKX(...)
#endif

#ifndef DPRINT_TOKF
#define DPRINT_TOKF(...)
#endif