Commit | Line | Data |
---|---|---|
0fac0b41 DJ |
1 | /* Command-line output logging for GDB, the GNU debugger. |
2 | ||
b811d2c2 | 3 | Copyright (C) 2003-2020 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" |
d770d56f | 24 | #include "cli/cli-style.h" |
0fac0b41 | 25 | |
0fac0b41 DJ |
26 | static char *saved_filename; |
27 | ||
28 | static char *logging_filename; | |
920d2a44 AC |
29 | static void |
30 | show_logging_filename (struct ui_file *file, int from_tty, | |
31 | struct cmd_list_element *c, const char *value) | |
32 | { | |
d770d56f TT |
33 | fprintf_filtered (file, _("The current logfile is \"%ps\".\n"), |
34 | styled_string (file_name_style.style (), value)); | |
920d2a44 AC |
35 | } |
36 | ||
491144b5 | 37 | static bool logging_overwrite; |
58b61394 JK |
38 | |
39 | static void | |
5be5dbf0 | 40 | maybe_warn_already_logging () |
58b61394 JK |
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 | ||
5be5dbf0 | 47 | static void |
eb4c3f4a TT |
48 | set_logging_overwrite (const char *args, |
49 | int from_tty, struct cmd_list_element *c) | |
5be5dbf0 PA |
50 | { |
51 | maybe_warn_already_logging (); | |
52 | } | |
53 | ||
920d2a44 AC |
54 | static void |
55 | show_logging_overwrite (struct ui_file *file, int from_tty, | |
56 | struct cmd_list_element *c, const char *value) | |
57 | { | |
9a2b4c1b MS |
58 | fprintf_filtered (file, |
59 | _("Whether logging overwrites or " | |
60 | "appends to the log file is %s.\n"), | |
920d2a44 AC |
61 | value); |
62 | } | |
63 | ||
58b61394 | 64 | /* Value as configured by the user. */ |
491144b5 CB |
65 | static bool logging_redirect; |
66 | static bool debug_redirect; | |
58b61394 | 67 | |
58b61394 | 68 | static void |
eb4c3f4a TT |
69 | set_logging_redirect (const char *args, |
70 | int from_tty, struct cmd_list_element *c) | |
58b61394 | 71 | { |
5be5dbf0 | 72 | maybe_warn_already_logging (); |
58b61394 JK |
73 | } |
74 | ||
920d2a44 AC |
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 | } | |
0fac0b41 DJ |
81 | |
82 | /* If we've pushed output files, close them and pop them. */ | |
83 | static void | |
83a8ccca | 84 | pop_output_files (void) |
0fac0b41 | 85 | { |
ca1285d1 | 86 | current_interp_set_logging (NULL, false, false); |
0fac0b41 | 87 | |
4d6cceb4 | 88 | /* Stay consistent with handle_redirections. */ |
112e8700 SM |
89 | if (!current_uiout->is_mi_like_p ()) |
90 | current_uiout->redirect (NULL); | |
0fac0b41 DJ |
91 | } |
92 | ||
93 | /* This is a helper for the `set logging' command. */ | |
94 | static void | |
95 | handle_redirections (int from_tty) | |
96 | { | |
0fac0b41 DJ |
97 | if (saved_filename != NULL) |
98 | { | |
99 | fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n", | |
100 | saved_filename); | |
101 | return; | |
102 | } | |
103 | ||
0735b091 | 104 | stdio_file_up log (new no_terminal_escape_file ()); |
d7e74731 | 105 | if (!log->open (logging_filename, logging_overwrite ? "w" : "a")) |
e2e0b3e5 | 106 | perror_with_name (_("set logging")); |
0fac0b41 DJ |
107 | |
108 | /* Redirects everything to gdb_stdout while this is running. */ | |
616268b6 | 109 | if (from_tty) |
0fac0b41 | 110 | { |
616268b6 | 111 | if (!logging_redirect) |
0fac0b41 DJ |
112 | fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n", |
113 | logging_filename); | |
616268b6 | 114 | else |
58b61394 JK |
115 | fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n", |
116 | logging_filename); | |
ca1285d1 AH |
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); | |
0fac0b41 | 124 | } |
0fac0b41 DJ |
125 | |
126 | saved_filename = xstrdup (logging_filename); | |
0fac0b41 | 127 | |
37ce89eb | 128 | /* Let the interpreter do anything it needs. */ |
ca1285d1 AH |
129 | current_interp_set_logging (std::move (log), logging_redirect, |
130 | debug_redirect); | |
616268b6 PA |
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"'. */ | |
112e8700 | 138 | if (!current_uiout->is_mi_like_p ()) |
d7e74731 | 139 | current_uiout->redirect (gdb_stdout); |
0fac0b41 DJ |
140 | } |
141 | ||
142 | static void | |
aa360cd5 | 143 | set_logging_on (const char *args, int from_tty) |
0fac0b41 | 144 | { |
aa360cd5 | 145 | const char *rest = args; |
cdb27c12 | 146 | |
0fac0b41 DJ |
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 | |
aa360cd5 | 156 | set_logging_off (const char *args, int from_tty) |
0fac0b41 DJ |
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 | ||
6c265988 | 168 | void _initialize_cli_logging (); |
0fac0b41 | 169 | void |
6c265988 | 170 | _initialize_cli_logging () |
0fac0b41 DJ |
171 | { |
172 | static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist; | |
173 | ||
3b6acaee TT |
174 | add_basic_prefix_cmd ("logging", class_support, |
175 | _("Set logging options."), &set_logging_cmdlist, | |
176 | "set logging ", 0, &setlist); | |
177 | add_show_prefix_cmd ("logging", class_support, | |
178 | _("Show logging options."), &show_logging_cmdlist, | |
179 | "show logging ", 0, &showlist); | |
7915a72c AC |
180 | add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\ |
181 | Set whether logging overwrites or appends to the log file."), _("\ | |
182 | Show whether logging overwrites or appends to the log file."), _("\ | |
183 | If set, logging overrides the log file."), | |
58b61394 | 184 | set_logging_overwrite, |
920d2a44 | 185 | show_logging_overwrite, |
2c5b56ce | 186 | &set_logging_cmdlist, &show_logging_cmdlist); |
7915a72c AC |
187 | add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\ |
188 | Set the logging output mode."), _("\ | |
189 | Show the logging output mode."), _("\ | |
3b64bf98 | 190 | If redirect is off, output will go to both the screen and the log file.\n\ |
7915a72c | 191 | If redirect is on, output will go only to the log file."), |
58b61394 | 192 | set_logging_redirect, |
920d2a44 | 193 | show_logging_redirect, |
2c5b56ce | 194 | &set_logging_cmdlist, &show_logging_cmdlist); |
ca1285d1 AH |
195 | add_setshow_boolean_cmd ("debugredirect", class_support, |
196 | &debug_redirect, _("\ | |
197 | Set the logging debug output mode."), _("\ | |
198 | Show the logging debug output mode."), _("\ | |
199 | If debug redirect is off, debug will go to both the screen and the log file.\n\ | |
200 | If debug redirect is on, debug will go only to the log file."), | |
201 | set_logging_redirect, | |
202 | show_logging_redirect, | |
203 | &set_logging_cmdlist, &show_logging_cmdlist); | |
7915a72c AC |
204 | add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\ |
205 | Set the current logfile."), _("\ | |
206 | Show the current logfile."), _("\ | |
207 | The logfile is used when directing GDB's output."), | |
2c5b56ce | 208 | NULL, |
920d2a44 | 209 | show_logging_filename, |
b3f42336 | 210 | &set_logging_cmdlist, &show_logging_cmdlist); |
0fac0b41 | 211 | add_cmd ("on", class_support, set_logging_on, |
1a966eab | 212 | _("Enable logging."), &set_logging_cmdlist); |
0fac0b41 | 213 | add_cmd ("off", class_support, set_logging_off, |
1a966eab | 214 | _("Disable logging."), &set_logging_cmdlist); |
0fac0b41 DJ |
215 | |
216 | logging_filename = xstrdup ("gdb.txt"); | |
217 | } |