]> ruin.nu Git - germs.git/blob - fann/src/fann_error.c
Make it possible to build statically against the included fann library.
[germs.git] / fann / src / fann_error.c
1 /*
2   Fast Artificial Neural Network Library (fann)
3   Copyright (C) 2003 Steffen Nissen (lukesky@diku.dk)
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include "config.h"
26 #include "fann.h"
27
28 #ifdef _MSC_VER
29 #define vsnprintf _vsnprintf
30 #define snprintf _snprintf
31 #endif
32
33 FILE * fann_default_error_log = (FILE *)-1;
34
35 /* resets the last error number
36  */
37 FANN_EXTERNAL void FANN_API fann_reset_errno(struct fann_error *errdat)
38 {
39         errdat->errno_f = FANN_E_NO_ERROR;
40 }
41
42 /* resets the last errstr
43  */
44 FANN_EXTERNAL void FANN_API fann_reset_errstr(struct fann_error *errdat)
45 {
46         if(errdat->errstr != NULL)
47                 free(errdat->errstr);
48         errdat->errstr = NULL;
49 }
50
51 /* returns the last error number
52  */
53 FANN_EXTERNAL enum fann_errno_enum FANN_API fann_get_errno(struct fann_error *errdat)
54 {
55         return errdat->errno_f;
56 }
57
58 /* returns the last errstr
59  */
60 FANN_EXTERNAL char *FANN_API fann_get_errstr(struct fann_error *errdat)
61 {
62         char *errstr = errdat->errstr;
63
64         fann_reset_errno(errdat);
65         fann_reset_errstr(errdat);
66
67         return errstr;
68 }
69
70 /* change where errors are logged to
71  */
72 FANN_EXTERNAL void FANN_API fann_set_error_log(struct fann_error *errdat, FILE * log_file)
73 {
74         if(errdat == NULL)
75                 fann_default_error_log = log_file;
76         else
77                 errdat->error_log = log_file;
78 }
79
80 /* prints the last error to stderr
81  */
82 FANN_EXTERNAL void FANN_API fann_print_error(struct fann_error *errdat)
83 {
84         if(errdat->errno_f != FANN_E_NO_ERROR && errdat->errstr != NULL)
85         {
86                 fprintf(stderr, "FANN Error %d: %s", errdat->errno_f, errdat->errstr);
87         }
88 }
89
90 /* INTERNAL FUNCTION
91    Populate the error information
92  */
93 void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, ...)
94 {
95         va_list ap;
96         char *errstr;
97         FILE * error_log = fann_default_error_log;
98
99         if(errdat != NULL)
100                 errdat->errno_f = errno_f;
101
102         if(errdat != NULL && errdat->errstr != NULL)
103         {
104                 errstr = errdat->errstr;
105         }
106         else
107         {
108                 errstr = (char *) malloc(FANN_ERRSTR_MAX);
109                 if(errstr == NULL)
110                 {
111                         fprintf(stderr, "Unable to allocate memory.\n");
112                         return;
113                 }
114         }
115
116         va_start(ap, errno_f);
117         switch (errno_f)
118         {
119         case FANN_E_NO_ERROR:
120                 break;
121         case FANN_E_CANT_OPEN_CONFIG_R:
122                 vsprintf(errstr, "Unable to open configuration file \"%s\" for reading.\n", ap);
123                 break;
124         case FANN_E_CANT_OPEN_CONFIG_W:
125                 vsprintf(errstr, "Unable to open configuration file \"%s\" for writing.\n", ap);
126                 break;
127         case FANN_E_WRONG_CONFIG_VERSION:
128                 vsprintf(errstr,
129                                  "Wrong version of configuration file, aborting read of configuration file \"%s\".\n",
130                                  ap);
131                 break;
132         case FANN_E_CANT_READ_CONFIG:
133                 vsprintf(errstr, "Error reading \"%s\" from configuration file \"%s\".\n", ap);
134                 break;
135         case FANN_E_CANT_READ_NEURON:
136                 vsprintf(errstr, "Error reading neuron info from configuration file \"%s\".\n", ap);
137                 break;
138         case FANN_E_CANT_READ_CONNECTIONS:
139                 vsprintf(errstr, "Error reading connections from configuration file \"%s\".\n", ap);
140                 break;
141         case FANN_E_WRONG_NUM_CONNECTIONS:
142                 vsprintf(errstr, "ERROR connections_so_far=%d, total_connections=%d\n", ap);
143                 break;
144         case FANN_E_CANT_OPEN_TD_W:
145                 vsprintf(errstr, "Unable to open train data file \"%s\" for writing.\n", ap);
146                 break;
147         case FANN_E_CANT_OPEN_TD_R:
148                 vsprintf(errstr, "Unable to open train data file \"%s\" for writing.\n", ap);
149                 break;
150         case FANN_E_CANT_READ_TD:
151                 vsprintf(errstr, "Error reading info from train data file \"%s\", line: %d.\n", ap);
152                 break;
153         case FANN_E_CANT_ALLOCATE_MEM:
154                 sprintf(errstr, "Unable to allocate memory.\n");
155                 break;
156         case FANN_E_CANT_TRAIN_ACTIVATION:
157                 sprintf(errstr, "Unable to train with the selected activation function.\n");
158                 break;
159         case FANN_E_CANT_USE_ACTIVATION:
160                 sprintf(errstr, "Unable to use the selected activation function.\n");
161                 break;
162         case FANN_E_TRAIN_DATA_MISMATCH:
163                 sprintf(errstr, "Training data must be of equivalent structure.\n");
164                 break;
165         case FANN_E_CANT_USE_TRAIN_ALG:
166                 sprintf(errstr, "Unable to use the selected training algorithm.\n");
167                 break;
168         case FANN_E_TRAIN_DATA_SUBSET:
169                 vsprintf(errstr, "Subset from %d of length %d not valid in training set of length %d.\n", ap);
170                 break;
171         case FANN_E_INDEX_OUT_OF_BOUND:
172                 vsprintf(errstr, "Index %d is out of bound.\n", ap);
173                 break;
174         }
175         va_end(ap);
176
177         if(errdat != NULL)
178         {
179                 errdat->errstr = errstr;
180                 error_log = errdat->error_log;
181                 printf("setting errorlog\n");           
182         }
183
184         if(error_log == (FILE *)-1) /* This is the default behavior and will give stderr */
185         {
186                 fprintf(stderr, "FANN Error %d: %s", errno_f, errstr);
187         }
188         else if(error_log != NULL)
189         {
190                 fprintf(error_log, "FANN Error %d: %s", errno_f, errstr);
191         }
192 }
193
194 /* INTERNAL FUNCTION
195    Initialize an error data strcuture
196  */
197 void fann_init_error_data(struct fann_error *errdat)
198 {
199         errdat->errstr = NULL;
200         errdat->errno_f = FANN_E_NO_ERROR;
201         errdat->error_log = fann_default_error_log;
202 }