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