Merge branch 'vendor/FILE'
[dragonfly.git] / contrib / mpfr / logging.c
1 /* MPFR Logging functions.
2
3 Copyright 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 2.1 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.LIB.  If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 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 <printf.h>
32 #include <stdarg.h>
33 #include <time.h>
34
35 /* Define LOGGING variables */
36
37 FILE *mpfr_log_file;
38 int   mpfr_log_type;
39 int   mpfr_log_level;
40 int   mpfr_log_base;
41 int   mpfr_log_current;
42 int   mpfr_log_worstcase_limit;
43 mp_prec_t mpfr_log_prec;
44
45 static int
46 mpfr_printf_mpfr_print (FILE *stream, const struct printf_info *info,
47                         const void * const *arg)
48 {
49   int length;
50   int org_type_logging;
51
52   /* TODO: Use much more flag from info  */
53   mpfr_srcptr w = *((mpfr_srcptr *) (arg[0]));
54   mp_prec_t prec = mpfr_log_prec != 0 ? mpfr_log_prec
55     : info->width == -1 ? 0 : (mp_prec_t) info->width;
56
57   org_type_logging = mpfr_log_type;
58   mpfr_log_type = 0; /* We disable the logging during this print! */
59   if (info->alt)
60     length = fprintf (stream, "%lu", (unsigned long) MPFR_PREC (w));
61   else
62     length = mpfr_out_str (stream, mpfr_log_base, prec, w, GMP_RNDN);
63   mpfr_log_type = org_type_logging;
64
65   return length;
66 }
67
68 static int
69 mpfr_printf_mpfr_arginfo (const struct printf_info *info, size_t n,
70                           int *argtypes)
71 {
72   if (n > 0)
73     argtypes[0] = PA_POINTER;
74   return 1;
75 }
76
77 static void mpfr_log_begin (void) __attribute__((constructor));
78
79 /* We let the system close the LOG itself
80    (Otherwise functions called by destructor can't use LOG File */
81 static void
82 mpfr_log_begin (void)
83 {
84   const char *var;
85   time_t tt;
86
87   /* Grab some information */
88   var = getenv ("MPFR_LOG_BASE");
89   mpfr_log_base = var == NULL || *var == 0 ? 10 : atoi (var);
90
91   var = getenv ("MPFR_LOG_LEVEL");
92   mpfr_log_level = var == NULL || *var == 0 ? 7 : atoi (var);
93   mpfr_log_current = 0;
94
95   var = getenv ("MPFR_LOG_PREC");
96   mpfr_log_prec = var == NULL || *var == 0 ? 0 : atol (var);
97
98   /* Get what we need to log */
99   mpfr_log_type = 0;
100   if (getenv ("MPFR_LOG_INPUT") != NULL)
101     mpfr_log_type |= MPFR_LOG_INPUT_F;
102   if (getenv ("MPFR_LOG_OUTPUT") != NULL)
103     mpfr_log_type |= MPFR_LOG_OUTPUT_F;
104   if (getenv ("MPFR_LOG_TIME") != NULL)
105     mpfr_log_type |= MPFR_LOG_TIME_F;
106   if (getenv ("MPFR_LOG_INTERNAL") != NULL)
107     mpfr_log_type |= MPFR_LOG_INTERNAL_F;
108   if (getenv ("MPFR_LOG_MSG") != NULL)
109     mpfr_log_type |= MPFR_LOG_MSG_F;
110   if (getenv ("MPFR_LOG_ZIV") != NULL)
111     mpfr_log_type |= MPFR_LOG_BADCASE_F;
112   if (getenv ("MPFR_LOG_STAT") != NULL)
113     mpfr_log_type |= MPFR_LOG_STAT_F;
114   if (getenv ("MPFR_LOG_ALL") != NULL)
115     mpfr_log_type = MPFR_LOG_INPUT_F|MPFR_LOG_OUTPUT_F|MPFR_LOG_TIME_F
116       |MPFR_LOG_INTERNAL_F|MPFR_LOG_MSG_F|MPFR_LOG_BADCASE_F|MPFR_LOG_STAT_F;
117
118   /* Register printf functions */
119   register_printf_function ('R', mpfr_printf_mpfr_print,
120                             mpfr_printf_mpfr_arginfo);
121
122   /* Open filename if needed */
123   var = getenv ("MPFR_LOG_FILE");
124   if (var == NULL || *var == 0)
125     var = "mpfr.log";
126   if (mpfr_log_type != 0)
127     {
128       mpfr_log_file = fopen (var, "w");
129       if (mpfr_log_file == NULL)
130         {
131           fprintf (stderr, "MPFR LOG: Can't open '%s' with w.\n", var);
132           abort ();
133         }
134       time (&tt);
135       fprintf (mpfr_log_file, "MPFR LOG FILE %s\n", ctime (&tt));
136     }
137 }
138
139 /* Return user CPU time measured in milliseconds. Thanks to Torbjorn. */
140
141 #if defined (ANSIONLY) || defined (USG) || defined (__SVR4) \
142  || defined (_UNICOS) || defined(__hpux)
143
144 int
145 mpfr_get_cputime (void)
146 {
147   return (int) ((unsigned long long) clock () * 1000 / CLOCKS_PER_SEC);
148 }
149
150 #else /* Use getrusage for cputime */
151
152 #include <sys/types.h>
153 #include <sys/resource.h>
154
155 int
156 mpfr_get_cputime (void)
157 {
158   struct rusage rus;
159   getrusage (0, &rus);
160   return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
161 }
162
163 #endif /* cputime */
164
165 #endif /* MPFR_USE_LOGGING */