libdev.a

C Helper library
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/libdev.a

commit b77d4e9c863eabff0a9926b3c9c313085e991e59
parent 8d5c8b325303db915cc6f7760548de934c5fd319
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri,  7 Oct 2016 18:53:05 +0200

Add(Assert):

Add the L_ASSERT macro, who does an assert, and if it failed, print the backtrace with basic informations.

Diffstat:
Mlibdev.h | 11+++++++++++
Mprint.c | 10++++------
2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/libdev.h b/libdev.h @@ -27,6 +27,16 @@ # define BACKTRACE_SIZE 10 +#define L_ASSERT(condition)\ +{\ + if (!condition) {\ + fprintf(stderr, "Assertion (%s) failed at %s:%d\n", #condition, __FILE__, __LINE__);\ + fprintf(stderr, "Function: %s\n", __FUNCTION__);\ + fprintf(stderr, "Backtrace:\n");\ + print_trace();\ + }\ +} + /* TYPEDEFS */ // Signed typedef signed char s8_t; @@ -71,5 +81,6 @@ t_list *singleton_lists(u_char list_type, t_list *ptr); void error(char *str, ...); void info(char *str, ...); void warning(char *str, ...); +void print_trace(void); #endif /* __LIBDEV__ */ diff --git a/print.c b/print.c @@ -1,6 +1,6 @@ #include <libdev.h> -static void print_trace(void) { +void print_trace(void) { void *array[BACKTRACE_SIZE]; char **strings; size_t size, i; @@ -8,11 +8,9 @@ static void print_trace(void) { size = backtrace(array, BACKTRACE_SIZE); strings = backtrace_symbols(array, size); for (i = 0; i < size; i++) { - write(1, "\033[0;37m> \033[0m", 13); - if (*strings[i] == '/') - write(1, "\t", 1); - write(1, strings[i], strlen(strings[i])); - write(1, "\n", 1); + write(2, "\033[0;37m> \033[0m", 13); + write(2, strings[i], strlen(strings[i])); + write(2, "\n", 1); } }