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