43 lines
887 B
C
43 lines
887 B
C
#ifndef __PANIC_H__
|
|
#define __PANIC_H__
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <execinfo.h>
|
|
|
|
#ifndef PANIC_FD
|
|
#define PANIC_FD stderr
|
|
#endif //ifndef PANIC_FD
|
|
|
|
#ifndef PANIC_STRACE_LEN
|
|
#define PANIC_STRACE_LEN 32
|
|
#endif //ifndef PANIC_STRACE_LEN
|
|
|
|
void print_stacktrace(FILE* fd);
|
|
|
|
#define panic(...) \
|
|
do { \
|
|
fprintf(PANIC_FD, "Panic at %s:%d\n", __FILE__, __LINE__); \
|
|
fprintf(PANIC_FD, "Panic message:\n\t" __VA_ARGS__); \
|
|
print_stacktrace(PANIC_FD); \
|
|
abort(); \
|
|
} while(0)
|
|
|
|
#define NULL_GUARD(ptr, ...) \
|
|
do { \
|
|
if((ptr) == NULL) \
|
|
{ \
|
|
if(sizeof(#__VA_ARGS__) > 1) \
|
|
{ \
|
|
panic(__VA_ARGS__); \
|
|
} \
|
|
else \
|
|
{ \
|
|
panic("NULL detected in %s", #ptr); \
|
|
} \
|
|
} \
|
|
} while(0)
|
|
|
|
|
|
#endif //ifndef __PANIC_H__
|