2005-02-15 Andrew Cagney <cagney@gnu.org>
[external/binutils.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 int logging_overwrite, logging_redirect;
42
43 /* If we've pushed output files, close them and pop them.  */
44 static void
45 pop_output_files (void)
46 {
47   /* Only delete one of the files -- they are all set to the same
48      value.  */
49   ui_file_delete (gdb_stdout);
50   gdb_stdout = saved_output.out;
51   gdb_stderr = saved_output.err;
52   gdb_stdlog = saved_output.log;
53   gdb_stdtarg = saved_output.targ;
54   saved_output.out = NULL;
55   saved_output.err = NULL;
56   saved_output.log = NULL;
57   saved_output.targ = NULL;
58
59   ui_out_redirect (uiout, NULL);
60 }
61
62 /* This is a helper for the `set logging' command.  */
63 static void
64 handle_redirections (int from_tty)
65 {
66   struct ui_file *output;
67
68   if (saved_filename != NULL)
69     {
70       fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
71                           saved_filename);
72       return;
73     }
74
75   output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
76   if (output == NULL)
77     perror_with_name (_("set logging"));
78
79   /* Redirects everything to gdb_stdout while this is running.  */
80   if (!logging_redirect)
81     {
82       output = tee_file_new (gdb_stdout, 0, output, 1);
83       if (output == NULL)
84         perror_with_name (_("set logging"));
85       if (from_tty)
86         fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
87                             logging_filename);
88     }
89   else if (from_tty)
90     fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
91                         logging_filename);
92
93   saved_filename = xstrdup (logging_filename);
94   saved_output.out = gdb_stdout;
95   saved_output.err = gdb_stderr;
96   saved_output.log = gdb_stdlog;
97   saved_output.targ = gdb_stdtarg;
98
99   gdb_stdout = output;
100   gdb_stderr = output;
101   gdb_stdlog = output;
102   gdb_stdtarg = output;
103
104   if (ui_out_redirect (uiout, gdb_stdout) < 0)
105     warning (_("Current output protocol does not support redirection"));
106 }
107
108 static void
109 set_logging_on (char *args, int from_tty)
110 {
111   char *rest = args;
112   if (rest && *rest)
113     {
114       xfree (logging_filename);
115       logging_filename = xstrdup (rest);
116     }
117   handle_redirections (from_tty);
118 }
119
120 static void 
121 set_logging_off (char *args, int from_tty)
122 {
123   if (saved_filename == NULL)
124     return;
125
126   pop_output_files ();
127   if (from_tty)
128     fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
129   xfree (saved_filename);
130   saved_filename = NULL;
131 }
132
133 static void
134 set_logging_command (char *args, int from_tty)
135 {
136   printf_unfiltered (_("\
137 \"set logging\" lets you log output to a file.\n\
138 Usage: set logging on [FILENAME]\n\
139        set logging off\n\
140        set logging file FILENAME\n\
141        set logging overwrite [on|off]\n\
142        set logging redirect [on|off]\n"));
143 }
144
145 void
146 show_logging_command (char *args, int from_tty)
147 {
148   if (saved_filename)
149     printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
150   if (saved_filename == NULL
151       || strcmp (logging_filename, saved_filename) != 0)
152     printf_unfiltered (_("Future logs will be written to %s.\n"),
153                        logging_filename);
154
155   if (logging_overwrite)
156     printf_unfiltered (_("Logs will overwrite the log file.\n"));
157   else
158     printf_unfiltered (_("Logs will be appended to the log file.\n"));
159
160   if (logging_redirect)
161     printf_unfiltered (_("Output will be sent only to the log file.\n"));
162   else
163     printf_unfiltered (_("Output will be logged and displayed.\n"));
164 }
165
166 void
167 _initialize_cli_logging (void)
168 {
169   static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
170
171   
172   add_prefix_cmd ("logging", class_support, set_logging_command,
173                   _("Set logging options"), &set_logging_cmdlist,
174                   "set logging ", 0, &setlist);
175   add_prefix_cmd ("logging", class_support, show_logging_command,
176                   _("Show logging options"), &show_logging_cmdlist,
177                   "show logging ", 0, &showlist);
178   add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
179 Set whether logging overwrites or appends to the log file."), _("\
180 Show whether logging overwrites or appends to the log file."), _("\
181 If set, logging overrides the log file."),
182                            NULL, /* FIXME: i18n: Whether logging overwrites or appends to the log file is %s.  */
183                            NULL, NULL, &set_logging_cmdlist, &show_logging_cmdlist);
184   add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
185 Set the logging output mode."), _("\
186 Show the logging output mode."), _("\
187 If redirect is off, output will go to both the screen and the log file.\n\
188 If redirect is on, output will go only to the log file."),
189                            NULL, /* FIXME: i18n: The logging output mode is %s.  */
190                            NULL, NULL, &set_logging_cmdlist, &show_logging_cmdlist);
191   add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
192 Set the current logfile."), _("\
193 Show the current logfile."), _("\
194 The logfile is used when directing GDB's output."),
195                             NULL, /* FIXME: i18n: The current logfile is %s.  */
196                             NULL, NULL,
197                             &set_logging_cmdlist, &show_logging_cmdlist);
198   add_cmd ("on", class_support, set_logging_on,
199            _("Enable logging."), &set_logging_cmdlist);
200   add_cmd ("off", class_support, set_logging_off,
201            _("Disable logging."), &set_logging_cmdlist);
202
203   logging_filename = xstrdup ("gdb.txt");
204 }