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/message-item.h"
32 #include "output/text-item.h"
34 #include "gl/fwriteerror.h"
35 #include "gl/xalloc.h"
38 #define _(msgid) gettext (msgid)
42 struct output_driver driver;
45 /* Name of journal file. */
50 static const struct output_driver_class journal_class;
52 /* Journal driver, if journaling is enabled. */
53 static struct journal_driver journal;
56 static struct journal_driver *
57 journal_driver_cast (struct output_driver *driver)
59 assert (driver->class == &journal_class);
60 return UP_CAST (driver, struct journal_driver, driver);
66 if (journal.file != NULL)
68 if (fwriteerror (journal.file))
69 msg_error (errno, _("error writing output file `%s'"),
77 journal_destroy (struct output_driver *driver)
79 struct journal_driver *j = journal_driver_cast (driver);
88 journal_output (struct journal_driver *j, const char *s)
93 fprintf (j->file, "%s\n", s);
95 /* Flush the journal in case the syntax we're about to write
96 causes a crash. Having the syntax already written to disk
97 makes postmortem analysis of the problem possible. */
102 journal_submit (struct output_driver *driver, const struct output_item *item)
104 struct journal_driver *j = journal_driver_cast (driver);
106 if (is_text_item (item))
108 const struct text_item *text_item = to_text_item (item);
109 enum text_item_type type = text_item_get_type (text_item);
111 if (type == TEXT_ITEM_SYNTAX)
113 char *text = text_item_get_plain_text (text_item);
114 journal_output (j, text);
118 else if (is_message_item (item))
120 const struct message_item *message_item = to_message_item (item);
121 char *s = msg_to_string (message_item_get_msg (message_item));
122 journal_output (j, s);
127 static const struct output_driver_class journal_class =
137 /* Enables journaling. */
141 /* Create journal driver. */
142 output_driver_init (&journal.driver, &journal_class, "journal",
143 SETTINGS_DEVICE_UNFILTERED);
146 /* Register journal driver. */
147 output_driver_register (&journal.driver);
150 journal.destroyed = false;
153 /* Disables journaling. */
155 journal_disable (void)
161 /* Enable journaling. */
163 journal_enable (void)
165 if (journal.file == NULL)
167 journal.file = fopen (journal_get_file_name (), "a");
168 if (journal.file == NULL)
170 msg_error (errno, _("error opening output file `%s'"),
171 journal_get_file_name ());
178 /* Returns true if journaling is enabled, false otherwise. */
180 journal_is_enabled (void)
182 return journal.file != NULL ;
185 /* Sets the name of the journal file to FILE_NAME. */
187 journal_set_file_name (const char *file_name)
190 free (journal.file_name);
191 journal.file_name = xstrdup (file_name);
194 /* Returns the name of the journal file. The caller must not modify or free
195 the returned string. */
197 journal_get_file_name (void)
199 if (journal.file_name == NULL)
201 const char *output_path = default_output_path ();
202 journal.file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
204 return journal.file_name;