1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2010 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"
25 #include "data/file-name.h"
26 #include "libpspp/cast.h"
27 #include "libpspp/str.h"
28 #include "output/driver-provider.h"
29 #include "output/message-item.h"
30 #include "output/text-item.h"
33 #include "gl/fwriteerror.h"
34 #include "gl/xalloc.h"
37 #define _(msgid) gettext (msgid)
41 struct output_driver driver;
46 static const struct output_driver_class journal_class;
48 /* Journal driver, if journaling is enabled. */
49 static struct journal_driver *journal;
51 /* Name of journal file. */
52 static char *journal_file_name;
54 static struct journal_driver *
55 journal_driver_cast (struct output_driver *driver)
57 assert (driver->class == &journal_class);
58 return UP_CAST (driver, struct journal_driver, driver);
64 if (journal != NULL && journal->file != NULL)
66 if (fwriteerror (journal->file))
67 error (0, errno, _("error writing output file \"%s\""),
74 journal_destroy (struct output_driver *driver)
76 struct journal_driver *j = journal_driver_cast (driver);
79 free (j->command_name);
86 journal_output (struct journal_driver *j, const char *s)
90 j->file = fopen (journal_file_name, "a");
93 error (0, errno, _("error opening output file \"%s\""),
95 output_driver_destroy (&j->driver);
100 fprintf (j->file, "%s\n", s);
104 journal_submit (struct output_driver *driver, const struct output_item *item)
106 struct journal_driver *j = journal_driver_cast (driver);
108 output_driver_track_current_command (item, &j->command_name);
110 if (is_text_item (item))
112 const struct text_item *text_item = to_text_item (item);
113 enum text_item_type type = text_item_get_type (text_item);
115 if (type == TEXT_ITEM_SYNTAX)
116 journal_output (j, text_item_get_text (text_item));
118 else if (is_message_item (item))
120 const struct message_item *message_item = to_message_item (item);
121 const struct msg *msg = message_item_get_msg (message_item);
122 char *s = msg_to_string (msg, j->command_name);
123 journal_output (j, s);
128 static const struct output_driver_class journal_class =
136 /* Enables journaling. */
138 journal_enable (void)
142 /* If no journal file name is configured, use the default. */
143 if (journal_file_name == NULL)
145 const char *output_path = default_output_path ();
146 journal_file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
149 /* Create journal driver. */
150 journal = xzalloc (sizeof *journal);
151 output_driver_init (&journal->driver, &journal_class, "journal",
152 SETTINGS_DEVICE_UNFILTERED);
153 journal->file = NULL;
154 journal->command_name = NULL;
156 /* Register journal driver. */
157 output_driver_register (&journal->driver);
161 /* Disables journaling. */
163 journal_disable (void)
166 output_driver_destroy (&journal->driver);
169 /* Sets the name of the journal file to FILE_NAME. */
171 journal_set_file_name (const char *file_name)
174 free (journal_file_name);
175 journal_file_name = xstrdup (file_name);