2 Fast Artificial Neural Network Library (fann)
3 Copyright (C) 2003 Steffen Nissen (lukesky@diku.dk)
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.
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.
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
29 #define vsnprintf _vsnprintf
30 #define snprintf _snprintf
33 FILE * fann_default_error_log = (FILE *)-1;
35 /* resets the last error number
37 FANN_EXTERNAL void FANN_API fann_reset_errno(struct fann_error *errdat)
39 errdat->errno_f = FANN_E_NO_ERROR;
42 /* resets the last errstr
44 FANN_EXTERNAL void FANN_API fann_reset_errstr(struct fann_error *errdat)
46 if(errdat->errstr != NULL)
48 errdat->errstr = NULL;
51 /* returns the last error number
53 FANN_EXTERNAL enum fann_errno_enum FANN_API fann_get_errno(struct fann_error *errdat)
55 return errdat->errno_f;
58 /* returns the last errstr
60 FANN_EXTERNAL char *FANN_API fann_get_errstr(struct fann_error *errdat)
62 char *errstr = errdat->errstr;
64 fann_reset_errno(errdat);
65 fann_reset_errstr(errdat);
70 /* change where errors are logged to
72 FANN_EXTERNAL void FANN_API fann_set_error_log(struct fann_error *errdat, FILE * log_file)
75 fann_default_error_log = log_file;
77 errdat->error_log = log_file;
80 /* prints the last error to stderr
82 FANN_EXTERNAL void FANN_API fann_print_error(struct fann_error *errdat)
84 if(errdat->errno_f != FANN_E_NO_ERROR && errdat->errstr != NULL)
86 fprintf(stderr, "FANN Error %d: %s", errdat->errno_f, errdat->errstr);
91 Populate the error information
93 void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, ...)
97 FILE * error_log = fann_default_error_log;
100 errdat->errno_f = errno_f;
102 if(errdat != NULL && errdat->errstr != NULL)
104 errstr = errdat->errstr;
108 errstr = (char *) malloc(FANN_ERRSTR_MAX);
111 fprintf(stderr, "Unable to allocate memory.\n");
116 va_start(ap, errno_f);
119 case FANN_E_NO_ERROR:
121 case FANN_E_CANT_OPEN_CONFIG_R:
122 vsprintf(errstr, "Unable to open configuration file \"%s\" for reading.\n", ap);
124 case FANN_E_CANT_OPEN_CONFIG_W:
125 vsprintf(errstr, "Unable to open configuration file \"%s\" for writing.\n", ap);
127 case FANN_E_WRONG_CONFIG_VERSION:
129 "Wrong version of configuration file, aborting read of configuration file \"%s\".\n",
132 case FANN_E_CANT_READ_CONFIG:
133 vsprintf(errstr, "Error reading \"%s\" from configuration file \"%s\".\n", ap);
135 case FANN_E_CANT_READ_NEURON:
136 vsprintf(errstr, "Error reading neuron info from configuration file \"%s\".\n", ap);
138 case FANN_E_CANT_READ_CONNECTIONS:
139 vsprintf(errstr, "Error reading connections from configuration file \"%s\".\n", ap);
141 case FANN_E_WRONG_NUM_CONNECTIONS:
142 vsprintf(errstr, "ERROR connections_so_far=%d, total_connections=%d\n", ap);
144 case FANN_E_CANT_OPEN_TD_W:
145 vsprintf(errstr, "Unable to open train data file \"%s\" for writing.\n", ap);
147 case FANN_E_CANT_OPEN_TD_R:
148 vsprintf(errstr, "Unable to open train data file \"%s\" for writing.\n", ap);
150 case FANN_E_CANT_READ_TD:
151 vsprintf(errstr, "Error reading info from train data file \"%s\", line: %d.\n", ap);
153 case FANN_E_CANT_ALLOCATE_MEM:
154 sprintf(errstr, "Unable to allocate memory.\n");
156 case FANN_E_CANT_TRAIN_ACTIVATION:
157 sprintf(errstr, "Unable to train with the selected activation function.\n");
159 case FANN_E_CANT_USE_ACTIVATION:
160 sprintf(errstr, "Unable to use the selected activation function.\n");
162 case FANN_E_TRAIN_DATA_MISMATCH:
163 sprintf(errstr, "Training data must be of equivalent structure.\n");
165 case FANN_E_CANT_USE_TRAIN_ALG:
166 sprintf(errstr, "Unable to use the selected training algorithm.\n");
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);
171 case FANN_E_INDEX_OUT_OF_BOUND:
172 vsprintf(errstr, "Index %d is out of bound.\n", ap);
179 errdat->errstr = errstr;
180 error_log = errdat->error_log;
181 printf("setting errorlog\n");
184 if(error_log == (FILE *)-1) /* This is the default behavior and will give stderr */
186 fprintf(stderr, "FANN Error %d: %s", errno_f, errstr);
188 else if(error_log != NULL)
190 fprintf(error_log, "FANN Error %d: %s", errno_f, errstr);
195 Initialize an error data strcuture
197 void fann_init_error_data(struct fann_error *errdat)
199 errdat->errstr = NULL;
200 errdat->errno_f = FANN_E_NO_ERROR;
201 errdat->error_log = fann_default_error_log;