OSDN Git Service

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