0d226a1732ed45a2825ac02ce263780f769947ee
[pspp] / src / libpspp / message.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "message.h"
20 #include "msg-locator.h"
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include <libpspp/str.h>
30 #include <libpspp/version.h>
31 #include <data/settings.h>
32
33 #include "gl/progname.h"
34 #include "gl/xalloc.h"
35 #include "gl/xvasprintf.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* Message handler as set by msg_init(). */
41 static void (*msg_handler)  (const struct msg *);
42
43 /* Disables emitting messages if positive. */
44 static int messages_disabled;
45
46 /* Public functions. */
47
48 /* Writes error message in CLASS, with text FORMAT, formatted with
49    printf, to the standard places. */
50 void
51 msg (enum msg_class class, const char *format, ...)
52 {
53   struct msg m;
54   va_list args;
55
56   m.category = msg_class_to_category (class);
57   m.severity = msg_class_to_severity (class);
58   va_start (args, format);
59   m.text = xvasprintf (format, args);
60   va_end (args);
61
62   msg_emit (&m);
63 }
64
65 static struct source_stream *s_stream;
66
67 void
68 msg_init (struct source_stream *ss,  void (*handler) (const struct msg *) )
69 {
70   s_stream = ss;
71   msg_handler = handler;
72 }
73
74 void
75 msg_done (void)
76 {
77 }
78 \f
79 /* Working with messages. */
80
81 /* Duplicate a message */
82 struct msg *
83 msg_dup (const struct msg *m)
84 {
85   struct msg *new_msg;
86
87   new_msg = xmemdup (m, sizeof *m);
88   if (m->where.file_name != NULL)
89     new_msg->where.file_name = xstrdup (m->where.file_name);
90   new_msg->text = xstrdup (m->text);
91
92   return new_msg;
93 }
94
95 /* Frees a message created by msg_dup().
96
97    (Messages not created by msg_dup(), as well as their where.file_name
98    members, are typically not dynamically allocated, so this function should
99    not be used to destroy them.) */
100 void
101 msg_destroy (struct msg *m)
102 {
103   free (m->where.file_name);
104   free (m->text);
105   free (m);
106 }
107
108 char *
109 msg_to_string (const struct msg *m, const char *command_name)
110 {
111   const char *label;
112   struct string s;
113
114   ds_init_empty (&s);
115
116   if (m->category != MSG_C_GENERAL
117       && (m->where.file_name || m->where.line_number != -1))
118     {
119       if (m->where.file_name)
120         ds_put_format (&s, "%s:", m->where.file_name);
121       if (m->where.line_number != -1)
122         ds_put_format (&s, "%d:", m->where.line_number);
123       ds_put_char (&s, ' ');
124     }
125
126   switch (m->severity)
127     {
128     case MSG_S_ERROR:
129       label = _("error");
130       break;
131     case MSG_S_WARNING:
132       label = _("warning");
133       break;
134     case MSG_S_NOTE:
135     default:
136       label = _("note");
137       break;
138     }
139   ds_put_format (&s, "%s: ", label);
140
141   if (m->category == MSG_C_SYNTAX && command_name != NULL)
142     ds_put_format (&s, "%s: ", command_name);
143
144   ds_put_cstr (&s, m->text);
145
146   return ds_cstr (&s);
147 }
148 \f
149
150 /* Number of messages reported, by severity level. */
151 static int counts[MSG_N_SEVERITIES];
152
153 /* True after the maximum number of errors or warnings has been exceeded. */
154 static bool too_many_errors;
155
156 /* True after the maximum number of notes has been exceeded. */
157 static bool too_many_notes;
158
159 /* Checks whether we've had so many errors that it's time to quit
160    processing this syntax file. */
161 bool
162 msg_ui_too_many_errors (void)
163 {
164   return too_many_errors;
165 }
166
167 void
168 msg_ui_reset_counts (void)
169 {
170   int i;
171
172   for (i = 0; i < MSG_N_SEVERITIES; i++)
173     counts[i] = 0;
174   too_many_errors = false;
175   too_many_notes = false;
176 }
177
178 bool
179 msg_ui_any_errors (void)
180 {
181   return counts[MSG_S_ERROR] > 0;
182 }
183
184 static void
185 submit_note (char *s)
186 {
187   struct msg m;
188
189   m.category = MSG_C_GENERAL;
190   m.severity = MSG_S_NOTE;
191   m.where.file_name = NULL;
192   m.where.line_number = -1;
193   m.text = s;
194   msg_handler (&m);
195   free (s);
196 }
197
198 static void
199 process_msg (const struct msg *m)
200 {
201   int n_msgs, max_msgs;
202
203   if (too_many_errors || (too_many_notes && m->severity == MSG_S_NOTE))
204     return;
205
206   msg_handler (m);
207
208   counts[m->severity]++;
209   max_msgs = settings_get_max_messages (m->severity);
210   n_msgs = counts[m->severity];
211   if (m->severity == MSG_S_WARNING)
212     n_msgs += counts[MSG_S_ERROR];
213   if (n_msgs > max_msgs)
214     {
215       if (m->severity == MSG_S_NOTE)
216         {
217           too_many_notes = true;
218           submit_note (xasprintf (_("Notes (%d) exceed limit (%d).  "
219                                     "Suppressing further notes."),
220                                   n_msgs, max_msgs));
221         }
222       else
223         {
224           too_many_errors = true;
225           if (m->severity == MSG_S_WARNING)
226             submit_note (xasprintf (_("Warnings (%d) exceed limit (%d)."),
227                                     n_msgs, max_msgs));
228           else
229             submit_note (xasprintf (_("Errors (%d) exceed limit (%d)."),
230                                     n_msgs, max_msgs));
231         }
232     }
233 }
234
235
236 /* Emits M as an error message.
237    Frees allocated data in M. */
238 void
239 msg_emit (struct msg *m)
240 {
241   if ( s_stream )
242     get_msg_location (s_stream, &m->where);
243   else
244     {
245       m->where.file_name = NULL;
246       m->where.line_number = -1;
247     }
248
249   if (!messages_disabled)
250      process_msg (m);
251
252   free (m->text);
253 }
254
255 /* Disables message output until the next call to msg_enable.  If
256    this function is called multiple times, msg_enable must be
257    called an equal number of times before messages are actually
258    re-enabled. */
259 void
260 msg_disable (void)
261 {
262   messages_disabled++;
263 }
264
265 /* Enables message output that was disabled by msg_disable. */
266 void
267 msg_enable (void)
268 {
269   assert (messages_disabled > 0);
270   messages_disabled--;
271 }
272 \f
273 /* Private functions. */
274
275 void
276 request_bug_report_and_abort (const char *msg)
277 {
278   fprintf (stderr, "******************************************************\n");
279   fprintf (stderr, "You have discovered a bug in PSPP.  Please report this\n");
280   fprintf (stderr, "to " PACKAGE_BUGREPORT ".  Please include this entire\n");
281   fprintf (stderr, "message, *plus* several lines of output just above it.\n");
282   fprintf (stderr, "For the best chance at having the bug fixed, also\n");
283   fprintf (stderr, "include the syntax file that triggered it and a sample\n");
284   fprintf (stderr, "of any data file used for input.\n");
285   fprintf (stderr, "proximate cause:     %s\n", msg);
286   fprintf (stderr, "version:             %s\n", stat_version);
287   fprintf (stderr, "host_system:         %s\n", host_system);
288   fprintf (stderr, "build_system:        %s\n", build_system);
289   fprintf (stderr, "locale_dir:          %s\n", locale_dir);
290   fprintf (stderr, "compiler version:    %s\n",
291 #ifdef __VERSION__
292            __VERSION__
293 #else
294            "Unknown"
295 #endif
296            );
297   fprintf (stderr, "******************************************************\n");
298
299   _exit (EXIT_FAILURE);
300 }
301