g_error.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file contains the generic warning and error methods. They simply
  16. write the specified message to the plotter error stream, if it has one.
  17. There is provision for user-specifiable warning/error message handlers
  18. (not yet documented). */
  19. /* All libplot warnings and error messages go through these functions, with
  20. the exception of libpng error messages produced by PNG Plotters (see
  21. z_write.c; they're different because they need to be produced by
  22. callbacks). */
  23. #include "sys-defines.h"
  24. #include "extern.h"
  25. /* mutex for locking the warning/error message subsystem */
  26. #ifdef PTHREAD_SUPPORT
  27. #ifdef HAVE_PTHREAD_H
  28. pthread_mutex_t _message_mutex = PTHREAD_MUTEX_INITIALIZER;
  29. #endif
  30. #endif
  31. /* user-settable handlers, defined in g_defplot.c to be NULL */
  32. extern int (*pl_libplot_warning_handler) (const char *msg);
  33. extern int (*pl_libplot_error_handler) (const char *msg);
  34. void
  35. _pl_g_warning (R___(Plotter *_plotter) const char *msg)
  36. {
  37. #ifdef PTHREAD_SUPPORT
  38. #ifdef HAVE_PTHREAD_H
  39. /* lock the message subsystem */
  40. pthread_mutex_lock (&_message_mutex);
  41. #endif
  42. #endif
  43. if (pl_libplot_warning_handler != NULL)
  44. (*pl_libplot_warning_handler)(msg);
  45. else if (_plotter->data->errfp)
  46. fprintf (_plotter->data->errfp, "libplot: %s\n", msg);
  47. #ifdef LIBPLOTTER
  48. else if (_plotter->data->errstream)
  49. (*(_plotter->data->errstream)) << "libplot: " << msg << '\n';
  50. #endif
  51. #ifdef PTHREAD_SUPPORT
  52. #ifdef HAVE_PTHREAD_H
  53. /* unlock the message subsystem */
  54. pthread_mutex_unlock (&_message_mutex);
  55. #endif
  56. #endif
  57. }
  58. void
  59. _pl_g_error (R___(Plotter *_plotter) const char *msg)
  60. {
  61. #ifdef PTHREAD_SUPPORT
  62. #ifdef HAVE_PTHREAD_H
  63. /* lock the message subsystem */
  64. pthread_mutex_lock (&_message_mutex);
  65. #endif
  66. #endif
  67. if (pl_libplot_error_handler != NULL)
  68. (*pl_libplot_error_handler)(msg);
  69. else if (_plotter->data->errfp)
  70. fprintf (_plotter->data->errfp, "libplot error: %s\n", msg);
  71. #ifdef LIBPLOTTER
  72. else if (_plotter->data->errstream)
  73. (*(_plotter->data->errstream)) << "libplot error: " << msg << '\n';
  74. #endif
  75. #ifdef PTHREAD_SUPPORT
  76. #ifdef HAVE_PTHREAD_H
  77. /* unlock the message subsystem */
  78. pthread_mutex_unlock (&_message_mutex);
  79. #endif
  80. #endif
  81. }