xref: /dflybsd-src/contrib/mpfr/src/logging.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* MPFR Logging functions.
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino 
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino 
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino 
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino 
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino 
234a238c70SJohn Marino #include "mpfr-impl.h"
244a238c70SJohn Marino 
254a238c70SJohn Marino /* Logging MPFR needs GCC >= 3.0 and GLIBC >= 2.0. */
264a238c70SJohn Marino 
274a238c70SJohn Marino #ifdef MPFR_USE_LOGGING
284a238c70SJohn Marino 
294a238c70SJohn Marino /* Can't include them before (in particular, printf.h) */
304a238c70SJohn Marino #include <stdlib.h>
314a238c70SJohn Marino #include <stdarg.h>
324a238c70SJohn Marino #include <time.h>
334a238c70SJohn Marino 
344a238c70SJohn Marino /* Define LOGGING variables */
354a238c70SJohn Marino 
364a238c70SJohn Marino FILE *mpfr_log_file;
374a238c70SJohn Marino int   mpfr_log_type;
384a238c70SJohn Marino int   mpfr_log_level;
394a238c70SJohn Marino int   mpfr_log_current;
404a238c70SJohn Marino int   mpfr_log_worstcase_limit;
414a238c70SJohn Marino mpfr_prec_t mpfr_log_prec;
424a238c70SJohn Marino 
434a238c70SJohn Marino static void mpfr_log_begin (void) __attribute__((constructor));
444a238c70SJohn Marino 
454a238c70SJohn Marino /* We let the system close the LOG itself
464a238c70SJohn Marino    (Otherwise functions called by destructor can't use LOG File */
474a238c70SJohn Marino static void
mpfr_log_begin(void)484a238c70SJohn Marino mpfr_log_begin (void)
494a238c70SJohn Marino {
504a238c70SJohn Marino   const char *var;
514a238c70SJohn Marino   time_t tt;
524a238c70SJohn Marino 
534a238c70SJohn Marino   /* Grab some information */
544a238c70SJohn Marino   var = getenv ("MPFR_LOG_LEVEL");
554a238c70SJohn Marino   mpfr_log_level = var == NULL || *var == 0 ? 7 : atoi (var);
564a238c70SJohn Marino   mpfr_log_current = 0;
574a238c70SJohn Marino 
584a238c70SJohn Marino   var = getenv ("MPFR_LOG_PREC");
594a238c70SJohn Marino   mpfr_log_prec = var == NULL ? 6 : atol (var);
604a238c70SJohn Marino 
614a238c70SJohn Marino   /* Get what we need to log */
624a238c70SJohn Marino   mpfr_log_type = 0;
634a238c70SJohn Marino   if (getenv ("MPFR_LOG_INPUT") != NULL)
644a238c70SJohn Marino     mpfr_log_type |= MPFR_LOG_INPUT_F;
654a238c70SJohn Marino   if (getenv ("MPFR_LOG_OUTPUT") != NULL)
664a238c70SJohn Marino     mpfr_log_type |= MPFR_LOG_OUTPUT_F;
674a238c70SJohn Marino   if (getenv ("MPFR_LOG_TIME") != NULL)
684a238c70SJohn Marino     mpfr_log_type |= MPFR_LOG_TIME_F;
694a238c70SJohn Marino   if (getenv ("MPFR_LOG_INTERNAL") != NULL)
704a238c70SJohn Marino     mpfr_log_type |= MPFR_LOG_INTERNAL_F;
714a238c70SJohn Marino   if (getenv ("MPFR_LOG_MSG") != NULL)
724a238c70SJohn Marino     mpfr_log_type |= MPFR_LOG_MSG_F;
734a238c70SJohn Marino   if (getenv ("MPFR_LOG_ZIV") != NULL)
744a238c70SJohn Marino     mpfr_log_type |= MPFR_LOG_BADCASE_F;
754a238c70SJohn Marino   if (getenv ("MPFR_LOG_STAT") != NULL)
764a238c70SJohn Marino     mpfr_log_type |= MPFR_LOG_STAT_F;
774a238c70SJohn Marino   if (getenv ("MPFR_LOG_ALL") != NULL)
784a238c70SJohn Marino     mpfr_log_type = MPFR_LOG_INPUT_F|MPFR_LOG_OUTPUT_F|MPFR_LOG_TIME_F
794a238c70SJohn Marino       |MPFR_LOG_INTERNAL_F|MPFR_LOG_MSG_F|MPFR_LOG_BADCASE_F|MPFR_LOG_STAT_F;
804a238c70SJohn Marino 
814a238c70SJohn Marino   /* Open filename if needed */
824a238c70SJohn Marino   var = getenv ("MPFR_LOG_FILE");
834a238c70SJohn Marino   if (var == NULL || *var == 0)
844a238c70SJohn Marino     var = "mpfr.log";
854a238c70SJohn Marino   if (mpfr_log_type != 0)
864a238c70SJohn Marino     {
874a238c70SJohn Marino       mpfr_log_file = fopen (var, "w");
884a238c70SJohn Marino       if (mpfr_log_file == NULL)
894a238c70SJohn Marino         {
904a238c70SJohn Marino           fprintf (stderr, "MPFR LOG: Can't open '%s' with w.\n", var);
914a238c70SJohn Marino           abort ();
924a238c70SJohn Marino         }
934a238c70SJohn Marino       time (&tt);
944a238c70SJohn Marino       fprintf (mpfr_log_file, "MPFR LOG FILE %s\n", ctime (&tt));
954a238c70SJohn Marino     }
964a238c70SJohn Marino }
974a238c70SJohn Marino 
984a238c70SJohn Marino /* Return user CPU time measured in milliseconds. Thanks to Torbjorn. */
994a238c70SJohn Marino 
1004a238c70SJohn Marino #if defined (ANSIONLY) || defined (USG) || defined (__SVR4) \
1014a238c70SJohn Marino  || defined (_UNICOS) || defined(__hpux)
1024a238c70SJohn Marino 
1034a238c70SJohn Marino int
mpfr_get_cputime(void)1044a238c70SJohn Marino mpfr_get_cputime (void)
1054a238c70SJohn Marino {
1064a238c70SJohn Marino   return (int) ((unsigned long long) clock () * 1000 / CLOCKS_PER_SEC);
1074a238c70SJohn Marino }
1084a238c70SJohn Marino 
1094a238c70SJohn Marino #else /* Use getrusage for cputime */
1104a238c70SJohn Marino 
1114a238c70SJohn Marino #include <sys/types.h>
1124a238c70SJohn Marino #include <sys/resource.h>
1134a238c70SJohn Marino 
1144a238c70SJohn Marino int
mpfr_get_cputime(void)1154a238c70SJohn Marino mpfr_get_cputime (void)
1164a238c70SJohn Marino {
1174a238c70SJohn Marino   struct rusage rus;
1184a238c70SJohn Marino   getrusage (0, &rus);
1194a238c70SJohn Marino   return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
1204a238c70SJohn Marino }
1214a238c70SJohn Marino 
1224a238c70SJohn Marino #endif /* cputime */
1234a238c70SJohn Marino 
1244a238c70SJohn Marino #endif /* MPFR_USE_LOGGING */
125