output-item: Make command name part of every output_item.
[pspp] / src / output / message-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 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 "output/message-item.h"
20
21 #include <stdlib.h>
22
23 #include "libpspp/i18n.h"
24 #include "libpspp/message.h"
25 #include "output/driver.h"
26 #include "output/output-item-provider.h"
27 #include "output/text-item.h"
28
29 #include "gl/xalloc.h"
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 struct message_item *
35 message_item_create (const struct msg *msg)
36 {
37   struct message_item *item = xmalloc (sizeof *msg);
38   *item = (struct message_item) {
39     .output_item = OUTPUT_ITEM_INITIALIZER (&message_item_class),
40     .msg = msg_dup (msg)
41   };
42   return item;
43 }
44
45 const struct msg *
46 message_item_get_msg (const struct message_item *item)
47 {
48   return item->msg;
49 }
50
51 struct text_item *
52 message_item_to_text_item (struct message_item *message_item)
53 {
54   struct text_item *text_item = text_item_create_nocopy (
55     TEXT_ITEM_LOG,
56     msg_to_string (message_item_get_msg (message_item)),
57     xstrdup (output_item_get_label (message_item_super (message_item))));
58   message_item_unref (message_item);
59   return text_item;
60 }
61
62 static const char *
63 message_item_get_label (const struct output_item *output_item)
64 {
65   const struct message_item *item = to_message_item (output_item);
66   return (item->msg->severity == MSG_S_ERROR ? _("Error")
67           : item->msg->severity == MSG_S_WARNING ? _("Warning")
68           : _("Note"));
69 }
70
71 static void
72 message_item_destroy (struct output_item *output_item)
73 {
74   struct message_item *item = to_message_item (output_item);
75   msg_destroy (item->msg);
76   free (item);
77 }
78
79 /* Submits ITEM to the configured output drivers, and transfers ownership to
80    the output subsystem. */
81 void
82 message_item_submit (struct message_item *item)
83 {
84   output_submit (&item->output_item);
85 }
86
87 const struct output_item_class message_item_class =
88   {
89     message_item_get_label,
90     message_item_destroy,
91   };