1 /* Command-line output logging for GDB, the GNU debugger.
3 Copyright (c) 2003, 2004, 2007, 2008, 2009 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 3 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, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_string.h"
26 /* These hold the pushed copies of the gdb output files.
27 If NULL then nothing has yet been pushed. */
28 struct saved_output_files
35 static struct saved_output_files saved_output
;
36 static char *saved_filename
;
38 static char *logging_filename
;
40 show_logging_filename (struct ui_file
*file
, int from_tty
,
41 struct cmd_list_element
*c
, const char *value
)
43 fprintf_filtered (file
, _("The current logfile is \"%s\".\n"),
47 int logging_overwrite
;
49 show_logging_overwrite (struct ui_file
*file
, int from_tty
,
50 struct cmd_list_element
*c
, const char *value
)
52 fprintf_filtered (file
, _("\
53 Whether logging overwrites or appends to the log file is %s.\n"),
59 show_logging_redirect (struct ui_file
*file
, int from_tty
,
60 struct cmd_list_element
*c
, const char *value
)
62 fprintf_filtered (file
, _("The logging output mode is %s.\n"), value
);
65 /* If we've pushed output files, close them and pop them. */
67 pop_output_files (void)
69 /* Only delete one of the files -- they are all set to the same
71 ui_file_delete (gdb_stdout
);
72 gdb_stdout
= saved_output
.out
;
73 gdb_stderr
= saved_output
.err
;
74 gdb_stdlog
= saved_output
.log
;
75 gdb_stdtarg
= saved_output
.targ
;
76 saved_output
.out
= NULL
;
77 saved_output
.err
= NULL
;
78 saved_output
.log
= NULL
;
79 saved_output
.targ
= NULL
;
81 ui_out_redirect (uiout
, NULL
);
84 /* This is a helper for the `set logging' command. */
86 handle_redirections (int from_tty
)
88 struct cleanup
*cleanups
;
89 struct ui_file
*output
;
91 if (saved_filename
!= NULL
)
93 fprintf_unfiltered (gdb_stdout
, "Already logging to %s.\n",
98 output
= gdb_fopen (logging_filename
, logging_overwrite
? "w" : "a");
100 perror_with_name (_("set logging"));
101 cleanups
= make_cleanup_ui_file_delete (output
);
103 /* Redirects everything to gdb_stdout while this is running. */
104 if (!logging_redirect
)
106 output
= tee_file_new (gdb_stdout
, 0, output
, 1);
108 perror_with_name (_("set logging"));
109 discard_cleanups (cleanups
);
110 cleanups
= make_cleanup_ui_file_delete (output
);
112 fprintf_unfiltered (gdb_stdout
, "Copying output to %s.\n",
116 fprintf_unfiltered (gdb_stdout
, "Redirecting output to %s.\n",
119 discard_cleanups (cleanups
);
121 saved_filename
= xstrdup (logging_filename
);
122 saved_output
.out
= gdb_stdout
;
123 saved_output
.err
= gdb_stderr
;
124 saved_output
.log
= gdb_stdlog
;
125 saved_output
.targ
= gdb_stdtarg
;
130 gdb_stdtarg
= output
;
132 if (ui_out_redirect (uiout
, gdb_stdout
) < 0)
133 warning (_("Current output protocol does not support redirection"));
137 set_logging_on (char *args
, int from_tty
)
142 xfree (logging_filename
);
143 logging_filename
= xstrdup (rest
);
145 handle_redirections (from_tty
);
149 set_logging_off (char *args
, int from_tty
)
151 if (saved_filename
== NULL
)
156 fprintf_unfiltered (gdb_stdout
, "Done logging to %s.\n", saved_filename
);
157 xfree (saved_filename
);
158 saved_filename
= NULL
;
162 set_logging_command (char *args
, int from_tty
)
164 printf_unfiltered (_("\
165 \"set logging\" lets you log output to a file.\n\
166 Usage: set logging on [FILENAME]\n\
168 set logging file FILENAME\n\
169 set logging overwrite [on|off]\n\
170 set logging redirect [on|off]\n"));
174 show_logging_command (char *args
, int from_tty
)
177 printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename
);
178 if (saved_filename
== NULL
179 || strcmp (logging_filename
, saved_filename
) != 0)
180 printf_unfiltered (_("Future logs will be written to %s.\n"),
183 if (logging_overwrite
)
184 printf_unfiltered (_("Logs will overwrite the log file.\n"));
186 printf_unfiltered (_("Logs will be appended to the log file.\n"));
188 if (logging_redirect
)
189 printf_unfiltered (_("Output will be sent only to the log file.\n"));
191 printf_unfiltered (_("Output will be logged and displayed.\n"));
195 _initialize_cli_logging (void)
197 static struct cmd_list_element
*set_logging_cmdlist
, *show_logging_cmdlist
;
200 add_prefix_cmd ("logging", class_support
, set_logging_command
,
201 _("Set logging options"), &set_logging_cmdlist
,
202 "set logging ", 0, &setlist
);
203 add_prefix_cmd ("logging", class_support
, show_logging_command
,
204 _("Show logging options"), &show_logging_cmdlist
,
205 "show logging ", 0, &showlist
);
206 add_setshow_boolean_cmd ("overwrite", class_support
, &logging_overwrite
, _("\
207 Set whether logging overwrites or appends to the log file."), _("\
208 Show whether logging overwrites or appends to the log file."), _("\
209 If set, logging overrides the log file."),
211 show_logging_overwrite
,
212 &set_logging_cmdlist
, &show_logging_cmdlist
);
213 add_setshow_boolean_cmd ("redirect", class_support
, &logging_redirect
, _("\
214 Set the logging output mode."), _("\
215 Show the logging output mode."), _("\
216 If redirect is off, output will go to both the screen and the log file.\n\
217 If redirect is on, output will go only to the log file."),
219 show_logging_redirect
,
220 &set_logging_cmdlist
, &show_logging_cmdlist
);
221 add_setshow_filename_cmd ("file", class_support
, &logging_filename
, _("\
222 Set the current logfile."), _("\
223 Show the current logfile."), _("\
224 The logfile is used when directing GDB's output."),
226 show_logging_filename
,
227 &set_logging_cmdlist
, &show_logging_cmdlist
);
228 add_cmd ("on", class_support
, set_logging_on
,
229 _("Enable logging."), &set_logging_cmdlist
);
230 add_cmd ("off", class_support
, set_logging_off
,
231 _("Disable logging."), &set_logging_cmdlist
);
233 logging_filename
= xstrdup ("gdb.txt");