GEOS
Logger.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_COMMON_LOGGER_HPP
21 #define GEOS_COMMON_LOGGER_HPP
22 
23 // Source includes
24 #include "LvArray/src/Macros.hpp"
26 
27 // System includes
28 #include <stdexcept>
29 
30 #if defined(GEOS_USE_MPI)
31  #include <mpi.h>
32 #endif
33 
38 #define GEOS_LOG( ... ) LVARRAY_LOG( __VA_ARGS__ )
39 
44 #define GEOS_LOG_VAR( ... ) LVARRAY_LOG_VAR( __VA_ARGS__ )
45 
46 
52 #if defined(GEOS_DEVICE_COMPILE)
53 #define GEOS_LOG_IF( EXP, msg )
54 #else
55 #define GEOS_LOG_IF( EXP, msg ) \
56  do { \
57  if( EXP ) \
58  { \
59  std::cout<< msg << std::endl; \
60  } \
61  } while( false )
62 #endif
63 
64 
70 #define GEOS_LOG_RANK_0_IF( EXP, msg ) \
71  do { \
72  if( ::geos::logger::internal::g_rank == 0 && EXP ) \
73  { \
74  std::ostringstream oss; \
75  oss << msg; \
76  std::cout << oss.str() << std::endl; \
77  } \
78  } while( false )
79 
85 #define GEOS_LOG_RANK_0_IF_NLR( EXP, msg ) \
86  do { \
87  if( ::geos::logger::internal::g_rank == 0 && EXP ) \
88  { \
89  std::ostringstream oss; \
90  oss << msg; \
91  std::cout << oss.str(); \
92  } \
93  } while( false )
94 
99 #define GEOS_LOG_RANK_0( msg ) GEOS_LOG_RANK_0_IF( true, msg )
100 
106 #if defined(GEOS_DEVICE_COMPILE)
107 #define GEOS_LOG_RANK_IF( EXP, msg )
108 #else
109 #define GEOS_LOG_RANK_IF( EXP, msg ) \
110  do { \
111  if( EXP ) \
112  { \
113  std::ostringstream oss; \
114  oss << "Rank " << ::geos::logger::internal::g_rankString << ": " << msg; \
115  *logger::internal::g_rankStream << oss.str() << std::endl; \
116  } \
117  } while( false )
118 #endif
119 
124 #define GEOS_LOG_RANK( msg ) GEOS_LOG_RANK_IF( true, msg )
125 
130 #define GEOS_LOG_RANK_VAR( var ) GEOS_LOG_RANK( #var " = " << var )
131 
137 #if !defined(GEOS_DEVICE_COMPILE) && !defined(GEOS_GLOBAL_LOGGER)
138 #define GEOS_GLOBAL_LOGGER ErrorLogger::global()
139 #endif
140 
150 #if !defined(GEOS_DEVICE_COMPILE)
151 #define GEOS_ERROR_IF_CAUSE( COND, CAUSE_MESSAGE, ... ) \
152  do \
153  { \
154  if( COND ) \
155  { \
156  std::ostringstream __msgoss; \
157  __msgoss << GEOS_DETAIL_FIRST_ARG( __VA_ARGS__ ); \
158  std::ostringstream __causemsgsoss; \
159  __causemsgsoss << CAUSE_MESSAGE; \
160  GEOS_GLOBAL_LOGGER.initCurrentExceptionMessage( MsgType::Error, __msgoss.str(), \
161  ::geos::logger::internal::g_rank ) \
162  .setCodeLocation( __FILE__, __LINE__ ) \
163  .setCause( __causemsgsoss.str() ) \
164  .addCallStackInfo( LvArray::system::stackTrace( true ) ) \
165  .addContextInfo( GEOS_DETAIL_REST_ARGS( __VA_ARGS__ )); \
166  GEOS_GLOBAL_LOGGER.flushCurrentExceptionMessage(); \
167  LvArray::system::callErrorHandler(); \
168  } \
169  }while( false )
170  #elif __CUDA_ARCH__
171 #define GEOS_ERROR_IF_CAUSE( COND, CAUSE_MESSAGE, ... ) \
172  do \
173  { \
174  if( COND ) \
175  { \
176  constexpr char const * formatString = "***** ERROR\n" \
177  "***** LOCATION" LOCATION "\n" \
178  "***** BLOCK: [%u, %u, %u]\n" \
179  "***** THREAD: [%u, %u, %u]\n" \
180  "***** " STRINGIZE( CAUSE_MESSAGE ) "\n" \
181  "***** " STRINGIZE( GEOS_DETAIL_FIRST_ARG( __VA_ARGS__ ) ) "\n\n"; \
182  printf( formatString, blockIdx.x, blockIdx.y, blockIdx.z, threadIdx.x, threadIdx.y, threadIdx.z ); \
183  asm ( "trap;" ); \
184  } \
185  } while( false )
186 #endif
187 
195 #define GEOS_ERROR_IF( COND, ... ) \
196  GEOS_ERROR_IF_CAUSE( COND, "Error cause: " STRINGIZE( COND ), __VA_ARGS__ )
197 
204 #define GEOS_ERROR( ... ) GEOS_ERROR_IF_CAUSE( true, "", __VA_ARGS__ )
205 
215  #if !defined(GEOS_DEVICE_COMPILE)
216 #define GEOS_THROW_IF_CAUSE( COND, CAUSE_MESSAGE, MSG, ... ) \
217  do \
218  { \
219  if( COND ) \
220  { \
221  std::ostringstream __msgoss; \
222  __msgoss << MSG; \
223  std::ostringstream __causemsgsoss; \
224  __causemsgsoss << CAUSE_MESSAGE; \
225  DiagnosticMsg exceptionMsg = GEOS_GLOBAL_LOGGER.initCurrentExceptionMessage( MsgType::Exception, __msgoss.str(), \
226  ::geos::logger::internal::g_rank ) \
227  .setCodeLocation( __FILE__, __LINE__ ) \
228  .setCause( __causemsgsoss.str() ) \
229  .addCallStackInfo( LvArray::system::stackTrace( true ) ) \
230  .addContextInfo( GEOS_DETAIL_REST_ARGS( __VA_ARGS__ )) \
231  .getDiagnosticMsg(); \
232  auto ex = GEOS_DETAIL_FIRST_ARG( __VA_ARGS__ )(); \
233  ex.prepareWhat( exceptionMsg ); \
234  throw ex; \
235  } \
236  }while( false )
237  #elif __CUDA_ARCH__
238 #define GEOS_THROW_IF_CAUSE( COND, CAUSE_MESSAGE, MSG, ... ) \
239  do \
240  { \
241  if( COND ) \
242  { \
243  static char const formatString[] = "***** ERROR\n" \
244  "***** LOCATION" LOCATION "\n" \
245  "***** BLOCK: [%u, %u, %u]\n" \
246  "***** THREAD: [%u, %u, %u]\n" \
247  "***** " STRINGIZE( CAUSE_MESSAGE ) "\n" \
248  "***** " STRINGIZE( GEOS_DETAIL_FIRST_ARG( __VA_ARGS__ ) ) "\n\n"; \
249  printf( formatString, blockIdx.x, blockIdx.y, blockIdx.z, threadIdx.x, threadIdx.y, threadIdx.z ); \
250  asm ( "trap;" ); \
251  } \
252  } while( false )
253 #endif
254 
263 #define GEOS_THROW_IF( COND, MSG, ... ) \
264  GEOS_THROW_IF_CAUSE( COND, "Error cause: " STRINGIZE( COND ), MSG, __VA_ARGS__ )
265 
273 #define GEOS_THROW( MSG, ... ) GEOS_THROW_IF_CAUSE( true, "", MSG, __VA_ARGS__ )
274 
283 #if !defined(GEOS_DEVICE_COMPILE)
284 #define GEOS_WARNING_IF_CAUSE( COND, CAUSE_MESSAGE, ... ) \
285  do \
286  { \
287  if( COND ) \
288  { \
289  std::ostringstream __msgoss; \
290  __msgoss << GEOS_DETAIL_FIRST_ARG( __VA_ARGS__ ); \
291  std::ostringstream __causemsgsoss; \
292  __causemsgsoss << CAUSE_MESSAGE; \
293  DiagnosticMsg __warningMsg; \
294  GEOS_GLOBAL_LOGGER.flushErrorMsg( DiagnosticMsgBuilder::init( __warningMsg, \
295  MsgType::Warning, __msgoss.str(), \
296  ::geos::logger::internal::g_rank ) \
297  .setCodeLocation( __FILE__, __LINE__ ) \
298  .setCause( __causemsgsoss.str() ) \
299  .addCallStackInfo( LvArray::system::stackTrace( true ) ) \
300  .addContextInfo( GEOS_DETAIL_REST_ARGS( __VA_ARGS__ )) \
301  .getDiagnosticMsg() ); \
302  } \
303  }while( false )
304 #elif __CUDA_ARCH__
305 #define GEOS_WARNING_IF_CAUSE( COND, CAUSE_MESSAGE, ... ) \
306  do \
307  { \
308  if( COND ) \
309  { \
310  static char const formatString[] = "***** WARNING\n" \
311  "***** LOCATION" LOCATION "\n" \
312  "***** BLOCK: [%u, %u, %u]\n" \
313  "***** THREAD: [%u, %u, %u]\n" \
314  "***** " STRINGIZE( CAUSE_MESSAGE ) "\n" \
315  "***** " STRINGIZE( GEOS_DETAIL_FIRST_ARG( __VA_ARGS__ ) ) "\n\n"; \
316  printf( formatString, blockIdx.x, blockIdx.y, blockIdx.z, threadIdx.x, threadIdx.y, threadIdx.z ); \
317  asm ( "trap;" ); \
318  } \
319  } while( false )
320 #endif
321 
329 #define GEOS_WARNING_IF( COND, ... ) \
330  GEOS_WARNING_IF_CAUSE( COND, "Warning cause: " STRINGIZE( COND ), __VA_ARGS__ )
331 
338 #define GEOS_WARNING( ... ) GEOS_WARNING_IF_CAUSE( true, "", __VA_ARGS__ )
339 
345 #define GEOS_INFO_IF( EXP, msg ) LVARRAY_INFO_IF( EXP, msg )
346 
351 #define GEOS_INFO( msg ) LVARRAY_INFO( msg )
352 
358 #define GEOS_ERROR_LHS_RHS_DECLS( lhs, rhs ) \
359  GEOS_MAYBE_UNUSED auto const lhsResult = (lhs); \
360  GEOS_MAYBE_UNUSED auto const rhsResult = (rhs)
361 
372 #define GEOS_ERROR_IF_OP_MSG( lhs, OP, NOP, rhs, ... ) \
373  do { \
374  GEOS_ERROR_LHS_RHS_DECLS( lhs, rhs ); \
375  GEOS_ERROR_IF_CAUSE( lhsResult OP rhsResult, \
376  "Expected: " #lhs " " #NOP " " #rhs "\n* " #lhs " = " << lhsResult << "\n* " #rhs " = " << rhsResult << "\n", \
377  __VA_ARGS__ ); \
378  } while(false)
379 
380 
389 #define GEOS_ERROR_IF_EQ_MSG( lhs, rhs, ... ) GEOS_ERROR_IF_OP_MSG( lhs, ==, !=, rhs, __VA_ARGS__ )
390 
396 #define GEOS_ERROR_IF_EQ( lhs, rhs ) GEOS_ERROR_IF_EQ_MSG( lhs, rhs, "" )
397 
406 #define GEOS_ERROR_IF_NE_MSG( lhs, rhs, ... ) GEOS_ERROR_IF_OP_MSG( lhs, !=, ==, rhs, __VA_ARGS__ )
407 
413 #define GEOS_ERROR_IF_NE( lhs, rhs ) GEOS_ERROR_IF_NE_MSG( lhs, rhs, "" )
414 
423 #define GEOS_ERROR_IF_GT_MSG( lhs, rhs, ... ) GEOS_ERROR_IF_OP_MSG( lhs, >, <=, rhs, __VA_ARGS__ )
424 
430 #define GEOS_ERROR_IF_GT( lhs, rhs ) GEOS_ERROR_IF_GT_MSG( lhs, rhs, "" )
431 
440 #define GEOS_ERROR_IF_GE_MSG( lhs, rhs, ... ) GEOS_ERROR_IF_OP_MSG( lhs, >=, <, rhs, __VA_ARGS__ )
441 
447 #define GEOS_ERROR_IF_GE( lhs, rhs ) GEOS_ERROR_IF_GE_MSG( lhs, rhs, "" )
448 
457 #define GEOS_ERROR_IF_LT_MSG( lhs, rhs, ... ) GEOS_ERROR_IF_OP_MSG( lhs, <, >=, rhs, __VA_ARGS__ )
458 
464 #define GEOS_ERROR_IF_LT( lhs, rhs ) GEOS_ERROR_IF_LT_MSG( lhs, rhs, "" )
465 
474 #define GEOS_ERROR_IF_LE_MSG( lhs, rhs, ... ) GEOS_ERROR_IF_OP_MSG( lhs, <=, >, rhs, __VA_ARGS__ )
475 
476 
482 #define GEOS_ERROR_IF_LE( lhs, rhs ) GEOS_ERROR_IF_LE_MSG( lhs, rhs, "" )
483 
494 #define GEOS_WARNING_IF_OP_MSG( lhs, OP, NOP, rhs, ... ) \
495  do { \
496  GEOS_ERROR_LHS_RHS_DECLS( lhs, rhs ); \
497  GEOS_WARNING_IF_CAUSE( lhsResult OP rhsResult, \
498  "Expected: " #lhs " " #NOP " " #rhs "\n* " #lhs " = " << lhsResult << "\n* " #rhs " = " << rhsResult << "\n", \
499  __VA_ARGS__ ); \
500  } while(false)
501 
502 
511 #define GEOS_WARNING_IF_EQ_MSG( lhs, rhs, ... ) GEOS_WARNING_IF_OP_MSG( lhs, ==, !=, rhs, __VA_ARGS__ )
512 
518 #define GEOS_WARNING_IF_EQ( lhs, rhs ) GEOS_WARNING_IF_EQ_MSG( lhs, rhs, "" )
519 
528 #define GEOS_WARNING_IF_NE_MSG( lhs, rhs, ... ) GEOS_WARNING_IF_OP_MSG( lhs, !=, ==, rhs, __VA_ARGS__ )
529 
535 #define GEOS_WARNING_IF_NE( lhs, rhs ) GEOS_WARNING_IF_NE_MSG( lhs, rhs, "" )
536 
545 #define GEOS_WARNING_IF_GT_MSG( lhs, rhs, ... ) GEOS_WARNING_IF_OP_MSG( lhs, >, <=, rhs, __VA_ARGS__ )
546 
552 #define GEOS_WARNING_IF_GT( lhs, rhs ) GEOS_WARNING_IF_GT_MSG( lhs, rhs, "" )
553 
562 #define GEOS_WARNING_IF_GE_MSG( lhs, rhs, ... ) GEOS_WARNING_IF_OP_MSG( lhs, >=, <, rhs, __VA_ARGS__ )
563 
569 #define GEOS_WARNING_IF_GE( lhs, rhs ) GEOS_WARNING_IF_GE_MSG( lhs, rhs, "" )
570 
579 #define GEOS_WARNING_IF_LT_MSG( lhs, rhs, ... ) GEOS_WARNING_IF_OP_MSG( lhs, <, >=, rhs, __VA_ARGS__ )
580 
586 #define GEOS_WARNING_IF_LT( lhs, rhs ) GEOS_WARNING_IF_LT_MSG( lhs, rhs, "" )
587 
596 #define GEOS_WARNING_IF_LE_MSG( lhs, rhs, ... ) GEOS_WARNING_IF_OP_MSG( lhs, <=, >, rhs, __VA_ARGS__ )
597 
603 #define GEOS_WARNING_IF_LE( lhs, rhs ) GEOS_WARNING_IF_LE_MSG( lhs, rhs, "" )
604 
616 #define GEOS_THROW_IF_OP_MSG( lhs, OP, NOP, rhs, MSG, ... ) \
617  do { \
618  GEOS_ERROR_LHS_RHS_DECLS( lhs, rhs ); \
619  GEOS_THROW_IF_CAUSE( lhsResult OP rhsResult, \
620  "Expected: " #lhs " " #NOP " " #rhs "\n* " #lhs " = " << lhsResult << "\n* " #rhs " = " << rhsResult << "\n", \
621  MSG, __VA_ARGS__ ); \
622  } while(false)
623 
624 
625 
635 #define GEOS_THROW_IF_EQ_MSG( lhs, rhs, MSG, ... ) GEOS_THROW_IF_OP_MSG( lhs, ==, !=, rhs, MSG, __VA_ARGS__ )
636 
645 #define GEOS_THROW_IF_EQ( lhs, rhs, ... ) GEOS_THROW_IF_EQ_MSG( lhs, rhs, "", __VA_ARGS__ )
646 
656 #define GEOS_THROW_IF_NE_MSG( lhs, rhs, MSG, ... ) GEOS_THROW_IF_OP_MSG( lhs, !=, ==, rhs, MSG, __VA_ARGS__ )
657 
666 #define GEOS_THROW_IF_NE( lhs, rhs, ... ) GEOS_THROW_IF_NE_MSG( lhs, rhs, "", __VA_ARGS__ )
667 
677 #define GEOS_THROW_IF_GT_MSG( lhs, rhs, MSG, ... ) GEOS_THROW_IF_OP_MSG( lhs, >, <=, rhs, MSG, __VA_ARGS__ )
678 
687 #define GEOS_THROW_IF_GT( lhs, rhs, ... ) GEOS_THROW_IF_GT_MSG( lhs, rhs, "", __VA_ARGS__ )
688 
698 #define GEOS_THROW_IF_GE_MSG( lhs, rhs, MSG, ... ) GEOS_THROW_IF_OP_MSG( lhs, >=, <, rhs, MSG, __VA_ARGS__ )
699 
708 #define GEOS_THROW_IF_GE( lhs, rhs, ... ) GEOS_THROW_IF_GE_MSG( lhs, rhs, "", __VA_ARGS__ )
709 
719 #define GEOS_THROW_IF_LT_MSG( lhs, rhs, MSG, ... ) GEOS_THROW_IF_OP_MSG( lhs, <, >=, rhs, MSG, __VA_ARGS__ )
720 
729 #define GEOS_THROW_IF_LT( lhs, rhs, ... ) GEOS_THROW_IF_LT_MSG( lhs, rhs, "", __VA_ARGS__ )
730 
740 #define GEOS_THROW_IF_LE_MSG( lhs, rhs, MSG, ... ) GEOS_THROW_IF_OP_MSG( lhs, <=, >, rhs, MSG, __VA_ARGS__ )
741 
750 #define GEOS_THROW_IF_LE( lhs, rhs, ... ) GEOS_THROW_IF_LE_MSG( lhs, rhs, "", __VA_ARGS__ )
751 
752 #if !defined(NDEBUG) || defined(GEOS_ASSERT_ENABLED)
753 
757 #define GEOS_ASSERT_ENABLED
758 
771 #define GEOS_ASSERT_MSG( COND, ... ) \
772  GEOS_ERROR_IF_CAUSE( !( COND ), "Expected: " STRINGIZE( COND ), __VA_ARGS__ )
773 
783 #define GEOS_ASSERT_OP_MSG( lhs, OP, rhs, ... ) \
784  { \
785  GEOS_ERROR_LHS_RHS_DECLS( lhs, rhs ); \
786  GEOS_ERROR_IF_CAUSE( !( lhsResult OP rhsResult ), \
787  "Expected: " #lhs " " #OP " " #rhs "\n* " #lhs " = " << lhsResult << "\n* " #rhs " = " << rhsResult << "\n", \
788  __VA_ARGS__ ); \
789  }
790 
791 #else
792 
793 #define GEOS_ASSERT_MSG( ... ) ((void) 0)
794 #define GEOS_ASSERT_OP_MSG( ... ) ((void) 0)
795 
796 #endif
797 
802 #define GEOS_ASSERT( COND ) GEOS_ASSERT_MSG( COND, "" )
803 
812 #define GEOS_ASSERT_EQ_MSG( lhs, rhs, ... ) GEOS_ASSERT_OP_MSG( lhs, ==, rhs, __VA_ARGS__ )
813 
819 #define GEOS_ASSERT_EQ( lhs, rhs ) GEOS_ASSERT_EQ_MSG( lhs, rhs, "" )
820 
829 #define GEOS_ASSERT_NE_MSG( lhs, rhs, ... ) GEOS_ASSERT_OP_MSG( lhs, !=, rhs, __VA_ARGS__ )
830 
836 #define GEOS_ASSERT_NE( lhs, rhs ) GEOS_ASSERT_NE_MSG( lhs, rhs, "" )
837 
846 #define GEOS_ASSERT_GT_MSG( lhs, rhs, ... ) GEOS_ASSERT_OP_MSG( lhs, >, rhs, __VA_ARGS__ )
847 
853 #define GEOS_ASSERT_GT( lhs, rhs ) GEOS_ASSERT_GT_MSG( lhs, rhs, "" )
854 
863 #define GEOS_ASSERT_GE_MSG( lhs, rhs, ... ) GEOS_ASSERT_OP_MSG( lhs, >=, rhs, __VA_ARGS__ )
864 
870 #define GEOS_ASSERT_GE( lhs, rhs ) GEOS_ASSERT_GE_MSG( lhs, rhs, "" )
871 
880 #define GEOS_ASSERT_LT_MSG( lhs, rhs, ... ) GEOS_ASSERT_OP_MSG( lhs, <, rhs, __VA_ARGS__ )
881 
887 #define GEOS_ASSERT_LT( lhs, rhs ) GEOS_ASSERT_LT_MSG( lhs, rhs, "" )
888 
897 #define GEOS_ASSERT_LE_MSG( lhs, rhs, ... ) GEOS_ASSERT_OP_MSG( lhs, <=, rhs, __VA_ARGS__ )
898 
904 #define GEOS_ASSERT_LE( lhs, rhs ) GEOS_ASSERT_LE_MSG( lhs, rhs, "" )
905 
906 namespace geos
907 {
908 
909 namespace logger
910 {
911 
912 namespace internal
913 {
914 
915 extern int g_rank;
916 
917 extern std::string g_rankString;
918 
919 extern std::ostream * g_rankStream;
920 
921 } // namespace internal
922 
923 #if defined(GEOS_USE_MPI)
929 void InitializeLogger( MPI_Comm comm, const std::string & rank_output_dir="" );
930 #endif
931 
936 void InitializeLogger( const std::string & rank_output_dir="" );
937 
942 
943 } // namespace logger
944 
945 } // namespace geos
946 
947 #endif /* GEOS_COMMON_LOGGER_HPP */
void FinalizeLogger()
Finalize the logger and close the rank streams.
void InitializeLogger(const std::string &rank_output_dir="")
Initialize the logger in a serial build.
std::string string
String type.
Definition: DataTypes.hpp:90