quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

tool_stderr.c (2128B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at https://curl.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  * SPDX-License-Identifier: curl
     22  *
     23  ***************************************************************************/
     24 
     25 #include "tool_setup.h"
     26 #include "tool_stderr.h"
     27 #include "tool_msgs.h"
     28 
     29 #include "memdebug.h" /* keep this as LAST include */
     30 
     31 FILE *tool_stderr;
     32 
     33 void tool_init_stderr(void)
     34 {
     35   /* !checksrc! disable STDERR 1 */
     36   tool_stderr = stderr;
     37 }
     38 
     39 void tool_set_stderr_file(struct GlobalConfig *global, const char *filename)
     40 {
     41   FILE *fp;
     42 
     43   if(!filename)
     44     return;
     45 
     46   if(!strcmp(filename, "-")) {
     47     tool_stderr = stdout;
     48     return;
     49   }
     50 
     51   /* precheck that filename is accessible to lessen the chance that the
     52      subsequent freopen will fail. */
     53   fp = fopen(filename, FOPEN_WRITETEXT);
     54   if(!fp) {
     55     warnf(global, "Warning: Failed to open %s", filename);
     56     return;
     57   }
     58   fclose(fp);
     59 
     60   /* freopen the actual stderr (stdio.h stderr) instead of tool_stderr since
     61      the latter may be set to stdout. */
     62   /* !checksrc! disable STDERR 1 */
     63   fp = freopen(filename, FOPEN_WRITETEXT, stderr);
     64   if(!fp) {
     65     /* stderr may have been closed by freopen. there is nothing to be done. */
     66     DEBUGASSERT(0);
     67     return;
     68   }
     69   /* !checksrc! disable STDERR 1 */
     70   tool_stderr = stderr;
     71 }