Updated copyright notices for most files.
[binutils-gdb.git] / gdb / cli / cli-logging.c
1 /* Command-line output logging for GDB, the GNU debugger.
2
3 Copyright (c) 2003, 2004, 2007, 2008 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 ui_file *output;
89
90 if (saved_filename != NULL)
91 {
92 fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
93 saved_filename);
94 return;
95 }
96
97 output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
98 if (output == NULL)
99 perror_with_name (_("set logging"));
100
101 /* Redirects everything to gdb_stdout while this is running. */
102 if (!logging_redirect)
103 {
104 output = tee_file_new (gdb_stdout, 0, output, 1);
105 if (output == NULL)
106 perror_with_name (_("set logging"));
107 if (from_tty)
108 fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
109 logging_filename);
110 }
111 else if (from_tty)
112 fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
113 logging_filename);
114
115 saved_filename = xstrdup (logging_filename);
116 saved_output.out = gdb_stdout;
117 saved_output.err = gdb_stderr;
118 saved_output.log = gdb_stdlog;
119 saved_output.targ = gdb_stdtarg;
120
121 gdb_stdout = output;
122 gdb_stderr = output;
123 gdb_stdlog = output;
124 gdb_stdtarg = output;
125
126 if (ui_out_redirect (uiout, gdb_stdout) < 0)
127 warning (_("Current output protocol does not support redirection"));
128 }
129
130 static void
131 set_logging_on (char *args, int from_tty)
132 {
133 char *rest = args;
134 if (rest && *rest)
135 {
136 xfree (logging_filename);
137 logging_filename = xstrdup (rest);
138 }
139 handle_redirections (from_tty);
140 }
141
142 static void
143 set_logging_off (char *args, int from_tty)
144 {
145 if (saved_filename == NULL)
146 return;
147
148 pop_output_files ();
149 if (from_tty)
150 fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
151 xfree (saved_filename);
152 saved_filename = NULL;
153 }
154
155 static void
156 set_logging_command (char *args, int from_tty)
157 {
158 printf_unfiltered (_("\
159 \"set logging\" lets you log output to a file.\n\
160 Usage: set logging on [FILENAME]\n\
161 set logging off\n\
162 set logging file FILENAME\n\
163 set logging overwrite [on|off]\n\
164 set logging redirect [on|off]\n"));
165 }
166
167 void
168 show_logging_command (char *args, int from_tty)
169 {
170 if (saved_filename)
171 printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
172 if (saved_filename == NULL
173 || strcmp (logging_filename, saved_filename) != 0)
174 printf_unfiltered (_("Future logs will be written to %s.\n"),
175 logging_filename);
176
177 if (logging_overwrite)
178 printf_unfiltered (_("Logs will overwrite the log file.\n"));
179 else
180 printf_unfiltered (_("Logs will be appended to the log file.\n"));
181
182 if (logging_redirect)
183 printf_unfiltered (_("Output will be sent only to the log file.\n"));
184 else
185 printf_unfiltered (_("Output will be logged and displayed.\n"));
186 }
187
188 void
189 _initialize_cli_logging (void)
190 {
191 static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
192
193
194 add_prefix_cmd ("logging", class_support, set_logging_command,
195 _("Set logging options"), &set_logging_cmdlist,
196 "set logging ", 0, &setlist);
197 add_prefix_cmd ("logging", class_support, show_logging_command,
198 _("Show logging options"), &show_logging_cmdlist,
199 "show logging ", 0, &showlist);
200 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
201 Set whether logging overwrites or appends to the log file."), _("\
202 Show whether logging overwrites or appends to the log file."), _("\
203 If set, logging overrides the log file."),
204 NULL,
205 show_logging_overwrite,
206 &set_logging_cmdlist, &show_logging_cmdlist);
207 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
208 Set the logging output mode."), _("\
209 Show the logging output mode."), _("\
210 If redirect is off, output will go to both the screen and the log file.\n\
211 If redirect is on, output will go only to the log file."),
212 NULL,
213 show_logging_redirect,
214 &set_logging_cmdlist, &show_logging_cmdlist);
215 add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
216 Set the current logfile."), _("\
217 Show the current logfile."), _("\
218 The logfile is used when directing GDB's output."),
219 NULL,
220 show_logging_filename,
221 &set_logging_cmdlist, &show_logging_cmdlist);
222 add_cmd ("on", class_support, set_logging_on,
223 _("Enable logging."), &set_logging_cmdlist);
224 add_cmd ("off", class_support, set_logging_off,
225 _("Disable logging."), &set_logging_cmdlist);
226
227 logging_filename = xstrdup ("gdb.txt");
228 }