GDB (xrefs)
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-2018 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 
25 static char *saved_filename;
26 
27 static char *logging_filename;
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 
36 static int logging_overwrite;
37 
38 static void
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 
46 static void
47 set_logging_overwrite (const char *args,
48  int from_tty, struct cmd_list_element *c)
49 {
51 }
52 
53 static void
54 show_logging_overwrite (struct ui_file *file, int from_tty,
55  struct cmd_list_element *c, const char *value)
56 {
57  fprintf_filtered (file,
58  _("Whether logging overwrites or "
59  "appends to the log file is %s.\n"),
60  value);
61 }
62 
63 /* Value as configured by the user. */
64 static int logging_redirect;
65 
66 static void
67 set_logging_redirect (const char *args,
68  int from_tty, struct cmd_list_element *c)
69 {
71 }
72 
73 static void
74 show_logging_redirect (struct ui_file *file, int from_tty,
75  struct cmd_list_element *c, const char *value)
76 {
77  fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
78 }
79 
80 /* If we've pushed output files, close them and pop them. */
81 static void
83 {
84  current_interp_set_logging (NULL, false);
85 
86  /* Stay consistent with handle_redirections. */
87  if (!current_uiout->is_mi_like_p ())
88  current_uiout->redirect (NULL);
89 }
90 
91 /* See cli-interp.h. */
92 
93 ui_file *
94 make_logging_output (ui_file *curr_output, ui_file_up logfile,
95  bool logging_redirect)
96 {
97  if (logging_redirect)
98  return logfile.release ();
99  else
100  {
101  /* Note that the "tee" takes ownership of the log file. */
102  ui_file *out = new tee_file (curr_output, false,
103  logfile.get (), true);
104  logfile.release ();
105  return out;
106  }
107 }
108 
109 /* This is a helper for the `set logging' command. */
110 static void
111 handle_redirections (int from_tty)
112 {
113  if (saved_filename != NULL)
114  {
115  fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
117  return;
118  }
119 
120  stdio_file_up log (new stdio_file ());
121  if (!log->open (logging_filename, logging_overwrite ? "w" : "a"))
122  perror_with_name (_("set logging"));
123 
124  /* Redirects everything to gdb_stdout while this is running. */
125  if (from_tty)
126  {
127  if (!logging_redirect)
128  fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
130  else
131  fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
133  }
134 
135  saved_filename = xstrdup (logging_filename);
136 
137  /* Let the interpreter do anything it needs. */
139 
140  /* Redirect the current ui-out object's output to the log. Use
141  gdb_stdout, not log, since the interpreter may have created a tee
142  that wraps the log. Don't do the redirect for MI, it confuses
143  MI's ui-out scheme. Note that we may get here with MI as current
144  interpreter, but with the current ui_out as a CLI ui_out, with
145  '-interpreter-exec console "set logging on"'. */
146  if (!current_uiout->is_mi_like_p ())
147  current_uiout->redirect (gdb_stdout);
148 }
149 
150 static void
151 set_logging_on (const char *args, int from_tty)
152 {
153  const char *rest = args;
154 
155  if (rest && *rest)
156  {
158  logging_filename = xstrdup (rest);
159  }
160  handle_redirections (from_tty);
161 }
162 
163 static void
164 set_logging_off (const char *args, int from_tty)
165 {
166  if (saved_filename == NULL)
167  return;
168 
169  pop_output_files ();
170  if (from_tty)
171  fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
173  saved_filename = NULL;
174 }
175 
176 static void
177 set_logging_command (const char *args, int from_tty)
178 {
179  printf_unfiltered (_("\"set logging\" lets you log output to a file.\n"
180  "Usage: set logging on [FILENAME]\n"
181  " set logging off\n"
182  " set logging file FILENAME\n"
183  " set logging overwrite [on|off]\n"
184  " set logging redirect [on|off]\n"));
185 }
186 
187 static void
188 show_logging_command (const char *args, int from_tty)
189 {
190  if (saved_filename)
191  printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
192  if (saved_filename == NULL
193  || strcmp (logging_filename, saved_filename) != 0)
194  printf_unfiltered (_("Future logs will be written to %s.\n"),
196 
197  if (logging_overwrite)
198  printf_unfiltered (_("Logs will overwrite the log file.\n"));
199  else
200  printf_unfiltered (_("Logs will be appended to the log file.\n"));
201 
202  if (logging_redirect)
203  printf_unfiltered (_("Output will be sent only to the log file.\n"));
204  else
205  printf_unfiltered (_("Output will be logged and displayed.\n"));
206 }
207 
208 void
210 {
211  static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
212 
214  _("Set logging options"), &set_logging_cmdlist,
215  "set logging ", 0, &setlist);
217  _("Show logging options"), &show_logging_cmdlist,
218  "show logging ", 0, &showlist);
220 Set whether logging overwrites or appends to the log file."), _("\
221 Show whether logging overwrites or appends to the log file."), _("\
222 If set, logging overrides the log file."),
225  &set_logging_cmdlist, &show_logging_cmdlist);
227 Set the logging output mode."), _("\
228 Show the logging output mode."), _("\
229 If redirect is off, output will go to both the screen and the log file.\n\
230 If redirect is on, output will go only to the log file."),
233  &set_logging_cmdlist, &show_logging_cmdlist);
235 Set the current logfile."), _("\
236 Show the current logfile."), _("\
237 The logfile is used when directing GDB's output."),
238  NULL,
240  &set_logging_cmdlist, &show_logging_cmdlist);
242  _("Enable logging."), &set_logging_cmdlist);
244  _("Disable logging."), &set_logging_cmdlist);
245 
246  logging_filename = xstrdup ("gdb.txt");
247 }
static void pop_output_files(void)
Definition: cli-logging.c:82
void add_setshow_filename_cmd(const char *name, enum command_class theclass, char **var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_const_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:591
static char * saved_filename
Definition: cli-logging.c:25
void xfree(void *)
std::unique_ptr< ui_file > ui_file_up
Definition: ui-file.h:76
void warning(const char *fmt,...)
Definition: errors.c:26
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:262
static void maybe_warn_already_logging()
Definition: cli-logging.c:39
#define _(String)
Definition: gdb_locale.h:35
void _initialize_cli_logging(void)
Definition: cli-logging.c:209
ui_file * make_logging_output(ui_file *curr_output, ui_file_up logfile, bool logging_redirect)
Definition: cli-logging.c:94
static void show_logging_command(const char *args, int from_tty)
Definition: cli-logging.c:188
struct cmd_list_element * add_prefix_cmd(const char *name, enum command_class theclass, cmd_const_cfunc_ftype *fun, const char *doc, struct cmd_list_element **prefixlist, const char *prefixname, int allow_unknown, struct cmd_list_element **list)
Definition: cli-decode.c:367
struct cmd_list_element * setlist
Definition: cli-cmds.c:111
static void show_logging_redirect(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: cli-logging.c:74
static int logging_redirect
Definition: cli-logging.c:64
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2008
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2018
struct cmd_list_element * showlist
Definition: cli-cmds.c:119
#define current_uiout
Definition: ui-out.h:39
std::unique_ptr< stdio_file > stdio_file_up
Definition: ui-file.h:200
static void show_logging_filename(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: cli-logging.c:29
static void show_logging_overwrite(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: cli-logging.c:54
void printf_unfiltered(const char *format,...)
Definition: utils.c:2056
static void handle_redirections(int from_tty)
Definition: cli-logging.c:111
static void set_logging_command(const char *args, int from_tty)
Definition: cli-logging.c:177
Definition: value.c:169
static char * logging_filename
Definition: cli-logging.c:27
static void set_logging_on(const char *args, int from_tty)
Definition: cli-logging.c:151
void void void void void void void void void perror_with_name(const char *string) ATTRIBUTE_NORETURN
Definition: utils.c:692
static void set_logging_redirect(const char *args, int from_tty, struct cmd_list_element *c)
Definition: cli-logging.c:67
void add_setshow_boolean_cmd(const char *name, enum command_class theclass, int *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_const_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:569
static int logging_overwrite
Definition: cli-logging.c:36
def log(msg, indent=0)
static void set_logging_off(const char *args, int from_tty)
Definition: cli-logging.c:164
void current_interp_set_logging(ui_file_up logfile, bool logging_redirect)
Definition: interps.c:267
static void set_logging_overwrite(const char *args, int from_tty, struct cmd_list_element *c)
Definition: cli-logging.c:47
#define gdb_stdout
Definition: utils.h:340