glpenv04.c 3.8 KB

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