1 /*************************************************************************/
2 /*  error_macros.cpp                                                     */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 #include "error_macros.h"
31 #include "os/os.h"
32 
33 bool _err_error_exists = false;
34 
35 static ErrorHandlerList *error_handler_list = NULL;
36 
_err_set_last_error(const char * p_err)37 void _err_set_last_error(const char *p_err) {
38 
39 	OS::get_singleton()->set_last_error(p_err);
40 }
41 
_err_clear_last_error()42 void _err_clear_last_error() {
43 
44 	OS::get_singleton()->clear_last_error();
45 }
46 
add_error_handler(ErrorHandlerList * p_handler)47 void add_error_handler(ErrorHandlerList *p_handler) {
48 
49 	_global_lock();
50 	p_handler->next = error_handler_list;
51 	error_handler_list = p_handler;
52 	_global_unlock();
53 }
54 
remove_error_handler(ErrorHandlerList * p_handler)55 void remove_error_handler(ErrorHandlerList *p_handler) {
56 
57 	_global_lock();
58 
59 	ErrorHandlerList *prev = NULL;
60 	ErrorHandlerList *l = error_handler_list;
61 
62 	while (l) {
63 
64 		if (l == p_handler) {
65 
66 			if (prev)
67 				prev->next = l->next;
68 			else
69 				error_handler_list = l->next;
70 			break;
71 		}
72 		prev = l;
73 		l = l->next;
74 	}
75 
76 	_global_unlock();
77 }
78 
_err_print_error(const char * p_function,const char * p_file,int p_line,const char * p_error,ErrorHandlerType p_type)79 void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, ErrorHandlerType p_type) {
80 
81 	OS::get_singleton()->print_error(p_function, p_file, p_line, p_error, _err_error_exists ? OS::get_singleton()->get_last_error() : "", (OS::ErrorType)p_type);
82 
83 	_global_lock();
84 	ErrorHandlerList *l = error_handler_list;
85 	while (l) {
86 
87 		l->errfunc(l->userdata, p_function, p_file, p_line, p_error, _err_error_exists ? OS::get_singleton()->get_last_error() : "", p_type);
88 		l = l->next;
89 	}
90 
91 	_global_unlock();
92 
93 	if (_err_error_exists) {
94 		OS::get_singleton()->clear_last_error();
95 		_err_error_exists = false;
96 	}
97 }
98