1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007 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/str.h>
28 #include "fwriteerror.h"
33 #define _(msgid) gettext (msgid)
35 /* Journaling enabled? */
36 static bool journal_enabled = false;
38 /* Name of the journal file. */
39 static char *journal_file_name = NULL;
42 static FILE *journal_file = NULL;
44 /* Enables journaling. */
48 journal_enabled = true;
51 /* Disables journaling. */
53 journal_disable (void)
55 journal_enabled = false;
56 if (journal_file != NULL)
57 fflush (journal_file);
60 /* Sets the name of the journal file to FILE_NAME. */
62 journal_set_file_name (const char *file_name)
64 assert (file_name != NULL);
66 if (journal_file != NULL)
68 if (fwriteerror (journal_file))
69 error (0, errno, _("error writing \"%s\""), journal_file_name);
72 free (journal_file_name);
73 journal_file_name = xstrdup (file_name);
76 /* Writes LINE to the journal file (if journaling is enabled).
77 If PREFIX is non-null, the line will be prefixed by "> ". */
79 journal_write (bool prefix, const char *line)
84 if (journal_file == NULL)
86 if (journal_file_name == NULL)
88 const char *output_path = default_output_path ();
89 journal_file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
91 journal_file = fopen (journal_file_name, "w");
92 if (journal_file == NULL)
94 error (0, errno, _("error creating \"%s\""), journal_file_name);
95 journal_enabled = false;
101 fputs ("> ", journal_file);
102 fputs (line, journal_file);
103 if (strchr (line, '\n') == NULL)
104 putc ('\n', journal_file);
105 fflush (journal_file);