GDB (xrefs)
Loading...
Searching...
No Matches
cli-logging.c
Go to the documentation of this file.
1/* Command-line output logging for GDB, the GNU debugger.
2
3 Copyright (C) 2003-2023 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
27static std::string saved_filename;
28
29static std::string logging_filename = "gdb.txt";
30static void
31show_logging_filename (struct ui_file *file, int from_tty,
32 struct cmd_list_element *c, const char *value)
33{
34 gdb_printf (file, _("The current logfile is \"%ps\".\n"),
36}
37
39
40static void
42{
43 if (!saved_filename.empty ())
44 warning (_("Currently logging to %s. Turn the logging off and on to "
45 "make the new setting effective."), saved_filename.c_str ());
46}
47
48static void
49set_logging_overwrite (const char *args,
50 int from_tty, struct cmd_list_element *c)
51{
53}
54
55static void
56show_logging_overwrite (struct ui_file *file, int from_tty,
57 struct cmd_list_element *c, const char *value)
58{
60 gdb_printf (file, _("on: Logging overwrites the log file.\n"));
61 else
62 gdb_printf (file, _("off: Logging appends to the log file.\n"));
63}
64
65/* Value as configured by the user. */
66static bool logging_redirect;
67static bool debug_redirect;
68
69static void
70set_logging_redirect (const char *args,
71 int from_tty, struct cmd_list_element *c)
72{
74}
75
76static void
77show_logging_redirect (struct ui_file *file, int from_tty,
78 struct cmd_list_element *c, const char *value)
79{
81 gdb_printf (file, _("on: Output will go only to the log file.\n"));
82 else
84 (file,
85 _("off: Output will go to both the screen and the log file.\n"));
86}
87
88static void
89show_logging_debug_redirect (struct ui_file *file, int from_tty,
90 struct cmd_list_element *c, const char *value)
91{
93 gdb_printf (file, _("on: Debug output will go only to the log file.\n"));
94 else
96 (file,
97 _("off: Debug output will go to both the screen and the log file.\n"));
98}
99
100/* If we've pushed output files, close them and pop them. */
101static void
103{
104 current_interp_set_logging (NULL, false, false);
105
106 /* Stay consistent with handle_redirections. */
107 if (!current_uiout->is_mi_like_p ())
108 current_uiout->redirect (NULL);
109}
110
111/* This is a helper for the `set logging' command. */
112static void
114{
115 if (!saved_filename.empty ())
116 {
117 gdb_printf ("Already logging to %s.\n",
118 saved_filename.c_str ());
119 return;
120 }
121
123 if (!log->open (logging_filename.c_str (), logging_overwrite ? "w" : "a"))
124 perror_with_name (_("set logging"));
125
126 /* Redirects everything to gdb_stdout while this is running. */
127 if (from_tty)
128 {
129 if (!logging_redirect)
130 gdb_printf ("Copying output to %s.\n",
131 logging_filename.c_str ());
132 else
133 gdb_printf ("Redirecting output to %s.\n",
134 logging_filename.c_str ());
135
136 if (!debug_redirect)
137 gdb_printf ("Copying debug output to %s.\n",
138 logging_filename.c_str ());
139 else
140 gdb_printf ("Redirecting debug output to %s.\n",
141 logging_filename.c_str ());
142 }
143
145
146 /* Let the interpreter do anything it needs. */
149
150 /* Redirect the current ui-out object's output to the log. Use
151 gdb_stdout, not log, since the interpreter may have created a tee
152 that wraps the log. Don't do the redirect for MI, it confuses
153 MI's ui-out scheme. Note that we may get here with MI as current
154 interpreter, but with the current ui_out as a CLI ui_out, with
155 '-interpreter-exec console "set logging on"'. */
156 if (!current_uiout->is_mi_like_p ())
157 current_uiout->redirect (gdb_stdout);
158}
159
160static void
161set_logging_on (const char *args, int from_tty)
162{
163 const char *rest = args;
164
165 if (rest && *rest)
166 logging_filename = rest;
167
168 handle_redirections (from_tty);
169}
170
171static void
172set_logging_off (const char *args, int from_tty)
173{
174 if (saved_filename.empty ())
175 return;
176
178 if (from_tty)
179 gdb_printf ("Done logging to %s.\n",
180 saved_filename.c_str ());
181 saved_filename.clear ();
182}
183
184static bool logging_enabled;
185
186static void
187set_logging_enabled (const char *args,
188 int from_tty, struct cmd_list_element *c)
189{
190 if (logging_enabled)
191 set_logging_on (args, from_tty);
192 else
193 set_logging_off (args, from_tty);
194}
195
196static void
197show_logging_enabled (struct ui_file *file, int from_tty,
198 struct cmd_list_element *c, const char *value)
199{
200 if (logging_enabled)
201 gdb_printf (file, _("on: Logging is enabled.\n"));
202 else
203 gdb_printf (file, _("off: Logging is disabled.\n"));
204}
205
207void
209{
210 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
211
212 /* Set/show logging. */
214 _("Set logging options."),
215 _("Show logging options."),
216 &set_logging_cmdlist, &show_logging_cmdlist,
217 &setlist, &showlist);
218
219 /* Set/show logging overwrite. */
221Set whether logging overwrites or appends to the log file."), _("\
222Show whether logging overwrites or appends to the log file."), _("\
223If set, logging overwrites the log file."),
226 &set_logging_cmdlist, &show_logging_cmdlist);
227
228 /* Set/show logging redirect. */
230Set the logging output mode."), _("\
231Show the logging output mode."), _("\
232If redirect is off, output will go to both the screen and the log file.\n\
233If redirect is on, output will go only to the log file."),
236 &set_logging_cmdlist, &show_logging_cmdlist);
237
238 /* Set/show logging debugredirect. */
239 add_setshow_boolean_cmd ("debugredirect", class_support,
240 &debug_redirect, _("\
241Set the logging debug output mode."), _("\
242Show the logging debug output mode."), _("\
243If debug redirect is off, debug will go to both the screen and the log file.\n\
244If debug redirect is on, debug will go only to the log file."),
247 &set_logging_cmdlist, &show_logging_cmdlist);
248
249 /* Set/show logging file. */
251Set the current logfile."), _("\
252Show the current logfile."), _("\
253The logfile is used when directing GDB's output."),
254 NULL,
256 &set_logging_cmdlist, &show_logging_cmdlist);
257
258 /* Set/show logging enabled. */
259 set_show_commands setshow_logging_enabled_cmds
261 _("Enable logging."),
262 _("Show whether logging is enabled."),
263 _("When on, enable logging."),
266 &set_logging_cmdlist, &show_logging_cmdlist);
267
268 /* Set logging on, deprecated alias. */
269 cmd_list_element *set_logging_on_cmd
270 = add_alias_cmd ("on", setshow_logging_enabled_cmds.set, class_support,
271 false, &set_logging_cmdlist);
272 deprecate_cmd (set_logging_on_cmd, "set logging enabled on");
273 set_logging_on_cmd->default_args = "on";
274
275 /* Set logging off, deprecated alias. */
276 cmd_list_element *set_logging_off_cmd
277 = add_alias_cmd ("off", setshow_logging_enabled_cmds.set, class_support,
278 false, &set_logging_cmdlist);
279 deprecate_cmd (set_logging_off_cmd, "set logging enabled off");
280 set_logging_off_cmd->default_args = "off";
281}
ui_file_style style() const
Definition cli-style.c:169
struct cmd_list_element * showlist
Definition cli-cmds.c:127
struct cmd_list_element * setlist
Definition cli-cmds.c:119
set_show_commands add_setshow_filename_cmd(const char *name, enum command_class theclass, std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:855
struct cmd_list_element * add_alias_cmd(const char *name, cmd_list_element *target, enum command_class theclass, int abbrev_flag, struct cmd_list_element **list)
Definition cli-decode.c:294
struct cmd_list_element * deprecate_cmd(struct cmd_list_element *cmd, const char *replacement)
Definition cli-decode.c:280
set_show_commands add_setshow_prefix_cmd(const char *name, command_class theclass, const char *set_doc, const char *show_doc, cmd_list_element **set_subcommands_list, cmd_list_element **show_subcommands_list, cmd_list_element **set_list, cmd_list_element **show_list)
Definition cli-decode.c:428
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:809
static void show_logging_enabled(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static bool debug_redirect
Definition cli-logging.c:67
static void show_logging_overwrite(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition cli-logging.c:56
static void maybe_warn_already_logging()
Definition cli-logging.c:41
static bool logging_redirect
Definition cli-logging.c:66
static void show_logging_debug_redirect(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition cli-logging.c:89
static void set_logging_redirect(const char *args, int from_tty, struct cmd_list_element *c)
Definition cli-logging.c:70
static bool logging_enabled
static std::string saved_filename
Definition cli-logging.c:27
static void show_logging_redirect(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition cli-logging.c:77
static bool logging_overwrite
Definition cli-logging.c:38
static void pop_output_files(void)
static void set_logging_off(const char *args, int from_tty)
static void set_logging_overwrite(const char *args, int from_tty, struct cmd_list_element *c)
Definition cli-logging.c:49
static void handle_redirections(int from_tty)
static void set_logging_on(const char *args, int from_tty)
static void set_logging_enabled(const char *args, int from_tty, struct cmd_list_element *c)
static void show_logging_filename(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition cli-logging.c:31
void _initialize_cli_logging()
static std::string logging_filename
Definition cli-logging.c:29
cli_style_option file_name_style
@ class_support
Definition command.h:58
void current_interp_set_logging(ui_file_up logfile, bool logging_redirect, bool debug_redirect)
Definition interps.c:203
std::string default_args
Definition cli-decode.h:210
cmd_list_element * set
Definition command.h:422
Definition value.h:130
std::unique_ptr< stdio_file > stdio_file_up
Definition ui-file.h:293
static styled_string_s * styled_string(const ui_file_style &style, const char *str, styled_string_s &&tmp={})
Definition ui-out.h:151
#define current_uiout
Definition ui-out.h:40
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stdout
Definition utils.h:182