Use the msg function to report errors wherever possible.
[pspp] / src / output / journal.c
index 1a700898ac5e1d6707de700d4b59de1723b32c74..5c84a5f84ee5b1fffa5e37d67a8667d6199076f5 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2007, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2007, 2010, 2012 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include "data/file-name.h"
 #include "libpspp/cast.h"
+#include "libpspp/message.h"
 #include "libpspp/str.h"
 #include "output/driver-provider.h"
 #include "output/message-item.h"
 #include "output/text-item.h"
 
-#include "gl/error.h"
 #include "gl/fwriteerror.h"
 #include "gl/xalloc.h"
 
@@ -64,7 +64,7 @@ journal_close (void)
   if (journal != NULL && journal->file != NULL)
     {
       if (fwriteerror (journal->file))
-        error (0, errno, _("error writing output file \"%s\""),
+        msg_error (errno, _("error writing output file `%s'"),
                journal_file_name);
       journal->file = NULL;
     }
@@ -87,17 +87,22 @@ journal_output (struct journal_driver *j, const char *s)
 {
   if (j->file == NULL)
     {
-      j->file = fopen (journal_file_name, "a");
+      j->file = fopen (journal_get_file_name (), "a");
       if (j->file == NULL)
         {
-          error (0, errno, _("error opening output file \"%s\""),
-                 journal_file_name);
+          msg_error (errno, _("error opening output file `%s'"),
+                 journal_get_file_name ());
           output_driver_destroy (&j->driver);
           return;
         }
     }
 
   fprintf (j->file, "%s\n", s);
+
+  /* Flush the journal in case the syntax we're about to write
+     causes a crash.  Having the syntax already written to disk
+     makes postmortem analysis of the problem possible. */
+  fflush (j->file);
 }
 
 static void
@@ -139,13 +144,6 @@ journal_enable (void)
 {
   if (journal == NULL)
     {
-      /* If no journal file name is configured, use the default. */
-      if (journal_file_name == NULL)
-       {
-         const char *output_path = default_output_path ();
-         journal_file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
-       }
-
       /* Create journal driver. */
       journal = xzalloc (sizeof *journal);
       output_driver_init (&journal->driver, &journal_class, "journal",
@@ -166,6 +164,13 @@ journal_disable (void)
     output_driver_destroy (&journal->driver);
 }
 
+/* Returns true if journaling is enabled, false otherwise. */
+bool
+journal_is_enabled (void)
+{
+  return journal != NULL;
+}
+
 /* Sets the name of the journal file to FILE_NAME. */
 void
 journal_set_file_name (const char *file_name)
@@ -174,3 +179,16 @@ journal_set_file_name (const char *file_name)
   free (journal_file_name);
   journal_file_name = xstrdup (file_name);
 }
+
+/* Returns the name of the journal file.  The caller must not modify or free
+   the returned string. */
+const char *
+journal_get_file_name (void)
+{
+  if (journal_file_name == NULL)
+    {
+      const char *output_path = default_output_path ();
+      journal_file_name = xasprintf ("%s%s", output_path, "pspp.jnl");
+    }
+  return journal_file_name;
+}