mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2025-07-18 08:37:43 +00:00
ggml : Callback before abort (#14481)
* Add a callback that will be called just before abort. This allows apps without a console to display a message to the user and save data if needed. * Return previous callback to allow callback chaining * style fixes --------- Co-authored-by: Diego Devesa <slarengh@gmail.com>
This commit is contained in:
@ -314,6 +314,13 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Function type used in fatal error callbacks
|
||||||
|
typedef void (*ggml_abort_callback_t)(const char * error_message);
|
||||||
|
|
||||||
|
// Set the abort callback (passing null will restore original abort functionality: printing a message to stdout)
|
||||||
|
// Returns the old callback for chaining
|
||||||
|
GGML_API ggml_abort_callback_t ggml_set_abort_callback(ggml_abort_callback_t callback);
|
||||||
|
|
||||||
GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4)
|
GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4)
|
||||||
GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...);
|
GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...);
|
||||||
|
|
||||||
|
@ -202,19 +202,34 @@ void ggml_print_backtrace(void) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static ggml_abort_callback_t g_abort_callback = NULL;
|
||||||
|
|
||||||
|
// Set the abort callback (passing null will restore original abort functionality: printing a message to stdout)
|
||||||
|
GGML_API ggml_abort_callback_t ggml_set_abort_callback(ggml_abort_callback_t callback) {
|
||||||
|
ggml_abort_callback_t ret_val = g_abort_callback;
|
||||||
|
g_abort_callback = callback;
|
||||||
|
return ret_val;
|
||||||
|
}
|
||||||
|
|
||||||
void ggml_abort(const char * file, int line, const char * fmt, ...) {
|
void ggml_abort(const char * file, int line, const char * fmt, ...) {
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
fprintf(stderr, "%s:%d: ", file, line);
|
char message[2048];
|
||||||
|
int offset = snprintf(message, sizeof(message), "%s:%d: ", file, line);
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
vfprintf(stderr, fmt, args);
|
vsnprintf(message + offset, sizeof(message) - offset, fmt, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
fprintf(stderr, "\n");
|
if (g_abort_callback) {
|
||||||
|
g_abort_callback(message);
|
||||||
|
} else {
|
||||||
|
// default: print error and backtrace to stderr
|
||||||
|
fprintf(stderr, "%s\n", message);
|
||||||
|
ggml_print_backtrace();
|
||||||
|
}
|
||||||
|
|
||||||
ggml_print_backtrace();
|
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user