output-item: Collapse the inheritance hierarchy into a single struct.
[pspp] / src / output / journal.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2010, 2012, 2013 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/journal.h"
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25
26 #include "data/file-name.h"
27 #include "libpspp/cast.h"
28 #include "libpspp/message.h"
29 #include "libpspp/str.h"
30 #include "output/driver-provider.h"
31 #include "output/output-item.h"
32
33 #include "gl/fwriteerror.h"
34 #include "gl/xalloc.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 struct journal_driver
40   {
41     struct output_driver driver;
42     FILE *file;
43
44     /* Name of journal file. */
45     char *file_name;
46     bool destroyed;
47   };
48
49 static const struct output_driver_class journal_class;
50
51 /* Journal driver, if journaling is enabled. */
52 static struct journal_driver journal;
53
54
55 static struct journal_driver *
56 journal_driver_cast (struct output_driver *driver)
57 {
58   assert (driver->class == &journal_class);
59   return UP_CAST (driver, struct journal_driver, driver);
60 }
61
62 static void
63 journal_close (void)
64 {
65   if (journal.file != NULL)
66     {
67       if (fwriteerror (journal.file))
68         msg_error (errno, _("error writing output file `%s'"),
69                    journal.file_name);
70
71       }
72   journal.file = NULL;
73 }
74
75 static void
76 journal_destroy (struct output_driver *driver)
77 {
78   struct journal_driver *j = journal_driver_cast (driver);
79
80   if (!j->destroyed)
81     journal_close ();
82
83   j->destroyed = true;
84 }
85
86 static void
87 journal_output (struct journal_driver *j, char *s)
88 {
89   if (j->file)
90     {
91       fprintf (j->file, "%s\n", s);
92
93       /* Flush the journal in case the syntax we're about to write
94          causes a crash.  Having the syntax already written to disk
95          makes postmortem analysis of the problem possible. */
96       fflush (j->file);
97     }
98
99   free (s);
100 }
101
102 static void
103 journal_submit (struct output_driver *driver, const struct output_item *item)
104 {
105   struct journal_driver *j = journal_driver_cast (driver);
106
107   switch (item->type)
108     {
109     case OUTPUT_ITEM_MESSAGE:
110       journal_output (j, msg_to_string (item->message));
111       break;
112
113     case OUTPUT_ITEM_TEXT:
114       if (item->text.subtype == TEXT_ITEM_SYNTAX)
115         journal_output (j, text_item_get_plain_text (item));
116       break;
117
118     case OUTPUT_ITEM_CHART:
119     case OUTPUT_ITEM_GROUP_OPEN:
120     case OUTPUT_ITEM_GROUP_CLOSE:
121     case OUTPUT_ITEM_IMAGE:
122     case OUTPUT_ITEM_PAGE_BREAK:
123     case OUTPUT_ITEM_PAGE_SETUP:
124     case OUTPUT_ITEM_TABLE:
125       break;
126     }
127 }
128
129 static const struct output_driver_class journal_class =
130   {
131     "journal",
132     journal_destroy,
133     journal_submit,
134     NULL                        /* flush */
135   };
136
137 \f
138
139 /* Enables journaling. */
140 void
141 journal_init (void)
142 {
143   /* Create journal driver. */
144   output_driver_init (&journal.driver, &journal_class, "journal",
145                       SETTINGS_DEVICE_UNFILTERED);
146   journal.file = NULL;
147
148   /* Register journal driver. */
149   output_driver_register (&journal.driver);
150
151   journal_enable ();
152   journal.destroyed = false;
153 }
154
155 /* Disables journaling. */
156 void
157 journal_disable (void)
158 {
159   journal_close ();
160 }
161
162
163 /* Enable journaling. */
164 void
165 journal_enable (void)
166 {
167   if (journal.file == NULL)
168     {
169       journal.file = fopen (journal_get_file_name (), "a");
170       if (journal.file == NULL)
171         {
172           msg_error (errno, _("error opening output file `%s'"),
173                      journal_get_file_name ());
174           journal_close ();
175         }
176     }
177 }
178
179
180 /* Returns true if journaling is enabled, false otherwise. */
181 bool
182 journal_is_enabled (void)
183 {
184   return journal.file != NULL ;
185 }
186
187 /* Sets the name of the journal file to FILE_NAME. */
188 void
189 journal_set_file_name (const char *file_name)
190 {
191   journal_close ();
192   free (journal.file_name);
193   journal.file_name = xstrdup (file_name);
194 }
195
196 /* Returns the name of the journal file.  The caller must not modify or free
197    the returned string. */
198 const char *
199 journal_get_file_name (void)
200 {
201   if (journal.file_name == NULL)
202     {
203       const char *output_path = default_output_path ();
204       journal.file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
205     }
206   return journal.file_name;
207 }