1 /* ------------------------------------------------------------- */
2 /* File: example_errorhandler.c                                  */
3 /* ------------------------------------------------------------- */
4 
5 /* Include UNURAN header file.                                   */
6 #include <unuran.h>
7 
8 /* ------------------------------------------------------------- */
9 
10 /* Simple example of an error handler that simply prints a       */
11 /* message to stderr.                                            */
12 
13 /* ------------------------------------------------------------- */
14 
my_error_handler(const char * objid,const char * file,int line,const char * errortype,int errorcode,const char * reason)15 void my_error_handler(
16         const char *objid,     /* id/type of object              */
17 	const char *file,      /* source file name (__FILE__)    */
18 	int line,              /* source line number (__LINE__)  */
19 	const char *errortype, /* "warning" or "error"           */
20 	int errorcode,         /* UNU.RAN error code             */
21 	const char *reason     /* short description of reason    */
22      )
23 {
24   FILE *LOG = stderr;
25   static int n = 0;
26 
27   fprintf(LOG,"\n");
28   fprintf(LOG,"[[ %d ]] my_error_handler: [ %s ]\n",++n,errortype);
29   fprintf(LOG,"\tobject = %s\n",objid);
30   fprintf(LOG,"\tfile   = %s\n",file);
31   fprintf(LOG,"\tline   = %d\n",line);
32   fprintf(LOG,"\tcode   = [%#x] %s\n",errorcode,
33 	                             unur_get_strerror(errorcode));
34   fprintf(LOG,"\treason = %s\n",reason);
35   fprintf(LOG,"\n");
36 
37 } /* end of my_error_handler() */
38 
39 /* ------------------------------------------------------------- */
40 
main(void)41 int main(void)
42 {
43   /* Declare UNURAN object.                                      */
44   UNUR_GEN   *gen;      /* generator object                      */
45 
46   /* Declare a point to hold old error handler.                  */
47   UNUR_ERROR_HANDLER *default_error_handler = NULL;
48 
49   /* Set new error handler.                                      */
50   default_error_handler = unur_set_error_handler( my_error_handler );
51 
52   /* The following statement causes an error */
53   gen = unur_str2gen("normal(0.,-1)");
54 
55   exit (EXIT_SUCCESS);
56 
57 } /* end of main() */
58 
59 /* ------------------------------------------------------------- */
60 
61