Skip to content
Permalink
Browse files

Add debug hook for EML_PRE/POSTCONDITION

  • Loading branch information...
jonnor committed Dec 29, 2018
1 parent 6340f7d commit 147160a476df8e667b5dcd8b62f4ced6905d8e6b
Showing with 76 additions and 27 deletions.
  1. +1 −0 bindings/eml_audio.cpp
  2. +75 −27 emlearn/eml_common.h
@@ -6,6 +6,7 @@
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

#define EML_DEBUG 1

#include <eml_fft.h>
#include <eml_audio.h>
@@ -6,32 +6,7 @@
#include <stdint.h>
#include <stddef.h>

// Return errorcode if expr not satisfied
#define EML_PRECONDITION(expr, errorcode) \
do { \
if (!(expr)) { \
return errorcode; \
} \
} while (0);

// Return errorcode if expr not satisfied
#define EML_POSTCONDITION(expr, errorcode) \
do { \
if (!(expr)) { \
return errorcode; \
} \
} while (0);

// Return if expr gives error
#define EML_CHECK_ERROR(expr) \
do { \
const EmlError _e = (expr); \
if (_e != EmlOk) { \
return _e; \
} \
} while (0);


// EmlError
typedef enum _EmlError {
EmlOk = 0,
EmlSizeMismatch,
@@ -62,10 +37,83 @@ eml_error_str(EmlError e) {
if (eml_error_valid(e)) {
return eml_error_strs[e];
} else {
return NULL;
return "Invalid error code";
}
}


typedef void (*EmlDebugFunction)(EmlError code, const char *file, int line);

#ifdef EML_DEBUG
void eml_debug_stderr(EmlError code, const char *file, int line)
{
const bool log_error = getenv("EML_DEBUG_LOG") != NULL;
const bool fatal_errors = getenv("EML_DEBUG_FATAL_ERRORS") != NULL;
if (log_error) {
const char *err_str = eml_error_str(code);
fprintf(stderr, "EmlError %s at %s:%d \n",
err_str, file, line);
}
if (fatal_errors) {
exit(11);
}
}

static EmlDebugFunction eml_debug = eml_debug_stderr;
#endif

#ifdef EML_DEBUG

#define EML_PRECONDITION(expr, errorcode) \
do { \
if (!(expr)) { \
if (eml_debug) { eml_debug(errorcode, __FILE__, __LINE__); } \
return errorcode; \
} \
} while (0);

// Return errorcode if expr not satisfied
#define EML_POSTCONDITION(expr, errorcode) \
do { \
if (!(expr)) { \
if (eml_debug) { eml_debug(errorcode, __FILE__, __LINE__); } \
return errorcode; \
} \
} while (0);


#else // EML_DEBUG


// Return errorcode if expr not satisfied
#define EML_PRECONDITION(expr, errorcode) \
do { \
if (!(expr)) { \
return errorcode; \
} \
} while (0);

// Return errorcode if expr not satisfied
#define EML_POSTCONDITION(expr, errorcode) \
do { \
if (!(expr)) { \
return errorcode; \
} \
} while (0);

#endif // EML_DEBUG

// Return if expr gives error
#define EML_CHECK_ERROR(expr) \
do { \
const EmlError _e = (expr); \
if (_e != EmlOk) { \
return _e; \
} \
} while (0);



#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

0 comments on commit 147160a

Please sign in to comment.
You can’t perform that action at this time.