Arcane  v4.1.2.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-2025 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-2025 */
9/* */
10/* Déclarations générales de Arccore. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
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#include "arccore/base/IRangeFunctor.h"
29#include "arccore/base/CheckedConvert.h"
30#include "arccore/base/ForLoopRunInfo.h"
31#include "arccore/base/ForLoopRanges.h"
32#include "arccore/base/ParallelLoopOptions.h"
33#include "arccore/base/internal/IDynamicLibraryLoader.h"
34
35#include <iostream>
36#include <cstring>
37#include <sstream>
38#include <cstdarg>
39
40#ifndef ARCCORE_OS_WIN32
41#include <unistd.h>
42#endif
43
44/*---------------------------------------------------------------------------*/
45/*---------------------------------------------------------------------------*/
59/*---------------------------------------------------------------------------*/
60/*---------------------------------------------------------------------------*/
61
62namespace Arcane
63{
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66
67namespace
68{
69#ifdef ARCCORE_CHECK
70static bool global_arccore_is_check = true;
71#else
72static bool global_arccore_is_check = false;
73#endif
74}
75
76extern "C++" ARCCORE_BASE_EXPORT
78{
79 return global_arccore_is_check;
80}
81
82extern "C++" ARCCORE_BASE_EXPORT
83void arccoreSetCheck(bool v)
84{
85 global_arccore_is_check = v;
86}
87
88extern "C++" ARCCORE_BASE_EXPORT
90{
91#ifdef ARCCORE_DEBUG
92 return true;
93#else
94 return false;
95#endif
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101namespace
102{
103bool global_pause_on_error = false;
104}
105
106extern "C++" ARCCORE_BASE_EXPORT void
108{
109 global_pause_on_error = v;
110}
111
112extern "C++" ARCCORE_BASE_EXPORT void
113arccoreDebugPause(const char* msg)
114{
115 if (global_pause_on_error){
116 std::ostringstream ostr;
117 String host_name(Platform::getHostName());
118 ostr << "** FATAL: Debug mode activated. Execution paused\n"
119 << "** FATAL: message:" << msg << "\n"
120 << "** FATAL: To find the location of the error, start\n"
121 << "** FATAL: start the debugger using the process number\n"
122 << "** FATAL: (pid=" << Platform::getProcessId() << ",host=" << host_name << ").\n";
123 std::cerr << ostr.str();
124#ifndef ARCCORE_OS_WIN32
125 ::pause();
126#endif
127 }
128}
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
133extern "C++" ARCCORE_BASE_EXPORT void
134arccoreRangeError(Int64 i,Int64 min_value_inclusive,Int64 max_value_exclusive)
135{
136 arccoreDebugPause("arccoreRangeError");
137 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,min_value_inclusive,max_value_exclusive);
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143extern "C++" ARCCORE_BASE_EXPORT void
144arccoreRangeError(Int32 i,Int32 max_size)
145{
146 arccoreDebugPause("arccoreRangeError");
147 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,0,max_size);
148}
149
150/*---------------------------------------------------------------------------*/
151/*---------------------------------------------------------------------------*/
152
153extern "C++" ARCCORE_BASE_EXPORT void
155{
156 arccoreDebugPause("arccoreRangeError");
157 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,0,max_size);
158}
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163extern "C++" ARCCORE_BASE_EXPORT void
165{
166 std::cerr << "** FATAL: null pointer.\n";
167 std::cerr << "** FATAL: Trying to dereference a null pointer.\n";
168 arccoreDebugPause("arcaneNullPointerPtr");
169 throw FatalErrorException(A_FUNCINFO,"null pointer");
170}
171
172/*---------------------------------------------------------------------------*/
173/*---------------------------------------------------------------------------*/
174
175extern "C++" ARCCORE_BASE_EXPORT void
176arccoreThrowNullPointerError(const char* ptr_name,const char* text)
177{
178 throw FatalErrorException(A_FUNCINFO,text ? text : ptr_name);
179}
180
181/*---------------------------------------------------------------------------*/
182/*---------------------------------------------------------------------------*/
183
184// Cette fonction peut être appelée souvent et certaines fois
185// dans des conditions d'exceptions. Pour cette raison, il ne
186// faut pas qu'elle fasse d'allocations.
187namespace
188{
189void _printFuncName(std::ostream& o,const char* name)
190{
191 const char* par_pos = std::strchr(name,'(');
192 if (!par_pos){
193 o << name;
194 return;
195 }
196
197 // Recherche quelque chose du type namespace::class_name::func_name
198 // et essaye de ne conserver que class_name::func_name
199 ptrdiff_t len = par_pos - name;
200 ptrdiff_t last_scope = 0;
201 ptrdiff_t last_scope2 = 0;
202 for( ptrdiff_t i=0; i<len; ++i ){
203 if (name[i]==':' && name[i+1]==':'){
204 last_scope2 = last_scope;
205 last_scope = i;
206 }
207 }
208 if (last_scope2!=0)
209 last_scope2+=2;
210 ptrdiff_t true_pos = last_scope2;
211 ptrdiff_t true_len = len - true_pos;
212 o.write(&name[true_pos],true_len);
213 o << "()";
214}
215}
216
217/*---------------------------------------------------------------------------*/
218/*---------------------------------------------------------------------------*/
219
220extern "C++" ARCCORE_BASE_EXPORT std::ostream&
221operator<<(std::ostream& o,const TraceInfo& t)
222{
223 if (t.printSignature())
224 o << t.name() << ":" << t.line();
225 else{
226 _printFuncName(o,t.name());
227 }
228 return o;
229}
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233
234/*---------------------------------------------------------------------------*/
235/*---------------------------------------------------------------------------*/
236
237namespace
238{
240typedef void (*fDoAssert)(const char*,const char*,const char*,size_t);
242typedef bool (*fCheckDebug)(unsigned int);
243
244fDoAssert g_do_assert_func = 0;
245}
246
247/*---------------------------------------------------------------------------*/
248/*---------------------------------------------------------------------------*/
252extern "C++" ARCCORE_BASE_EXPORT void
253_doAssert(const char* text,const char* file,const char* func,int line)
254{
255 if (g_do_assert_func)
256 (*g_do_assert_func)(text,file,func,line);
257 else{
258 std::ostringstream ostr;
259 ostr << text << ':' << file << ':' << func << ':' << line << ": ";
260 throw FatalErrorException("Assert",ostr.str());
261 }
262}
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
267extern "C++" ARCCORE_BASE_EXPORT void
268arccorePrintf(const char* format,...)
269{
270 // \n écrit en meme temps pour éviter des écritures intermédiares parasites
271 char buffer[4096];
272 va_list ap;
273 va_start(ap,format);
274 vsnprintf(buffer,4095,format,ap);
275 va_end(ap);
276 std::cerr << buffer << "\n";
277 std::cout << "*E* " << buffer << "\n";
278}
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
282
283} // End namespace Arccore
284
285/*---------------------------------------------------------------------------*/
286/*---------------------------------------------------------------------------*/
Définitions et globaux de Arccore.
Gestion des références à une classe C++.
Exception lorsqu'une erreur fatale est survenue.
Exception lorsqu'une valeur n'est pas dans un intervalle donné.
Chaîne de caractères unicode.
Integer len(const char *s)
Retourne la longueur de la chaîne s.
ARCCORE_BASE_EXPORT String getHostName()
Nom de la machine sur lequel tourne le processus.
ARCCORE_BASE_EXPORT int getProcessId()
Numéro du processus.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
ARCCORE_BASE_EXPORT void arccoreNullPointerError()
Signalue l'utilisation d'un pointeur nul.
std::int64_t Int64
Type entier signé sur 64 bits.
ARCCORE_BASE_EXPORT bool arccoreIsDebug()
Vrai si la macro ARCCORE_DEBUG est définie.
ARCCORE_BASE_EXPORT void arccorePrintf(const char *format,...)
Encapsulation de la fonction C printf.
void(* fDoAssert)(const char *, const char *, const char *, size_t)
Fonction appelée lorsqu'une assertion échoue.
Definition Misc.cc:212
ARCCORE_BASE_EXPORT void arccoreDebugPause(const char *msg)
Passe en mode pause ou lance une erreur fatale.
bool(* fCheckDebug)(unsigned int)
Fonction appelée pour indiquer s'il faut afficher l'information de débug.
Definition Misc.cc:214
ARCCORE_BASE_EXPORT void arccoreRangeError(Int64 i, Int64 min_value_inclusive, Int64 max_value_exclusive)
Signale qu'une valeur n'est pas dans l'intervalle souhaité.
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 bool arccoreIsCheck()
Vrai si on est en mode vérification.
void _doAssert(const char *text, const char *file, const char *func, size_t line)
Definition Misc.cc:225
ARCCORE_BASE_EXPORT void arccoreSetPauseOnError(bool v)
Indique si on l'appel à arccoreDebugPause() effectue une pause.
ARCCORE_BASE_EXPORT void arccoreSetCheck(bool v)
Active ou désactive le mode vérification.