Arcane  v3.15.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
ArccoreGlobal.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ArccoreGlobal.cc (C) 2000-2024 */
9/* */
10/* Déclarations générales de Arccore. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/base/ArccoreGlobal.h"
15#include "arccore/base/TraceInfo.h"
16#include "arccore/base/PlatformUtils.h"
17#include "arccore/base/String.h"
18#include "arccore/base/IndexOutOfRangeException.h"
19#include "arccore/base/FatalErrorException.h"
20#include "arccore/base/Ref.h"
21
22// Nécessaire pour les exports de symboles
24#include "arccore/base/Float16.h"
25#include "arccore/base/BFloat16.h"
26#include "arccore/base/Float128.h"
27#include "arccore/base/Int128.h"
28
29#include <iostream>
30#include <cstring>
31#include <sstream>
32#include <cstdarg>
33
34#ifndef ARCCORE_OS_WIN32
35#include <unistd.h>
36#endif
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
51namespace Arccore
52{
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56namespace
57{
58#ifdef ARCCORE_CHECK
59static bool global_arccore_is_check = true;
60#else
61static bool global_arccore_is_check = false;
62#endif
63}
64
65extern "C++" ARCCORE_BASE_EXPORT
67{
68 return global_arccore_is_check;
69}
70
71extern "C++" ARCCORE_BASE_EXPORT
72void arccoreSetCheck(bool v)
73{
74 global_arccore_is_check = v;
75}
76
77/*---------------------------------------------------------------------------*/
78/*---------------------------------------------------------------------------*/
79
80namespace
81{
82bool global_pause_on_error = false;
83}
84
85extern "C++" ARCCORE_BASE_EXPORT void
87{
88 global_pause_on_error = v;
89}
90
91extern "C++" ARCCORE_BASE_EXPORT void
92arccoreDebugPause(const char* msg)
93{
94 if (global_pause_on_error){
95 std::ostringstream ostr;
96 String host_name(Platform::getHostName());
97 ostr << "** FATAL: Debug mode activated. Execution paused\n"
98 << "** FATAL: message:" << msg << "\n"
99 << "** FATAL: To find the location of the error, start\n"
100 << "** FATAL: start the debugger using the process number\n"
101 << "** FATAL: (pid=" << Platform::getProcessId() << ",host=" << host_name << ").\n";
102 std::cerr << ostr.str();
103#ifndef ARCCORE_OS_WIN32
104 ::pause();
105#endif
106 }
107}
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
111
112extern "C++" ARCCORE_BASE_EXPORT void
113arccoreRangeError(Int64 i,Int64 min_value_inclusive,Int64 max_value_exclusive)
114{
115 arccoreDebugPause("arccoreRangeError");
116 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,min_value_inclusive,max_value_exclusive);
117}
118
119/*---------------------------------------------------------------------------*/
120/*---------------------------------------------------------------------------*/
121
122extern "C++" ARCCORE_BASE_EXPORT void
123arccoreRangeError(Int32 i,Int32 max_size)
124{
125 arccoreDebugPause("arccoreRangeError");
126 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,0,max_size);
127}
128
129/*---------------------------------------------------------------------------*/
130/*---------------------------------------------------------------------------*/
131
132extern "C++" ARCCORE_BASE_EXPORT void
133arccoreRangeError(Int64 i,Int64 max_size)
134{
135 arccoreDebugPause("arccoreRangeError");
136 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,0,max_size);
137}
138
139/*---------------------------------------------------------------------------*/
140/*---------------------------------------------------------------------------*/
141
142extern "C++" ARCCORE_BASE_EXPORT void
143arccoreNullPointerError()
144{
145 std::cerr << "** FATAL: null pointer.\n";
146 std::cerr << "** FATAL: Trying to dereference a null pointer.\n";
147 arccoreDebugPause("arcaneNullPointerPtr");
148 throw FatalErrorException(A_FUNCINFO,"null pointer");
149}
150
151/*---------------------------------------------------------------------------*/
152/*---------------------------------------------------------------------------*/
153
154extern "C++" ARCCORE_BASE_EXPORT void
155arccoreThrowNullPointerError(const char* ptr_name,const char* text)
156{
157 throw FatalErrorException(A_FUNCINFO,text ? text : ptr_name);
158}
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163// Cette fonction peut être appelée souvent et certaines fois
164// dans des conditions d'exceptions. Pour cette raison, il ne
165// faut pas qu'elle fasse d'allocations.
166namespace
167{
168void _printFuncName(std::ostream& o,const char* name)
169{
170 const char* par_pos = std::strchr(name,'(');
171 if (!par_pos){
172 o << name;
173 return;
174 }
175
176 // Recherche quelque chose du type namespace::class_name::func_name
177 // et essaye de ne conserver que class_name::func_name
178 ptrdiff_t len = par_pos - name;
179 ptrdiff_t last_scope = 0;
180 ptrdiff_t last_scope2 = 0;
181 for( ptrdiff_t i=0; i<len; ++i ){
182 if (name[i]==':' && name[i+1]==':'){
183 last_scope2 = last_scope;
184 last_scope = i;
185 }
186 }
187 if (last_scope2!=0)
188 last_scope2+=2;
189 ptrdiff_t true_pos = last_scope2;
190 ptrdiff_t true_len = len - true_pos;
191 o.write(&name[true_pos],true_len);
192 o << "()";
193}
194}
195
196/*---------------------------------------------------------------------------*/
197/*---------------------------------------------------------------------------*/
198
199extern "C++" ARCCORE_BASE_EXPORT std::ostream&
200operator<<(std::ostream& o,const TraceInfo& t)
201{
202 if (t.printSignature())
203 o << t.name() << ":" << t.line();
204 else{
205 _printFuncName(o,t.name());
206 }
207 return o;
208}
209
210/*---------------------------------------------------------------------------*/
211/*---------------------------------------------------------------------------*/
212
213/*---------------------------------------------------------------------------*/
214/*---------------------------------------------------------------------------*/
215
216namespace
217{
219typedef void (*fDoAssert)(const char*,const char*,const char*,size_t);
221typedef bool (*fCheckDebug)(unsigned int);
222
223fDoAssert g_do_assert_func = 0;
224}
225
226/*---------------------------------------------------------------------------*/
227/*---------------------------------------------------------------------------*/
231extern "C++" ARCCORE_BASE_EXPORT void
232_doAssert(const char* text,const char* file,const char* func,int line)
233{
234 if (g_do_assert_func)
235 (*g_do_assert_func)(text,file,func,line);
236 else{
237 std::ostringstream ostr;
238 ostr << text << ':' << file << ':' << func << ':' << line << ": ";
239 throw FatalErrorException("Assert",ostr.str());
240 }
241}
242
243/*---------------------------------------------------------------------------*/
244/*---------------------------------------------------------------------------*/
245
246extern "C++" ARCCORE_BASE_EXPORT void
247arccorePrintf(const char* format,...)
248{
249 // \n écrit en meme temps pour éviter des écritures intermédiares parasites
250 char buffer[4096];
251 va_list ap;
252 va_start(ap,format);
253 vsnprintf(buffer,4095,format,ap);
254 va_end(ap);
255 std::cerr << buffer << "\n";
256 std::cout << "*E* " << buffer << "\n";
257}
258
259/*---------------------------------------------------------------------------*/
260/*---------------------------------------------------------------------------*/
261
262} // End namespace Arccore
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
Gestion des références à une classe C++.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:149
Exception lorsqu'une erreur fatale est survenue.
Chaîne de caractères unicode.
std::ostream & operator<<(std::ostream &o, eExecutionPolicy exec_policy)
Affiche le nom de la politique d'exécution.
Integer len(const char *s)
Retourne la longueur de la chaîne s.
void(* fDoAssert)(const char *, const char *, const char *, size_t)
Fonction appelée lorsqu'une assertion échoue.
Definition Misc.cc:222
ARCCORE_BASE_EXPORT int getProcessId()
Numéro du processus.
ARCCORE_BASE_EXPORT String getHostName()
Nom de la machine sur lequel tourne le processus.
Espace de nom de Arccore.
Definition ArcaneTypes.h:24
ARCCORE_BASE_EXPORT void arccoreThrowNullPointerError(const char *ptr_name, const char *text)
Signalee l'utilisation d'un pointeur nul en envoyant une exception.
ARCCORE_BASE_EXPORT void _doAssert(const char *text, const char *file, const char *func, int line)
ARCCORE_BASE_EXPORT void arccoreSetCheck(bool v)
Active ou désactive le mode vérification.
ARCCORE_BASE_EXPORT void arccorePrintf(const char *format,...)
Encapsulation de la fonction C printf.
ARCCORE_BASE_EXPORT void arccoreDebugPause(const char *msg)
Passe en mode pause ou lance une erreur fatale.
ARCCORE_BASE_EXPORT bool arccoreIsCheck()
Vrai si on est en mode vérification.
ARCCORE_BASE_EXPORT void arccoreSetPauseOnError(bool v)
Indique si on l'appel à arccoreDebugPause() effectue une pause.