1 /* Command-line output logging for GDB, the GNU debugger.
3 Copyright 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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
37 static struct saved_output_files saved_output;
38 static char *saved_filename;
40 static char *logging_filename;
41 int logging_overwrite, logging_redirect;
43 /* If we've pushed output files, close them and pop them. */
45 pop_output_files (void)
47 /* Only delete one of the files -- they are all set to the same
49 ui_file_delete (gdb_stdout);
50 gdb_stdout = saved_output.out;
51 gdb_stderr = saved_output.err;
52 gdb_stdlog = saved_output.log;
53 gdb_stdtarg = saved_output.targ;
54 saved_output.out = NULL;
55 saved_output.err = NULL;
56 saved_output.log = NULL;
57 saved_output.targ = NULL;
59 ui_out_redirect (uiout, NULL);
62 /* This is a helper for the `set logging' command. */
64 handle_redirections (int from_tty)
66 struct ui_file *output;
68 if (saved_filename != NULL)
70 fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
75 output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
77 perror_with_name (_("set logging"));
79 /* Redirects everything to gdb_stdout while this is running. */
80 if (!logging_redirect)
82 output = tee_file_new (gdb_stdout, 0, output, 1);
84 perror_with_name (_("set logging"));
86 fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
90 fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
93 saved_filename = xstrdup (logging_filename);
94 saved_output.out = gdb_stdout;
95 saved_output.err = gdb_stderr;
96 saved_output.log = gdb_stdlog;
97 saved_output.targ = gdb_stdtarg;
102 gdb_stdtarg = output;
104 if (ui_out_redirect (uiout, gdb_stdout) < 0)
105 warning (_("Current output protocol does not support redirection"));
109 set_logging_on (char *args, int from_tty)
114 xfree (logging_filename);
115 logging_filename = xstrdup (rest);
117 handle_redirections (from_tty);
121 set_logging_off (char *args, int from_tty)
123 if (saved_filename == NULL)
128 fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
129 xfree (saved_filename);
130 saved_filename = NULL;
134 set_logging_command (char *args, int from_tty)
136 printf_unfiltered (_("\
137 \"set logging\" lets you log output to a file.\n\
138 Usage: set logging on [FILENAME]\n\
140 set logging file FILENAME\n\
141 set logging overwrite [on|off]\n\
142 set logging redirect [on|off]\n"));
146 show_logging_command (char *args, int from_tty)
149 printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
150 if (saved_filename == NULL
151 || strcmp (logging_filename, saved_filename) != 0)
152 printf_unfiltered (_("Future logs will be written to %s.\n"),
155 if (logging_overwrite)
156 printf_unfiltered (_("Logs will overwrite the log file.\n"));
158 printf_unfiltered (_("Logs will be appended to the log file.\n"));
160 if (logging_redirect)
161 printf_unfiltered (_("Output will be sent only to the log file.\n"));
163 printf_unfiltered (_("Output will be logged and displayed.\n"));
167 _initialize_cli_logging (void)
169 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
172 add_prefix_cmd ("logging", class_support, set_logging_command,
173 _("Set logging options"), &set_logging_cmdlist,
174 "set logging ", 0, &setlist);
175 add_prefix_cmd ("logging", class_support, show_logging_command,
176 _("Show logging options"), &show_logging_cmdlist,
177 "show logging ", 0, &showlist);
178 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
179 Set whether logging overwrites or appends to the log file."), _("\
180 Show whether logging overwrites or appends to the log file."), _("\
181 If set, logging overrides the log file."),
183 NULL, /* FIXME: i18n: Whether logging overwrites or appends to the log file is %s. */
184 &set_logging_cmdlist, &show_logging_cmdlist);
185 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
186 Set the logging output mode."), _("\
187 Show the logging output mode."), _("\
188 If redirect is off, output will go to both the screen and the log file.\n\
189 If redirect is on, output will go only to the log file."),
191 NULL, /* FIXME: i18n: The logging output mode is %s. */
192 &set_logging_cmdlist, &show_logging_cmdlist);
193 add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
194 Set the current logfile."), _("\
195 Show the current logfile."), _("\
196 The logfile is used when directing GDB's output."),
198 NULL, /* FIXME: i18n: The current logfile is %s. */
199 &set_logging_cmdlist, &show_logging_cmdlist);
200 add_cmd ("on", class_support, set_logging_on,
201 _("Enable logging."), &set_logging_cmdlist);
202 add_cmd ("off", class_support, set_logging_off,
203 _("Disable logging."), &set_logging_cmdlist);
205 logging_filename = xstrdup ("gdb.txt");