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