Encapsulated msg_location inside msg_emit
[pspp] / src / libpspp / message.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 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 #include <string.h>
28
29 #include <libpspp/alloc.h>
30 #include <libpspp/version.h>
31
32 #include "progname.h"
33 #include "xvasprintf.h"
34
35 /* Current command name as set by msg_set_command_name(). */
36 static char *command_name;
37
38 /* Message handler as set by msg_init(). */
39 static void (*msg_handler)  (const struct msg *);
40 static void (*msg_location) (struct msg_locator *);
41
42
43 /* Public functions. */
44
45 /* Writes error message in CLASS, with text FORMAT, formatted with
46    printf, to the standard places. */
47 void
48 msg (enum msg_class class, const char *format, ...)
49 {
50   struct msg m;
51   va_list args;
52
53   m.category = msg_class_to_category (class);
54   m.severity = msg_class_to_severity (class);
55   va_start (args, format);
56   m.text = xvasprintf (format, args);
57   va_end (args);
58
59   msg_emit (&m);
60 }
61
62 void
63 msg_init ( void (*handler) (const struct msg *), 
64            void (*location) (struct msg_locator *) ) 
65 {
66   msg_handler = handler;
67   msg_location = location;
68 }
69
70 void
71 msg_done (void) 
72 {
73 }
74
75
76 /* Duplicate a message */
77 struct msg * 
78 msg_dup(const struct msg *m)
79 {
80   struct msg *new_msg = xmalloc (sizeof *m);
81
82   *new_msg = *m;
83   new_msg->text = strdup(m->text);
84
85   return new_msg;
86 }
87
88 void
89 msg_destroy(struct msg *m)
90 {
91   free(m->text);
92   free(m);
93 }
94
95
96 /* Emits M as an error message.
97    Frees allocated data in M. */
98 void
99 msg_emit (struct msg *m) 
100 {
101   msg_location (&m->where);
102   msg_handler (m);
103   free (m->text);
104 }
105 \f
106 /* Private functions. */
107
108 /* Sets COMMAND_NAME as the command name included in some kinds
109    of error messages. */
110 void
111 msg_set_command_name (const char *command_name_) 
112 {
113   free (command_name);
114   command_name = command_name_ ? xstrdup (command_name_) : NULL;
115 }
116
117 /* Returns the current command name, or NULL if none. */
118 const char *
119 msg_get_command_name (void) 
120 {
121   return command_name;
122 }
123
124 void 
125 request_bug_report_and_abort(const char *msg )
126 {
127   fprintf(stderr,
128           "******************************************************************\n"
129           "You have discovered a bug in PSPP.\n\n"
130           "  Please report this, by sending "
131           "an email to " PACKAGE_BUGREPORT ",\n"
132           "explaining what you were doing when this happened, and including\n"
133           "a sample of your input file which caused it.\n");
134
135   fprintf(stderr,
136           "Also, please copy the following lines into your bug report:\n\n"
137           "bare_version:        %s\n" 
138           "version:             %s\n"
139           "stat_version:        %s\n"
140           "host_system:         %s\n"
141           "build_system:        %s\n"
142           "default_config_path: %s\n"
143           "include_path:        %s\n"
144           "locale_dir:          %s\n"
145           "compiler version:    %s\n"
146           ,
147
148           bare_version,         
149           version,
150           stat_version,
151           host_system,        
152           build_system,
153           default_config_path,
154           include_path, 
155           locale_dir,
156 #ifdef __VERSION__
157           __VERSION__
158 #else
159           "Unknown"
160 #endif
161           );     
162
163   if ( msg )
164     fprintf(stderr,"Diagnosis: %s\n",msg);
165
166   fprintf(stderr,
167     "******************************************************************\n");
168
169   abort();
170 }
171
172 void 
173 msg_assert_fail(const char *expr, const char *file, int line)
174 {
175   char msg[256];
176   snprintf(msg,256,"Assertion failed: %s:%d; (%s)",file,line,expr);
177   request_bug_report_and_abort( msg );
178 }
179