]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cli/cli-logging.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / cli / cli-logging.c
CommitLineData
0fac0b41
DJ
1/* Command-line output logging for GDB, the GNU debugger.
2
6aba47ca 3 Copyright (c) 2003, 2004, 2007 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
9 the Free Software Foundation; either version 2 of the License, or
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
18 along with this program; if not, write to the Free Software
10f9c213
EZ
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
0fac0b41
DJ
21
22#include "defs.h"
23#include "gdbcmd.h"
24#include "ui-out.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. */
30struct 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};
37static struct saved_output_files saved_output;
38static char *saved_filename;
39
40static char *logging_filename;
920d2a44
AC
41static void
42show_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
49int logging_overwrite;
50static void
51show_logging_overwrite (struct ui_file *file, int from_tty,
52 struct cmd_list_element *c, const char *value)
53{
54 fprintf_filtered (file, _("\
55Whether logging overwrites or appends to the log file is %s.\n"),
56 value);
57}
58
59int logging_redirect;
60static void
61show_logging_redirect (struct ui_file *file, int from_tty,
62 struct cmd_list_element *c, const char *value)
63{
64 fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
65}
0fac0b41
DJ
66
67/* If we've pushed output files, close them and pop them. */
68static void
83a8ccca 69pop_output_files (void)
0fac0b41
DJ
70{
71 /* Only delete one of the files -- they are all set to the same
72 value. */
73 ui_file_delete (gdb_stdout);
74 gdb_stdout = saved_output.out;
75 gdb_stderr = saved_output.err;
76 gdb_stdlog = saved_output.log;
77 gdb_stdtarg = saved_output.targ;
78 saved_output.out = NULL;
79 saved_output.err = NULL;
80 saved_output.log = NULL;
81 saved_output.targ = NULL;
82
83 ui_out_redirect (uiout, NULL);
84}
85
86/* This is a helper for the `set logging' command. */
87static void
88handle_redirections (int from_tty)
89{
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"));
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"));
0fac0b41
DJ
109 if (from_tty)
110 fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
111 logging_filename);
112 }
113 else if (from_tty)
114 fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
115 logging_filename);
116
117 saved_filename = xstrdup (logging_filename);
118 saved_output.out = gdb_stdout;
119 saved_output.err = gdb_stderr;
120 saved_output.log = gdb_stdlog;
121 saved_output.targ = gdb_stdtarg;
122
123 gdb_stdout = output;
124 gdb_stderr = output;
125 gdb_stdlog = output;
126 gdb_stdtarg = output;
127
128 if (ui_out_redirect (uiout, gdb_stdout) < 0)
8a3fe4f8 129 warning (_("Current output protocol does not support redirection"));
0fac0b41
DJ
130}
131
132static void
133set_logging_on (char *args, int from_tty)
134{
135 char *rest = args;
136 if (rest && *rest)
137 {
138 xfree (logging_filename);
139 logging_filename = xstrdup (rest);
140 }
141 handle_redirections (from_tty);
142}
143
144static void
145set_logging_off (char *args, int from_tty)
146{
147 if (saved_filename == NULL)
148 return;
149
150 pop_output_files ();
151 if (from_tty)
152 fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
153 xfree (saved_filename);
154 saved_filename = NULL;
155}
156
157static void
158set_logging_command (char *args, int from_tty)
159{
a3f17187
AC
160 printf_unfiltered (_("\
161\"set logging\" lets you log output to a file.\n\
162Usage: set logging on [FILENAME]\n\
163 set logging off\n\
164 set logging file FILENAME\n\
165 set logging overwrite [on|off]\n\
166 set logging redirect [on|off]\n"));
0fac0b41
DJ
167}
168
169void
170show_logging_command (char *args, int from_tty)
171{
172 if (saved_filename)
a3f17187 173 printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
0fac0b41
DJ
174 if (saved_filename == NULL
175 || strcmp (logging_filename, saved_filename) != 0)
a3f17187 176 printf_unfiltered (_("Future logs will be written to %s.\n"),
0fac0b41
DJ
177 logging_filename);
178
179 if (logging_overwrite)
a3f17187 180 printf_unfiltered (_("Logs will overwrite the log file.\n"));
0fac0b41 181 else
a3f17187 182 printf_unfiltered (_("Logs will be appended to the log file.\n"));
0fac0b41
DJ
183
184 if (logging_redirect)
a3f17187 185 printf_unfiltered (_("Output will be sent only to the log file.\n"));
0fac0b41 186 else
a3f17187 187 printf_unfiltered (_("Output will be logged and displayed.\n"));
0fac0b41
DJ
188}
189
190void
191_initialize_cli_logging (void)
192{
193 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
194
195
196 add_prefix_cmd ("logging", class_support, set_logging_command,
1bedd215 197 _("Set logging options"), &set_logging_cmdlist,
0fac0b41
DJ
198 "set logging ", 0, &setlist);
199 add_prefix_cmd ("logging", class_support, show_logging_command,
1bedd215 200 _("Show logging options"), &show_logging_cmdlist,
0fac0b41 201 "show logging ", 0, &showlist);
7915a72c
AC
202 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
203Set whether logging overwrites or appends to the log file."), _("\
204Show whether logging overwrites or appends to the log file."), _("\
205If set, logging overrides the log file."),
2c5b56ce 206 NULL,
920d2a44 207 show_logging_overwrite,
2c5b56ce 208 &set_logging_cmdlist, &show_logging_cmdlist);
7915a72c
AC
209 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
210Set the logging output mode."), _("\
211Show the logging output mode."), _("\
3b64bf98 212If redirect is off, output will go to both the screen and the log file.\n\
7915a72c 213If redirect is on, output will go only to the log file."),
2c5b56ce 214 NULL,
920d2a44 215 show_logging_redirect,
2c5b56ce 216 &set_logging_cmdlist, &show_logging_cmdlist);
7915a72c
AC
217 add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
218Set the current logfile."), _("\
219Show the current logfile."), _("\
220The logfile is used when directing GDB's output."),
2c5b56ce 221 NULL,
920d2a44 222 show_logging_filename,
b3f42336 223 &set_logging_cmdlist, &show_logging_cmdlist);
0fac0b41 224 add_cmd ("on", class_support, set_logging_on,
1a966eab 225 _("Enable logging."), &set_logging_cmdlist);
0fac0b41 226 add_cmd ("off", class_support, set_logging_off,
1a966eab 227 _("Disable logging."), &set_logging_cmdlist);
0fac0b41
DJ
228
229 logging_filename = xstrdup ("gdb.txt");
230}