|
1 | 1 | # C Traceback |
2 | 2 | A colorful, lightweight error-propagation framework for C. |
3 | 3 |
|
4 | | -Website: [https://www.ctraceback.com](https://www.ctraceback.com) |
| 4 | +Documentation Website: [https://www.ctraceback.com](https://www.ctraceback.com) |
5 | 5 |
|
6 | | -**This library is in early development. Stay tuned!** |
| 6 | + |
7 | 7 |
|
8 | 8 | ## Features |
9 | 9 | * Beautiful tracebacks |
10 | | -* Exceptions in C |
| 10 | +* Works with Signal Handlers |
| 11 | +* Fast and Thread-safe |
11 | 12 | * Explicit control flow |
12 | | -* Thread-safe |
13 | | -* Low performance overhead |
14 | | -* Written in C99 with minimal dependencies |
15 | 13 | * Works with MSVC, Clang and GCC |
| 14 | +* Written in C99 with minimal dependencies |
16 | 15 | * Detailed documentations |
17 | 16 |
|
18 | | -## To-dos before first release |
19 | | -* Test MSVC |
20 | | -* Finish docs |
| 17 | +## Sample usage |
| 18 | +```c |
| 19 | +#include <stdio.h> |
| 20 | +#include "c_traceback.h" |
| 21 | + |
| 22 | +#define N 100 |
| 23 | + |
| 24 | +static void do_calculation(double *vec); |
| 25 | + |
| 26 | +int main(void) |
| 27 | +{ |
| 28 | + ctb_clear_context(); |
| 29 | + ctb_install_signal_handlers(); |
| 30 | + |
| 31 | + double *vec = malloc(N * sizeof(double)); |
| 32 | + if (!vec) |
| 33 | + { |
| 34 | + THROW(CTB_MEMORY_ERROR, "Failed to allocate memory"); |
| 35 | + goto error; |
| 36 | + } |
| 37 | + |
| 38 | + TRY_GOTO(do_calculation(vec), error); |
| 39 | + printf("This shouldn't be printed if there is error"); |
| 40 | + |
| 41 | + free(vec); |
| 42 | + return 0; |
| 43 | + |
| 44 | +error: |
| 45 | + free(vec); |
| 46 | + ctb_dump_traceback(); // Log traceback and reset context |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +static void do_calculation(double *vec) |
| 51 | +{ |
| 52 | + // Initialize array |
| 53 | + for (int i = 0; i < N; i++) |
| 54 | + { |
| 55 | + vec[i] = 0; |
| 56 | + } |
| 57 | + |
| 58 | + // Calculations |
| 59 | + for (int i = 0; i < N; i++) |
| 60 | + { |
| 61 | + vec[i] += 10; |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
21 | 65 |
|
22 | 66 | ## Support Us |
23 | 67 | * ⭐ Give us a star |
24 | | -* ❤️ Sponsor |
| 68 | +<!-- * ❤️ Sponsor --> |
25 | 69 | * 👤 [Follow](https://github.com/alvinng4) |
0 commit comments