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