GCC Code Coverage Report


Directory: ./
File: Core/felisce_error.cpp
Date: 2024-04-14 07:32:34
Exec Total Coverage
Lines: 13 36 36.1%
Branches: 5 26 19.2%

Line Branch Exec Source
1 // ______ ______ _ _ _____ ______
2 // | ____| ____| | (_)/ ____| | ____|
3 // | |__ | |__ | | _| (___ ___| |__
4 // | __| | __| | | | |\___ \ / __| __|
5 // | | | |____| |____| |____) | (__| |____
6 // |_| |______|______|_|_____/ \___|______|
7 // Finite Elements for Life Sciences and Engineering
8 //
9 // License: LGL2.1 License
10 // FELiScE default license: LICENSE in root folder
11 //
12 // Main authors: J-F. Gerbeau
13 //
14
15 // System includes
16
17 // External includes
18
19 // Project includes
20 #include "Core/felisce.hpp"
21 #include "Core/felisce_error.hpp"
22
23 namespace felisce {
24
25 #ifdef FELISCE_WITH_EXCEPTION
26
27 // FelisceError
28 FelisceError::FelisceError():
29 str_(NULL) {
30 }
31
32 FelisceError::FelisceError(const char * str,const char * file,int line):
33 str_(NULL) {
34 std::ostringstream msg;
35 msg << file << ":" << line << ": " << str ;
36 msg.flush();
37
38 str_ = new char[ strlen( msg.str().c_str() )+1 ];
39 strcpy(str_, msg.str().c_str() );
40 }
41
42 const char* FelisceError::what() const throw() {
43 return str_;
44 }
45
46 // FelisceAssertionError
47 FelisceAssertionError::FelisceAssertionError() {
48 }
49
50 FelisceAssertionError::FelisceAssertionError(const char * str,const char * file,int line):
51 str_(NULL) {
52 std::ostringstream msg;
53 msg << file << ":" << line << ": " << str ;
54 msg.flush();
55
56 str_ = new char[ strlen( msg.str().c_str() )+1 ];
57 strcpy(str_, msg.str().c_str() );
58 }
59
60 const char* FelisceAssertionError::what() const throw() {
61 return str_;
62 }
63
64 // FelisceEqualAssertionError
65 FelisceEqualAssertionError::FelisceEqualAssertionError() {
66 }
67
68 FelisceEqualAssertionError::FelisceEqualAssertionError(const char * str,const char * file,int line):
69 str_(NULL) {
70 std::ostringstream msg;
71 msg << file << ":" << line << ": " << str ;
72 msg.flush();
73
74 str_ = new char[ strlen( msg.str().c_str() )+1 ];
75 strcpy(str_, msg.str().c_str() );
76 }
77
78 const char* FelisceEqualAssertionError::what() const throw() {
79 return str_;
80 }
81
82 // error
83 void felisce_error(const char * str,const char * file,int line) {
84 FelisceError err(str,file,line);
85 throw err;
86 }
87
88 void felisce_error(std::string str,const char * file,int line) {
89 FelisceError err(str.c_str(),file,line);
90 throw err;
91 }
92
93 //assert
94 void felisce_assert(const char * str,const char * file,int line) {
95 FelisceAssertionError err(str,file,line);
96 throw err;
97 }
98
99 void felisce_assert(std::string str,const char * file,int line) {
100 FelisceAssertionError err(str.c_str(),file,line);
101 throw err;
102 }
103
104 //assert equal
105 void felisce_assert_equal(const char* expr_a, const char* expr_b, int a, int b, const char * file,int line) {
106 if (a!=b) {
107 std::ostringstream stream;
108 stream << "expected '" << expr_a << " == " << expr_b << "'"
109 << ", got: '" << a << " != " << b << "'";
110 FelisceEqualAssertionError err(stream.str().c_str(),file,line);
111 throw err;
112 }
113 }
114
115 //assert lt
116 void felisce_assert_lt(const char* expr_a, const char* expr_b, int a, int b, const char * file,int line) {
117 if (a>=b) {
118 std::ostringstream stream;
119 stream << "expected '" << expr_a << " < " << expr_b << "'"
120 << ", got: '" << a << " >= " << b << "'";
121 FelisceEqualAssertionError err(stream.str().c_str(),file,line);
122 throw err;
123 }
124 }
125
126 #else
127
128 //I changed all this exit(1) in order to be able to get the stack in gdb. Now debugging is easier.
129 __attribute__ ((noreturn)) void felisce_error(const char * str,const char * file,int line) {
130 std::cout << file << ":" << line << ": error: " << str << std::endl;
131 std::abort();
132 //exit(1);
133 }
134
135 __attribute__ ((noreturn)) void felisce_error(std::string str,const char * file,int line) {
136 std::cout << file << ":" << line << ": error: " << str << std::endl;
137 std::abort();
138 //exit(1);
139 }
140
141 __attribute__ ((noreturn)) void felisce_assert(const char * str,const char * file,int line) {
142 (void) str;
143 std::cout << file << ":" << line << ": error: Assertion failed." << std::endl;
144 std::abort();
145 //exit(1);
146 }
147
148 __attribute__ ((noreturn)) void felisce_assert(std::string str,const char * file,int line) {
149 (void) str;
150 std::cout << file << ":" << line << ": error: Assertion failed." << std::endl;
151 std::abort();
152 //exit(1);
153 }
154
155 14989744 void felisce_assert_equal(const char* expr_a, const char* expr_b, int a, int b, const char * file,int line) {
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14989744 times.
14989744 if (a!=b) {
157 printf("%s:%i: error: Expected '%s == %s', got: '%i != %i'.\n", file,line,expr_a,expr_b,a,b);
158 std::abort();
159 //exit(1);
160 }
161 14989744 }
162
163 1578000 void felisce_assert_lt(const char* expr_a, const char* expr_b, int a, int b, const char * file,int line) {
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1578000 times.
1578000 if (a>=b) {
165 printf("%s:%i: error: Expected '%s < %s', got: '%i >= %i'.\n", file,line,expr_a,expr_b,a,b);
166 std::abort();
167 //exit(1);
168 }
169 1578000 }
170 #endif
171
172 void felisce_info(const char * str,const char * file,int line) {
173 std::cout << file << ":" << line << ": info: " << str << std::endl;
174 }
175
176 154 void felisce_warning(const char * str,const char * file,int line) {
177 154 std::cout << file << ":" << line << ": warning: " << str << std::endl;
178 154 }
179
180 void felisce_warning(std::string str,const char * file,int line) {
181 std::cout << file << ":" << line << ": warning: " << str << std::endl;
182 }
183
184 1395 void felisce_warning_if_no_directory(const char* dirName, const std::string dirPath,
185 const char* file, int line) {
186
1/2
✓ Branch 1 taken 1395 times.
✗ Branch 2 not taken.
1395 std::ostringstream msg;
187
2/4
✓ Branch 2 taken 1395 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1395 times.
1395 if (! filesystemUtil::directoryExists(dirPath.c_str()) ) {
188 msg << "Directory " << dirName
189 << " <" << dirPath << "> "
190 << "does not exists.";
191 felisce_warning(msg.str(),file,line);
192 }
193 1395 }
194
195 void initFELiScE() {
196 FEL_DEBUG("Init FELiScE");
197 }
198 }
199