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