~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/logging.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* MPFR Logging functions.
 
2
 
 
3
Copyright 2005 Free Software Foundation, Inc.
 
4
 
 
5
This file is part of the MPFR Library.
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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. */
 
21
 
 
22
#include <stdio.h>
 
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
# if !__MPFR_GLIBC(2,0)
 
36
#  error "Logging not supported. (Glibc >= 2.0)"
 
37
# endif
 
38
 
 
39
/* Define LOGGING variables */
 
40
 
 
41
FILE *mpfr_log_file;
 
42
int   mpfr_log_type;
 
43
int   mpfr_log_level;
 
44
int   mpfr_log_base;
 
45
int   mpfr_log_current;
 
46
int   mpfr_log_worstcase_limit;
 
47
mp_prec_t mpfr_log_prec;
 
48
 
 
49
static int
 
50
mpfr_printf_mpfr_print (FILE *stream, const struct printf_info *info,
 
51
                        const void * const *arg)
 
52
{
 
53
  int length;
 
54
  int org_type_logging;
 
55
 
 
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;
 
60
 
 
61
  org_type_logging = mpfr_log_type;
 
62
  mpfr_log_type = 0; /* We disable the logging during this print! */
 
63
  if (info->alt)
 
64
    length = fprintf (stream, "%lu", MPFR_PREC (w));
 
65
  else
 
66
    length = mpfr_out_str (stream, mpfr_log_base, prec, w, GMP_RNDN);
 
67
  mpfr_log_type = org_type_logging;
 
68
 
 
69
  return length;
 
70
}
 
71
 
 
72
static int
 
73
mpfr_printf_mpfr_arginfo (const struct printf_info *info, size_t n,
 
74
                          int *argtypes)
 
75
{
 
76
  if (n > 0)
 
77
    argtypes[0] = PA_POINTER;
 
78
  return 1;
 
79
}
 
80
 
 
81
static void mpfr_log_begin (void) __attribute__((constructor));
 
82
 
 
83
/* We let the system close the LOG itself
 
84
   (Otherwise functions called by destructor can't use LOG File */
 
85
static void
 
86
mpfr_log_begin (void)
 
87
{
 
88
  const char *filename;
 
89
  time_t tt;
 
90
 
 
91
  /* Grab some information */
 
92
  filename = getenv ("MPFR_LOG_BASE");
 
93
  mpfr_log_base = filename == NULL || *filename == 0 ? 10 : atoi (filename);
 
94
 
 
95
  filename = getenv ("MPFR_LOG_LEVEL");
 
96
  mpfr_log_level = filename == NULL || *filename == 0 ? 7 : atoi (filename);
 
97
  mpfr_log_current = 0;
 
98
 
 
99
  filename = getenv ("MPFR_LOG_PREC");
 
100
  mpfr_log_prec = filename == NULL || *filename == 0 ? 0 : atol (filename);
 
101
 
 
102
  /* Get what we need to log */
 
103
  mpfr_log_type = 0;
 
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;
 
121
 
 
122
  /* Register printf functions */
 
123
  register_printf_function ('R', mpfr_printf_mpfr_print,
 
124
                            mpfr_printf_mpfr_arginfo);
 
125
 
 
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)
 
130
    {
 
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);
 
134
        abort ();
 
135
      }
 
136
      time (&tt);
 
137
      fprintf (mpfr_log_file, "MPFR LOG FILE %s\n", ctime (&tt));
 
138
    }
 
139
}
 
140
 
 
141
/* Return user CPU time measured in milliseconds. Thanks to Torbjorn. */
 
142
 
 
143
#if defined (ANSIONLY) || defined (USG) || defined (__SVR4) \
 
144
 || defined (_UNICOS) || defined(__hpux)
 
145
 
 
146
int mpfr_get_cputime (void) {
 
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 mpfr_get_cputime (void) {
 
156
  struct rusage rus;
 
157
  getrusage (0, &rus);
 
158
  return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
 
159
}
 
160
 
 
161
#endif /* cputime */
 
162
 
 
163
#endif /* MPFR_USE_LOGGING */