2022-01-24 03:28:16 +00:00
|
|
|
#ifndef LAIKA_ERROR_H
|
|
|
|
#define LAIKA_ERROR_H
|
|
|
|
|
2022-01-31 21:54:39 +00:00
|
|
|
#include "laika.h"
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2022-01-24 03:28:16 +00:00
|
|
|
/* defines errorstack size */
|
|
|
|
#define LAIKA_MAXERRORS 32
|
|
|
|
|
|
|
|
/* DO NOT RETURN/GOTO/BREAK or otherwise skip LAIKA_TRYEND */
|
2022-06-27 23:57:00 +00:00
|
|
|
#define LAIKA_TRY if (setjmp(eLaika_errStack[++eLaika_errIndx]) == 0) {
|
|
|
|
#define LAIKA_CATCH \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{
|
|
|
|
#define LAIKA_TRYEND \
|
|
|
|
} \
|
|
|
|
--eLaika_errIndx;
|
2022-01-24 03:28:16 +00:00
|
|
|
|
|
|
|
/* if eLaika_errIndx is >= 0, we have a safe spot to jump too if an error is thrown */
|
|
|
|
#define LAIKA_ISPROTECTED (eLaika_errIndx >= 0)
|
|
|
|
|
2022-01-24 15:51:29 +00:00
|
|
|
/* LAIKA_ERROR(printf args):
|
2022-01-24 03:28:16 +00:00
|
|
|
if called after a LAIKA_TRY block will jump to the previous LAIKA_CATCH/LAIKA_TRYEND block,
|
|
|
|
otherwise program is exit()'d. if DEBUG is defined printf is called with passed args, else
|
|
|
|
arguments are ignored.
|
|
|
|
*/
|
|
|
|
#ifndef DEBUG
|
2022-06-27 23:57:00 +00:00
|
|
|
# define LAIKA_ERROR(...) \
|
|
|
|
do { \
|
|
|
|
if (LAIKA_ISPROTECTED) \
|
|
|
|
longjmp(eLaika_errStack[eLaika_errIndx], 1); \
|
|
|
|
else \
|
|
|
|
exit(1); \
|
|
|
|
} while (0);
|
|
|
|
# define LAIKA_WARN(...) ((void)0) /* no op */
|
2022-01-24 03:28:16 +00:00
|
|
|
#else
|
2022-06-27 23:57:00 +00:00
|
|
|
# define LAIKA_ERROR(...) \
|
|
|
|
do { \
|
|
|
|
printf("[ERROR] : " __VA_ARGS__); \
|
|
|
|
if (LAIKA_ISPROTECTED) \
|
|
|
|
longjmp(eLaika_errStack[eLaika_errIndx], 1); \
|
|
|
|
else \
|
|
|
|
exit(1); \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
# define LAIKA_WARN(...) printf("[WARN] : " __VA_ARGS__);
|
2022-01-24 03:28:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
extern int eLaika_errIndx;
|
|
|
|
extern jmp_buf eLaika_errStack[LAIKA_MAXERRORS];
|
|
|
|
|
|
|
|
#endif
|