67657f6247cc8ec731cf8154c4f45aa48d369003
[pspp-builds.git] / src / output / journal.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007 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
25 #include <data/file-name.h>
26 #include <libpspp/str.h>
27
28 #include "fwriteerror.h"
29 #include "error.h"
30 #include "xalloc.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 /* Journaling enabled? */
36 static bool journal_enabled = false;
37
38 /* Name of the journal file. */
39 static char *journal_file_name = NULL;
40
41 /* Journal file. */
42 static FILE *journal_file = NULL;
43
44 /* Enables journaling. */
45 void
46 journal_enable (void)
47 {
48   journal_enabled = true;
49 }
50
51 /* Disables journaling. */
52 void
53 journal_disable (void)
54 {
55   journal_enabled = false;
56   if (journal_file != NULL)
57     fflush (journal_file);
58 }
59
60 /* Sets the name of the journal file to FILE_NAME. */
61 void
62 journal_set_file_name (const char *file_name)
63 {
64   assert (file_name != NULL);
65
66   if (journal_file != NULL)
67     {
68       if (fwriteerror (journal_file))
69         error (0, errno, _("error writing \"%s\""), journal_file_name);
70     }
71
72   free (journal_file_name);
73   journal_file_name = xstrdup (file_name);
74 }
75
76 /* Writes LINE to the journal file (if journaling is enabled).
77    If PREFIX is non-null, the line will be prefixed by "> ". */
78 void
79 journal_write (bool prefix, const char *line)
80 {
81   if (!journal_enabled)
82     return;
83
84   if (journal_file == NULL)
85     {
86       if (journal_file_name == NULL)
87         {
88           const char *output_path = default_output_path ();
89           journal_file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
90         }
91       journal_file = fopen (journal_file_name, "w");
92       if (journal_file == NULL)
93         {
94           error (0, errno, _("error creating \"%s\""), journal_file_name);
95           journal_enabled = false;
96           return;
97         }
98     }
99
100   if (prefix)
101     fputs ("> ", journal_file);
102   fputs (line, journal_file);
103   if (strchr (line, '\n') == NULL)
104     putc ('\n', journal_file);
105   fflush (journal_file);
106 }