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