[gdb/cli] Add "set logging enabled", deprecate "set logging on/off"
[binutils-gdb.git] / gdb / cli / cli-logging.c
1 /* Command-line output logging for GDB, the GNU debugger.
2
3 Copyright (C) 2003-2021 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "gdbcmd.h"
22 #include "ui-out.h"
23 #include "interps.h"
24 #include "cli/cli-style.h"
25 #include "cli/cli-decode.h"
26
27 static char *saved_filename;
28
29 static std::string logging_filename = "gdb.txt";
30 static void
31 show_logging_filename (struct ui_file *file, int from_tty,
32 struct cmd_list_element *c, const char *value)
33 {
34 fprintf_filtered (file, _("The current logfile is \"%ps\".\n"),
35 styled_string (file_name_style.style (), value));
36 }
37
38 static bool logging_overwrite;
39
40 static void
41 maybe_warn_already_logging ()
42 {
43 if (saved_filename)
44 warning (_("Currently logging to %s. Turn the logging off and on to "
45 "make the new setting effective."), saved_filename);
46 }
47
48 static void
49 set_logging_overwrite (const char *args,
50 int from_tty, struct cmd_list_element *c)
51 {
52 maybe_warn_already_logging ();
53 }
54
55 static void
56 show_logging_overwrite (struct ui_file *file, int from_tty,
57 struct cmd_list_element *c, const char *value)
58 {
59 fprintf_filtered (file,
60 _("Whether logging overwrites or "
61 "appends to the log file is %s.\n"),
62 value);
63 }
64
65 /* Value as configured by the user. */
66 static bool logging_redirect;
67 static bool debug_redirect;
68
69 static void
70 set_logging_redirect (const char *args,
71 int from_tty, struct cmd_list_element *c)
72 {
73 maybe_warn_already_logging ();
74 }
75
76 static void
77 show_logging_redirect (struct ui_file *file, int from_tty,
78 struct cmd_list_element *c, const char *value)
79 {
80 fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
81 }
82
83 /* If we've pushed output files, close them and pop them. */
84 static void
85 pop_output_files (void)
86 {
87 current_interp_set_logging (NULL, false, false);
88
89 /* Stay consistent with handle_redirections. */
90 if (!current_uiout->is_mi_like_p ())
91 current_uiout->redirect (NULL);
92 }
93
94 /* This is a helper for the `set logging' command. */
95 static void
96 handle_redirections (int from_tty)
97 {
98 if (saved_filename != NULL)
99 {
100 fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
101 saved_filename);
102 return;
103 }
104
105 stdio_file_up log (new no_terminal_escape_file ());
106 if (!log->open (logging_filename.c_str (), logging_overwrite ? "w" : "a"))
107 perror_with_name (_("set logging"));
108
109 /* Redirects everything to gdb_stdout while this is running. */
110 if (from_tty)
111 {
112 if (!logging_redirect)
113 fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
114 logging_filename.c_str ());
115 else
116 fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
117 logging_filename.c_str ());
118
119 if (!debug_redirect)
120 fprintf_unfiltered (gdb_stdout, "Copying debug output to %s.\n",
121 logging_filename.c_str ());
122 else
123 fprintf_unfiltered (gdb_stdout, "Redirecting debug output to %s.\n",
124 logging_filename.c_str ());
125 }
126
127 saved_filename = xstrdup (logging_filename.c_str ());
128
129 /* Let the interpreter do anything it needs. */
130 current_interp_set_logging (std::move (log), logging_redirect,
131 debug_redirect);
132
133 /* Redirect the current ui-out object's output to the log. Use
134 gdb_stdout, not log, since the interpreter may have created a tee
135 that wraps the log. Don't do the redirect for MI, it confuses
136 MI's ui-out scheme. Note that we may get here with MI as current
137 interpreter, but with the current ui_out as a CLI ui_out, with
138 '-interpreter-exec console "set logging on"'. */
139 if (!current_uiout->is_mi_like_p ())
140 current_uiout->redirect (gdb_stdout);
141 }
142
143 static void
144 set_logging_on (const char *args, int from_tty)
145 {
146 const char *rest = args;
147
148 if (rest && *rest)
149 logging_filename = rest;
150
151 handle_redirections (from_tty);
152 }
153
154 static void
155 set_logging_off (const char *args, int from_tty)
156 {
157 if (saved_filename == NULL)
158 return;
159
160 pop_output_files ();
161 if (from_tty)
162 fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
163 xfree (saved_filename);
164 saved_filename = NULL;
165 }
166
167 static bool logging_enabled;
168
169 static void
170 set_logging_enabled (const char *args,
171 int from_tty, struct cmd_list_element *c)
172 {
173 if (logging_enabled)
174 set_logging_on (args, from_tty);
175 else
176 set_logging_off (args, from_tty);
177 }
178
179 static void
180 show_logging_enabled (struct ui_file *file, int from_tty,
181 struct cmd_list_element *c, const char *value)
182 {
183 if (logging_enabled)
184 printf_unfiltered (_("Logging is enabled.\n"));
185 else
186 printf_unfiltered (_("Logging is disabled.\n"));
187 }
188
189 void _initialize_cli_logging ();
190 void
191 _initialize_cli_logging ()
192 {
193 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
194
195 /* Set/show logging. */
196 add_setshow_prefix_cmd ("logging", class_support,
197 _("Set logging options."),
198 _("Show logging options."),
199 &set_logging_cmdlist, &show_logging_cmdlist,
200 &setlist, &showlist);
201
202 /* Set/show logging overwrite. */
203 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
204 Set whether logging overwrites or appends to the log file."), _("\
205 Show whether logging overwrites or appends to the log file."), _("\
206 If set, logging overwrites the log file."),
207 set_logging_overwrite,
208 show_logging_overwrite,
209 &set_logging_cmdlist, &show_logging_cmdlist);
210
211 /* Set/show logging redirect. */
212 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
213 Set the logging output mode."), _("\
214 Show the logging output mode."), _("\
215 If redirect is off, output will go to both the screen and the log file.\n\
216 If redirect is on, output will go only to the log file."),
217 set_logging_redirect,
218 show_logging_redirect,
219 &set_logging_cmdlist, &show_logging_cmdlist);
220
221 /* Set/show logging debugredirect. */
222 add_setshow_boolean_cmd ("debugredirect", class_support,
223 &debug_redirect, _("\
224 Set the logging debug output mode."), _("\
225 Show the logging debug output mode."), _("\
226 If debug redirect is off, debug will go to both the screen and the log file.\n\
227 If debug redirect is on, debug will go only to the log file."),
228 set_logging_redirect,
229 show_logging_redirect,
230 &set_logging_cmdlist, &show_logging_cmdlist);
231
232 /* Set/show logging file. */
233 add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
234 Set the current logfile."), _("\
235 Show the current logfile."), _("\
236 The logfile is used when directing GDB's output."),
237 NULL,
238 show_logging_filename,
239 &set_logging_cmdlist, &show_logging_cmdlist);
240
241 /* Set/show logging enabled. */
242 set_show_commands setshow_logging_enabled_cmds
243 = add_setshow_boolean_cmd ("enabled", class_support, &logging_enabled,
244 _("Enable logging."),
245 _("Show whether logging is enabled."),
246 _("When on, enable logging."),
247 set_logging_enabled,
248 show_logging_enabled,
249 &set_logging_cmdlist, &show_logging_cmdlist);
250
251 /* Set logging on, deprecated alias. */
252 cmd_list_element *set_logging_on_cmd
253 = add_alias_cmd ("on", setshow_logging_enabled_cmds.set, class_support,
254 false, &set_logging_cmdlist);
255 deprecate_cmd (set_logging_on_cmd, "set logging enabled on");
256 set_logging_on_cmd->default_args = "on";
257
258 /* Set logging off, deprecated alias. */
259 cmd_list_element *set_logging_off_cmd
260 = add_alias_cmd ("off", setshow_logging_enabled_cmds.set, class_support,
261 false, &set_logging_cmdlist);
262 deprecate_cmd (set_logging_off_cmd, "set logging enabled off");
263 set_logging_off_cmd->default_args = "off";
264 }