2 Copyright (C) 1998 Free Software Foundation, Inc.
4 This file is part of Wget.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 #if defined(__STDC__) && defined(HAVE_STDARG_H)
30 /* If we have __STDC__ and stdarg.h, we'll assume it's an ANSI system. */
49 /* We expect no message passed to logprintf() to be bigger than this.
50 Before a message is printed, we make sure that at least this much
51 room is left for printing it. */
52 #define SAVED_LOG_MAXMSG 32768
54 /* Maximum allowed growing size. */
55 #define SAVED_LOG_MAXSIZE (10 * 1L << 20)
57 static char *saved_log;
58 /* Size of the current log. */
59 static long saved_log_size;
60 /* Offset into the log where we are about to print (size of the
61 used-up part of SAVED_LOG). */
62 static long saved_log_offset;
63 /* Whether logging is saved at all. */
68 /* Check X against opt.verbose and opt.quiet. The semantics is as
71 * LOG_ALWAYS - print the message unconditionally;
73 * LOG_NOTQUIET - print the message if opt.quiet is non-zero;
75 * LOG_NONVERBOSE - print the message if opt.verbose is zero;
77 * LOG_VERBOSE - print the message if opt.verbose is non-zero. */
78 #define CHECK_VERBOSE(x) \
87 case LOG_NONVERBOSE: \
88 if (opt.verbose || opt.quiet) \
96 #define CANONICALIZE_LOGFP_OR_RETURN do { \
100 /* #### Should this ever happen? */ \
106 logputs (enum log_options o, const char *s)
109 CANONICALIZE_LOGFP_OR_RETURN;
115 if (save_log_p && saved_log_size < SAVED_LOG_MAXSIZE)
117 int len = strlen (s);
119 /* Increase size of SAVED_LOG exponentially. */
120 DO_REALLOC (saved_log, saved_log_size,
121 saved_log_offset + len + 1, char);
122 memcpy (saved_log + saved_log_offset, s, len + 1);
123 saved_log_offset += len;
127 /* Print a message to the log file logfp. If logfp is NULL, print to
128 stderr. If logfp is stdin, don't print at all. A copy of message
129 will be saved to saved_log, for later reusal by dump_log(). */
131 logvprintf (enum log_options o, const char *fmt, va_list args)
134 CANONICALIZE_LOGFP_OR_RETURN;
136 /* Originally, we first used vfprintf(), and then checked whether
137 the message needs to be stored with vsprintf(). However, Watcom
138 C didn't like ARGS being used twice, so now we first vsprintf()
139 the message, and then fwrite() it to LOGFP. */
140 if (save_log_p && saved_log_size < SAVED_LOG_MAXSIZE)
143 /* Increase size of `saved_log' exponentially. */
144 DO_REALLOC (saved_log, saved_log_size,
145 saved_log_offset + SAVED_LOG_MAXMSG, char);
146 /* Print the message to the log saver... */
147 #ifdef HAVE_VSNPRINTF
148 vsnprintf (saved_log + saved_log_offset, SAVED_LOG_MAXMSG, fmt, args);
149 #else /* not HAVE_VSNPRINTF */
150 vsprintf (saved_log + saved_log_offset, fmt, args);
151 #endif /* not HAVE_VSNPRINTF */
152 /* ...and then dump it to LOGFP. */
153 len = strlen (saved_log + saved_log_offset);
154 fwrite (saved_log + saved_log_offset, len, 1, logfp);
155 saved_log_offset += len;
156 /* If we ran off the limits and corrupted something, bail out
158 assert (saved_log_size >= saved_log_offset);
161 vfprintf (logfp, fmt, args);
171 CANONICALIZE_LOGFP_OR_RETURN;
175 /* Portability makes these two functions look like @#%#@$@#$. */
179 logprintf (enum log_options o, const char *fmt, ...)
180 #else /* not USE_STDARG */
184 #endif /* not USE_STDARG */
193 va_start (args, fmt);
196 o = va_arg (args, enum log_options);
197 fmt = va_arg (args, char *);
199 logvprintf (o, fmt, args);
204 /* The same as logprintf(), but does anything only if opt.debug is
208 debug_logprintf (const char *fmt, ...)
209 #else /* not USE_STDARG */
211 debug_logprintf (va_alist)
213 #endif /* not USE_STDARG */
223 va_start (args, fmt);
226 fmt = va_arg (args, char *);
228 logvprintf (LOG_ALWAYS, fmt, args);
234 /* Open FILE and set up a logging stream. If FILE cannot be opened,
235 exit with status of 1. */
237 log_init (const char *file, int appendp)
241 logfp = fopen (file, appendp ? "a" : "w");
244 perror (opt.lfilename);
251 /* If the output is a TTY, enable logging, which will make Wget
252 remember all the printed messages, to be able to dump them to
253 a log file in case SIGHUP or SIGUSR1 is received (or
254 Ctrl+Break is pressed under Windows). */
257 && isatty (fileno (logfp))
266 /* Close LOGFP, inhibit further logging and free the memory associated
273 FREE_MAYBE (saved_log);
275 saved_log_size = saved_log_offset = 0;
278 /* Dump SAVED_LOG using logprintf(), but quit further logging to memory.
279 Also, free the memory occupied by saved_log. */
286 logputs (LOG_ALWAYS, saved_log);
289 saved_log_size = saved_log_offset = 0;
292 /* Redirect output to `wget-log' if opt.lfile is stdout. MESSIJ is
293 printed on stdout, and should contain *exactly one* `%s', which
294 will be replaced by the log file name.
296 If logging was not enabled, MESSIJ will not be printed. */
298 redirect_output (const char *messij)
305 logfile = unique_name (DEFAULT_LOGFILE);
306 logfp = fopen (logfile, "w");
309 printf ("%s: %s: %s\n", exec_name, logfile, strerror (errno));
310 /* `stdin' is magic to not print anything. */
313 printf (messij, logfile);
315 /* Dump all the previous messages to LOGFILE. */