1 /*
2 * This file is part of the XForms library package.
3 *
4 * XForms is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1, or
7 * (at your option) any later version.
8 *
9 * XForms is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with XForms. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19 /**
20 * \file errmsg.c
21 *
22 * Copyright(c) 1993,1994 by T.C. Zhao
23 * All rights reserved.
24 *
25 * Error handling routines.
26 *
27 * Messages are divided into graphics and non-graphics types.
28 * For graphical error messages, a user input may be demanded,
29 * while for non-graphical messages, a string is printed and
30 * does nothing else.
31 *
32 * The graphical output routine must have the following form:
33 * void (*gmout)(const char *, const char *, const char *, int);
34 */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <sys/types.h>
44 #include <stdarg.h>
45 #include <errno.h>
46 #include "local.h" /* up stairs */
47
48 #include "include/forms.h"
49 #include "flinternal.h"
50 #include "private/flvasprintf.h"
51 #include "ulib.h"
52
53 extern int errno; /* system error no */
54
55 #ifndef HAVE_STRERROR
56 extern char *sys_errlist[ ];
57 #endif
58
59
60 /**********************************************************************
61 * msg_threshold controls amount of message to print
62 * 0: only error 1: error and warning
63 * 2: info 3: more info
64 * 4: debugging 5: trace
65 **********************************************************************/
66
67 /************ Local variables ****************************************/
68
69 static FILE *errlog; /* where the msg is going */
70 static int threshold; /* current threshold */
71 static int level; /* requested message level */
72 static const char *file; /* source file name */
73 static int lineno = 0; /* line no. in that file */
74
75
76 FL_ERROR_FUNC efp_; /* global pointer to shut up lint */
77 FL_ERROR_FUNC user_error_function_; /* hook for application error handler */
78
79
80 /***************************************
81 * set up where err is gonna go
82 ***************************************/
83
84 void
fl_set_err_logfp(FILE * fp)85 fl_set_err_logfp( FILE * fp )
86 {
87 if ( fp )
88 errlog = fp;
89 }
90
91
92 /***************************************
93 ***************************************/
94
95 void
fl_set_error_handler(FL_ERROR_FUNC user_func)96 fl_set_error_handler( FL_ERROR_FUNC user_func)
97 {
98 user_error_function_ = user_func;
99 }
100
101
102 /***************************************
103 ***************************************/
104
105 const char *
fli_get_syserror_msg(void)106 fli_get_syserror_msg( void )
107 {
108 const char *pp;
109
110 #ifdef HAVE_STRERROR
111 pp = errno ? strerror( errno ) : "";
112 #else
113 pp = errno ? sys_errlist[ errno ] : "";
114 #endif
115
116 return pp;
117 }
118
119
120 /***************************************
121 * Message levels
122 ***************************************/
123
124 void
fli_set_msg_threshold(int mlevel)125 fli_set_msg_threshold( int mlevel )
126 {
127 threshold = mlevel;
128 }
129
130
131 /********************************************************************
132 * Generate two strings that contain where and why an error occured
133 *********************************************************************/
134
135 static void
P_errmsg(const char * func,const char * fmt,...)136 P_errmsg( const char * func,
137 const char * fmt,
138 ... )
139 {
140 char line[ ( int ) log10( INT_MAX ) + 3 ],
141 *why;
142
143 /* Return if there is nothing to do */
144
145 if ( level >= threshold )
146 return;
147
148 if ( ! errlog )
149 errlog = stderr;
150
151 EXPAND_FORMAT_STRING( why, fmt );
152
153 if ( lineno > 0 )
154 sprintf( line, "%d", lineno );
155 else
156 strcpy( line, "?" );
157
158 if ( func && *func )
159 fprintf( errlog, "In %s() [%s:%s]: %s\n", func, file, line,
160 why ? why : "" );
161 else
162 fprintf( errlog, "In [%s:%s]: %s\n", file, line, why ? why : "" );
163
164 fli_safe_free( why );
165 }
166
167
168 /*********************************************************************
169 * Set the level, line number and file where error occurred, return
170 * the function to use for outputting the error message
171 ********************************************************************/
172
173 FL_ERROR_FUNC
fli_error_setup(int lev,const char * f,int l)174 fli_error_setup( int lev,
175 const char * f,
176 int l )
177 {
178 file = f;
179 lineno = l;
180 level = lev;
181
182 return user_error_function_ ? user_error_function_ : P_errmsg;
183 }
184
185
186 /*
187 * Local variables:
188 * tab-width: 4
189 * indent-tabs-mode: nil
190 * End:
191 */
192