-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic.c
38 lines (32 loc) · 858 Bytes
/
panic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "panic.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include "vector.h"
jmp_buf _panicJump;
static int catchLevel = 0;
void _catchPanic() { catchLevel++; }
void _releasePanic() { catchLevel--; }
void _panicFailure(uint16_t code) {
printf("\033[0;31m--- UNCAUGHT ---\033[0m\n");
exit(code & _PANIC_CODE_MASK);
}
void panic(uint16_t code, char* fmt, ...) {
#ifndef OCRPI_DEBUG
if (catchLevel > 0 && (code & _PANIC_CATCHABLE_FLAG)) longjmp(_panicJump, code);
#endif
printf("\033[0;31m");
va_list va;
va_start(va, fmt);
vprintf(fmt, va);
va_end(va);
printf("\033[0m\n");
#ifdef OCRPI_DEBUG
if (catchLevel > 0 && (code & _PANIC_CATCHABLE_FLAG)) {
printf("[panic caught!]\n");
longjmp(_panicJump, code);
}
#endif
exit(code & _PANIC_CODE_MASK);
}