1 | /* glpenv04.c (error handling) */ |
---|
2 | |
---|
3 | /*********************************************************************** |
---|
4 | * This code is part of GLPK (GNU Linear Programming Kit). |
---|
5 | * |
---|
6 | * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
---|
7 | * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics, |
---|
8 | * Moscow Aviation Institute, Moscow, Russia. All rights reserved. |
---|
9 | * E-mail: <mao@gnu.org>. |
---|
10 | * |
---|
11 | * GLPK is free software: you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation, either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * GLPK is distributed in the hope that it will be useful, but WITHOUT |
---|
17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
18 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
---|
19 | * License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU General Public License |
---|
22 | * along with GLPK. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | ***********************************************************************/ |
---|
24 | |
---|
25 | #include "glpapi.h" |
---|
26 | |
---|
27 | /*********************************************************************** |
---|
28 | * NAME |
---|
29 | * |
---|
30 | * glp_error - display error message and terminate execution |
---|
31 | * |
---|
32 | * SYNOPSIS |
---|
33 | * |
---|
34 | * void glp_error(const char *fmt, ...); |
---|
35 | * |
---|
36 | * DESCRIPTION |
---|
37 | * |
---|
38 | * The routine glp_error (implemented as a macro) formats its |
---|
39 | * parameters using the format control string fmt, writes the formatted |
---|
40 | * message to the terminal, and abnormally terminates the program. */ |
---|
41 | |
---|
42 | static void error(const char *fmt, ...) |
---|
43 | { ENV *env = get_env_ptr(); |
---|
44 | va_list arg; |
---|
45 | env->term_out = GLP_ON; |
---|
46 | va_start(arg, fmt); |
---|
47 | xvprintf(fmt, arg); |
---|
48 | va_end(arg); |
---|
49 | xprintf("Error detected in file %s at line %d\n", env->err_file, |
---|
50 | env->err_line); |
---|
51 | if (env->err_hook != NULL) |
---|
52 | env->err_hook(env->err_info); |
---|
53 | abort(); |
---|
54 | exit(EXIT_FAILURE); |
---|
55 | /* no return */ |
---|
56 | } |
---|
57 | |
---|
58 | _glp_error glp_error_(const char *file, int line) |
---|
59 | { ENV *env = get_env_ptr(); |
---|
60 | env->err_file = file; |
---|
61 | env->err_line = line; |
---|
62 | return error; |
---|
63 | } |
---|
64 | |
---|
65 | /*********************************************************************** |
---|
66 | * NAME |
---|
67 | * |
---|
68 | * glp_assert - check for logical condition |
---|
69 | * |
---|
70 | * SYNOPSIS |
---|
71 | * |
---|
72 | * #include "glplib.h" |
---|
73 | * void glp_assert(int expr); |
---|
74 | * |
---|
75 | * DESCRIPTION |
---|
76 | * |
---|
77 | * The routine glp_assert (implemented as a macro) checks for a logical |
---|
78 | * condition specified by the parameter expr. If the condition is false |
---|
79 | * (i.e. the value of expr is zero), the routine writes a message to |
---|
80 | * the terminal and abnormally terminates the program. */ |
---|
81 | |
---|
82 | void glp_assert_(const char *expr, const char *file, int line) |
---|
83 | { glp_error_(file, line)("Assertion failed: %s\n", expr); |
---|
84 | /* no return */ |
---|
85 | } |
---|
86 | |
---|
87 | /*********************************************************************** |
---|
88 | * NAME |
---|
89 | * |
---|
90 | * glp_error_hook - install hook to intercept abnormal termination |
---|
91 | * |
---|
92 | * SYNOPSIS |
---|
93 | * |
---|
94 | * void glp_error_hook(void (*func)(void *info), void *info); |
---|
95 | * |
---|
96 | * DESCRIPTION |
---|
97 | * |
---|
98 | * The routine glp_error_hook installs a user-defined hook routine to |
---|
99 | * intercept abnormal termination. |
---|
100 | * |
---|
101 | * The parameter func specifies the user-defined hook routine. It is |
---|
102 | * called from the routine glp_error before the latter calls the abort |
---|
103 | * function to abnormally terminate the application program because of |
---|
104 | * fatal error. The parameter info is a transit pointer, specified in |
---|
105 | * the corresponding call to the routine glp_error_hook; it may be used |
---|
106 | * to pass some information to the hook routine. |
---|
107 | * |
---|
108 | * To uninstall the hook routine the parameters func and info should be |
---|
109 | * specified as NULL. */ |
---|
110 | |
---|
111 | void glp_error_hook(void (*func)(void *info), void *info) |
---|
112 | { ENV *env = get_env_ptr(); |
---|
113 | if (func == NULL) |
---|
114 | { env->err_hook = NULL; |
---|
115 | env->err_info = NULL; |
---|
116 | } |
---|
117 | else |
---|
118 | { env->err_hook = func; |
---|
119 | env->err_info = info; |
---|
120 | } |
---|
121 | return; |
---|
122 | } |
---|
123 | |
---|
124 | /* eof */ |
---|