1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2010, 2012, 2013 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "output/journal.h"
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"
33 #include "gl/fwriteerror.h"
34 #include "gl/xalloc.h"
37 #define _(msgid) gettext (msgid)
41 struct output_driver driver;
44 /* Name of journal file. */
49 static const struct output_driver_class journal_class;
51 /* Journal driver, if journaling is enabled. */
52 static struct journal_driver journal;
55 static struct journal_driver *
56 journal_driver_cast (struct output_driver *driver)
58 assert (driver->class == &journal_class);
59 return UP_CAST (driver, struct journal_driver, driver);
65 if (journal.file != NULL)
67 if (fwriteerror (journal.file))
68 msg_error (errno, _("error writing output file `%s'"),
76 journal_destroy (struct output_driver *driver)
78 struct journal_driver *j = journal_driver_cast (driver);
87 journal_output (struct journal_driver *j, char *s)
91 fprintf (j->file, "%s\n", s);
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. */
103 journal_submit (struct output_driver *driver, const struct output_item *item)
105 struct journal_driver *j = journal_driver_cast (driver);
109 case OUTPUT_ITEM_MESSAGE:
110 journal_output (j, msg_to_string (item->message));
113 case OUTPUT_ITEM_TEXT:
114 if (item->text.subtype == TEXT_ITEM_SYNTAX)
115 journal_output (j, text_item_get_plain_text (item));
118 case OUTPUT_ITEM_GROUP:
119 for (size_t i = 0; i < item->group.n_children; i++)
120 journal_submit (driver, item->group.children[i]);
123 case OUTPUT_ITEM_CHART:
124 case OUTPUT_ITEM_IMAGE:
125 case OUTPUT_ITEM_PAGE_BREAK:
126 case OUTPUT_ITEM_TABLE:
131 static const struct output_driver_class journal_class =
134 .destroy = journal_destroy,
135 .submit = journal_submit,
139 /* Enables journaling. */
143 journal = (struct journal_driver) {
145 .class = &journal_class,
146 .name = xstrdup ("journal"),
147 .device_type = SETTINGS_DEVICE_UNFILTERED,
151 output_driver_register (&journal.driver);
155 /* Disables journaling. */
157 journal_disable (void)
163 /* Enable journaling. */
165 journal_enable (void)
167 if (journal.file == NULL)
169 journal.file = fopen (journal_get_file_name (), "a");
170 if (journal.file == NULL)
172 msg_error (errno, _("error opening output file `%s'"),
173 journal_get_file_name ());
180 /* Returns true if journaling is enabled, false otherwise. */
182 journal_is_enabled (void)
184 return journal.file != NULL ;
187 /* Sets the name of the journal file to FILE_NAME. */
189 journal_set_file_name (const char *file_name)
192 free (journal.file_name);
193 journal.file_name = xstrdup (file_name);
196 /* Returns the name of the journal file. The caller must not modify or free
197 the returned string. */
199 journal_get_file_name (void)
201 if (journal.file_name == NULL)
203 const char *output_path = default_output_path ();
204 journal.file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
206 return journal.file_name;