Upgrade GDB from 7.0 and 7.2 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / cli / cli-logging.c
1 /* Command-line output logging for GDB, the GNU debugger.
2
3    Copyright (c) 2003, 2004, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "gdbcmd.h"
23 #include "ui-out.h"
24
25 #include "gdb_string.h"
26
27 /* These hold the pushed copies of the gdb output files.
28    If NULL then nothing has yet been pushed.  */
29 struct saved_output_files
30 {
31   struct ui_file *out;
32   struct ui_file *err;
33   struct ui_file *log;
34   struct ui_file *targ;
35 };
36 static struct saved_output_files saved_output;
37 static char *saved_filename;
38
39 static char *logging_filename;
40 static void
41 show_logging_filename (struct ui_file *file, int from_tty,
42                        struct cmd_list_element *c, const char *value)
43 {
44   fprintf_filtered (file, _("The current logfile is \"%s\".\n"),
45                     value);
46 }
47
48 int logging_overwrite;
49 static void
50 show_logging_overwrite (struct ui_file *file, int from_tty,
51                         struct cmd_list_element *c, const char *value)
52 {
53   fprintf_filtered (file, _("\
54 Whether logging overwrites or appends to the log file is %s.\n"),
55                     value);
56 }
57
58 int logging_redirect;
59 static void
60 show_logging_redirect (struct ui_file *file, int from_tty,
61                        struct cmd_list_element *c, const char *value)
62 {
63   fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
64 }
65
66 /* If we've pushed output files, close them and pop them.  */
67 static void
68 pop_output_files (void)
69 {
70   /* Only delete one of the files -- they are all set to the same
71      value.  */
72   ui_file_delete (gdb_stdout);
73   gdb_stdout = saved_output.out;
74   gdb_stderr = saved_output.err;
75   gdb_stdlog = saved_output.log;
76   gdb_stdtarg = saved_output.targ;
77   saved_output.out = NULL;
78   saved_output.err = NULL;
79   saved_output.log = NULL;
80   saved_output.targ = NULL;
81
82   ui_out_redirect (uiout, NULL);
83 }
84
85 /* This is a helper for the `set logging' command.  */
86 static void
87 handle_redirections (int from_tty)
88 {
89   struct cleanup *cleanups;
90   struct ui_file *output;
91
92   if (saved_filename != NULL)
93     {
94       fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
95                           saved_filename);
96       return;
97     }
98
99   output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
100   if (output == NULL)
101     perror_with_name (_("set logging"));
102   cleanups = make_cleanup_ui_file_delete (output);
103
104   /* Redirects everything to gdb_stdout while this is running.  */
105   if (!logging_redirect)
106     {
107       output = tee_file_new (gdb_stdout, 0, output, 1);
108       if (output == NULL)
109         perror_with_name (_("set logging"));
110       discard_cleanups (cleanups);
111       cleanups = make_cleanup_ui_file_delete (output);
112       if (from_tty)
113         fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
114                             logging_filename);
115     }
116   else if (from_tty)
117     fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
118                         logging_filename);
119
120   discard_cleanups (cleanups);
121
122   saved_filename = xstrdup (logging_filename);
123   saved_output.out = gdb_stdout;
124   saved_output.err = gdb_stderr;
125   saved_output.log = gdb_stdlog;
126   saved_output.targ = gdb_stdtarg;
127
128   gdb_stdout = output;
129   gdb_stderr = output;
130   gdb_stdlog = output;
131   gdb_stdtarg = output;
132
133   if (ui_out_redirect (uiout, gdb_stdout) < 0)
134     warning (_("Current output protocol does not support redirection"));
135 }
136
137 static void
138 set_logging_on (char *args, int from_tty)
139 {
140   char *rest = args;
141
142   if (rest && *rest)
143     {
144       xfree (logging_filename);
145       logging_filename = xstrdup (rest);
146     }
147   handle_redirections (from_tty);
148 }
149
150 static void 
151 set_logging_off (char *args, int from_tty)
152 {
153   if (saved_filename == NULL)
154     return;
155
156   pop_output_files ();
157   if (from_tty)
158     fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
159   xfree (saved_filename);
160   saved_filename = NULL;
161 }
162
163 static void
164 set_logging_command (char *args, int from_tty)
165 {
166   printf_unfiltered (_("\
167 \"set logging\" lets you log output to a file.\n\
168 Usage: set logging on [FILENAME]\n\
169        set logging off\n\
170        set logging file FILENAME\n\
171        set logging overwrite [on|off]\n\
172        set logging redirect [on|off]\n"));
173 }
174
175 static void
176 show_logging_command (char *args, int from_tty)
177 {
178   if (saved_filename)
179     printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
180   if (saved_filename == NULL
181       || strcmp (logging_filename, saved_filename) != 0)
182     printf_unfiltered (_("Future logs will be written to %s.\n"),
183                        logging_filename);
184
185   if (logging_overwrite)
186     printf_unfiltered (_("Logs will overwrite the log file.\n"));
187   else
188     printf_unfiltered (_("Logs will be appended to the log file.\n"));
189
190   if (logging_redirect)
191     printf_unfiltered (_("Output will be sent only to the log file.\n"));
192   else
193     printf_unfiltered (_("Output will be logged and displayed.\n"));
194 }
195
196 /* Provide a prototype to silence -Wmissing-prototypes.  */
197 extern initialize_file_ftype _initialize_cli_logging;
198
199 void
200 _initialize_cli_logging (void)
201 {
202   static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
203
204   add_prefix_cmd ("logging", class_support, set_logging_command,
205                   _("Set logging options"), &set_logging_cmdlist,
206                   "set logging ", 0, &setlist);
207   add_prefix_cmd ("logging", class_support, show_logging_command,
208                   _("Show logging options"), &show_logging_cmdlist,
209                   "show logging ", 0, &showlist);
210   add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
211 Set whether logging overwrites or appends to the log file."), _("\
212 Show whether logging overwrites or appends to the log file."), _("\
213 If set, logging overrides the log file."),
214                            NULL,
215                            show_logging_overwrite,
216                            &set_logging_cmdlist, &show_logging_cmdlist);
217   add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
218 Set the logging output mode."), _("\
219 Show the logging output mode."), _("\
220 If redirect is off, output will go to both the screen and the log file.\n\
221 If redirect is on, output will go only to the log file."),
222                            NULL,
223                            show_logging_redirect,
224                            &set_logging_cmdlist, &show_logging_cmdlist);
225   add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
226 Set the current logfile."), _("\
227 Show the current logfile."), _("\
228 The logfile is used when directing GDB's output."),
229                             NULL,
230                             show_logging_filename,
231                             &set_logging_cmdlist, &show_logging_cmdlist);
232   add_cmd ("on", class_support, set_logging_on,
233            _("Enable logging."), &set_logging_cmdlist);
234   add_cmd ("off", class_support, set_logging_off,
235            _("Disable logging."), &set_logging_cmdlist);
236
237   logging_filename = xstrdup ("gdb.txt");
238 }