alpar@9: /* glpenv04.c (error handling) */ alpar@9: alpar@9: /*********************************************************************** alpar@9: * This code is part of GLPK (GNU Linear Programming Kit). alpar@9: * alpar@9: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@9: * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics, alpar@9: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@9: * E-mail: . alpar@9: * alpar@9: * GLPK is free software: you can redistribute it and/or modify it alpar@9: * under the terms of the GNU General Public License as published by alpar@9: * the Free Software Foundation, either version 3 of the License, or alpar@9: * (at your option) any later version. alpar@9: * alpar@9: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@9: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@9: * License for more details. alpar@9: * alpar@9: * You should have received a copy of the GNU General Public License alpar@9: * along with GLPK. If not, see . alpar@9: ***********************************************************************/ alpar@9: alpar@9: #include "glpapi.h" alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_error - display error message and terminate execution alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_error(const char *fmt, ...); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_error (implemented as a macro) formats its alpar@9: * parameters using the format control string fmt, writes the formatted alpar@9: * message to the terminal, and abnormally terminates the program. */ alpar@9: alpar@9: static void error(const char *fmt, ...) alpar@9: { ENV *env = get_env_ptr(); alpar@9: va_list arg; alpar@9: env->term_out = GLP_ON; alpar@9: va_start(arg, fmt); alpar@9: xvprintf(fmt, arg); alpar@9: va_end(arg); alpar@9: xprintf("Error detected in file %s at line %d\n", env->err_file, alpar@9: env->err_line); alpar@9: if (env->err_hook != NULL) alpar@9: env->err_hook(env->err_info); alpar@9: abort(); alpar@9: exit(EXIT_FAILURE); alpar@9: /* no return */ alpar@9: } alpar@9: alpar@9: _glp_error glp_error_(const char *file, int line) alpar@9: { ENV *env = get_env_ptr(); alpar@9: env->err_file = file; alpar@9: env->err_line = line; alpar@9: return error; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_assert - check for logical condition alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * #include "glplib.h" alpar@9: * void glp_assert(int expr); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_assert (implemented as a macro) checks for a logical alpar@9: * condition specified by the parameter expr. If the condition is false alpar@9: * (i.e. the value of expr is zero), the routine writes a message to alpar@9: * the terminal and abnormally terminates the program. */ alpar@9: alpar@9: void glp_assert_(const char *expr, const char *file, int line) alpar@9: { glp_error_(file, line)("Assertion failed: %s\n", expr); alpar@9: /* no return */ alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_error_hook - install hook to intercept abnormal termination alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_error_hook(void (*func)(void *info), void *info); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_error_hook installs a user-defined hook routine to alpar@9: * intercept abnormal termination. alpar@9: * alpar@9: * The parameter func specifies the user-defined hook routine. It is alpar@9: * called from the routine glp_error before the latter calls the abort alpar@9: * function to abnormally terminate the application program because of alpar@9: * fatal error. The parameter info is a transit pointer, specified in alpar@9: * the corresponding call to the routine glp_error_hook; it may be used alpar@9: * to pass some information to the hook routine. alpar@9: * alpar@9: * To uninstall the hook routine the parameters func and info should be alpar@9: * specified as NULL. */ alpar@9: alpar@9: void glp_error_hook(void (*func)(void *info), void *info) alpar@9: { ENV *env = get_env_ptr(); alpar@9: if (func == NULL) alpar@9: { env->err_hook = NULL; alpar@9: env->err_info = NULL; alpar@9: } alpar@9: else alpar@9: { env->err_hook = func; alpar@9: env->err_info = info; alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /* eof */