1 /*
2  * This file is part of the Advance project.
3  *
4  * Copyright (C) 1999, 2000, 2001, 2002, 2003 Andrea Mazzoleni
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * In addition, as a special exception, Andrea Mazzoleni
21  * gives permission to link the code of this program with
22  * the MAME library (or with modified versions of MAME that use the
23  * same license as MAME), and distribute linked combinations including
24  * the two.  You must obey the GNU General Public License in all
25  * respects for all of the code used other than MAME.  If you modify
26  * this file, you may extend this exception to your version of the
27  * file, but you are not obligated to do so.  If you do not wish to
28  * do so, delete this exception statement from your version.
29  */
30 
31 #include "portable.h"
32 
33 #include "extra.h"
34 
35 #ifndef USE_ERROR_SILENT
36 #include "log.h"
37 #endif
38 
39 #include "error.h"
40 #include "snstring.h"
41 
42 /****************************************************************************/
43 /* Error */
44 
45 /**
46  * Max length of the error description.
47  */
48 #define ERROR_DESC_MAX 2048
49 
50 /**
51  * Last error description.
52  */
53 static char error_desc_buffer[ERROR_DESC_MAX];
54 
55 /**
56  * Flag set if an unsupported feature is found.
57  */
58 static adv_bool error_unsupported_flag;
59 
60 /**
61  * Flag for cat mode.
62  */
63 static adv_bool error_cat_flag;
64 
65 /**
66  * Prefix for cat mode.
67  */
68 static char error_cat_prefix_buffer[ERROR_DESC_MAX];
69 
70 /**
71  * Set the error cat mode.
72  * If enabled the error text is appended at the end of the previous error.
73  */
error_cat_set(const char * prefix,adv_bool mode)74 void error_cat_set(const char* prefix, adv_bool mode)
75 {
76 	if (prefix)
77 		sncpy(error_cat_prefix_buffer, sizeof(error_cat_prefix_buffer), prefix);
78 	else
79 		error_cat_prefix_buffer[0] = 0;
80 	error_cat_flag = mode;
81 }
82 
83 /**
84  * Get the current error description.
85  */
error_get(void)86 const char* error_get(void)
87 {
88 	/* remove the trailing \n */
89 	while (error_desc_buffer[0] && isspace(error_desc_buffer[strlen(error_desc_buffer)-1]))
90 		error_desc_buffer[strlen(error_desc_buffer)-1] = 0;
91 	return error_desc_buffer;
92 }
93 
94 /**
95  * Reset the description of the last error.
96  */
error_reset(void)97 void error_reset(void)
98 {
99 	error_unsupported_flag = 0;
100 	error_desc_buffer[0] = 0;
101 }
102 
103 /**
104  * Set the description of the last error.
105  * The previous description is overwritten.
106  * \note The description IS logged.
107  */
error_set(const char * text,...)108 void error_set(const char* text, ...)
109 {
110 	va_list arg;
111 	char* p;
112 	unsigned size;
113 
114 	error_unsupported_flag = 0;
115 
116 	if (error_cat_flag) {
117 		if (error_cat_prefix_buffer[0]) {
118 			sncat(error_desc_buffer, sizeof(error_desc_buffer), error_cat_prefix_buffer);
119 			sncat(error_desc_buffer, sizeof(error_desc_buffer), ": ");
120 		}
121 		p = error_desc_buffer + strlen(error_desc_buffer);
122 		size = sizeof(error_desc_buffer) - strlen(error_desc_buffer);
123 	} else {
124 		p = error_desc_buffer;
125 		size = sizeof(error_desc_buffer);
126 	}
127 
128 	va_start(arg, text);
129 	vsnprintf(p, size, text, arg);
130 
131 #ifndef USE_ERROR_SILENT
132 	log_std(("advance:msg:"));
133 	if (error_cat_flag && error_cat_prefix_buffer[0]) {
134 		log_std(("%s:", error_cat_prefix_buffer));
135 	}
136 	log_std((" "));
137 	log_va(text, arg);
138 	if (!text[0] || text[strlen(text)-1] != '\n')
139 		log_std(("\n"));
140 #endif
141 
142 	va_end(arg);
143 }
144 
145 /**
146  * Set the description of the last error due unsupported feature.
147  * \note The description IS logged.
148  */
error_unsupported_set(const char * text,...)149 void error_unsupported_set(const char* text, ...)
150 {
151 	va_list arg;
152 
153 	error_unsupported_flag = 1;
154 
155 	va_start(arg, text);
156 	vsnprintf(error_desc_buffer, sizeof(error_desc_buffer), text, arg);
157 
158 #ifndef USE_ERROR_SILENT
159 	log_std(("advance: set_error_description \""));
160 	log_va(text, arg);
161 	log_std(("\"\n"));
162 #endif
163 
164 	va_end(arg);
165 }
166 
167 /**
168  * Check if a unsupported feature is found.
169  * This function can be called only if another function returns with error.
170  * \return
171  *  - ==0 Not found.
172  *  - !=0 Unsupported feature found.
173  */
error_unsupported_get(void)174 adv_bool error_unsupported_get(void)
175 {
176 	return error_unsupported_flag;
177 }
178 
179 #ifndef USE_ERROR_SILENT
180 /**
181  * Set the error description.
182  * The previous description is overwritten.
183  * The description is not logged.
184  */
error_nolog_set(const char * text,...)185 void error_nolog_set(const char* text, ...)
186 {
187 	va_list arg;
188 	char* p;
189 	unsigned size;
190 
191 	error_unsupported_flag = 0;
192 
193 	if (error_cat_flag) {
194 		if (error_cat_prefix_buffer[0]) {
195 			sncat(error_desc_buffer, sizeof(error_desc_buffer), error_cat_prefix_buffer);
196 			sncat(error_desc_buffer, sizeof(error_desc_buffer), ": ");
197 		}
198 		p = error_desc_buffer + strlen(error_desc_buffer);
199 		size = sizeof(error_desc_buffer) - strlen(error_desc_buffer);
200 	} else {
201 		p = error_desc_buffer;
202 		size = sizeof(error_desc_buffer);
203 	}
204 
205 	va_start(arg, text);
206 	vsnprintf(p, size, text, arg);
207 	va_end(arg);
208 }
209 
210 #endif
211 
212