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