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 ("\"set logging\" lets you log output to a file.\n");
137 printf_unfiltered ("Usage: set logging on [FILENAME]\n");
138 printf_unfiltered (" set logging off\n");
139 printf_unfiltered (" set logging file FILENAME\n");
140 printf_unfiltered (" set logging overwrite [on|off]\n");
141 printf_unfiltered (" set logging redirect [on|off]\n");
145 show_logging_command (char *args, int from_tty)
148 printf_unfiltered ("Currently logging to \"%s\".\n", saved_filename);
149 if (saved_filename == NULL
150 || strcmp (logging_filename, saved_filename) != 0)
151 printf_unfiltered ("Future logs will be written to %s.\n",
154 if (logging_overwrite)
155 printf_unfiltered ("Logs will overwrite the log file.\n");
157 printf_unfiltered ("Logs will be appended to the log file.\n");
159 if (logging_redirect)
160 printf_unfiltered ("Output will be sent only to the log file.\n");
162 printf_unfiltered ("Output will be logged and displayed.\n");
166 _initialize_cli_logging (void)
168 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
171 add_prefix_cmd ("logging", class_support, set_logging_command,
172 "Set logging options", &set_logging_cmdlist,
173 "set logging ", 0, &setlist);
174 add_prefix_cmd ("logging", class_support, show_logging_command,
175 "Show logging options", &show_logging_cmdlist,
176 "show logging ", 0, &showlist);
177 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, "\
178 Set whether logging overwrites or appends to the log file.", "\
179 Show whether logging overwrites or appends to the log file.", "\
180 If set, logging overrides the log file.", "\
181 Whether logging overwrites or appends to the log file is %s.",
182 NULL, NULL, &set_logging_cmdlist, &show_logging_cmdlist);
183 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, "\
184 Set the logging output mode.", "\
185 Show the logging output mode.", "\
186 If redirect is off, output will go to both the screen and the log file.\n\
187 If redirect is on, output will go only to the log file.", "\
188 The logging output mode is %s.",
189 NULL, NULL, &set_logging_cmdlist, &show_logging_cmdlist);
190 add_setshow_filename_cmd ("file", class_support, &logging_filename, "\
191 Set the current logfile.", "\
192 Show the current logfile.", "\
193 The logfile is used when directing GDB's output.", "\
194 The current logfile is %s.",
196 &set_logging_cmdlist, &show_logging_cmdlist);
197 add_cmd ("on", class_support, set_logging_on,
198 "Enable logging.", &set_logging_cmdlist);
199 add_cmd ("off", class_support, set_logging_off,
200 "Disable logging.", &set_logging_cmdlist);
202 logging_filename = xstrdup ("gdb.txt");