1 /* Command-line output logging for GDB, the GNU debugger.
3 Copyright (c) 2003, 2004, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdb_string.h"
27 /* These hold the pushed copies of the gdb output files.
28 If NULL then nothing has yet been pushed. */
29 struct saved_output_files
36 static struct saved_output_files saved_output;
37 static char *saved_filename;
39 static char *logging_filename;
41 show_logging_filename (struct ui_file *file, int from_tty,
42 struct cmd_list_element *c, const char *value)
44 fprintf_filtered (file, _("The current logfile is \"%s\".\n"),
48 int logging_overwrite;
50 show_logging_overwrite (struct ui_file *file, int from_tty,
51 struct cmd_list_element *c, const char *value)
53 fprintf_filtered (file, _("\
54 Whether logging overwrites or appends to the log file is %s.\n"),
60 show_logging_redirect (struct ui_file *file, int from_tty,
61 struct cmd_list_element *c, const char *value)
63 fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
66 /* If we've pushed output files, close them and pop them. */
68 pop_output_files (void)
70 /* Only delete one of the files -- they are all set to the same
72 ui_file_delete (gdb_stdout);
73 gdb_stdout = saved_output.out;
74 gdb_stderr = saved_output.err;
75 gdb_stdlog = saved_output.log;
76 gdb_stdtarg = saved_output.targ;
77 saved_output.out = NULL;
78 saved_output.err = NULL;
79 saved_output.log = NULL;
80 saved_output.targ = NULL;
82 ui_out_redirect (uiout, NULL);
85 /* This is a helper for the `set logging' command. */
87 handle_redirections (int from_tty)
89 struct cleanup *cleanups;
90 struct ui_file *output;
92 if (saved_filename != NULL)
94 fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
99 output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
101 perror_with_name (_("set logging"));
102 cleanups = make_cleanup_ui_file_delete (output);
104 /* Redirects everything to gdb_stdout while this is running. */
105 if (!logging_redirect)
107 output = tee_file_new (gdb_stdout, 0, output, 1);
109 perror_with_name (_("set logging"));
110 discard_cleanups (cleanups);
111 cleanups = make_cleanup_ui_file_delete (output);
113 fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
117 fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
120 discard_cleanups (cleanups);
122 saved_filename = xstrdup (logging_filename);
123 saved_output.out = gdb_stdout;
124 saved_output.err = gdb_stderr;
125 saved_output.log = gdb_stdlog;
126 saved_output.targ = gdb_stdtarg;
131 gdb_stdtarg = output;
133 if (ui_out_redirect (uiout, gdb_stdout) < 0)
134 warning (_("Current output protocol does not support redirection"));
138 set_logging_on (char *args, int from_tty)
143 xfree (logging_filename);
144 logging_filename = xstrdup (rest);
146 handle_redirections (from_tty);
150 set_logging_off (char *args, int from_tty)
152 if (saved_filename == NULL)
157 fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
158 xfree (saved_filename);
159 saved_filename = NULL;
163 set_logging_command (char *args, int from_tty)
165 printf_unfiltered (_("\
166 \"set logging\" lets you log output to a file.\n\
167 Usage: set logging on [FILENAME]\n\
169 set logging file FILENAME\n\
170 set logging overwrite [on|off]\n\
171 set logging redirect [on|off]\n"));
175 show_logging_command (char *args, int from_tty)
178 printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
179 if (saved_filename == NULL
180 || strcmp (logging_filename, saved_filename) != 0)
181 printf_unfiltered (_("Future logs will be written to %s.\n"),
184 if (logging_overwrite)
185 printf_unfiltered (_("Logs will overwrite the log file.\n"));
187 printf_unfiltered (_("Logs will be appended to the log file.\n"));
189 if (logging_redirect)
190 printf_unfiltered (_("Output will be sent only to the log file.\n"));
192 printf_unfiltered (_("Output will be logged and displayed.\n"));
195 /* Provide a prototype to silence -Wmissing-prototypes. */
196 extern initialize_file_ftype _initialize_cli_logging;
199 _initialize_cli_logging (void)
201 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
204 add_prefix_cmd ("logging", class_support, set_logging_command,
205 _("Set logging options"), &set_logging_cmdlist,
206 "set logging ", 0, &setlist);
207 add_prefix_cmd ("logging", class_support, show_logging_command,
208 _("Show logging options"), &show_logging_cmdlist,
209 "show logging ", 0, &showlist);
210 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
211 Set whether logging overwrites or appends to the log file."), _("\
212 Show whether logging overwrites or appends to the log file."), _("\
213 If set, logging overrides the log file."),
215 show_logging_overwrite,
216 &set_logging_cmdlist, &show_logging_cmdlist);
217 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
218 Set the logging output mode."), _("\
219 Show the logging output mode."), _("\
220 If redirect is off, output will go to both the screen and the log file.\n\
221 If redirect is on, output will go only to the log file."),
223 show_logging_redirect,
224 &set_logging_cmdlist, &show_logging_cmdlist);
225 add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
226 Set the current logfile."), _("\
227 Show the current logfile."), _("\
228 The logfile is used when directing GDB's output."),
230 show_logging_filename,
231 &set_logging_cmdlist, &show_logging_cmdlist);
232 add_cmd ("on", class_support, set_logging_on,
233 _("Enable logging."), &set_logging_cmdlist);
234 add_cmd ("off", class_support, set_logging_off,
235 _("Disable logging."), &set_logging_cmdlist);
237 logging_filename = xstrdup ("gdb.txt");