pivot table procedure conceptually works
[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     char *command_name;
45
46     /* Name of journal file. */
47     char *file_name;
48     bool destroyed;
49   };
50
51 static const struct output_driver_class journal_class;
52
53 /* Journal driver, if journaling is enabled. */
54 static struct journal_driver journal;
55
56
57 static struct journal_driver *
58 journal_driver_cast (struct output_driver *driver)
59 {
60   assert (driver->class == &journal_class);
61   return UP_CAST (driver, struct journal_driver, driver);
62 }
63
64 static void
65 journal_close (void)
66 {
67   if (journal.file != NULL)
68     {
69       if (fwriteerror (journal.file))
70         msg_error (errno, _("error writing output file `%s'"),
71                    journal.file_name);
72
73       }
74   journal.file = NULL;
75 }
76
77 static void
78 journal_destroy (struct output_driver *driver)
79 {
80   struct journal_driver *j = journal_driver_cast (driver);
81
82   if ( !j->destroyed)
83     {
84       journal_close ();
85       free (j->command_name);
86     }
87
88   j->destroyed = true;
89 }
90
91 static void
92 journal_output (struct journal_driver *j, const char *s)
93 {
94   if ( j->file == NULL)
95     return;
96
97   fprintf (j->file, "%s\n", s);
98
99   /* Flush the journal in case the syntax we're about to write
100      causes a crash.  Having the syntax already written to disk
101      makes postmortem analysis of the problem possible. */
102   fflush (j->file);
103 }
104
105 static void
106 journal_submit (struct output_driver *driver, const struct output_item *item)
107 {
108   struct journal_driver *j = journal_driver_cast (driver);
109
110   output_driver_track_current_command (item, &j->command_name);
111
112   if (is_text_item (item))
113     {
114       const struct text_item *text_item = to_text_item (item);
115       enum text_item_type type = text_item_get_type (text_item);
116
117       if (type == TEXT_ITEM_SYNTAX)
118         journal_output (j, text_item_get_text (text_item));
119     }
120   else if (is_message_item (item))
121     {
122       const struct message_item *message_item = to_message_item (item);
123       const struct msg *msg = message_item_get_msg (message_item);
124       char *s = msg_to_string (msg, j->command_name);
125       journal_output (j, s);
126       free (s);
127     }
128 }
129
130 static const struct output_driver_class journal_class =
131   {
132     "journal",
133     journal_destroy,
134     journal_submit,
135     NULL                        /* flush */
136   };
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   journal.command_name = NULL;
149   
150   /* Register journal driver. */
151   output_driver_register (&journal.driver);
152
153   journal_enable ();
154   journal.destroyed = false;
155 }
156
157 /* Disables journaling. */
158 void
159 journal_disable (void)
160 {
161   journal_close ();
162 }
163
164
165 /* Enable journaling. */
166 void
167 journal_enable (void)
168 {
169   if (journal.file == NULL)
170     {
171       journal.file = fopen (journal_get_file_name (), "a");
172       if (journal.file == NULL)
173         {
174           msg_error (errno, _("error opening output file `%s'"),
175                      journal_get_file_name ());
176           journal_close ();
177         }
178     }
179 }
180
181
182 /* Returns true if journaling is enabled, false otherwise. */
183 bool
184 journal_is_enabled (void)
185 {
186   return journal.file != NULL ;
187 }
188
189 /* Sets the name of the journal file to FILE_NAME. */
190 void
191 journal_set_file_name (const char *file_name)
192 {
193   journal_close ();
194   free (journal.file_name);
195   journal.file_name = xstrdup (file_name);
196 }
197
198 /* Returns the name of the journal file.  The caller must not modify or free
199    the returned string. */
200 const char *
201 journal_get_file_name (void)
202 {
203   if (journal.file_name == NULL)
204     {
205       const char *output_path = default_output_path ();
206       journal.file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
207     }
208   return journal.file_name;
209 }