1
/* MPFR Logging functions.
3
Copyright 2005 Free Software Foundation, Inc.
5
This file is part of the MPFR Library.
7
The MPFR Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The MPFR Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the MPFR Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
23
#include "mpfr-impl.h"
25
/* Logging MPFR needs GCC >= 3.0 and GLIBC >= 2.0. */
27
#ifdef MPFR_USE_LOGGING
29
/* Can't include them before (in particular, printf.h) */
35
# if !__MPFR_GLIBC(2,0)
36
# error "Logging not supported. (Glibc >= 2.0)"
39
/* Define LOGGING variables */
46
int mpfr_log_worstcase_limit;
47
mp_prec_t mpfr_log_prec;
50
mpfr_printf_mpfr_print (FILE *stream, const struct printf_info *info,
51
const void * const *arg)
56
/* TODO: Use much more flag from info */
57
mpfr_srcptr w = *((mpfr_srcptr *) (arg[0]));
58
mp_prec_t prec = mpfr_log_prec != 0 ? mpfr_log_prec
59
: info->width == -1 ? 0 : (mp_prec_t) info->width;
61
org_type_logging = mpfr_log_type;
62
mpfr_log_type = 0; /* We disable the logging during this print! */
64
length = fprintf (stream, "%lu", MPFR_PREC (w));
66
length = mpfr_out_str (stream, mpfr_log_base, prec, w, GMP_RNDN);
67
mpfr_log_type = org_type_logging;
73
mpfr_printf_mpfr_arginfo (const struct printf_info *info, size_t n,
77
argtypes[0] = PA_POINTER;
81
static void mpfr_log_begin (void) __attribute__((constructor));
83
/* We let the system close the LOG itself
84
(Otherwise functions called by destructor can't use LOG File */
91
/* Grab some information */
92
filename = getenv ("MPFR_LOG_BASE");
93
mpfr_log_base = filename == NULL || *filename == 0 ? 10 : atoi (filename);
95
filename = getenv ("MPFR_LOG_LEVEL");
96
mpfr_log_level = filename == NULL || *filename == 0 ? 7 : atoi (filename);
99
filename = getenv ("MPFR_LOG_PREC");
100
mpfr_log_prec = filename == NULL || *filename == 0 ? 0 : atol (filename);
102
/* Get what we need to log */
104
if (getenv ("MPFR_LOG_INPUT") != NULL)
105
mpfr_log_type |= MPFR_LOG_INPUT_F;
106
if (getenv ("MPFR_LOG_OUTPUT") != NULL)
107
mpfr_log_type |= MPFR_LOG_OUTPUT_F;
108
if (getenv ("MPFR_LOG_TIME") != NULL)
109
mpfr_log_type |= MPFR_LOG_TIME_F;
110
if (getenv ("MPFR_LOG_INTERNAL") != NULL)
111
mpfr_log_type |= MPFR_LOG_INTERNAL_F;
112
if (getenv ("MPFR_LOG_MSG") != NULL)
113
mpfr_log_type |= MPFR_LOG_MSG_F;
114
if (getenv ("MPFR_LOG_ZIV") != NULL)
115
mpfr_log_type |= MPFR_LOG_BADCASE_F;
116
if (getenv ("MPFR_LOG_STAT") != NULL)
117
mpfr_log_type |= MPFR_LOG_STAT_F;
118
if (getenv ("MPFR_LOG_ALL") != NULL)
119
mpfr_log_type = MPFR_LOG_INPUT_F|MPFR_LOG_OUTPUT_F|MPFR_LOG_TIME_F
120
|MPFR_LOG_INTERNAL_F|MPFR_LOG_MSG_F|MPFR_LOG_BADCASE_F|MPFR_LOG_STAT_F;
122
/* Register printf functions */
123
register_printf_function ('R', mpfr_printf_mpfr_print,
124
mpfr_printf_mpfr_arginfo);
126
/* Open filename if needed */
127
filename = getenv ("MPFR_LOG_FILE");
128
filename = filename != NULL && *filename != 0 ? filename : "./mpfr.log";
129
if (mpfr_log_type != 0)
131
mpfr_log_file = fopen (filename, "w");
132
if (mpfr_log_file == NULL) {
133
fprintf (stderr, "MPFR LOG: Can't open '%s' with w.\n", filename);
137
fprintf (mpfr_log_file, "MPFR LOG FILE %s\n", ctime (&tt));
141
/* Return user CPU time measured in milliseconds. Thanks to Torbjorn. */
143
#if defined (ANSIONLY) || defined (USG) || defined (__SVR4) \
144
|| defined (_UNICOS) || defined(__hpux)
146
int mpfr_get_cputime (void) {
147
return (int) ((unsigned long long) clock () * 1000 / CLOCKS_PER_SEC);
150
#else /* Use getrusage for cputime */
152
#include <sys/types.h>
153
#include <sys/resource.h>
155
int mpfr_get_cputime (void) {
158
return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
163
#endif /* MPFR_USE_LOGGING */