| 1 |
/* Messages logging. |
| 2 |
Copyright (C) 1998-2011, 2015, 2018-2024 Free Software Foundation, |
| 3 |
Inc. |
| 4 |
|
| 5 |
This file is part of GNU Wget. |
| 6 |
|
| 7 |
GNU Wget 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 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
GNU Wget 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 Wget. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Additional permission under GNU GPL version 3 section 7 |
| 21 |
|
| 22 |
If you modify this program, or any covered work, by linking or |
| 23 |
combining it with the OpenSSL project's OpenSSL library (or a |
| 24 |
modified version of that library), containing parts covered by the |
| 25 |
terms of the OpenSSL or SSLeay licenses, the Free Software Foundation |
| 26 |
grants you additional permission to convey the resulting work. |
| 27 |
Corresponding Source for a non-source form of such a combination |
| 28 |
shall include the source code for the parts of OpenSSL used as well |
| 29 |
as that of the covered work. */ |
| 30 |
|
| 31 |
#include "wget.h" |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
#include <string.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <stdarg.h> |
| 37 |
#include <unistd.h> |
| 38 |
#include <assert.h> |
| 39 |
#include <errno.h> |
| 40 |
|
| 41 |
#include "utils.h" |
| 42 |
#include "exits.h" |
| 43 |
#include "log.h" |
| 44 |
|
| 45 |
/* 2005-10-25 SMS. |
| 46 |
VMS log files are often VFC record format, not stream, so fputs() can |
| 47 |
produce multiple records, even when there's no newline terminator in |
| 48 |
the buffer. The result is unsightly output with spurious newlines. |
| 49 |
Using fprintf() instead of fputs(), along with inhibiting some |
| 50 |
fflush() activity below, seems to solve the problem. |
| 51 |
*/ |
| 52 |
#ifdef __VMS |
| 53 |
# define FPUTS( s, f) fprintf( (f), "%s", (s)) |
| 54 |
#else /* def __VMS */ |
| 55 |
# define FPUTS( s, f) fputs( (s), (f)) |
| 56 |
#endif /* def __VMS [else] */ |
| 57 |
|
| 58 |
/* This file implements support for "logging". Logging means printing |
| 59 |
output, plus several additional features: |
| 60 |
|
| 61 |
- Cataloguing output by importance. You can specify that a log |
| 62 |
message is "verbose" or "debug", and it will not be printed unless |
| 63 |
in verbose or debug mode, respectively. |
| 64 |
|
| 65 |
- Redirecting the log to the file. When Wget's output goes to the |
| 66 |
terminal, and Wget receives SIGHUP, all further output is |
| 67 |
redirected to a log file. When this is the case, Wget can also |
| 68 |
print the last several lines of "context" to the log file so that |
| 69 |
it does not begin in the middle of a line. For this to work, the |
| 70 |
logging code stores the last several lines of context. Callers may |
| 71 |
request for certain output not to be stored. |
| 72 |
|
| 73 |
- Inhibiting output. When Wget receives SIGHUP, but redirecting |
| 74 |
the output fails, logging is inhibited. */ |
| 75 |
|
| 76 |
|
| 77 |
/* The file descriptor used for logging. This is NULL before log_init |
| 78 |
is called; logging functions log to stderr then. log_init sets it |
| 79 |
either to stderr or to a file pointer obtained from fopen(). If |
| 80 |
logging is inhibited, logfp is set back to NULL. */ |
| 81 |
static FILE *logfp; |
| 82 |
|
| 83 |
/* Descriptor of the stdout|stderr */ |
| 84 |
static FILE *stdlogfp; |
| 85 |
|
| 86 |
/* Descriptor of the wget.log* file (if created) */ |
| 87 |
static FILE *filelogfp; |
| 88 |
|
| 89 |
/* Name of log file */ |
| 90 |
static char *logfile; |
| 91 |
|
| 92 |
/* Is interactive shell ? */ |
| 93 |
static int shell_is_interactive; |
| 94 |
|
| 95 |
/* A second file descriptor pointing to the temporary log file for the |
| 96 |
WARC writer. If WARC writing is disabled, this is NULL. */ |
| 97 |
static FILE *warclogfp; |
| 98 |
|
| 99 |
/* If true, it means logging is inhibited, i.e. nothing is printed or |
| 100 |
stored. */ |
| 101 |
static bool inhibit_logging; |
| 102 |
|
| 103 |
/* Whether the last output lines are stored for use as context. */ |
| 104 |
static bool save_context_p; |
| 105 |
|
| 106 |
/* Whether the log is flushed after each command. */ |
| 107 |
static bool flush_log_p = true; |
| 108 |
|
| 109 |
/* Whether any output has been received while flush_log_p was 0. */ |
| 110 |
static bool needs_flushing; |
| 111 |
|
| 112 |
/* In the event of a hang-up, and if its output was on a TTY, Wget |
| 113 |
redirects its output to `wget-log'. |
| 114 |
|
| 115 |
For the convenience of reading this newly-created log, we store the |
| 116 |
last several lines ("screenful", hence the choice of 24) of Wget |
| 117 |
output, and dump them as context when the time comes. */ |
| 118 |
#define SAVED_LOG_LINES 24 |
| 119 |
|
| 120 |
/* log_lines is a circular buffer that stores SAVED_LOG_LINES lines of |
| 121 |
output. log_line_current always points to the position in the |
| 122 |
buffer that will be written to next. When log_line_current reaches |
| 123 |
SAVED_LOG_LINES, it is reset to zero. |
| 124 |
|
| 125 |
The problem here is that we'd have to either (re)allocate and free |
| 126 |
strings all the time, or limit the lines to an arbitrary number of |
| 127 |
characters. Instead of settling for either of these, we do both: |
| 128 |
if the line is smaller than a certain "usual" line length (128 |
| 129 |
chars by default), a preallocated memory is used. The rare lines |
| 130 |
that are longer than 128 characters are malloc'ed and freed |
| 131 |
separately. This gives good performance with minimum memory |
| 132 |
consumption and fragmentation. */ |
| 133 |
|
| 134 |
#define STATIC_LENGTH 128 |
| 135 |
|
| 136 |
static struct log_ln { |
| 137 |
char static_line[STATIC_LENGTH + 1]; /* statically allocated |
| 138 |
line. */ |
| 139 |
char *malloced_line; /* malloc'ed line, for lines of output |
| 140 |
larger than 80 characters. */ |
| 141 |
char *content; /* this points either to malloced_line |
| 142 |
or to the appropriate static_line. |
| 143 |
If this is NULL, it means the line |
| 144 |
has not yet been used. */ |
| 145 |
} log_lines[SAVED_LOG_LINES]; |
| 146 |
|
| 147 |
/* The current position in the ring. */ |
| 148 |
static int log_line_current = -1; |
| 149 |
|
| 150 |
/* Whether the most recently written line was "trailing", i.e. did not |
| 151 |
finish with \n. This is an important piece of information because |
| 152 |
the code is always careful to append data to trailing lines, rather |
| 153 |
than create new ones. */ |
| 154 |
static bool trailing_line; |
| 155 |
|
| 156 |
static void check_redirect_output (void); |
| 157 |
|
| 158 |
#define ROT_ADVANCE(num) do { \ |
| 159 |
if (++num >= SAVED_LOG_LINES) \ |
| 160 |
num = 0; \ |
| 161 |
} while (0) |
| 162 |
|
| 163 |
/* Free the log line index with NUM. This calls free on |
| 164 |
ln->malloced_line if it's non-NULL, and it also resets |
| 165 |
ln->malloced_line and ln->content to NULL. */ |
| 166 |
|
| 167 |
static void |
| 168 |
free_log_line (int num) |
| 169 |
{ |
| 170 |
struct log_ln *ln = log_lines + num; |
| 171 |
xfree (ln->malloced_line); |
| 172 |
ln->content = NULL; |
| 173 |
} |
| 174 |
|
| 175 |
/* Append bytes in the range [start, end) to one line in the log. The |
| 176 |
region is not supposed to contain newlines, except for the last |
| 177 |
character (at end[-1]). */ |
| 178 |
|
| 179 |
static void |
| 180 |
saved_append_1 (const char *start, const char *end) |
| 181 |
{ |
| 182 |
int len = end - start; |
| 183 |
if (!len) |
| 184 |
return; |
| 185 |
|
| 186 |
/* First, check whether we need to append to an existing line or to |
| 187 |
create a new one. */ |
| 188 |
if (!trailing_line) |
| 189 |
{ |
| 190 |
/* Create a new line. */ |
| 191 |
struct log_ln *ln; |
| 192 |
|
| 193 |
if (log_line_current == -1) |
| 194 |
log_line_current = 0; |
| 195 |
else |
| 196 |
free_log_line (log_line_current); |
| 197 |
ln = log_lines + log_line_current; |
| 198 |
if (len > STATIC_LENGTH) |
| 199 |
{ |
| 200 |
ln->malloced_line = strdupdelim (start, end); |
| 201 |
ln->content = ln->malloced_line; |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
memcpy (ln->static_line, start, len); |
| 206 |
ln->static_line[len] = '\0'; |
| 207 |
ln->content = ln->static_line; |
| 208 |
} |
| 209 |
} |
| 210 |
else |
| 211 |
{ |
| 212 |
/* Append to the last line. If the line is malloc'ed, we just |
| 213 |
call realloc and append the new string. If the line is |
| 214 |
static, we have to check whether appending the new string |
| 215 |
would make it exceed STATIC_LENGTH characters, and if so, |
| 216 |
convert it to malloc(). */ |
| 217 |
struct log_ln *ln = log_lines + log_line_current; |
| 218 |
if (ln->malloced_line) |
| 219 |
{ |
| 220 |
/* Resize malloc'ed line and append. */ |
| 221 |
int old_len = strlen (ln->malloced_line); |
| 222 |
ln->malloced_line = xrealloc (ln->malloced_line, old_len + len + 1); |
| 223 |
memcpy (ln->malloced_line + old_len, start, len); |
| 224 |
ln->malloced_line[old_len + len] = '\0'; |
| 225 |
/* might have changed due to realloc */ |
| 226 |
ln->content = ln->malloced_line; |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
int old_len = strlen (ln->static_line); |
| 231 |
if (old_len + len > STATIC_LENGTH) |
| 232 |
{ |
| 233 |
/* Allocate memory and concatenate the old and the new |
| 234 |
contents. */ |
| 235 |
ln->malloced_line = xmalloc (old_len + len + 1); |
| 236 |
memcpy (ln->malloced_line, ln->static_line, |
| 237 |
old_len); |
| 238 |
memcpy (ln->malloced_line + old_len, start, len); |
| 239 |
ln->malloced_line[old_len + len] = '\0'; |
| 240 |
ln->content = ln->malloced_line; |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
/* Just append to the old, statically allocated |
| 245 |
contents. */ |
| 246 |
memcpy (ln->static_line + old_len, start, len); |
| 247 |
ln->static_line[old_len + len] = '\0'; |
| 248 |
ln->content = ln->static_line; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
trailing_line = !(end[-1] == '\n'); |
| 253 |
if (!trailing_line) |
| 254 |
ROT_ADVANCE (log_line_current); |
| 255 |
} |
| 256 |
|
| 257 |
/* Log the contents of S, as explained above. If S consists of |
| 258 |
multiple lines, they are logged separately. If S does not end with |
| 259 |
a newline, it will form a "trailing" line, to which things will get |
| 260 |
appended the next time this function is called. */ |
| 261 |
|
| 262 |
static void |
| 263 |
saved_append (const char *s) |
| 264 |
{ |
| 265 |
while (*s) |
| 266 |
{ |
| 267 |
const char *end = strchr (s, '\n'); |
| 268 |
if (!end) |
| 269 |
end = s + strlen (s); |
| 270 |
else |
| 271 |
++end; |
| 272 |
saved_append_1 (s, end); |
| 273 |
s = end; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
/* Check X against opt.verbose and opt.quiet. The semantics is as |
| 278 |
follows: |
| 279 |
|
| 280 |
* LOG_ALWAYS - print the message unconditionally; |
| 281 |
|
| 282 |
* LOG_NOTQUIET - print the message if opt.quiet is non-zero; |
| 283 |
|
| 284 |
* LOG_NONVERBOSE - print the message if opt.verbose is zero; |
| 285 |
|
| 286 |
* LOG_VERBOSE - print the message if opt.verbose is non-zero. */ |
| 287 |
#define CHECK_VERBOSE(x) \ |
| 288 |
switch (x) \ |
| 289 |
{ \ |
| 290 |
case LOG_PROGRESS: \ |
| 291 |
if (!opt.show_progress) \ |
| 292 |
return; \ |
| 293 |
break; \ |
| 294 |
case LOG_ALWAYS: \ |
| 295 |
break; \ |
| 296 |
case LOG_NOTQUIET: \ |
| 297 |
if (opt.quiet) \ |
| 298 |
return; \ |
| 299 |
break; \ |
| 300 |
case LOG_NONVERBOSE: \ |
| 301 |
if (opt.verbose || opt.quiet) \ |
| 302 |
return; \ |
| 303 |
break; \ |
| 304 |
case LOG_VERBOSE: \ |
| 305 |
if (!opt.verbose) \ |
| 306 |
return; \ |
| 307 |
} |
| 308 |
|
| 309 |
/* Returns the file descriptor for logging. This is LOGFP, except if |
| 310 |
called before log_init, in which case it returns stderr. This is |
| 311 |
useful in case someone calls a logging function before log_init. |
| 312 |
|
| 313 |
If logging is inhibited, return NULL. */ |
| 314 |
|
| 315 |
static FILE * |
| 316 |
get_log_fp (void) |
| 317 |
{ |
| 318 |
if (inhibit_logging) |
| 319 |
return NULL; |
| 320 |
if (logfp) |
| 321 |
return logfp; |
| 322 |
return stderr; |
| 323 |
} |
| 324 |
|
| 325 |
static FILE * |
| 326 |
get_progress_fp (void) |
| 327 |
{ |
| 328 |
if (opt.show_progress == true) |
| 329 |
return stderr; |
| 330 |
return get_log_fp(); |
| 331 |
} |
| 332 |
|
| 333 |
/* Returns the file descriptor for the secondary log file. This is |
| 334 |
WARCLOGFP, except if called before log_init, in which case it |
| 335 |
returns stderr. This is useful in case someone calls a logging |
| 336 |
function before log_init. |
| 337 |
|
| 338 |
If logging is inhibited, return NULL. */ |
| 339 |
|
| 340 |
static FILE * |
| 341 |
get_warc_log_fp (void) |
| 342 |
{ |
| 343 |
if (inhibit_logging) |
| 344 |
return NULL; |
| 345 |
if (warclogfp) |
| 346 |
return warclogfp; |
| 347 |
if (logfp) |
| 348 |
return NULL; |
| 349 |
return stderr; |
| 350 |
} |
| 351 |
|
| 352 |
/* Sets the file descriptor for the secondary log file. */ |
| 353 |
|
| 354 |
void |
| 355 |
log_set_warc_log_fp (FILE * fp) |
| 356 |
{ |
| 357 |
warclogfp = fp; |
| 358 |
} |
| 359 |
|
| 360 |
/* Log a literal string S. The string is logged as-is, without a |
| 361 |
newline appended. */ |
| 362 |
|
| 363 |
void |
| 364 |
logputs (enum log_options o, const char *s) |
| 365 |
{ |
| 366 |
FILE *fp; |
| 367 |
FILE *warcfp; |
| 368 |
int errno_save = errno; |
| 369 |
|
| 370 |
check_redirect_output (); |
| 371 |
if (o == LOG_PROGRESS) |
| 372 |
fp = get_progress_fp (); |
| 373 |
else |
| 374 |
fp = get_log_fp (); |
| 375 |
|
| 376 |
errno = errno_save; |
| 377 |
|
| 378 |
if (fp == NULL) |
| 379 |
return; |
| 380 |
|
| 381 |
warcfp = get_warc_log_fp (); |
| 382 |
errno = errno_save; |
| 383 |
|
| 384 |
CHECK_VERBOSE (o); |
| 385 |
|
| 386 |
FPUTS (s, fp); |
| 387 |
if (warcfp != NULL) |
| 388 |
FPUTS (s, warcfp); |
| 389 |
if (save_context_p) |
| 390 |
saved_append (s); |
| 391 |
if (flush_log_p) |
| 392 |
logflush (); |
| 393 |
else |
| 394 |
needs_flushing = true; |
| 395 |
|
| 396 |
errno = errno_save; |
| 397 |
} |
| 398 |
|
| 399 |
struct logvprintf_state { |
| 400 |
char *bigmsg; |
| 401 |
int expected_size; |
| 402 |
int allocated; |
| 403 |
}; |
| 404 |
|
| 405 |
/* Print a message to the log. A copy of message will be saved to |
| 406 |
saved_log, for later reusal by log_dump_context(). |
| 407 |
|
| 408 |
Normally we'd want this function to loop around vsnprintf until |
| 409 |
sufficient room is allocated, as the Linux man page recommends. |
| 410 |
However each call to vsnprintf() must be preceded by va_start and |
| 411 |
followed by va_end. Since calling va_start/va_end is possible only |
| 412 |
in the function that contains the `...' declaration, we cannot call |
| 413 |
vsnprintf more than once. Therefore this function saves its state |
| 414 |
to logvprintf_state and signals the parent to call it again. |
| 415 |
|
| 416 |
(An alternative approach would be to use va_copy, but that's not |
| 417 |
portable.) */ |
| 418 |
|
| 419 |
static bool GCC_FORMAT_ATTR (2, 0) |
| 420 |
log_vprintf_internal (struct logvprintf_state *state, const char *fmt, |
| 421 |
va_list args) |
| 422 |
{ |
| 423 |
char smallmsg[128]; |
| 424 |
char *write_ptr = smallmsg; |
| 425 |
int available_size = sizeof (smallmsg); |
| 426 |
int numwritten; |
| 427 |
FILE *fp = get_log_fp (); |
| 428 |
FILE *warcfp = get_warc_log_fp (); |
| 429 |
|
| 430 |
if (fp == NULL) |
| 431 |
return false; |
| 432 |
|
| 433 |
if (!save_context_p && warcfp == NULL) |
| 434 |
{ |
| 435 |
/* In the simple case just call vfprintf(), to avoid needless |
| 436 |
allocation and games with vsnprintf(). */ |
| 437 |
vfprintf (fp, fmt, args); |
| 438 |
goto flush; |
| 439 |
} |
| 440 |
|
| 441 |
if (state->allocated != 0) |
| 442 |
{ |
| 443 |
write_ptr = state->bigmsg; |
| 444 |
available_size = state->allocated; |
| 445 |
} |
| 446 |
|
| 447 |
/* The GNU coding standards advise not to rely on the return value |
| 448 |
of sprintf(). However, vsnprintf() is a relatively new function |
| 449 |
missing from legacy systems. Therefore I consider it safe to |
| 450 |
assume that its return value is meaningful. On the systems where |
| 451 |
vsnprintf() is not available, we use the implementation from |
| 452 |
snprintf.c which does return the correct value. */ |
| 453 |
numwritten = vsnprintf (write_ptr, available_size, fmt, args); |
| 454 |
|
| 455 |
/* vsnprintf() will not step over the limit given by available_size. |
| 456 |
If it fails, it returns either -1 (older implementations) or the |
| 457 |
number of characters (not counting the terminating \0) that |
| 458 |
*would have* been written if there had been enough room (C99). |
| 459 |
In the former case, we double available_size and malloc to get a |
| 460 |
larger buffer, and try again. In the latter case, we use the |
| 461 |
returned information to build a buffer of the correct size. */ |
| 462 |
|
| 463 |
if (numwritten == -1) |
| 464 |
{ |
| 465 |
/* Writing failed, and we don't know the needed size. Try |
| 466 |
again with doubled size. */ |
| 467 |
int newsize = available_size << 1; |
| 468 |
state->bigmsg = xrealloc (state->bigmsg, newsize); |
| 469 |
state->allocated = newsize; |
| 470 |
return false; |
| 471 |
} |
| 472 |
else if (numwritten >= available_size) |
| 473 |
{ |
| 474 |
/* Writing failed, but we know exactly how much space we |
| 475 |
need. */ |
| 476 |
int newsize = numwritten + 1; |
| 477 |
state->bigmsg = xrealloc (state->bigmsg, newsize); |
| 478 |
state->allocated = newsize; |
| 479 |
return false; |
| 480 |
} |
| 481 |
|
| 482 |
/* Writing succeeded. */ |
| 483 |
if (save_context_p) |
| 484 |
saved_append (write_ptr); |
| 485 |
FPUTS (write_ptr, fp); |
| 486 |
if (warcfp != NULL && warcfp != fp) |
| 487 |
FPUTS (write_ptr, warcfp); |
| 488 |
xfree (state->bigmsg); |
| 489 |
|
| 490 |
flush: |
| 491 |
if (flush_log_p) |
| 492 |
logflush (); |
| 493 |
else |
| 494 |
needs_flushing = true; |
| 495 |
|
| 496 |
return true; |
| 497 |
} |
| 498 |
|
| 499 |
/* Flush LOGFP. Useful while flushing is disabled. */ |
| 500 |
void |
| 501 |
logflush (void) |
| 502 |
{ |
| 503 |
FILE *fp = get_log_fp (); |
| 504 |
FILE *warcfp = get_warc_log_fp (); |
| 505 |
if (fp) |
| 506 |
{ |
| 507 |
/* 2005-10-25 SMS. |
| 508 |
On VMS, flush only for a terminal. See note at FPUTS macro, above. |
| 509 |
*/ |
| 510 |
#ifdef __VMS |
| 511 |
if (isatty( fileno( fp))) |
| 512 |
{ |
| 513 |
fflush (fp); |
| 514 |
} |
| 515 |
#else /* def __VMS */ |
| 516 |
fflush (fp); |
| 517 |
#endif /* def __VMS [else] */ |
| 518 |
} |
| 519 |
|
| 520 |
if (warcfp != NULL) |
| 521 |
fflush (warcfp); |
| 522 |
|
| 523 |
needs_flushing = false; |
| 524 |
} |
| 525 |
|
| 526 |
/* Enable or disable log flushing. */ |
| 527 |
void |
| 528 |
log_set_flush (bool flush) |
| 529 |
{ |
| 530 |
if (flush == flush_log_p) |
| 531 |
return; |
| 532 |
|
| 533 |
if (flush == false) |
| 534 |
{ |
| 535 |
/* Disable flushing by setting flush_log_p to 0. */ |
| 536 |
flush_log_p = false; |
| 537 |
} |
| 538 |
else |
| 539 |
{ |
| 540 |
/* Re-enable flushing. If anything was printed in no-flush mode, |
| 541 |
flush the log now. */ |
| 542 |
if (needs_flushing) |
| 543 |
logflush (); |
| 544 |
flush_log_p = true; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
/* (Temporarily) disable storing log to memory. Returns the old |
| 549 |
status of storing, with which this function can be called again to |
| 550 |
reestablish storing. */ |
| 551 |
|
| 552 |
bool |
| 553 |
log_set_save_context (bool savep) |
| 554 |
{ |
| 555 |
bool old = save_context_p; |
| 556 |
save_context_p = savep; |
| 557 |
return old; |
| 558 |
} |
| 559 |
|
| 560 |
/* Print a message to the screen or to the log. The first argument |
| 561 |
defines the verbosity of the message, and the rest are as in |
| 562 |
printf(3). */ |
| 563 |
|
| 564 |
void |
| 565 |
logprintf (enum log_options o, const char *fmt, ...) |
| 566 |
{ |
| 567 |
va_list args; |
| 568 |
struct logvprintf_state lpstate; |
| 569 |
bool done; |
| 570 |
int errno_saved = errno; |
| 571 |
|
| 572 |
CHECK_VERBOSE (o); |
| 573 |
|
| 574 |
check_redirect_output (); |
| 575 |
errno = errno_saved; |
| 576 |
if (inhibit_logging) |
| 577 |
return; |
| 578 |
|
| 579 |
xzero (lpstate); |
| 580 |
errno = 0; |
| 581 |
do |
| 582 |
{ |
| 583 |
va_start (args, fmt); |
| 584 |
done = log_vprintf_internal (&lpstate, fmt, args); |
| 585 |
va_end (args); |
| 586 |
|
| 587 |
if (done && errno == EPIPE) |
| 588 |
exit (WGET_EXIT_GENERIC_ERROR); |
| 589 |
} |
| 590 |
while (!done); |
| 591 |
|
| 592 |
errno = errno_saved; |
| 593 |
} |
| 594 |
|
| 595 |
#ifdef ENABLE_DEBUG |
| 596 |
/* The same as logprintf(), but does anything only if opt.debug is |
| 597 |
true. */ |
| 598 |
void |
| 599 |
debug_logprintf (const char *fmt, ...) |
| 600 |
{ |
| 601 |
if (opt.debug) |
| 602 |
{ |
| 603 |
va_list args; |
| 604 |
struct logvprintf_state lpstate; |
| 605 |
bool done; |
| 606 |
|
| 607 |
#ifndef TESTING |
| 608 |
check_redirect_output (); |
| 609 |
#endif |
| 610 |
if (inhibit_logging) |
| 611 |
return; |
| 612 |
|
| 613 |
xzero (lpstate); |
| 614 |
do |
| 615 |
{ |
| 616 |
va_start (args, fmt); |
| 617 |
done = log_vprintf_internal (&lpstate, fmt, args); |
| 618 |
va_end (args); |
| 619 |
} |
| 620 |
while (!done); |
| 621 |
} |
| 622 |
} |
| 623 |
#endif /* ENABLE_DEBUG */ |
| 624 |
|
| 625 |
/* Open FILE and set up a logging stream. If FILE cannot be opened, |
| 626 |
exit with status of 1. */ |
| 627 |
void |
| 628 |
log_init (const char *file, bool appendp) |
| 629 |
{ |
| 630 |
if (file) |
| 631 |
{ |
| 632 |
if (HYPHENP (file)) |
| 633 |
{ |
| 634 |
stdlogfp = stdout; |
| 635 |
logfp = stdlogfp; |
| 636 |
} |
| 637 |
else |
| 638 |
{ |
| 639 |
filelogfp = fopen (file, appendp ? "a" : "w"); |
| 640 |
if (!filelogfp) |
| 641 |
{ |
| 642 |
fprintf (stderr, "%s: %s: %s\n", exec_name, file, strerror (errno)); |
| 643 |
exit (WGET_EXIT_GENERIC_ERROR); |
| 644 |
} |
| 645 |
logfp = filelogfp; |
| 646 |
} |
| 647 |
} |
| 648 |
else |
| 649 |
{ |
| 650 |
/* The log goes to stderr to avoid collisions with the output if |
| 651 |
the user specifies `-O -'. #### Francois Pinard suggests |
| 652 |
that it's a better idea to print to stdout by default, and to |
| 653 |
stderr only if the user actually specifies `-O -'. He says |
| 654 |
this inconsistency is harder to document, but is overall |
| 655 |
easier on the user. */ |
| 656 |
stdlogfp = stderr; |
| 657 |
logfp = stdlogfp; |
| 658 |
|
| 659 |
if (1 |
| 660 |
#ifdef HAVE_ISATTY |
| 661 |
&& isatty (fileno (logfp)) |
| 662 |
#endif |
| 663 |
) |
| 664 |
{ |
| 665 |
/* If the output is a TTY, enable save context, i.e. store |
| 666 |
the most recent several messages ("context") and dump |
| 667 |
them to a log file in case SIGHUP or SIGUSR1 is received |
| 668 |
(or Ctrl+Break is pressed under Windows). */ |
| 669 |
save_context_p = true; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
#ifndef WINDOWS |
| 674 |
/* Initialize this values so we don't have to ask every time we print line */ |
| 675 |
shell_is_interactive = isatty (STDIN_FILENO); |
| 676 |
#endif |
| 677 |
} |
| 678 |
|
| 679 |
/* Close LOGFP (only if we opened it, not if it's stderr), inhibit |
| 680 |
further logging and free the memory associated with it. */ |
| 681 |
void |
| 682 |
log_close (void) |
| 683 |
{ |
| 684 |
int i; |
| 685 |
|
| 686 |
if (logfp && logfp != stderr && logfp != stdout) |
| 687 |
{ |
| 688 |
if (logfp == stdlogfp) |
| 689 |
stdlogfp = NULL; |
| 690 |
if (logfp == filelogfp) |
| 691 |
filelogfp = NULL; |
| 692 |
fclose (logfp); |
| 693 |
} |
| 694 |
logfp = NULL; |
| 695 |
|
| 696 |
inhibit_logging = true; |
| 697 |
save_context_p = false; |
| 698 |
|
| 699 |
for (i = 0; i < SAVED_LOG_LINES; i++) |
| 700 |
free_log_line (i); |
| 701 |
log_line_current = -1; |
| 702 |
trailing_line = false; |
| 703 |
} |
| 704 |
|
| 705 |
/* Dump saved lines to logfp. */ |
| 706 |
static void |
| 707 |
log_dump_context (void) |
| 708 |
{ |
| 709 |
int num = log_line_current; |
| 710 |
FILE *fp = get_log_fp (); |
| 711 |
FILE *warcfp = get_warc_log_fp (); |
| 712 |
if (!fp) |
| 713 |
return; |
| 714 |
|
| 715 |
if (num == -1) |
| 716 |
return; |
| 717 |
if (trailing_line) |
| 718 |
ROT_ADVANCE (num); |
| 719 |
do |
| 720 |
{ |
| 721 |
struct log_ln *ln = log_lines + num; |
| 722 |
if (ln->content) |
| 723 |
{ |
| 724 |
FPUTS (ln->content, fp); |
| 725 |
if (warcfp != NULL) |
| 726 |
FPUTS (ln->content, warcfp); |
| 727 |
} |
| 728 |
ROT_ADVANCE (num); |
| 729 |
} |
| 730 |
while (num != log_line_current); |
| 731 |
if (trailing_line) |
| 732 |
if (log_lines[log_line_current].content) |
| 733 |
{ |
| 734 |
FPUTS (log_lines[log_line_current].content, fp); |
| 735 |
if (warcfp != NULL) |
| 736 |
FPUTS (log_lines[log_line_current].content, warcfp); |
| 737 |
} |
| 738 |
fflush (fp); |
| 739 |
fflush (warcfp); |
| 740 |
} |
| 741 |
|
| 742 |
/* String escape functions. */ |
| 743 |
|
| 744 |
/* Return the number of non-printable characters in SOURCE. |
| 745 |
Non-printable characters are determined as per c-ctype.c. */ |
| 746 |
|
| 747 |
static int |
| 748 |
count_nonprint (const char *source) |
| 749 |
{ |
| 750 |
const char *p; |
| 751 |
int cnt; |
| 752 |
for (p = source, cnt = 0; *p; p++) |
| 753 |
if (!c_isprint (*p)) |
| 754 |
++cnt; |
| 755 |
return cnt; |
| 756 |
} |
| 757 |
|
| 758 |
/* Copy SOURCE to DEST, escaping non-printable characters. |
| 759 |
|
| 760 |
Non-printable refers to anything outside the non-control ASCII |
| 761 |
range (32-126) which means that, for example, CR, LF, and TAB are |
| 762 |
considered non-printable along with ESC, BS, and other control |
| 763 |
chars. This is by design: it makes sure that messages from remote |
| 764 |
servers cannot be easily used to deceive the users by mimicking |
| 765 |
Wget's output. Disallowing non-ASCII characters is another |
| 766 |
necessary security measure, which makes sure that remote servers |
| 767 |
cannot garble the screen or guess the local charset and perform |
| 768 |
homographic attacks. |
| 769 |
|
| 770 |
Of course, the above mandates that escnonprint only be used in |
| 771 |
contexts expected to be ASCII, such as when printing host names, |
| 772 |
URL components, HTTP headers, FTP server messages, and the like. |
| 773 |
|
| 774 |
ESCAPE is the leading character of the escape sequence. BASE |
| 775 |
should be the base of the escape sequence, and must be either 8 for |
| 776 |
octal or 16 for hex. |
| 777 |
|
| 778 |
DEST must point to a location with sufficient room to store an |
| 779 |
encoded version of SOURCE. */ |
| 780 |
|
| 781 |
static void |
| 782 |
copy_and_escape (const char *source, char *dest, char escape, int base) |
| 783 |
{ |
| 784 |
const char *from = source; |
| 785 |
char *to = dest; |
| 786 |
unsigned char c; |
| 787 |
|
| 788 |
/* Copy chars from SOURCE to DEST, escaping non-printable ones. */ |
| 789 |
switch (base) |
| 790 |
{ |
| 791 |
case 8: |
| 792 |
while ((c = *from++) != '\0') |
| 793 |
if (c_isprint (c)) |
| 794 |
*to++ = c; |
| 795 |
else |
| 796 |
{ |
| 797 |
*to++ = escape; |
| 798 |
*to++ = '0' + (c >> 6); |
| 799 |
*to++ = '0' + ((c >> 3) & 7); |
| 800 |
*to++ = '0' + (c & 7); |
| 801 |
} |
| 802 |
break; |
| 803 |
case 16: |
| 804 |
while ((c = *from++) != '\0') |
| 805 |
if (c_isprint (c)) |
| 806 |
*to++ = c; |
| 807 |
else |
| 808 |
{ |
| 809 |
*to++ = escape; |
| 810 |
*to++ = XNUM_TO_DIGIT (c >> 4); |
| 811 |
*to++ = XNUM_TO_DIGIT (c & 0xf); |
| 812 |
} |
| 813 |
break; |
| 814 |
default: |
| 815 |
abort (); |
| 816 |
} |
| 817 |
*to = '\0'; |
| 818 |
} |
| 819 |
|
| 820 |
#define RING_SIZE 3 |
| 821 |
struct ringel { |
| 822 |
char *buffer; |
| 823 |
int size; |
| 824 |
}; |
| 825 |
static struct ringel ring[RING_SIZE]; /* ring data */ |
| 826 |
|
| 827 |
static const char * |
| 828 |
escnonprint_internal (const char *str, char escape, int base) |
| 829 |
{ |
| 830 |
static int ringpos; /* current ring position */ |
| 831 |
int nprcnt; |
| 832 |
|
| 833 |
assert (base == 8 || base == 16); |
| 834 |
|
| 835 |
nprcnt = count_nonprint (str); |
| 836 |
if (nprcnt == 0) |
| 837 |
/* If there are no non-printable chars in STR, don't bother |
| 838 |
copying anything, just return STR. */ |
| 839 |
return str; |
| 840 |
|
| 841 |
{ |
| 842 |
/* Set up a pointer to the current ring position, so we can write |
| 843 |
simply r->X instead of ring[ringpos].X. */ |
| 844 |
struct ringel *r = ring + ringpos; |
| 845 |
|
| 846 |
/* Every non-printable character is replaced with the escape char |
| 847 |
and three (or two, depending on BASE) *additional* chars. Size |
| 848 |
must also include the length of the original string and one |
| 849 |
additional char for the terminating \0. */ |
| 850 |
int needed_size = strlen (str) + 1 + (base == 8 ? 3 * nprcnt : 2 * nprcnt); |
| 851 |
|
| 852 |
/* If the current buffer is uninitialized or too small, |
| 853 |
(re)allocate it. */ |
| 854 |
if (r->buffer == NULL || r->size < needed_size) |
| 855 |
{ |
| 856 |
r->buffer = xrealloc (r->buffer, needed_size); |
| 857 |
r->size = needed_size; |
| 858 |
} |
| 859 |
|
| 860 |
copy_and_escape (str, r->buffer, escape, base); |
| 861 |
ringpos = (ringpos + 1) % RING_SIZE; |
| 862 |
return r->buffer; |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
/* Return a pointer to a static copy of STR with the non-printable |
| 867 |
characters escaped as \ooo. If there are no non-printable |
| 868 |
characters in STR, STR is returned. See copy_and_escape for more |
| 869 |
information on which characters are considered non-printable. |
| 870 |
|
| 871 |
DON'T call this function on translated strings because escaping |
| 872 |
will break them. Don't call it on literal strings from the source, |
| 873 |
which are by definition trusted. If newlines are allowed in the |
| 874 |
string, escape and print it line by line because escaping the whole |
| 875 |
string will convert newlines to \012. (This is so that expectedly |
| 876 |
single-line messages cannot use embedded newlines to mimic Wget's |
| 877 |
output and deceive the user.) |
| 878 |
|
| 879 |
escnonprint doesn't quote its escape character because it is notf |
| 880 |
meant as a general and reversible quoting mechanism, but as a quick |
| 881 |
way to defang binary junk sent by malicious or buggy servers. |
| 882 |
|
| 883 |
NOTE: since this function can return a pointer to static data, be |
| 884 |
careful to copy its result before calling it again. However, to be |
| 885 |
more useful with printf, it maintains an internal ring of static |
| 886 |
buffers to return. Currently the ring size is 3, which means you |
| 887 |
can print up to three values in the same printf; if more is needed, |
| 888 |
bump RING_SIZE. */ |
| 889 |
|
| 890 |
const char * |
| 891 |
escnonprint (const char *str) |
| 892 |
{ |
| 893 |
return escnonprint_internal (str, '\\', 8); |
| 894 |
} |
| 895 |
|
| 896 |
/* Return a pointer to a static copy of STR with the non-printable |
| 897 |
characters escaped as %XX. If there are no non-printable |
| 898 |
characters in STR, STR is returned. |
| 899 |
|
| 900 |
See escnonprint for usage details. */ |
| 901 |
|
| 902 |
const char * |
| 903 |
escnonprint_uri (const char *str) |
| 904 |
{ |
| 905 |
return escnonprint_internal (str, '%', 16); |
| 906 |
} |
| 907 |
|
| 908 |
#if defined DEBUG_MALLOC || defined TESTING |
| 909 |
void |
| 910 |
log_cleanup (void) |
| 911 |
{ |
| 912 |
size_t i; |
| 913 |
for (i = 0; i < countof (ring); i++) |
| 914 |
xfree (ring[i].buffer); |
| 915 |
} |
| 916 |
#endif |
| 917 |
|
| 918 |
/* When SIGHUP or SIGUSR1 are received, the output is redirected |
| 919 |
elsewhere. Such redirection is only allowed once. */ |
| 920 |
static const char *redirect_request_signal_name; |
| 921 |
|
| 922 |
/* Redirect output to `wget-log' or back to stdout/stderr. */ |
| 923 |
|
| 924 |
void |
| 925 |
redirect_output (bool to_file, const char *signal_name) |
| 926 |
{ |
| 927 |
if (to_file && logfp != filelogfp) |
| 928 |
{ |
| 929 |
if (signal_name) |
| 930 |
{ |
| 931 |
fprintf (stderr, "\n%s received.", signal_name); |
| 932 |
} |
| 933 |
if (!filelogfp) |
| 934 |
{ |
| 935 |
filelogfp = unique_create (DEFAULT_LOGFILE, false, &logfile); |
| 936 |
if (filelogfp) |
| 937 |
{ |
| 938 |
fprintf (stderr, _("\nRedirecting output to %s.\n"), |
| 939 |
quote (logfile)); |
| 940 |
/* Store signal name to tell wget it's permanent redirect to log file */ |
| 941 |
redirect_request_signal_name = signal_name; |
| 942 |
logfp = filelogfp; |
| 943 |
/* Dump the context output to the newly opened log. */ |
| 944 |
log_dump_context (); |
| 945 |
} |
| 946 |
else |
| 947 |
{ |
| 948 |
/* Eek! Opening the alternate log file has failed. Nothing we |
| 949 |
can do but disable printing completely. */ |
| 950 |
fprintf (stderr, _("%s: %s; disabling logging.\n"), |
| 951 |
(logfile) ? logfile : DEFAULT_LOGFILE, strerror (errno)); |
| 952 |
inhibit_logging = true; |
| 953 |
} |
| 954 |
} |
| 955 |
else |
| 956 |
{ |
| 957 |
fprintf (stderr, _("\nRedirecting output to %s.\n"), |
| 958 |
quote (logfile)); |
| 959 |
logfp = filelogfp; |
| 960 |
log_dump_context (); |
| 961 |
} |
| 962 |
} |
| 963 |
else if (!to_file && logfp != stdlogfp) |
| 964 |
{ |
| 965 |
logfp = stdlogfp; |
| 966 |
log_dump_context (); |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
/* Check whether there's a need to redirect output. */ |
| 971 |
|
| 972 |
static void |
| 973 |
check_redirect_output (void) |
| 974 |
{ |
| 975 |
#if !defined(WINDOWS) && !defined(__VMS) |
| 976 |
/* If it was redirected already to log file by SIGHUP, SIGUSR1 or -o parameter, |
| 977 |
* it was permanent. |
| 978 |
* If there was no SIGHUP or SIGUSR1 and shell is interactive |
| 979 |
* we check if process is fg or bg before every line is printed.*/ |
| 980 |
if (!redirect_request_signal_name && shell_is_interactive && !opt.lfilename) |
| 981 |
{ |
| 982 |
pid_t foreground_pgrp = tcgetpgrp (STDIN_FILENO); |
| 983 |
|
| 984 |
if (foreground_pgrp != -1 && foreground_pgrp != getpgrp () && !opt.quiet) |
| 985 |
{ |
| 986 |
/* Process backgrounded */ |
| 987 |
redirect_output (true,NULL); |
| 988 |
} |
| 989 |
else |
| 990 |
{ |
| 991 |
/* Process foregrounded */ |
| 992 |
redirect_output (false,NULL); |
| 993 |
} |
| 994 |
} |
| 995 |
#endif /* !defined(WINDOWS) && !defined(__VMS) */ |
| 996 |
} |