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