*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / cli / cli-logging.c
CommitLineData
0fac0b41
DJ
1/* Command-line output logging for GDB, the GNU debugger.
2
6aba47ca 3 Copyright (c) 2003, 2004, 2007 Free Software Foundation, Inc.
0fac0b41
DJ
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
0fac0b41
DJ
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
0fac0b41
DJ
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. */
28struct 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};
35static struct saved_output_files saved_output;
36static char *saved_filename;
37
38static char *logging_filename;
920d2a44
AC
39static void
40show_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
47int logging_overwrite;
48static void
49show_logging_overwrite (struct ui_file *file, int from_tty,
50 struct cmd_list_element *c, const char *value)
51{
52 fprintf_filtered (file, _("\
53Whether logging overwrites or appends to the log file is %s.\n"),
54 value);
55}
56
57int logging_redirect;
58static void
59show_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}
0fac0b41
DJ
64
65/* If we've pushed output files, close them and pop them. */
66static void
83a8ccca 67pop_output_files (void)
0fac0b41
DJ
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. */
85static void
86handle_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)
e2e0b3e5 99 perror_with_name (_("set logging"));
0fac0b41
DJ
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)
e2e0b3e5 106 perror_with_name (_("set logging"));
0fac0b41
DJ
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)
8a3fe4f8 127 warning (_("Current output protocol does not support redirection"));
0fac0b41
DJ
128}
129
130static void
131set_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
142static void
143set_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
155static void
156set_logging_command (char *args, int from_tty)
157{
a3f17187
AC
158 printf_unfiltered (_("\
159\"set logging\" lets you log output to a file.\n\
160Usage: 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"));
0fac0b41
DJ
165}
166
167void
168show_logging_command (char *args, int from_tty)
169{
170 if (saved_filename)
a3f17187 171 printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
0fac0b41
DJ
172 if (saved_filename == NULL
173 || strcmp (logging_filename, saved_filename) != 0)
a3f17187 174 printf_unfiltered (_("Future logs will be written to %s.\n"),
0fac0b41
DJ
175 logging_filename);
176
177 if (logging_overwrite)
a3f17187 178 printf_unfiltered (_("Logs will overwrite the log file.\n"));
0fac0b41 179 else
a3f17187 180 printf_unfiltered (_("Logs will be appended to the log file.\n"));
0fac0b41
DJ
181
182 if (logging_redirect)
a3f17187 183 printf_unfiltered (_("Output will be sent only to the log file.\n"));
0fac0b41 184 else
a3f17187 185 printf_unfiltered (_("Output will be logged and displayed.\n"));
0fac0b41
DJ
186}
187
188void
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,
1bedd215 195 _("Set logging options"), &set_logging_cmdlist,
0fac0b41
DJ
196 "set logging ", 0, &setlist);
197 add_prefix_cmd ("logging", class_support, show_logging_command,
1bedd215 198 _("Show logging options"), &show_logging_cmdlist,
0fac0b41 199 "show logging ", 0, &showlist);
7915a72c
AC
200 add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
201Set whether logging overwrites or appends to the log file."), _("\
202Show whether logging overwrites or appends to the log file."), _("\
203If set, logging overrides the log file."),
2c5b56ce 204 NULL,
920d2a44 205 show_logging_overwrite,
2c5b56ce 206 &set_logging_cmdlist, &show_logging_cmdlist);
7915a72c
AC
207 add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
208Set the logging output mode."), _("\
209Show the logging output mode."), _("\
3b64bf98 210If redirect is off, output will go to both the screen and the log file.\n\
7915a72c 211If redirect is on, output will go only to the log file."),
2c5b56ce 212 NULL,
920d2a44 213 show_logging_redirect,
2c5b56ce 214 &set_logging_cmdlist, &show_logging_cmdlist);
7915a72c
AC
215 add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
216Set the current logfile."), _("\
217Show the current logfile."), _("\
218The logfile is used when directing GDB's output."),
2c5b56ce 219 NULL,
920d2a44 220 show_logging_filename,
b3f42336 221 &set_logging_cmdlist, &show_logging_cmdlist);
0fac0b41 222 add_cmd ("on", class_support, set_logging_on,
1a966eab 223 _("Enable logging."), &set_logging_cmdlist);
0fac0b41 224 add_cmd ("off", class_support, set_logging_off,
1a966eab 225 _("Disable logging."), &set_logging_cmdlist);
0fac0b41
DJ
226
227 logging_filename = xstrdup ("gdb.txt");
228}
This page took 0.269744 seconds and 4 git commands to generate.