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