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_PAGE_SETUP:
127 case OUTPUT_ITEM_TABLE:
132 static const struct output_driver_class journal_class =
135 .destroy = journal_destroy,
136 .submit = journal_submit,
140 /* Enables journaling. */
144 /* Create journal driver. */
145 output_driver_init (&journal.driver, &journal_class, "journal",
146 SETTINGS_DEVICE_UNFILTERED);
149 /* Register journal driver. */
150 output_driver_register (&journal.driver);
153 journal.destroyed = false;
156 /* Disables journaling. */
158 journal_disable (void)
164 /* Enable journaling. */
166 journal_enable (void)
168 if (journal.file == NULL)
170 journal.file = fopen (journal_get_file_name (), "a");
171 if (journal.file == NULL)
173 msg_error (errno, _("error opening output file `%s'"),
174 journal_get_file_name ());
181 /* Returns true if journaling is enabled, false otherwise. */
183 journal_is_enabled (void)
185 return journal.file != NULL ;
188 /* Sets the name of the journal file to FILE_NAME. */
190 journal_set_file_name (const char *file_name)
193 free (journal.file_name);
194 journal.file_name = xstrdup (file_name);
197 /* Returns the name of the journal file. The caller must not modify or free
198 the returned string. */
200 journal_get_file_name (void)
202 if (journal.file_name == NULL)
204 const char *output_path = default_output_path ();
205 journal.file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
207 return journal.file_name;