WIP: add sml_error function pointer, allow user to intercept error messages#93
WIP: add sml_error function pointer, allow user to intercept error messages#93r00t- wants to merge 1 commit intovolkszaehler:masterfrom
Conversation
ab2acf3 to
9ad3a4d
Compare
9e84db4 to
b68e635
Compare
b68e635 to
1028ef1
Compare
| strcat(format2, "\n"); | ||
|
|
||
| vfprintf(stderr, format2, args); | ||
|
|
| va_list args; | ||
| va_start(args, format); | ||
|
|
||
| char *format2 = malloc(9 + strlen(format) + 1 + 1); |
There was a problem hiding this comment.
seems somewhat esoteric, as format will usually be static anyway.
but might throw in a quick `format=format?format:"(null)";
|
|
||
| char *format2 = malloc(9 + strlen(format) + 1 + 1); | ||
| strcpy(format2, "libsml: "); | ||
| strcat(format2, format); |
There was a problem hiding this comment.
strncpy/strncat? (some compilers/static code checker might sooner or later complain otherwise)
There was a problem hiding this comment.
seems needless as it's all static strings, i'd leave that to whoever adds that code-checker.
| va_list args; | ||
| va_start(args, format); | ||
|
|
||
| char *format2 = malloc(9 + strlen(format) + 1 + 1); |
There was a problem hiding this comment.
btw: why 9? the terminating zero is already at the end, or? So I'd expect
8 (= strlen("libsml: ") + ... + 1 (\n) + 1 (term. zero)
| void hexdump(unsigned char *buffer, size_t buffer_len); | ||
|
|
||
| // Allow user to intercept error messages | ||
| extern void (*sml_error)(const char *format, ...) |
There was a problem hiding this comment.
this forces anybody using this to call va_* and vaprintf himself. (see the example in sml_error_default() below)
i wonder if this is any good,
or if we should maybe rather have an intermediate function and pass out the completely formatted string?
There was a problem hiding this comment.
might also pass more details, like a loglevel (warning/error).
| va_list args; | ||
| va_start(args, format); | ||
|
|
||
| char *format2 = malloc(9 + strlen(format) + 1 + 1); |
There was a problem hiding this comment.
seems somewhat esoteric, as format will usually be static anyway.
but might throw in a quick `format=format?format:"(null)";
|
|
||
| char *format2 = malloc(9 + strlen(format) + 1 + 1); | ||
| strcpy(format2, "libsml: "); | ||
| strcat(format2, format); |
There was a problem hiding this comment.
seems needless as it's all static strings, i'd leave that to whoever adds that code-checker.
| va_list args; | ||
| va_start(args, format); | ||
|
|
||
| char *format2 = malloc(9 + strlen(format) + 1 + 1); |
|
@jahir: |
a basic attempt at fixing #17 .
replaces hardcoded
fprintf()calls with ansml_errorfunction pointer that can be overwritten by code using the library.a default is provided that mimics the old behaviour (write to stderr with a "libsml:" prefix and appended newline.
includes a test, test also demonstrates usage.
note that this breaks backwards compatibility (code assuming
sml_errorexists won't build or run with older versions of the library),so we should probably bump the version when merging this.
also we should consider if we maybe want a more sophisticated interface.