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