Home | History | Annotate | Line # | Download | only in src
      1 /* MPFR Logging functions.
      2 
      3 Copyright 2005-2023 Free Software Foundation, Inc.
      4 Contributed by the AriC and Caramba projects, INRIA.
      5 
      6 This file is part of the GNU MPFR Library.
      7 
      8 The GNU MPFR Library is free software; you can redistribute it and/or modify
      9 it under the terms of the GNU Lesser General Public License as published by
     10 the Free Software Foundation; either version 3 of the License, or (at your
     11 option) any later version.
     12 
     13 The GNU MPFR Library is distributed in the hope that it will be useful, but
     14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16 License for more details.
     17 
     18 You should have received a copy of the GNU Lesser General Public License
     19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22 
     23 #include "mpfr-impl.h"
     24 
     25 /* Logging MPFR needs GCC >= 3.0 and GLIBC >= 2.0. */
     26 
     27 #ifdef MPFR_USE_LOGGING
     28 
     29 /* The <time.h> header might not be available everywhere (it is standard,
     30    but not even required by freestanding C implementations); thus it isn't
     31    included unconditionally. */
     32 #include <time.h>
     33 
     34 /* Define LOGGING variables */
     35 
     36 FILE *mpfr_log_file;
     37 int   mpfr_log_flush;
     38 int   mpfr_log_type;
     39 int   mpfr_log_level;
     40 int   mpfr_log_current;
     41 int   mpfr_log_worstcase_limit;
     42 mpfr_prec_t mpfr_log_prec;
     43 
     44 static void mpfr_log_begin (void) __attribute__((constructor));
     45 
     46 /* We let the system close the LOG itself
     47    (Otherwise functions called by destructor can't use LOG File */
     48 static void
     49 mpfr_log_begin (void)
     50 {
     51   const char *var;
     52   time_t tt;
     53 
     54   /* Grab some information */
     55   var = getenv ("MPFR_LOG_LEVEL");
     56   mpfr_log_level = var == NULL || *var == 0 ? 7 : atoi (var);
     57   mpfr_log_current = 0;
     58 
     59   var = getenv ("MPFR_LOG_PREC");
     60   mpfr_log_prec = var == NULL ? 6 : atol (var);
     61 
     62   /* Get what we need to log */
     63   mpfr_log_type = 0;
     64   if (getenv ("MPFR_LOG_INPUT") != NULL)
     65     mpfr_log_type |= MPFR_LOG_INPUT_F;
     66   if (getenv ("MPFR_LOG_OUTPUT") != NULL)
     67     mpfr_log_type |= MPFR_LOG_OUTPUT_F;
     68   if (getenv ("MPFR_LOG_TIME") != NULL)
     69     mpfr_log_type |= MPFR_LOG_TIME_F;
     70   if (getenv ("MPFR_LOG_INTERNAL") != NULL)
     71     mpfr_log_type |= MPFR_LOG_INTERNAL_F;
     72   if (getenv ("MPFR_LOG_MSG") != NULL)
     73     mpfr_log_type |= MPFR_LOG_MSG_F;
     74   if (getenv ("MPFR_LOG_ZIV") != NULL)
     75     mpfr_log_type |= MPFR_LOG_BADCASE_F;
     76   if (getenv ("MPFR_LOG_STAT") != NULL)
     77     mpfr_log_type |= MPFR_LOG_STAT_F;
     78   if (getenv ("MPFR_LOG_ALL") != NULL)
     79     mpfr_log_type = MPFR_LOG_INPUT_F|MPFR_LOG_OUTPUT_F|MPFR_LOG_TIME_F
     80       |MPFR_LOG_INTERNAL_F|MPFR_LOG_MSG_F|MPFR_LOG_BADCASE_F|MPFR_LOG_STAT_F;
     81 
     82   mpfr_log_flush = getenv ("MPFR_LOG_FLUSH") != NULL;
     83 
     84   /* Open filename if needed */
     85   var = getenv ("MPFR_LOG_FILE");
     86   if (var == NULL || *var == 0)
     87     var = "mpfr.log";
     88   if (mpfr_log_type != 0)
     89     {
     90       mpfr_log_file = fopen (var, "w");
     91       if (mpfr_log_file == NULL)
     92         {
     93           fprintf (stderr, "MPFR LOG: Can't open '%s' with w.\n", var);
     94           abort ();
     95         }
     96       time (&tt);
     97       fprintf (mpfr_log_file, "MPFR LOG FILE %s\n", ctime (&tt));
     98       fflush (mpfr_log_file);  /* always done */
     99     }
    100 }
    101 
    102 /* Return user CPU time measured in milliseconds. Thanks to Torbjorn. */
    103 
    104 #if defined (ANSIONLY) || defined (USG) || defined (__SVR4) \
    105  || defined (_UNICOS) || defined(__hpux)
    106 
    107 int
    108 mpfr_get_cputime (void)
    109 {
    110   return (int) ((unsigned long long) clock () * 1000 / CLOCKS_PER_SEC);
    111 }
    112 
    113 #else /* Use getrusage for cputime */
    114 
    115 #include <sys/types.h>
    116 #include <sys/resource.h>
    117 
    118 int
    119 mpfr_get_cputime (void)
    120 {
    121   struct rusage rus;
    122   getrusage (0, &rus);
    123   return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
    124 }
    125 
    126 #endif /* cputime */
    127 
    128 #endif /* MPFR_USE_LOGGING */
    129