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. */
30 # define WGET_USE_STDARG
48 /* We expect no message passed to logprintf() to be bigger than this.
49 Before a message is printed, we make sure that at least this much
50 room is left for printing it. */
51 #define SAVED_LOG_MAXMSG 32768
53 /* Maximum allowed growing size. */
54 #define SAVED_LOG_MAXSIZE (10 * 1L << 20)
56 static char *saved_log;
57 /* Size of the current log. */
58 static long saved_log_size;
59 /* Offset into the log where we are about to print (size of the
60 used-up part of SAVED_LOG). */
61 static long saved_log_offset;
62 /* Whether logging is saved at all. */
67 /* Check X against opt.verbose and opt.quiet. The semantics is as
70 * LOG_ALWAYS - print the message unconditionally;
72 * LOG_NOTQUIET - print the message if opt.quiet is non-zero;
74 * LOG_NONVERBOSE - print the message if opt.verbose is zero;
76 * LOG_VERBOSE - print the message if opt.verbose is non-zero. */
77 #define CHECK_VERBOSE(x) \
86 case LOG_NONVERBOSE: \
87 if (opt.verbose || opt.quiet) \
95 #define CANONICALIZE_LOGFP_OR_RETURN do { \
99 /* #### Should this ever happen? */ \
105 logputs (enum log_options o, const char *s)
108 CANONICALIZE_LOGFP_OR_RETURN;
114 if (save_log_p && saved_log_size < SAVED_LOG_MAXSIZE)
116 int len = strlen (s);
118 /* Increase size of SAVED_LOG exponentially. */
119 DO_REALLOC (saved_log, saved_log_size,
120 saved_log_offset + len + 1, char);
121 memcpy (saved_log + saved_log_offset, s, len + 1);
122 saved_log_offset += len;
126 /* Print a message to the log file logfp. If logfp is NULL, print to
127 stderr. If logfp is stdin, don't print at all. A copy of message
128 will be saved to saved_log, for later reusal by dump_log(). */
130 logvprintf (enum log_options o, const char *fmt, va_list args)
133 CANONICALIZE_LOGFP_OR_RETURN;
135 /* Originally, we first used vfprintf(), and then checked whether
136 the message needs to be stored with vsprintf(). However, Watcom
137 C didn't like ARGS being used twice, so now we first vsprintf()
138 the message, and then fwrite() it to LOGFP. */
139 if (save_log_p && saved_log_size < SAVED_LOG_MAXSIZE)
142 /* Increase size of `saved_log' exponentially. */
143 DO_REALLOC (saved_log, saved_log_size,
144 saved_log_offset + SAVED_LOG_MAXMSG, char);
145 /* Print the message to the log saver... */
146 vsnprintf (saved_log + saved_log_offset, SAVED_LOG_MAXMSG, fmt, args);
147 /* ...and then dump it to LOGFP. */
148 len = strlen (saved_log + saved_log_offset);
149 fwrite (saved_log + saved_log_offset, len, 1, logfp);
150 saved_log_offset += len;
151 /* If we ran off the limits and corrupted something, bail out
153 assert (saved_log_size >= saved_log_offset);
156 vfprintf (logfp, fmt, args);
166 CANONICALIZE_LOGFP_OR_RETURN;
170 /* Portability makes these two functions look like @#%#@$@#$. */
172 #ifdef WGET_USE_STDARG
174 logprintf (enum log_options o, const char *fmt, ...)
175 #else /* not WGET_USE_STDARG */
179 #endif /* not WGET_USE_STDARG */
182 #ifndef WGET_USE_STDARG
187 #ifdef WGET_USE_STDARG
188 va_start (args, fmt);
191 o = va_arg (args, enum log_options);
192 fmt = va_arg (args, char *);
194 logvprintf (o, fmt, args);
199 /* The same as logprintf(), but does anything only if opt.debug is
201 #ifdef WGET_USE_STDARG
203 debug_logprintf (const char *fmt, ...)
204 #else /* not WGET_USE_STDARG */
206 debug_logprintf (va_alist)
208 #endif /* not WGET_USE_STDARG */
213 #ifndef WGET_USE_STDARG
217 #ifdef WGET_USE_STDARG
218 va_start (args, fmt);
221 fmt = va_arg (args, char *);
223 logvprintf (LOG_ALWAYS, fmt, args);
229 /* Open FILE and set up a logging stream. If FILE cannot be opened,
230 exit with status of 1. */
232 log_init (const char *file, int appendp)
236 logfp = fopen (file, appendp ? "a" : "w");
239 perror (opt.lfilename);
246 /* If the output is a TTY, enable logging, which will make Wget
247 remember all the printed messages, to be able to dump them to
248 a log file in case SIGHUP or SIGUSR1 is received (or
249 Ctrl+Break is pressed under Windows). */
252 && isatty (fileno (logfp))
261 /* Close LOGFP, inhibit further logging and free the memory associated
268 FREE_MAYBE (saved_log);
270 saved_log_size = saved_log_offset = 0;
273 /* Dump SAVED_LOG using logprintf(), but quit further logging to memory.
274 Also, free the memory occupied by saved_log. */
281 logputs (LOG_ALWAYS, saved_log);
284 saved_log_size = saved_log_offset = 0;
287 /* Redirect output to `wget-log' if opt.lfile is stdout. MESSIJ is
288 printed on stdout, and should contain *exactly one* `%s', which
289 will be replaced by the log file name.
291 If logging was not enabled, MESSIJ will not be printed. */
293 redirect_output (const char *messij)
300 logfile = unique_name (DEFAULT_LOGFILE);
301 logfp = fopen (logfile, "w");
304 printf ("%s: %s: %s\n", exec_name, logfile, strerror (errno));
305 /* `stdin' is magic to not print anything. */
308 printf (messij, logfile);
310 /* Dump all the previous messages to LOGFILE. */