src/glpenv04.c
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

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