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