Finish reforming error message support. In this phase, move message.c
[pspp-builds.git] / src / libpspp / message.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <libpspp/message.h>
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include <libpspp/alloc.h>
29 #include <libpspp/version.h>
30
31 #include "progname.h"
32 #include "xvasprintf.h"
33
34 /* Current command name as set by msg_set_command_name(). */
35 static char *command_name;
36
37 /* Message handler as set by msg_init(). */
38 static void (*msg_handler) (const struct msg *);
39
40 /* Public functions. */
41
42 /* Writes error message in CLASS, with text FORMAT, formatted with
43    printf, to the standard places. */
44 void
45 msg (enum msg_class class, const char *format, ...)
46 {
47   struct msg m;
48   va_list args;
49
50   m.category = msg_class_to_category (class);
51   m.severity = msg_class_to_severity (class);
52   msg_location (&m.where);
53   va_start (args, format);
54   m.text = xvasprintf (format, args);
55   va_end (args);
56
57   msg_emit (&m);
58 }
59
60 void
61 msg_init (void (*handler) (const struct msg *)) 
62 {
63   msg_handler = handler;
64 }
65
66 void
67 msg_done (void) 
68 {
69 }
70
71 /* Emits M as an error message.
72    Frees allocated data in M. */
73 void
74 msg_emit (struct msg *m) 
75 {
76   msg_handler (m);
77   free (m->text);
78 }
79 \f
80 /* Private functions. */
81
82 /* Sets COMMAND_NAME as the command name included in some kinds
83    of error messages. */
84 void
85 msg_set_command_name (const char *command_name_) 
86 {
87   free (command_name);
88   command_name = command_name_ ? xstrdup (command_name_) : NULL;
89 }
90
91 /* Returns the current command name, or NULL if none. */
92 const char *
93 msg_get_command_name (void) 
94 {
95   return command_name;
96 }
97
98 void 
99 request_bug_report_and_abort(const char *msg )
100 {
101   fprintf(stderr,
102           "******************************************************************\n"
103           "You have discovered a bug in PSPP.\n\n"
104           "  Please report this, by sending "
105           "an email to " PACKAGE_BUGREPORT ",\n"
106           "explaining what you were doing when this happened, and including\n"
107           "a sample of your input file which caused it.\n");
108
109   fprintf(stderr,
110           "Also, please copy the following lines into your bug report:\n\n"
111           "bare_version:        %s\n" 
112           "version:             %s\n"
113           "stat_version:        %s\n"
114           "host_system:         %s\n"
115           "build_system:        %s\n"
116           "default_config_path: %s\n"
117           "include_path:        %s\n"
118           "groff_font_path:     %s\n"
119           "locale_dir:          %s\n"
120           "compiler version:    %s\n"
121           ,
122
123           bare_version,         
124           version,
125           stat_version,
126           host_system,        
127           build_system,
128           default_config_path,
129           include_path, 
130           groff_font_path,
131           locale_dir,
132 #ifdef __VERSION__
133           __VERSION__
134 #else
135           "Unknown"
136 #endif
137           );     
138
139   if ( msg )
140     fprintf(stderr,"Diagnosis: %s\n",msg);
141
142   fprintf(stderr,
143     "******************************************************************\n");
144
145   abort();
146 }
147
148 void 
149 msg_assert_fail(const char *expr, const char *file, int line)
150 {
151   char msg[256];
152   snprintf(msg,256,"Assertion failed: %s:%d; (%s)",file,line,expr);
153   request_bug_report_and_abort( msg );
154 }
155