gdb/
[deliverable/binutils-gdb.git] / gdb / cli / cli-logging.c
CommitLineData
0fac0b41
DJ
1/* Command-line output logging for GDB, the GNU debugger.
2
4c38e0a4
JB
3 Copyright (c) 2003, 2004, 2007, 2008, 2009, 2010
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"
24
25#include "gdb_string.h"
26
27/* These hold the pushed copies of the gdb output files.
28 If NULL then nothing has yet been pushed. */
29struct saved_output_files
30{
31 struct ui_file *out;
32 struct ui_file *err;
33 struct ui_file *log;
34 struct ui_file *targ;
35};
36static struct saved_output_files saved_output;
37static char *saved_filename;
38
39static char *logging_filename;
920d2a44
AC
40static void
41show_logging_filename (struct ui_file *file, int from_tty,
42 struct cmd_list_element *c, const char *value)
43{
44 fprintf_filtered (file, _("The current logfile is \"%s\".\n"),
45 value);
46}
47
48int logging_overwrite;
49static void
50show_logging_overwrite (struct ui_file *file, int from_tty,
51 struct cmd_list_element *c, const char *value)
52{
53 fprintf_filtered (file, _("\
54Whether logging overwrites or appends to the log file is %s.\n"),
55 value);
56}
57
58int logging_redirect;
59static void
60show_logging_redirect (struct ui_file *file, int from_tty,
61 struct cmd_list_element *c, const char *value)
62{
63 fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
64}
0fac0b41
DJ
65
66/* If we've pushed output files, close them and pop them. */
67static void
83a8ccca 68pop_output_files (void)
0fac0b41
DJ
69{
70 /* Only delete one of the files -- they are all set to the same
71 value. */
72 ui_file_delete (gdb_stdout);
73 gdb_stdout = saved_output.out;
74 gdb_stderr = saved_output.err;
75 gdb_stdlog = saved_output.log;
76 gdb_stdtarg = saved_output.targ;
77 saved_output.out = NULL;
78 saved_output.err = NULL;
79 saved_output.log = NULL;
80 saved_output.targ = NULL;
81
82 ui_out_redirect (uiout, NULL);
83}
84
85/* This is a helper for the `set logging' command. */
86static void
87handle_redirections (int from_tty)
88{
724b958c 89 struct cleanup *cleanups;
0fac0b41
DJ
90 struct ui_file *output;
91
92 if (saved_filename != NULL)
93 {
94 fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
95 saved_filename);
96 return;
97 }
98
99 output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
100 if (output == NULL)
e2e0b3e5 101 perror_with_name (_("set logging"));
724b958c 102 cleanups = make_cleanup_ui_file_delete (output);
0fac0b41
DJ
103
104 /* Redirects everything to gdb_stdout while this is running. */
105 if (!logging_redirect)
106 {
107 output = tee_file_new (gdb_stdout, 0, output, 1);
108 if (output == NULL)
e2e0b3e5 109 perror_with_name (_("set logging"));
724b958c
TT
110 discard_cleanups (cleanups);
111 cleanups = make_cleanup_ui_file_delete (output);
0fac0b41
DJ
112 if (from_tty)
113 fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
114 logging_filename);
115 }
116 else if (from_tty)
117 fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
118 logging_filename);
119
724b958c
TT
120 discard_cleanups (cleanups);
121
0fac0b41
DJ
122 saved_filename = xstrdup (logging_filename);
123 saved_output.out = gdb_stdout;
124 saved_output.err = gdb_stderr;
125 saved_output.log = gdb_stdlog;
126 saved_output.targ = gdb_stdtarg;
127
128 gdb_stdout = output;
129 gdb_stderr = output;
130 gdb_stdlog = output;
131 gdb_stdtarg = output;
132
133 if (ui_out_redirect (uiout, gdb_stdout) < 0)
8a3fe4f8 134 warning (_("Current output protocol does not support redirection"));
0fac0b41
DJ
135}
136
137static void
138set_logging_on (char *args, int from_tty)
139{
140 char *rest = args;
141 if (rest && *rest)
142 {
143 xfree (logging_filename);
144 logging_filename = xstrdup (rest);
145 }
146 handle_redirections (from_tty);
147}
148
149static void
150set_logging_off (char *args, int from_tty)
151{
152 if (saved_filename == NULL)
153 return;
154
155 pop_output_files ();
156 if (from_tty)
157 fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
158 xfree (saved_filename);
159 saved_filename = NULL;
160}
161
162static void
163set_logging_command (char *args, int from_tty)
164{
a3f17187
AC
165 printf_unfiltered (_("\
166\"set logging\" lets you log output to a file.\n\
167Usage: set logging on [FILENAME]\n\
168 set logging off\n\
169 set logging file FILENAME\n\
170 set logging overwrite [on|off]\n\
171 set logging redirect [on|off]\n"));
0fac0b41
DJ
172}
173
2c0b251b 174static void
0fac0b41
DJ
175show_logging_command (char *args, int from_tty)
176{
177 if (saved_filename)
a3f17187 178 printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
0fac0b41
DJ
179 if (saved_filename == NULL
180 || strcmp (logging_filename, saved_filename) != 0)
a3f17187 181 printf_unfiltered (_("Future logs will be written to %s.\n"),
0fac0b41
DJ
182 logging_filename);
183
184 if (logging_overwrite)
a3f17187 185 printf_unfiltered (_("Logs will overwrite the log file.\n"));
0fac0b41 186 else
a3f17187 187 printf_unfiltered (_("Logs will be appended to the log file.\n"));
0fac0b41
DJ
188
189 if (logging_redirect)
a3f17187 190 printf_unfiltered (_("Output will be sent only to the log file.\n"));
0fac0b41 191 else
a3f17187 192 printf_unfiltered (_("Output will be logged and displayed.\n"));
0fac0b41
DJ
193}
194
2c0b251b
PA
195/* Provide a prototype to silence -Wmissing-prototypes. */
196extern initialize_file_ftype _initialize_cli_logging;
197
0fac0b41
DJ
198void
199_initialize_cli_logging (void)
200{
201 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
202
203
204 add_prefix_cmd ("logging", class_support, set_logging_command,
1bedd215 205 _("Set logging options"), &set_logging_cmdlist,
0fac0b41
DJ
206 "set logging ", 0, &setlist);
207 add_prefix_cmd ("logging", class_support, show_logging_command,
1bedd215 208 _("Show logging options"), &show_logging_cmdlist,
0fac0b41 209 "show logging ", 0, &showlist);
7915a72c
AC
210 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
211Set whether logging overwrites or appends to the log file."), _("\
212Show whether logging overwrites or appends to the log file."), _("\
213If set, logging overrides the log file."),
2c5b56ce 214 NULL,
920d2a44 215 show_logging_overwrite,
2c5b56ce 216 &set_logging_cmdlist, &show_logging_cmdlist);
7915a72c
AC
217 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
218Set the logging output mode."), _("\
219Show the logging output mode."), _("\
3b64bf98 220If redirect is off, output will go to both the screen and the log file.\n\
7915a72c 221If redirect is on, output will go only to the log file."),
2c5b56ce 222 NULL,
920d2a44 223 show_logging_redirect,
2c5b56ce 224 &set_logging_cmdlist, &show_logging_cmdlist);
7915a72c
AC
225 add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
226Set the current logfile."), _("\
227Show the current logfile."), _("\
228The logfile is used when directing GDB's output."),
2c5b56ce 229 NULL,
920d2a44 230 show_logging_filename,
b3f42336 231 &set_logging_cmdlist, &show_logging_cmdlist);
0fac0b41 232 add_cmd ("on", class_support, set_logging_on,
1a966eab 233 _("Enable logging."), &set_logging_cmdlist);
0fac0b41 234 add_cmd ("off", class_support, set_logging_off,
1a966eab 235 _("Disable logging."), &set_logging_cmdlist);
0fac0b41
DJ
236
237 logging_filename = xstrdup ("gdb.txt");
238}
This page took 0.685955 seconds and 4 git commands to generate.