2011-03-18 Phil Muldoon <pmuldoon@redhat.com>
[deliverable/binutils-gdb.git] / gdb / cli / cli-logging.c
CommitLineData
0fac0b41
DJ
1/* Command-line output logging for GDB, the GNU debugger.
2
7b6bb8da 3 Copyright (c) 2003, 2004, 2007, 2008, 2009, 2010, 2011
4c38e0a4 4 Free Software Foundation, Inc.
0fac0b41
DJ
5
6 This file is part of GDB.
7
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
0fac0b41
DJ
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
0fac0b41
DJ
20
21#include "defs.h"
22#include "gdbcmd.h"
23#include "ui-out.h"
58b61394 24#include "gdb_assert.h"
0fac0b41
DJ
25
26#include "gdb_string.h"
27
28/* These hold the pushed copies of the gdb output files.
29 If NULL then nothing has yet been pushed. */
30struct saved_output_files
31{
32 struct ui_file *out;
33 struct ui_file *err;
34 struct ui_file *log;
35 struct ui_file *targ;
8d4d924b 36 struct ui_file *targerr;
0fac0b41
DJ
37};
38static struct saved_output_files saved_output;
39static char *saved_filename;
40
41static char *logging_filename;
920d2a44
AC
42static void
43show_logging_filename (struct ui_file *file, int from_tty,
44 struct cmd_list_element *c, const char *value)
45{
46 fprintf_filtered (file, _("The current logfile is \"%s\".\n"),
47 value);
48}
49
44be957e 50static int logging_overwrite;
58b61394
JK
51
52static void
53set_logging_overwrite (char *args, int from_tty, struct cmd_list_element *c)
54{
55 if (saved_filename)
56 warning (_("Currently logging to %s. Turn the logging off and on to "
57 "make the new setting effective."), saved_filename);
58}
59
920d2a44
AC
60static void
61show_logging_overwrite (struct ui_file *file, int from_tty,
62 struct cmd_list_element *c, const char *value)
63{
9a2b4c1b
MS
64 fprintf_filtered (file,
65 _("Whether logging overwrites or "
66 "appends to the log file is %s.\n"),
920d2a44
AC
67 value);
68}
69
58b61394 70/* Value as configured by the user. */
44be957e 71static int logging_redirect;
58b61394 72
ebcd3b23
MS
73/* The on-disk file in use if logging is currently active together
74 with redirection turned off (and therefore using tee_file_new).
75 For active logging with redirection the on-disk file is directly in
76 GDB_STDOUT and this variable is NULL. */
58b61394
JK
77static struct ui_file *logging_no_redirect_file;
78
79static void
80set_logging_redirect (char *args, int from_tty, struct cmd_list_element *c)
81{
82 struct cleanup *cleanups = NULL;
83 struct ui_file *output, *new_logging_no_redirect_file;
84
85 if (saved_filename == NULL
86 || (logging_redirect != 0 && logging_no_redirect_file == NULL)
87 || (logging_redirect == 0 && logging_no_redirect_file != NULL))
88 return;
89
90 if (logging_redirect != 0)
91 {
92 gdb_assert (logging_no_redirect_file != NULL);
93
ebcd3b23
MS
94 /* ui_out_redirect still has not been called for next
95 gdb_stdout. */
58b61394
JK
96 cleanups = make_cleanup_ui_file_delete (gdb_stdout);
97
98 output = logging_no_redirect_file;
99 new_logging_no_redirect_file = NULL;
100
101 if (from_tty)
102 fprintf_unfiltered (saved_output.out, "Redirecting output to %s.\n",
103 logging_filename);
104 }
105 else
106 {
107 gdb_assert (logging_no_redirect_file == NULL);
108 output = tee_file_new (saved_output.out, 0, gdb_stdout, 0);
109 if (output == NULL)
110 perror_with_name (_("set logging"));
111 new_logging_no_redirect_file = gdb_stdout;
112
113 if (from_tty)
114 fprintf_unfiltered (saved_output.out, "Copying output to %s.\n",
115 logging_filename);
116 }
117
118 gdb_stdout = output;
119 gdb_stderr = output;
120 gdb_stdlog = output;
121 gdb_stdtarg = output;
8d4d924b 122 gdb_stdtargerr = output;
58b61394
JK
123 logging_no_redirect_file = new_logging_no_redirect_file;
124
ebcd3b23
MS
125 /* There is a former output pushed on the ui_out_redirect stack. We
126 want to replace it by OUTPUT so we must pop the former value
127 first. We should either do both the pop and push or to do
128 neither of it. At least do not try to push OUTPUT if the pop
129 already failed. */
14dba4b4
JK
130
131 if (ui_out_redirect (uiout, NULL) < 0
132 || ui_out_redirect (uiout, output) < 0)
58b61394
JK
133 warning (_("Current output protocol does not support redirection"));
134
135 if (logging_redirect != 0)
136 do_cleanups (cleanups);
137}
138
920d2a44
AC
139static void
140show_logging_redirect (struct ui_file *file, int from_tty,
141 struct cmd_list_element *c, const char *value)
142{
143 fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
144}
0fac0b41
DJ
145
146/* If we've pushed output files, close them and pop them. */
147static void
83a8ccca 148pop_output_files (void)
0fac0b41
DJ
149{
150 /* Only delete one of the files -- they are all set to the same
151 value. */
152 ui_file_delete (gdb_stdout);
58b61394
JK
153 if (logging_no_redirect_file)
154 {
155 ui_file_delete (logging_no_redirect_file);
156 logging_no_redirect_file = NULL;
157 }
0fac0b41
DJ
158 gdb_stdout = saved_output.out;
159 gdb_stderr = saved_output.err;
160 gdb_stdlog = saved_output.log;
161 gdb_stdtarg = saved_output.targ;
8d4d924b 162 gdb_stdtargerr = saved_output.targ;
0fac0b41
DJ
163 saved_output.out = NULL;
164 saved_output.err = NULL;
165 saved_output.log = NULL;
166 saved_output.targ = NULL;
8d4d924b 167 saved_output.targerr = NULL;
0fac0b41
DJ
168
169 ui_out_redirect (uiout, NULL);
170}
171
172/* This is a helper for the `set logging' command. */
173static void
174handle_redirections (int from_tty)
175{
724b958c 176 struct cleanup *cleanups;
0fac0b41
DJ
177 struct ui_file *output;
178
179 if (saved_filename != NULL)
180 {
181 fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
182 saved_filename);
183 return;
184 }
185
186 output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
187 if (output == NULL)
e2e0b3e5 188 perror_with_name (_("set logging"));
724b958c 189 cleanups = make_cleanup_ui_file_delete (output);
0fac0b41
DJ
190
191 /* Redirects everything to gdb_stdout while this is running. */
192 if (!logging_redirect)
193 {
58b61394
JK
194 struct ui_file *no_redirect_file = output;
195
196 output = tee_file_new (gdb_stdout, 0, no_redirect_file, 0);
0fac0b41 197 if (output == NULL)
e2e0b3e5 198 perror_with_name (_("set logging"));
58b61394 199 make_cleanup_ui_file_delete (output);
0fac0b41
DJ
200 if (from_tty)
201 fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
202 logging_filename);
58b61394
JK
203 logging_no_redirect_file = no_redirect_file;
204 }
205 else
206 {
207 gdb_assert (logging_no_redirect_file == NULL);
208
209 if (from_tty)
210 fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
211 logging_filename);
0fac0b41 212 }
0fac0b41 213
724b958c
TT
214 discard_cleanups (cleanups);
215
0fac0b41
DJ
216 saved_filename = xstrdup (logging_filename);
217 saved_output.out = gdb_stdout;
218 saved_output.err = gdb_stderr;
219 saved_output.log = gdb_stdlog;
220 saved_output.targ = gdb_stdtarg;
8d4d924b 221 saved_output.targerr = gdb_stdtargerr;
0fac0b41
DJ
222
223 gdb_stdout = output;
224 gdb_stderr = output;
225 gdb_stdlog = output;
226 gdb_stdtarg = output;
8d4d924b 227 gdb_stdtargerr = output;
0fac0b41 228
14dba4b4 229 if (ui_out_redirect (uiout, output) < 0)
8a3fe4f8 230 warning (_("Current output protocol does not support redirection"));
0fac0b41
DJ
231}
232
233static void
234set_logging_on (char *args, int from_tty)
235{
236 char *rest = args;
cdb27c12 237
0fac0b41
DJ
238 if (rest && *rest)
239 {
240 xfree (logging_filename);
241 logging_filename = xstrdup (rest);
242 }
243 handle_redirections (from_tty);
244}
245
246static void
247set_logging_off (char *args, int from_tty)
248{
249 if (saved_filename == NULL)
250 return;
251
252 pop_output_files ();
253 if (from_tty)
254 fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
255 xfree (saved_filename);
256 saved_filename = NULL;
257}
258
259static void
260set_logging_command (char *args, int from_tty)
261{
9a2b4c1b
MS
262 printf_unfiltered (_("\"set logging\" lets you log output to a file.\n"
263 "Usage: set logging on [FILENAME]\n"
264 " set logging off\n"
265 " set logging file FILENAME\n"
266 " set logging overwrite [on|off]\n"
267 " set logging redirect [on|off]\n"));
0fac0b41
DJ
268}
269
2c0b251b 270static void
0fac0b41
DJ
271show_logging_command (char *args, int from_tty)
272{
273 if (saved_filename)
a3f17187 274 printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
0fac0b41
DJ
275 if (saved_filename == NULL
276 || strcmp (logging_filename, saved_filename) != 0)
a3f17187 277 printf_unfiltered (_("Future logs will be written to %s.\n"),
0fac0b41
DJ
278 logging_filename);
279
280 if (logging_overwrite)
a3f17187 281 printf_unfiltered (_("Logs will overwrite the log file.\n"));
0fac0b41 282 else
a3f17187 283 printf_unfiltered (_("Logs will be appended to the log file.\n"));
0fac0b41 284
58b61394
JK
285 if (saved_filename)
286 {
287 if (logging_redirect)
288 printf_unfiltered (_("Output is being sent only to the log file.\n"));
289 else
290 printf_unfiltered (_("Output is being logged and displayed.\n"));
291 }
0fac0b41 292 else
58b61394
JK
293 {
294 if (logging_redirect)
295 printf_unfiltered (_("Output will be sent only to the log file.\n"));
296 else
297 printf_unfiltered (_("Output will be logged and displayed.\n"));
298 }
0fac0b41
DJ
299}
300
2c0b251b
PA
301/* Provide a prototype to silence -Wmissing-prototypes. */
302extern initialize_file_ftype _initialize_cli_logging;
303
0fac0b41
DJ
304void
305_initialize_cli_logging (void)
306{
307 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
308
0fac0b41 309 add_prefix_cmd ("logging", class_support, set_logging_command,
1bedd215 310 _("Set logging options"), &set_logging_cmdlist,
0fac0b41
DJ
311 "set logging ", 0, &setlist);
312 add_prefix_cmd ("logging", class_support, show_logging_command,
1bedd215 313 _("Show logging options"), &show_logging_cmdlist,
0fac0b41 314 "show logging ", 0, &showlist);
7915a72c
AC
315 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
316Set whether logging overwrites or appends to the log file."), _("\
317Show whether logging overwrites or appends to the log file."), _("\
318If set, logging overrides the log file."),
58b61394 319 set_logging_overwrite,
920d2a44 320 show_logging_overwrite,
2c5b56ce 321 &set_logging_cmdlist, &show_logging_cmdlist);
7915a72c
AC
322 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
323Set the logging output mode."), _("\
324Show the logging output mode."), _("\
3b64bf98 325If redirect is off, output will go to both the screen and the log file.\n\
7915a72c 326If redirect is on, output will go only to the log file."),
58b61394 327 set_logging_redirect,
920d2a44 328 show_logging_redirect,
2c5b56ce 329 &set_logging_cmdlist, &show_logging_cmdlist);
7915a72c
AC
330 add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
331Set the current logfile."), _("\
332Show the current logfile."), _("\
333The logfile is used when directing GDB's output."),
2c5b56ce 334 NULL,
920d2a44 335 show_logging_filename,
b3f42336 336 &set_logging_cmdlist, &show_logging_cmdlist);
0fac0b41 337 add_cmd ("on", class_support, set_logging_on,
1a966eab 338 _("Enable logging."), &set_logging_cmdlist);
0fac0b41 339 add_cmd ("off", class_support, set_logging_off,
1a966eab 340 _("Disable logging."), &set_logging_cmdlist);
0fac0b41
DJ
341
342 logging_filename = xstrdup ("gdb.txt");
343}
This page took 0.477931 seconds and 4 git commands to generate.