1 /*****************************************************************************
2  *                                                                           *
3  *          UNURAN -- Universal Non-Uniform Random number generator          *
4  *                                                                           *
5  *****************************************************************************
6  *                                                                           *
7  *   FILE: debug.c                                                           *
8  *                                                                           *
9  *   debugging routines                                                      *
10  *                                                                           *
11  *****************************************************************************
12  *                                                                           *
13  *   Copyright (c) 2000-2006 Wolfgang Hoermann and Josef Leydold             *
14  *   Department of Statistics and Mathematics, WU Wien, Austria              *
15  *                                                                           *
16  *   This program is free software; you can redistribute it and/or modify    *
17  *   it under the terms of the GNU General Public License as published by    *
18  *   the Free Software Foundation; either version 2 of the License, or       *
19  *   (at your option) any later version.                                     *
20  *                                                                           *
21  *   This program is distributed in the hope that it will be useful,         *
22  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
23  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
24  *   GNU General Public License for more details.                            *
25  *                                                                           *
26  *   You should have received a copy of the GNU General Public License       *
27  *   along with this program; if not, write to the                           *
28  *   Free Software Foundation, Inc.,                                         *
29  *   59 Temple Place, Suite 330, Boston, MA 02111-1307, USA                  *
30  *                                                                           *
31  *****************************************************************************/
32 
33 /*---------------------------------------------------------------------------*/
34 
35 #include <unur_source.h>
36 
37 /*---------------------------------------------------------------------------*/
38 
39 /*****************************************************************************/
40 /**                                                                         **/
41 /**  Set debuging flags                                                     **/
42 /**                                                                         **/
43 /*****************************************************************************/
44 
45 /*---------------------------------------------------------------------------*/
46 
47 /* global variable for default debugging flags */
48 unsigned _unur_default_debugflag = UNUR_DEBUGFLAG_DEFAULT;
49 
50 /*---------------------------------------------------------------------------*/
51 
52 int
unur_set_debug(struct unur_par * par ATTRIBUTE__UNUSED,unsigned debug ATTRIBUTE__UNUSED)53 unur_set_debug( struct unur_par *par ATTRIBUTE__UNUSED,
54 		unsigned debug ATTRIBUTE__UNUSED )
55      /*----------------------------------------------------------------------*/
56      /* set debugging flag for generator                                     */
57      /*                                                                      */
58      /* parameters:                                                          */
59      /*   par   ... pointer to parameter for building generator object       */
60      /*   debug ... debugging flag                                           */
61      /*----------------------------------------------------------------------*/
62 {
63 #ifdef UNUR_ENABLE_LOGGING
64   _unur_check_NULL( NULL,par,UNUR_ERR_NULL );
65   par->debug = debug;
66   return UNUR_SUCCESS;
67 #else
68   _unur_warning("DEBUG",UNUR_ERR_COMPILE,"debugging not enabled");
69   return UNUR_ERR_COMPILE;
70 #endif
71 
72 } /* end of unur_set_debug() */
73 
74 /*---------------------------------------------------------------------------*/
75 
76 int
unur_chg_debug(struct unur_gen * gen ATTRIBUTE__UNUSED,unsigned debug ATTRIBUTE__UNUSED)77 unur_chg_debug( struct unur_gen *gen ATTRIBUTE__UNUSED,
78 		unsigned debug ATTRIBUTE__UNUSED )
79      /*----------------------------------------------------------------------*/
80      /* change debugging flag for generator                                  */
81      /*                                                                      */
82      /* parameters:                                                          */
83      /*   gen   ... pointer to generator object                              */
84      /*   debug ... debugging flag                                           */
85      /*----------------------------------------------------------------------*/
86 {
87 #ifdef UNUR_ENABLE_LOGGING
88   CHECK_NULL( gen, UNUR_ERR_NULL );
89   gen->debug = debug;
90   return UNUR_SUCCESS;
91 #else
92   _unur_warning("DEBUG",UNUR_ERR_COMPILE,"debugging not enabled");
93   return UNUR_ERR_COMPILE;
94 #endif
95 
96 } /* end of unur_chg_debug() */
97 
98 /*---------------------------------------------------------------------------*/
99 
100 int
unur_set_default_debug(unsigned debug)101 unur_set_default_debug( unsigned debug )
102      /*----------------------------------------------------------------------*/
103      /* set default debugging flag for generator                             */
104      /*                                                                      */
105      /* parameters:                                                          */
106      /*   par   ... pointer to parameter for building generator object       */
107      /*   debug ... debugging flag                                           */
108      /*----------------------------------------------------------------------*/
109 {
110   _unur_default_debugflag = debug;
111   return UNUR_SUCCESS;
112 } /* end of unur_set_default_debug() */
113 
114 /*---------------------------------------------------------------------------*/
115 
116 char *
_unur_make_genid(const char * gentype)117 _unur_make_genid( const char *gentype )
118      /*----------------------------------------------------------------------*/
119      /* make a new generator identifier                                      */
120      /*                                                                      */
121      /* parameters:                                                          */
122      /*   gentype ... type of generator                                      */
123      /*                                                                      */
124      /* return:                                                              */
125      /*   pointer generator id (char string)                                 */
126      /*----------------------------------------------------------------------*/
127 {
128   static int count = 0;   /* counter for identifiers */
129   char *genid;
130   size_t len;
131 
132   /* allocate memory for identifier */
133   len = strlen(gentype);
134   genid = _unur_xmalloc(sizeof(char)*(len+5));
135 
136   /* make new identifier */
137   ++count; count %= 1000;
138   /* 1000 different generators should be enough */
139 
140 #if HAVE_DECL_SNPRINTF
141   /* this is a GNU extension */
142   snprintf(genid, len+5, "%s.%03d", gentype, count);
143 #else
144   sprintf(genid, "%s.%03d", gentype, count);
145 #endif
146 
147   return genid;
148 
149 } /* end of _unur_make_genid() */
150 
151 /*---------------------------------------------------------------------------*/
152 
153