Salome HOME
Porting to OCCT 7.8.0
[modules/kernel.git] / src / SALOMELocalTrace / utilities.h
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library 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 GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME Utils : general SALOME's definitions and tools
24 //  File   : utilities.h
25 //  Author : Antoine YESSAYAN, Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28 //
29 /* ---  Definition macros file to print information if _DEBUG_ is defined --- */
30
31 #ifndef UTILITIES_H
32 #define UTILITIES_H
33
34 #include <iostream>
35 #include <sstream>
36 #include <cstdlib>
37
38
39 #include "LocalTraceBufferPool.hxx"
40 #include "libSALOMELog.hxx"
41
42 /*!
43  * For each message to put in the trace, a specific ostingstream object is
44  * created and destroyed automatically at the end of the message macro.
45  * The insert function of LocalTraceBufferPool class gets a buffer in a
46  * buffer pool (unique with the help of mutexes and semaphores) and copy the
47  * message in the buffer.
48  * This buffer is read later by a specific thread in charge of trace print.
49  * Order of trace entries is globally respected. Nevertheless, if there are
50  * several threads waiting for a free buffer to trace, the order of
51  * thread waken up is not guaranteed (no fifo or priority rules in Linux Kernel)
52  */
53
54 #define MESS_INIT(deb) std::ostringstream os; SALOME::AppendTimeClock(os); os << deb
55 #define MESS_BEGIN(deb) MESS_INIT(deb)<<__FILE__ <<" ["<<__LINE__<<"] : "
56 #define MESS_END std::endl; LocalTraceBufferPool::instance()->insert(NORMAL_MESS, os.str().c_str());
57 #define MESS_ABORT std::endl; LocalTraceBufferPool::instance()->insert(ABORT_MESS, os.str().c_str());
58
59 // Macros for messages with separated structure in c++ file in _DEBUG mode
60 #define MESSAGE_BEGIN(msg) {std::ostringstream ss; ss <<__FILE__ <<" ["<<__LINE__<<"] : "<< msg; LocalTraceBufferPool::instance()->insert(NORMAL_MESS, ss.str().c_str());}
61 #define MESSAGE_ADD(msg) {std::ostringstream ss; ss << msg; LocalTraceBufferPool::instance()->insert(NORMAL_MESS, ss.str().c_str());}
62 #define MESSAGE_END(msg) {std::ostringstream ss; ss << msg << std::endl; LocalTraceBufferPool::instance()->insert(NORMAL_MESS, ss.str().c_str());}
63
64 // --- Some macros are always defined (without _DEBUG_): for use with release version
65
66 #define INFOS(msg) {MESS_BEGIN("Infos - ") << msg << MESS_END}
67 #define PYSCRIPT(msg) {MESS_INIT("---PYSCRIPT--- ") << msg << MESS_END}
68 #define INTERRUPTION(msg) {MESS_BEGIN("- INTERRUPTION: ")<< msg << MESS_ABORT}
69
70 #ifdef WIN32
71 #define IMMEDIATE_ABORT(code) {std::cout <<std::flush; \
72                                std::cerr << "- ABORT " << __FILE__ << " [" <<__LINE__<< "] : " << std::flush; \
73                                std::cerr << "ABORT return code= "<< code << std::endl; \
74                                /*std::*/exit(code);}
75 #else
76 #define IMMEDIATE_ABORT(code) {std::cout <<std::flush; \
77                                std::cerr << "- ABORT " << __FILE__ << " [" <<__LINE__<< "] : " << std::flush; \
78                                std::cerr << "ABORT return code= "<< code << std::endl; \
79                                std::exit(code);}
80 #endif
81
82 /* --- To print date and time of compilation of current source --- */
83
84 #if defined ( __GNUC__ )
85 #define COMPILER                "g++" 
86 #elif defined ( __sun )
87 #define COMPILER                "CC" 
88 #elif defined ( __KCC )
89 #define COMPILER                "KCC" 
90 #elif defined ( __PGI )
91 #define COMPILER                "pgCC" 
92 #elif defined ( __alpha )
93 #define COMPILER                "cxx" 
94 #elif defined ( _MSC_VER )
95 #define COMPILER                "cl.exe" 
96 #else
97 #define COMPILER                "undefined" 
98 #endif
99
100 #ifdef INFOS_COMPILATION
101 #error INFOS_COMPILATION already defined
102 #endif
103
104 // --- the following MACROS can be enabled in debug and release versions
105 // --- by SALOME_VERBOSE environment variable
106
107 #define INFOS_COMPILATION { if (SALOME::VerbosityActivated()) { MESS_BEGIN("COMPILED with ") << COMPILER \
108                                        << ", " << __DATE__ \
109                                        << " at " << __TIME__ << MESS_END }}
110                                                                                 
111
112 #define MESSAGE(msg) { if (SALOME::VerbosityActivated()) {MESS_BEGIN("Trace -") << msg << MESS_END}}
113 #define SCRUTE(var)  { if (SALOME::VerbosityActivated()) {MESS_BEGIN("Trace -") << #var << "=" << var <<MESS_END}}
114 #define ERROR_MESSAGE(msg) { if (SALOME::VerbosityActivated() && SALOME::IsErrorLevel()) {MESS_BEGIN("ERROR -") << msg << MESS_END}}
115 #define WARNING_MESSAGE(msg) { if (SALOME::VerbosityActivated() && SALOME::IsWarningLevel()) {MESS_BEGIN("WARNING -") << msg << MESS_END}}
116 #define INFO_MESSAGE(msg) { if (SALOME::VerbosityActivated() && SALOME::IsInfoLevel()) {MESS_BEGIN("INFO -") << msg << MESS_END}}
117 #define DEBUG_MESSAGE(msg) { if (SALOME::VerbosityActivated() && SALOME::IsDebugLevel()) {MESS_BEGIN("DBG -") << msg << MESS_END}}
118
119 #define REPERE ("------- ")
120 #define BEGIN_OF(msg) { if (SALOME::VerbosityActivated()) {MESS_BEGIN(REPERE) << "Begin of: " << msg << MESS_END}}
121 #define END_OF(msg)   { if (SALOME::VerbosityActivated()) {MESS_BEGIN(REPERE) << "Normal end of: " << msg << MESS_END}}
122
123 #ifndef ASSERT
124 #define ASSERT(condition) \
125         { if (SALOME::VerbosityActivated()) { \
126         if (!(condition)){INTERRUPTION("CONDITION "<<#condition<<" NOT VERIFIED")} \
127         }}
128 #endif /* ASSERT */
129
130 #endif /* ifndef UTILITIES_H */