ba35b35104c9c67736dd52f21abe5130c1405ffa
[pspp-builds.git] / src / libpspp / message.h
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 #ifndef MESSAGE_H
21 #define MESSAGE_H 1
22
23 #include <stdarg.h>
24 #include <stdbool.h>
25 #include <libpspp/compiler.h>
26
27 /* What kind of message is this? */
28 enum msg_category 
29   {
30     MSG_GENERAL,        /* General info. */
31     MSG_SYNTAX,         /* Messages that relate to syntax files. */
32     MSG_DATA            /* Messages that relate to data files. */
33   };
34
35 /* How important a condition is it? */
36 enum msg_severity 
37   {
38     MSG_ERROR,
39     MSG_WARNING,
40     MSG_NOTE
41   };
42
43 /* Combination of a category and a severity for convenience. */
44 enum msg_class
45   {
46     ME, MW, MN,                 /* General error/warning/note. */
47     SE, SW, SN,                 /* Script error/warning/note. */
48     DE, DW, DN,                 /* Data-file error/note. */
49     MSG_CLASS_CNT,
50   };
51
52
53 static inline enum msg_category
54 msg_class_to_category (enum msg_class class) 
55 {
56   return class / 3;
57 }
58
59 static inline enum msg_severity
60 msg_class_to_severity (enum msg_class class) 
61 {
62   return class % 3;
63 }
64
65 static inline enum msg_class
66 msg_class_from_category_and_severity (enum msg_category category,
67                                       enum msg_severity severity) 
68 {
69   return category * 3 + severity;
70 }
71
72 /* A file location.  */
73 struct msg_locator
74   {
75     const char *file_name;              /* File name. */
76     int line_number;                    /* Line number. */
77   };
78
79 /* A message. */
80 struct msg
81   {
82     enum msg_category category; /* Message category. */
83     enum msg_severity severity; /* Message severity. */
84     struct msg_locator where;   /* File location, or (NULL, -1). */
85     char *text;                 /* Error text. */
86   };
87
88 struct source_stream ;
89
90 /* Initialization. */
91 void msg_init (struct source_stream *, void (*handler) (const struct msg *) );
92
93 void msg_done (void);
94
95 struct msg * msg_dup(const struct msg *m);
96 void msg_destroy(struct msg *m);
97
98 /* Emitting messages. */
99 void msg (enum msg_class, const char *format, ...)
100      PRINTF_FORMAT (2, 3);
101 void msg_emit (struct msg *);
102
103 /* Enable and disable messages. */
104 void msg_enable (void);
105 void msg_disable (void);
106
107 /* Error context. */
108 void msg_set_command_name (const char *);
109 const char *msg_get_command_name (void);
110 void msg_push_msg_locator (const struct msg_locator *);
111 void msg_pop_msg_locator (const struct msg_locator *);
112
113
114 /* Used in panic situations only. */
115 void request_bug_report_and_abort (const char *msg) NO_RETURN;
116
117 #endif /* message.h */