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.  */
30struct 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};
37static struct saved_output_files saved_output;
38static char *saved_filename;
39
40static char *logging_filename;
41int logging_overwrite, logging_redirect;
42
43/* If we've pushed output files, close them and pop them.  */
44static void
45pop_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.  */
63static void
64handle_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
108static void
109set_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
120static void
121set_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
133static void
134set_logging_command (char *args, int from_tty)
135{
136  printf_unfiltered ("\"set logging\" lets you log output to a file.\n");
137  printf_unfiltered ("Usage: set logging on [FILENAME]\n");
138  printf_unfiltered ("       set logging off\n");
139  printf_unfiltered ("       set logging file FILENAME\n");
140  printf_unfiltered ("       set logging overwrite [on|off]\n");
141  printf_unfiltered ("       set logging redirect [on|off]\n");
142}
143
144void
145show_logging_command (char *args, int from_tty)
146{
147  if (saved_filename)
148    printf_unfiltered ("Currently logging to \"%s\".\n", saved_filename);
149  if (saved_filename == NULL
150      || strcmp (logging_filename, saved_filename) != 0)
151    printf_unfiltered ("Future logs will be written to %s.\n",
152		       logging_filename);
153
154  if (logging_overwrite)
155    printf_unfiltered ("Logs will overwrite the log file.\n");
156  else
157    printf_unfiltered ("Logs will be appended to the log file.\n");
158
159  if (logging_redirect)
160    printf_unfiltered ("Output will be sent only to the log file.\n");
161  else
162    printf_unfiltered ("Output will be logged and displayed.\n");
163}
164
165void
166_initialize_cli_logging (void)
167{
168  static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
169
170
171  add_prefix_cmd ("logging", class_support, set_logging_command,
172		  "Set logging options", &set_logging_cmdlist,
173		  "set logging ", 0, &setlist);
174  add_prefix_cmd ("logging", class_support, show_logging_command,
175		  "Show logging options", &show_logging_cmdlist,
176		  "show logging ", 0, &showlist);
177  add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, "\
178Set whether logging overwrites or appends to the log file.", "\
179Show whether logging overwrites or appends to the log file.", "\
180If set, logging overrides the log file.", "\
181Whether logging overwrites or appends to the log file is %s.",
182			   NULL, NULL, &set_logging_cmdlist, &show_logging_cmdlist);
183  add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, "\
184Set the logging output mode.", "\
185Show the logging output mode.", "\
186If redirect is off, output will go to both the screen and the log file.\n\
187If redirect is on, output will go only to the log file.", "\
188The logging output mode is %s.",
189			   NULL, NULL, &set_logging_cmdlist, &show_logging_cmdlist);
190  add_setshow_filename_cmd ("file", class_support, &logging_filename, "\
191Set the current logfile.", "\
192Show the current logfile.", "\
193The logfile is used when directing GDB's output.", "\
194The current logfile is %s.",
195			    NULL, NULL,
196			    &set_logging_cmdlist, &show_logging_cmdlist);
197  add_cmd ("on", class_support, set_logging_on,
198	   "Enable logging.", &set_logging_cmdlist);
199  add_cmd ("off", class_support, set_logging_off,
200	   "Disable logging.", &set_logging_cmdlist);
201
202  logging_filename = xstrdup ("gdb.txt");
203}
204