1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010 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/msglog.h"
26 #include "data/file-name.h"
27 #include "data/file-handle-def.h"
28 #include "data/settings.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/message.h"
31 #include "output/driver-provider.h"
32 #include "output/message-item.h"
34 #include "gl/fwriteerror.h"
35 #include "gl/xalloc.h"
38 #define _(msgid) gettext (msgid)
42 struct output_driver driver;
44 struct file_handle *handle;
48 static const struct output_driver_class msglog_class;
50 static struct msglog_driver *
51 msglog_driver_cast (struct output_driver *driver)
53 assert (driver->class == &msglog_class);
54 return UP_CAST (driver, struct msglog_driver, driver);
57 struct output_driver *
58 msglog_create (const char *file_name)
60 enum settings_output_devices type;
61 struct msglog_driver *ml;
64 struct file_handle *handle = fh_create_file (NULL, file_name, NULL, fh_default_properties ());
66 file = fn_open (handle, "w");
69 msg_error (errno, _("error opening output file `%s'"), file_name);
73 type = (!strcmp (file_name, "-") || isatty (fileno (file))
74 ? SETTINGS_DEVICE_TERMINAL
75 : SETTINGS_DEVICE_UNFILTERED);
77 ml = xzalloc (sizeof *ml);
79 output_driver_init (&ml->driver, &msglog_class, file_name, type);
81 ml->command_name = NULL;
83 output_driver_register (&ml->driver);
89 msglog_destroy (struct output_driver *driver)
91 struct msglog_driver *ml = msglog_driver_cast (driver);
93 fn_close (ml->handle, ml->file);
94 free (ml->command_name);
95 fh_unref (ml->handle);
100 msglog_submit (struct output_driver *driver, const struct output_item *item)
102 struct msglog_driver *ml = msglog_driver_cast (driver);
104 output_driver_track_current_command (item, &ml->command_name);
106 if (is_message_item (item))
108 const struct message_item *message_item = to_message_item (item);
109 const struct msg *msg = message_item_get_msg (message_item);
110 char *s = msg_to_string (msg, ml->command_name);
111 fprintf (ml->file, "%s\n", s);
116 static const struct output_driver_class msglog_class =