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