text-item: Merge "title" and "subtitle" items into a new "page title".
[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/message-item.h"
32 #include "output/text-item.h"
33
34 #include "gl/fwriteerror.h"
35 #include "gl/xalloc.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 struct journal_driver
41   {
42     struct output_driver driver;
43     FILE *file;
44
45     /* Name of journal file. */
46     char *file_name;
47     bool destroyed;
48   };
49
50 static const struct output_driver_class journal_class;
51
52 /* Journal driver, if journaling is enabled. */
53 static struct journal_driver journal;
54
55
56 static struct journal_driver *
57 journal_driver_cast (struct output_driver *driver)
58 {
59   assert (driver->class == &journal_class);
60   return UP_CAST (driver, struct journal_driver, driver);
61 }
62
63 static void
64 journal_close (void)
65 {
66   if (journal.file != NULL)
67     {
68       if (fwriteerror (journal.file))
69         msg_error (errno, _("error writing output file `%s'"),
70                    journal.file_name);
71
72       }
73   journal.file = NULL;
74 }
75
76 static void
77 journal_destroy (struct output_driver *driver)
78 {
79   struct journal_driver *j = journal_driver_cast (driver);
80
81   if ( !j->destroyed)
82     journal_close ();
83
84   j->destroyed = true;
85 }
86
87 static void
88 journal_output (struct journal_driver *j, const char *s)
89 {
90   if ( j->file == NULL)
91     return;
92
93   fprintf (j->file, "%s\n", s);
94
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. */
98   fflush (j->file);
99 }
100
101 static void
102 journal_submit (struct output_driver *driver, const struct output_item *item)
103 {
104   struct journal_driver *j = journal_driver_cast (driver);
105
106   if (is_text_item (item))
107     {
108       const struct text_item *text_item = to_text_item (item);
109       enum text_item_type type = text_item_get_type (text_item);
110
111       if (type == TEXT_ITEM_SYNTAX)
112         journal_output (j, text_item_get_text (text_item));
113     }
114   else if (is_message_item (item))
115     {
116       const struct message_item *message_item = to_message_item (item);
117       const struct msg *msg = message_item_get_msg (message_item);
118       char *s = msg_to_string (msg, message_item->command_name);
119       journal_output (j, s);
120       free (s);
121     }
122 }
123
124 static const struct output_driver_class journal_class =
125   {
126     "journal",
127     journal_destroy,
128     journal_submit,
129     NULL                        /* flush */
130   };
131
132 \f
133
134 /* Enables journaling. */
135 void
136 journal_init (void)
137 {
138   /* Create journal driver. */
139   output_driver_init (&journal.driver, &journal_class, "journal",
140                       SETTINGS_DEVICE_UNFILTERED);
141   journal.file = NULL;
142
143   /* Register journal driver. */
144   output_driver_register (&journal.driver);
145
146   journal_enable ();
147   journal.destroyed = false;
148 }
149
150 /* Disables journaling. */
151 void
152 journal_disable (void)
153 {
154   journal_close ();
155 }
156
157
158 /* Enable journaling. */
159 void
160 journal_enable (void)
161 {
162   if (journal.file == NULL)
163     {
164       journal.file = fopen (journal_get_file_name (), "a");
165       if (journal.file == NULL)
166         {
167           msg_error (errno, _("error opening output file `%s'"),
168                      journal_get_file_name ());
169           journal_close ();
170         }
171     }
172 }
173
174
175 /* Returns true if journaling is enabled, false otherwise. */
176 bool
177 journal_is_enabled (void)
178 {
179   return journal.file != NULL ;
180 }
181
182 /* Sets the name of the journal file to FILE_NAME. */
183 void
184 journal_set_file_name (const char *file_name)
185 {
186   journal_close ();
187   free (journal.file_name);
188   journal.file_name = xstrdup (file_name);
189 }
190
191 /* Returns the name of the journal file.  The caller must not modify or free
192    the returned string. */
193 const char *
194 journal_get_file_name (void)
195 {
196   if (journal.file_name == NULL)
197     {
198       const char *output_path = default_output_path ();
199       journal.file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
200     }
201   return journal.file_name;
202 }