4945db2ee9d831aa1fcbdb78790836a6617f3e5b
[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_GROUP:
119       for (size_t i = 0; i < item->group.n_children; i++)
120         journal_submit (driver, item->group.children[i]);
121       break;
122
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:
128       break;
129     }
130 }
131
132 static const struct output_driver_class journal_class =
133   {
134     .name = "journal",
135     .destroy = journal_destroy,
136     .submit = journal_submit,
137   };
138 \f
139
140 /* Enables journaling. */
141 void
142 journal_init (void)
143 {
144   /* Create journal driver. */
145   output_driver_init (&journal.driver, &journal_class, "journal",
146                       SETTINGS_DEVICE_UNFILTERED);
147   journal.file = NULL;
148
149   /* Register journal driver. */
150   output_driver_register (&journal.driver);
151
152   journal_enable ();
153   journal.destroyed = false;
154 }
155
156 /* Disables journaling. */
157 void
158 journal_disable (void)
159 {
160   journal_close ();
161 }
162
163
164 /* Enable journaling. */
165 void
166 journal_enable (void)
167 {
168   if (journal.file == NULL)
169     {
170       journal.file = fopen (journal_get_file_name (), "a");
171       if (journal.file == NULL)
172         {
173           msg_error (errno, _("error opening output file `%s'"),
174                      journal_get_file_name ());
175           journal_close ();
176         }
177     }
178 }
179
180
181 /* Returns true if journaling is enabled, false otherwise. */
182 bool
183 journal_is_enabled (void)
184 {
185   return journal.file != NULL ;
186 }
187
188 /* Sets the name of the journal file to FILE_NAME. */
189 void
190 journal_set_file_name (const char *file_name)
191 {
192   journal_close ();
193   free (journal.file_name);
194   journal.file_name = xstrdup (file_name);
195 }
196
197 /* Returns the name of the journal file.  The caller must not modify or free
198    the returned string. */
199 const char *
200 journal_get_file_name (void)
201 {
202   if (journal.file_name == NULL)
203     {
204       const char *output_path = default_output_path ();
205       journal.file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
206     }
207   return journal.file_name;
208 }