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/>. */
24 #include "gdb_assert.h"
26 #include "gdb_string.h"
28 /* These hold the pushed copies of the gdb output files.
29 If NULL then nothing has yet been pushed. */
30 struct saved_output_files
36 struct ui_file *targerr;
38 static struct saved_output_files saved_output;
39 static char *saved_filename;
41 static char *logging_filename;
43 show_logging_filename (struct ui_file *file, int from_tty,
44 struct cmd_list_element *c, const char *value)
46 fprintf_filtered (file, _("The current logfile is \"%s\".\n"),
50 static int logging_overwrite;
53 set_logging_overwrite (char *args, int from_tty, struct cmd_list_element *c)
56 warning (_("Currently logging to %s. Turn the logging off and on to "
57 "make the new setting effective."), saved_filename);
61 show_logging_overwrite (struct ui_file *file, int from_tty,
62 struct cmd_list_element *c, const char *value)
64 fprintf_filtered (file, _("\
65 Whether logging overwrites or appends to the log file is %s.\n"),
69 /* Value as configured by the user. */
70 static int logging_redirect;
72 /* The on-disk file in use if logging is currently active together
73 with redirection turned off (and therefore using tee_file_new).
74 For active logging with redirection the on-disk file is directly in
75 GDB_STDOUT and this variable is NULL. */
76 static struct ui_file *logging_no_redirect_file;
79 set_logging_redirect (char *args, int from_tty, struct cmd_list_element *c)
81 struct cleanup *cleanups = NULL;
82 struct ui_file *output, *new_logging_no_redirect_file;
84 if (saved_filename == NULL
85 || (logging_redirect != 0 && logging_no_redirect_file == NULL)
86 || (logging_redirect == 0 && logging_no_redirect_file != NULL))
89 if (logging_redirect != 0)
91 gdb_assert (logging_no_redirect_file != NULL);
93 /* ui_out_redirect still has not been called for next
95 cleanups = make_cleanup_ui_file_delete (gdb_stdout);
97 output = logging_no_redirect_file;
98 new_logging_no_redirect_file = NULL;
101 fprintf_unfiltered (saved_output.out, "Redirecting output to %s.\n",
106 gdb_assert (logging_no_redirect_file == NULL);
107 output = tee_file_new (saved_output.out, 0, gdb_stdout, 0);
109 perror_with_name (_("set logging"));
110 new_logging_no_redirect_file = gdb_stdout;
113 fprintf_unfiltered (saved_output.out, "Copying output to %s.\n",
120 gdb_stdtarg = output;
121 gdb_stdtargerr = output;
122 logging_no_redirect_file = new_logging_no_redirect_file;
124 /* There is a former output pushed on the ui_out_redirect stack. We
125 want to replace it by OUTPUT so we must pop the former value
126 first. We should either do both the pop and push or to do
127 neither of it. At least do not try to push OUTPUT if the pop
130 if (ui_out_redirect (uiout, NULL) < 0
131 || ui_out_redirect (uiout, output) < 0)
132 warning (_("Current output protocol does not support redirection"));
134 if (logging_redirect != 0)
135 do_cleanups (cleanups);
139 show_logging_redirect (struct ui_file *file, int from_tty,
140 struct cmd_list_element *c, const char *value)
142 fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
145 /* If we've pushed output files, close them and pop them. */
147 pop_output_files (void)
149 /* Only delete one of the files -- they are all set to the same
151 ui_file_delete (gdb_stdout);
152 if (logging_no_redirect_file)
154 ui_file_delete (logging_no_redirect_file);
155 logging_no_redirect_file = NULL;
157 gdb_stdout = saved_output.out;
158 gdb_stderr = saved_output.err;
159 gdb_stdlog = saved_output.log;
160 gdb_stdtarg = saved_output.targ;
161 gdb_stdtargerr = saved_output.targ;
162 saved_output.out = NULL;
163 saved_output.err = NULL;
164 saved_output.log = NULL;
165 saved_output.targ = NULL;
166 saved_output.targerr = NULL;
168 ui_out_redirect (uiout, NULL);
171 /* This is a helper for the `set logging' command. */
173 handle_redirections (int from_tty)
175 struct cleanup *cleanups;
176 struct ui_file *output;
178 if (saved_filename != NULL)
180 fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
185 output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
187 perror_with_name (_("set logging"));
188 cleanups = make_cleanup_ui_file_delete (output);
190 /* Redirects everything to gdb_stdout while this is running. */
191 if (!logging_redirect)
193 struct ui_file *no_redirect_file = output;
195 output = tee_file_new (gdb_stdout, 0, no_redirect_file, 0);
197 perror_with_name (_("set logging"));
198 make_cleanup_ui_file_delete (output);
200 fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
202 logging_no_redirect_file = no_redirect_file;
206 gdb_assert (logging_no_redirect_file == NULL);
209 fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
213 discard_cleanups (cleanups);
215 saved_filename = xstrdup (logging_filename);
216 saved_output.out = gdb_stdout;
217 saved_output.err = gdb_stderr;
218 saved_output.log = gdb_stdlog;
219 saved_output.targ = gdb_stdtarg;
220 saved_output.targerr = gdb_stdtargerr;
225 gdb_stdtarg = output;
226 gdb_stdtargerr = output;
228 if (ui_out_redirect (uiout, output) < 0)
229 warning (_("Current output protocol does not support redirection"));
233 set_logging_on (char *args, int from_tty)
239 xfree (logging_filename);
240 logging_filename = xstrdup (rest);
242 handle_redirections (from_tty);
246 set_logging_off (char *args, int from_tty)
248 if (saved_filename == NULL)
253 fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
254 xfree (saved_filename);
255 saved_filename = NULL;
259 set_logging_command (char *args, int from_tty)
261 printf_unfiltered (_("\
262 \"set logging\" lets you log output to a file.\n\
263 Usage: set logging on [FILENAME]\n\
265 set logging file FILENAME\n\
266 set logging overwrite [on|off]\n\
267 set logging redirect [on|off]\n"));
271 show_logging_command (char *args, int from_tty)
274 printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
275 if (saved_filename == NULL
276 || strcmp (logging_filename, saved_filename) != 0)
277 printf_unfiltered (_("Future logs will be written to %s.\n"),
280 if (logging_overwrite)
281 printf_unfiltered (_("Logs will overwrite the log file.\n"));
283 printf_unfiltered (_("Logs will be appended to the log file.\n"));
287 if (logging_redirect)
288 printf_unfiltered (_("Output is being sent only to the log file.\n"));
290 printf_unfiltered (_("Output is being logged and displayed.\n"));
294 if (logging_redirect)
295 printf_unfiltered (_("Output will be sent only to the log file.\n"));
297 printf_unfiltered (_("Output will be logged and displayed.\n"));
301 /* Provide a prototype to silence -Wmissing-prototypes. */
302 extern initialize_file_ftype _initialize_cli_logging;
305 _initialize_cli_logging (void)
307 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
309 add_prefix_cmd ("logging", class_support, set_logging_command,
310 _("Set logging options"), &set_logging_cmdlist,
311 "set logging ", 0, &setlist);
312 add_prefix_cmd ("logging", class_support, show_logging_command,
313 _("Show logging options"), &show_logging_cmdlist,
314 "show logging ", 0, &showlist);
315 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
316 Set whether logging overwrites or appends to the log file."), _("\
317 Show whether logging overwrites or appends to the log file."), _("\
318 If set, logging overrides the log file."),
319 set_logging_overwrite,
320 show_logging_overwrite,
321 &set_logging_cmdlist, &show_logging_cmdlist);
322 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
323 Set the logging output mode."), _("\
324 Show the logging output mode."), _("\
325 If redirect is off, output will go to both the screen and the log file.\n\
326 If redirect is on, output will go only to the log file."),
327 set_logging_redirect,
328 show_logging_redirect,
329 &set_logging_cmdlist, &show_logging_cmdlist);
330 add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
331 Set the current logfile."), _("\
332 Show the current logfile."), _("\
333 The logfile is used when directing GDB's output."),
335 show_logging_filename,
336 &set_logging_cmdlist, &show_logging_cmdlist);
337 add_cmd ("on", class_support, set_logging_on,
338 _("Enable logging."), &set_logging_cmdlist);
339 add_cmd ("off", class_support, set_logging_off,
340 _("Disable logging."), &set_logging_cmdlist);
342 logging_filename = xstrdup ("gdb.txt");