libdev.a

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

commit 8d5c8b325303db915cc6f7760548de934c5fd319
parent d2c63a20d88f514785b3928f097159c1b5a68be5
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri,  7 Oct 2016 18:36:55 +0200

Add(Print helpers):

Some nice prints with colors, with a backtrace on error.
As always, not tested at all.

Diffstat:
Mlibdev.h | 11+++++++++++
Aprint.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Msingleton.c | 1-
3 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/libdev.h b/libdev.h @@ -3,6 +3,10 @@ # include <stdlib.h> # include <assert.h> # include <string.h> +# include <execinfo.h> +# include <stdarg.h> +# include <stdio.h> +# include <unistd.h> /* DEFINES */ # define list_add(ptr, member, sizeZ) ptr = list_add_member(ptr, member, sizeZ); @@ -21,6 +25,8 @@ # define list_add_before(org_list, p_tr1, p_tr2, sizeZ) org_list = list_insert_before(org_list, p_tr1, p_tr2, sizeZ); +# define BACKTRACE_SIZE 10 + /* TYPEDEFS */ // Signed typedef signed char s8_t; @@ -61,4 +67,9 @@ size_t list_size(t_list *list); // singleton.c t_list *singleton_lists(u_char list_type, t_list *ptr); +// print.c +void error(char *str, ...); +void info(char *str, ...); +void warning(char *str, ...); + #endif /* __LIBDEV__ */ diff --git a/print.c b/print.c @@ -0,0 +1,46 @@ +#include <libdev.h> + +static void print_trace(void) { + void *array[BACKTRACE_SIZE]; + char **strings; + size_t size, i; + + 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); + } +} + +void error(char *str, ...) { + va_list ap; + + write(1, "\033[0;31m> \033[0m", 13); + va_start(ap, str); + vprintf(str, ap); + if (str[strlen(str) - 1] != '\n') + printf("\n"); + printf("\033[0;31m> Program must stop: Backtrace:\033[0m\n"); + print_trace(); + _exit(1); +} + +void info(char *str, ...) { + va_list ap; + + write(1, "\033[0;34m> \033[0m", 13); + va_start(ap, str); + vprintf(str, ap); +} + +void warning(char *str, ...) { + va_list ap; + + write(1, "\033[0;31m> \033[0m", 13); + va_start(ap, str); + vprintf(str, ap); +} diff --git a/singleton.c b/singleton.c @@ -33,4 +33,3 @@ t_list *singleton_lists(u_char list_type, t_list *ptr) { } return 0x0; } -