da00ea9645effd4d1c332bb7d30257fd38d36c71
[pspp-builds.git] / src / libpspp / message.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    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
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #ifndef MESSAGE_H
20 #define MESSAGE_H 1
21
22 #include <stdarg.h>
23 #include <stdbool.h>
24 #include <libpspp/compiler.h>
25
26 /* What kind of message is this? */
27 enum msg_category
28   {
29     MSG_GENERAL,        /* General info. */
30     MSG_SYNTAX,         /* Messages that relate to syntax files. */
31     MSG_DATA            /* Messages that relate to data files. */
32   };
33
34 /* How important a condition is it? */
35 enum msg_severity
36   {
37     MSG_ERROR,
38     MSG_WARNING,
39     MSG_NOTE
40   };
41
42 /* Combination of a category and a severity for convenience. */
43 enum msg_class
44   {
45     ME, MW, MN,                 /* General error/warning/note. */
46     SE, SW, SN,                 /* Script error/warning/note. */
47     DE, DW, DN,                 /* Data-file error/note. */
48     MSG_CLASS_CNT,
49   };
50
51
52 static inline enum msg_category
53 msg_class_to_category (enum msg_class class)
54 {
55   return class / 3;
56 }
57
58 static inline enum msg_severity
59 msg_class_to_severity (enum msg_class class)
60 {
61   return class % 3;
62 }
63
64 static inline enum msg_class
65 msg_class_from_category_and_severity (enum msg_category category,
66                                       enum msg_severity severity)
67 {
68   return category * 3 + severity;
69 }
70
71 /* A file location.  */
72 struct msg_locator
73   {
74     const char *file_name;              /* File name. */
75     int line_number;                    /* Line number. */
76   };
77
78 /* A message. */
79 struct msg
80   {
81     enum msg_category category; /* Message category. */
82     enum msg_severity severity; /* Message severity. */
83     struct msg_locator where;   /* File location, or (NULL, -1). */
84     char *text;                 /* Error text. */
85   };
86
87 struct source_stream ;
88
89 /* Initialization. */
90 void msg_init (struct source_stream *, void (*handler) (const struct msg *) );
91
92 void msg_done (void);
93
94 struct msg * msg_dup(const struct msg *m);
95 void msg_destroy(struct msg *m);
96
97 /* Emitting messages. */
98 void msg (enum msg_class, const char *format, ...)
99      PRINTF_FORMAT (2, 3);
100 void msg_emit (struct msg *);
101
102 /* Enable and disable messages. */
103 void msg_enable (void);
104 void msg_disable (void);
105
106 /* Error context. */
107 void msg_set_command_name (const char *);
108 const char *msg_get_command_name (void);
109 void msg_push_msg_locator (const struct msg_locator *);
110 void msg_pop_msg_locator (const struct msg_locator *);
111
112
113 /* Used in panic situations only. */
114 void request_bug_report_and_abort (const char *msg) NO_RETURN;
115
116 #endif /* message.h */