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