Skip to content

Commit

Permalink
Implemented timed assert definition
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Aug 8, 2024
1 parent e3d45ce commit ca7cdb9
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# https://github.com/kala13x/smake #
####################################

CFLAGS = -D_XUTILS_USE_SSL -g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_GNU
CFLAGS = -D_XUTILS_USE_SSL -g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_GNU -D_ASSERT_TIMED
CFLAGS += -I./src/crypt -I./src/data -I./src/net -I./src/sys -I./src
LIBS = -lssl -lcrypto -lpthread
NAME = libxutils.a
Expand Down
2 changes: 1 addition & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# https://github.com/kala13x/smake #
####################################

CFLAGS = -g -O2 -Wall
CFLAGS = -g -O2 -Wall -D_XUTILS_DEBUG -D_ASSERT_TIMED
LIBS = -lpthread
LD_LIBS = ../build/libxutils.a

Expand Down
2 changes: 2 additions & 0 deletions examples/xlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ int main()
/* Greet users */
greet();

XASSERT(0, 0);

/* Initialize XLog with default parameters */
XLog_Init("example", XLOG_ALL, 0);
xlog_separator("[xutils]");
Expand Down
2 changes: 1 addition & 1 deletion misc/Makefile.tmp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# https://github.com/kala13x/smake #
####################################

CFLAGS = _SSL_FLAGS_ -g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_GNU
CFLAGS = _SSL_FLAGS_ -g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_GNU -D_ASSERT_TIMED
CFLAGS += -I./src/crypt -I./src/data -I./src/net -I./src/sys -I./src
LIBS = _SSL_LIBS_ -lpthread
NAME = libxutils.a
Expand Down
2 changes: 1 addition & 1 deletion misc/smake.tmp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"build": {
"name": "libxutils.a",
"outputDir": "./build",
"flags": "-g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_GNU",
"flags": "-g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_GNU -D_ASSERT_TIMED",
"libs": "-lpthread",
"overwrite": true,
"verbose": 2,
Expand Down
2 changes: 1 addition & 1 deletion smake.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"build": {
"name": "libxutils.a",
"outputDir": "./build",
"flags": "-g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_GNU",
"flags": "-g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_GNU -D_ASSERT_TIMED",
"libs": "-lpthread",
"overwrite": true,
"verbose": 2,
Expand Down
21 changes: 21 additions & 0 deletions src/xdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ typedef uint8_t xbool_t;
#define XLOCATION_LVL2(line) XLOCATION_LVL1(line)
#define __XLOCATION__ XLOCATION_LVL2(__LINE__)

#ifdef _ASSERT_TIMED
#include <time.h>
#define XTROW_LOCATION \
do { \
char timeStr[20]; \
time_t now = time(NULL); \
struct tm *tmInfo = localtime(&now); \
strftime(timeStr, sizeof(timeStr), \
"%H:%M:%S.%03d", tmInfo ); \
printf("%s %s<error>%s " \
"Assert failed: " \
"%s:%s():%s\n", \
timeStr, \
XCLR_RED, XCLR_RES, \
__FILE__, \
__FUNCTION__, \
__XLOCATION__); \
} \
while (XSTDNON)
#else
#define XTROW_LOCATION \
printf("%s<error>%s " \
"Assert failed: " \
Expand All @@ -118,6 +138,7 @@ typedef uint8_t xbool_t;
__FILE__, \
__FUNCTION__, \
__XLOCATION__)
#endif

#define XASSERT_RET(condition, value) \
if (!condition) return value
Expand Down
2 changes: 1 addition & 1 deletion src/xver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define XUTILS_VERSION_MAX 2
#define XUTILS_VERSION_MIN 5
#define XUTILS_BUILD_NUMBER 52
#define XUTILS_BUILD_NUMBER 53

#ifdef __cplusplus
extern "C" {
Expand Down

0 comments on commit ca7cdb9

Please sign in to comment.