| 1 | #include "utils.h" |
| 2 | |
| 3 | #include <stdlib.h> |
| 4 | #include <stdarg.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include <stdbool.h> |
| 8 | |
| 9 | bool panic_use_colours = false; |
| 10 | |
| 11 | /* TODO these are actually a duplicate of the ones in text_reporter.c */ |
| 12 | #define GREEN "\x1b[32m" |
| 13 | #define YELLOW "\x1b[33m" |
| 14 | #define RED "\x1b[31m" |
| 15 | #define MAGENTA "\x1b[35m" |
| 16 | #define RESET "\x1b[0m" |
| 17 | |
| 18 | #include "../gitrevision.h" |
| 19 | |
| 20 | char *cgreen_library_version = VERSION; |
| 21 | char *cgreen_library_revision = GITREVISION; |
| 22 | |
| 23 | char *string_dup(const char *string) { |
| 24 | char *dup = (char *)malloc(strlen(string)+1); |
| 25 | if (dup) |
| 26 | strcpy(dup, string); |
| 27 | return dup; |
| 28 | } |
| 29 | |
| 30 | |
| 31 | static char *panic_message_buffer = NULL; |
| 32 | |
| 33 | /* For unittesting */ |
| 34 | void panic_set_output_buffer(const char *buffer) { |
| 35 | panic_message_buffer = (char *)buffer; |
| 36 | } |
| 37 | |
| 38 | void panic(const char *filename, int line, const char *fmt, ...) { |
| 39 | va_list args; |
| 40 | char buffer[1000]; |
| 41 | |
| 42 | va_start(args, fmt); |
| 43 | sprintf(buffer, "%sCGREEN EXCEPTION%s: <%s:%d>", |
| 44 | panic_use_colours?MAGENTA:"", |
| 45 | panic_use_colours?RESET:"", |
| 46 | filename, line); |
| 47 | vsprintf(&buffer[strlen(buffer)], fmt, args); |
| 48 | va_end(args); |
| 49 | |
| 50 | if (panic_message_buffer != NULL) |
| 51 | strcpy(panic_message_buffer, buffer); |
| 52 | else |
| 53 | fprintf(stderr, "%s\n", buffer); |
| 54 | } |