GEOSX
Logger.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOS_COMMON_LOGGER_HPP
20 #define GEOS_COMMON_LOGGER_HPP
21 
22 // Source incldes
23 #include "common/GeosxConfig.hpp"
24 #include "common/GeosxMacros.hpp"
25 #include "common/Format.hpp"
26 #include "LvArray/src/Macros.hpp"
27 
28 // System includes
29 #include <stdexcept>
30 
31 #if defined(GEOSX_USE_MPI)
32  #include <mpi.h>
33 #endif
34 
39 #define GEOS_LOG( ... ) LVARRAY_LOG( __VA_ARGS__ )
40 
45 #define GEOS_LOG_VAR( ... ) LVARRAY_LOG_VAR( __VA_ARGS__ )
46 
52 #define GEOS_LOG_RANK_0_IF( EXP, msg ) \
53  do { \
54  if( ::geos::logger::internal::rank == 0 && EXP ) \
55  { \
56  std::ostringstream oss; \
57  oss << msg; \
58  std::cout << oss.str() << std::endl; \
59  } \
60  } while( false )
61 
66 #define GEOS_LOG_RANK_0( msg ) GEOS_LOG_RANK_0_IF( true, msg )
67 
73 #if defined(GEOS_DEVICE_COMPILE)
74 #define GEOS_LOG_RANK_IF( EXP, msg )
75 #else
76 #define GEOS_LOG_RANK_IF( EXP, msg ) \
77  do { \
78  if( EXP ) \
79  { \
80  std::ostringstream oss; \
81  oss << "Rank " << ::geos::logger::internal::rankString << ": " << msg; \
82  *logger::internal::rankStream << oss.str() << std::endl; \
83  } \
84  } while( false )
85 #endif
86 
91 #define GEOS_LOG_RANK( msg ) GEOS_LOG_RANK_IF( true, msg )
92 
97 #define GEOS_LOG_RANK_VAR( var ) GEOS_LOG_RANK( #var " = " << var )
98 
104 #if defined(GEOS_DEVICE_COMPILE)
105 #define GEOS_ERROR_IF( EXP, msg ) LVARRAY_ERROR_IF( EXP, msg )
106 #else
107 #define GEOS_ERROR_IF( EXP, msg ) LVARRAY_ERROR_IF( EXP, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
108 #endif
109 
116 #define GEOS_THROW_IF( EXP, msg, TYPE ) LVARRAY_THROW_IF( EXP, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
117 
122 #define GEOS_ERROR( msg ) GEOS_ERROR_IF( true, msg )
123 
129 #define GEOS_THROW( msg, TYPE ) GEOS_THROW_IF( true, msg, TYPE )
130 
136 #define GEOS_ASSERT_MSG( EXP, msg ) LVARRAY_ASSERT_MSG( EXP, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
137 
142 #define GEOS_ASSERT( EXP ) GEOS_ASSERT_MSG( EXP, "" )
143 
149 #define GEOS_WARNING_IF( EXP, msg ) LVARRAY_WARNING_IF( EXP, msg )
150 
155 #define GEOS_WARNING( msg ) LVARRAY_WARNING( msg )
156 
162 #define GEOS_INFO_IF( EXP, msg ) LVARRAY_INFO_IF( EXP, msg )
163 
168 #define GEOS_INFO( msg ) LVARRAY_INFO( msg )
169 
176 #define GEOS_ERROR_IF_EQ_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_EQ_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
177 
185 #define GEOS_THROW_IF_EQ_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_EQ_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
186 
192 #define GEOS_ERROR_IF_EQ( lhs, rhs ) GEOS_ERROR_IF_EQ_MSG( lhs, rhs, "" )
193 
200 #define GEOS_THROW_IF_EQ( lhs, rhs, TYPE ) GEOS_THROW_IF_EQ_MSG( lhs, rhs, "", TYPE )
201 
208 #define GEOS_ERROR_IF_NE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_NE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
209 
217 #define GEOS_THROW_IF_NE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_NE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
218 
224 #define GEOS_ERROR_IF_NE( lhs, rhs ) GEOS_ERROR_IF_NE_MSG( lhs, rhs, "" )
225 
232 #define GEOS_THROW_IF_NE( lhs, rhs, TYPE ) GEOS_THROW_IF_NE_MSG( lhs, rhs, "", TYPE )
233 
240 #define GEOS_ERROR_IF_GT_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_GT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
241 
249 #define GEOS_THROW_IF_GT_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_GT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
250 
256 #define GEOS_ERROR_IF_GT( lhs, rhs ) GEOS_ERROR_IF_GT_MSG( lhs, rhs, "" )
257 
264 #define GEOS_THROW_IF_GT( lhs, rhs, TYPE ) GEOS_ERROR_IF_GT_MSG( lhs, rhs, "", TYPE )
265 
272 #define GEOS_ERROR_IF_GE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_GE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
273 
281 #define GEOS_THROW_IF_GE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_GE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
282 
288 #define GEOS_ERROR_IF_GE( lhs, rhs ) GEOS_ERROR_IF_GE_MSG( lhs, rhs, "" )
289 
296 #define GEOS_THROW_IF_GE( lhs, rhs, TYPE ) GEOS_ERROR_IF_GE_MSG( lhs, rhs, "", TYPE )
297 
304 #define GEOS_ERROR_IF_LT_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_LT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
305 
313 #define GEOS_THROW_IF_LT_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_LT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
314 
320 #define GEOS_ERROR_IF_LT( lhs, rhs ) GEOS_ERROR_IF_LT_MSG( lhs, rhs, "" )
321 
328 #define GEOS_THROW_IF_LT( lhs, rhs, TYPE ) GEOS_ERROR_IF_LT_MSG( lhs, rhs, "", TYPE )
329 
336 #define GEOS_ERROR_IF_LE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_LE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
337 
345 #define GEOS_THROW_IF_LE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_LE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
346 
352 #define GEOS_ERROR_IF_LE( lhs, rhs ) GEOS_ERROR_IF_LE_MSG( lhs, rhs, "" )
353 
360 #define GEOS_THROW_IF_LE( lhs, rhs, TYPE ) GEOS_ERROR_IF_LE_MSG( lhs, rhs, "", TYPE )
361 
368 #define GEOS_ASSERT_EQ_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_EQ_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
369 
375 #define GEOS_ASSERT_EQ( lhs, rhs ) GEOS_ASSERT_EQ_MSG( lhs, rhs, "" )
376 
383 #define GEOS_ASSERT_NE_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_NE_MSG( lhs, rhs, msg )
384 
390 #define GEOS_ASSERT_NE( lhs, rhs ) LVARRAY_ASSERT_NE( lhs, rhs )
391 
398 #define GEOS_ASSERT_GT_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_GT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
399 
405 #define GEOS_ASSERT_GT( lhs, rhs ) GEOS_ASSERT_GT_MSG( lhs, rhs, "" )
406 
413 #define GEOS_ASSERT_GE_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_GE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
414 
420 #define GEOS_ASSERT_GE( lhs, rhs ) GEOS_ASSERT_GE_MSG( lhs, rhs, "" )
421 
427 #define GEOS_LOG_LEVEL_FN( minLevel, fn ) \
428  do { \
429  if( this->getLogLevel() >= minLevel ) \
430  { \
431  fn; \
432  } \
433  } while( false )
434 
440 #define GEOS_LOG_LEVEL( minLevel, msg ) GEOS_INFO_IF( this->getLogLevel() >= minLevel, msg );
441 
447 #define GEOS_LOG_LEVEL_RANK_0( minLevel, msg ) GEOS_LOG_RANK_0_IF( this->getLogLevel() >= minLevel, msg )
448 
454 #define GEOS_LOG_LEVEL_BY_RANK( minLevel, msg ) GEOS_LOG_RANK_IF( this->getLogLevel() >= minLevel, msg )
455 
456 
457 namespace geos
458 {
459 
463 struct InputError : public std::runtime_error
464 {
469  InputError( std::string const & what ):
470  std::runtime_error( what )
471  {}
472 
477  InputError( char const * const what ):
478  std::runtime_error( what )
479  {}
480 
486  InputError( std::exception const & subException, std::string const & msgToInsert );
487 };
488 
492 struct SimulationError : public std::runtime_error
493 {
498  SimulationError( std::string const & what ):
499  std::runtime_error( what )
500  {}
501 
506  SimulationError( char const * const what ):
507  std::runtime_error( what )
508  {}
509 
516  SimulationError( std::exception const & subException, std::string const & msgToInsert );
517 };
518 
522 class NotAnError : public std::exception
523 {};
524 
525 namespace logger
526 {
527 
528 namespace internal
529 {
530 
531 extern int rank;
532 
533 extern std::string rankString;
534 
535 extern int n_ranks;
536 
537 extern std::ostream * rankStream;
538 
539 #if defined(GEOSX_USE_MPI)
540 extern MPI_Comm comm;
541 #endif
542 } // namespace internal
543 
544 #if defined(GEOSX_USE_MPI)
550 void InitializeLogger( MPI_Comm comm, const std::string & rank_output_dir="" );
551 #endif
552 
557 void InitializeLogger( const std::string & rank_output_dir="" );
558 
563 
564 } // namespace logger
565 
566 } // namespace geos
567 
568 #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.
Exception class used for special control flow.
Definition: Logger.hpp:523
std::string string
String type.
Definition: DataTypes.hpp:131
Exception class used to report errors in user input.
Definition: Logger.hpp:464
InputError(char const *const what)
Constructor.
Definition: Logger.hpp:477
InputError(std::exception const &subException, std::string const &msgToInsert)
Constructs an InputError from an underlying exception.
InputError(std::string const &what)
Constructor.
Definition: Logger.hpp:469
Exception class used to report errors in user input.
Definition: Logger.hpp:493
SimulationError(std::exception const &subException, std::string const &msgToInsert)
Construct a SimulationError from an underlying exception.
SimulationError(std::string const &what)
Constructor.
Definition: Logger.hpp:498
SimulationError(char const *const what)
Constructor.
Definition: Logger.hpp:506