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 Total, S.A
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 incldes
24 #include "common/GeosxConfig.hpp"
25 #include "common/GeosxMacros.hpp"
26 #include "common/format/Format.hpp"
27 #include "LvArray/src/Macros.hpp"
28 
29 // System includes
30 #include <stdexcept>
31 
32 #if defined(GEOS_USE_MPI)
33  #include <mpi.h>
34 #endif
35 
40 #define GEOS_LOG( ... ) LVARRAY_LOG( __VA_ARGS__ )
41 
46 #define GEOS_LOG_VAR( ... ) LVARRAY_LOG_VAR( __VA_ARGS__ )
47 
48 
54 #if defined(GEOS_DEVICE_COMPILE)
55 #define GEOS_LOG_IF( EXP, msg )
56 #else
57 #define GEOS_LOG_IF( EXP, msg ) \
58  do { \
59  if( EXP ) \
60  { \
61  std::cout<< msg << std::endl; \
62  } \
63  } while( false )
64 #endif
65 
66 
72 #define GEOS_LOG_RANK_0_IF( EXP, msg ) \
73  do { \
74  if( ::geos::logger::internal::rank == 0 && EXP ) \
75  { \
76  std::ostringstream oss; \
77  oss << msg; \
78  std::cout << oss.str() << std::endl; \
79  } \
80  } while( false )
81 
87 #define GEOS_LOG_RANK_0_IF_NLR( EXP, msg ) \
88  do { \
89  if( ::geos::logger::internal::rank == 0 && EXP ) \
90  { \
91  std::ostringstream oss; \
92  oss << msg; \
93  std::cout << oss.str(); \
94  } \
95  } while( false )
96 
101 #define GEOS_LOG_RANK_0( msg ) GEOS_LOG_RANK_0_IF( true, msg )
102 
108 #if defined(GEOS_DEVICE_COMPILE)
109 #define GEOS_LOG_RANK_IF( EXP, msg )
110 #else
111 #define GEOS_LOG_RANK_IF( EXP, msg ) \
112  do { \
113  if( EXP ) \
114  { \
115  std::ostringstream oss; \
116  oss << "Rank " << ::geos::logger::internal::rankString << ": " << msg; \
117  *logger::internal::rankStream << oss.str() << std::endl; \
118  } \
119  } while( false )
120 #endif
121 
126 #define GEOS_LOG_RANK( msg ) GEOS_LOG_RANK_IF( true, msg )
127 
132 #define GEOS_LOG_RANK_VAR( var ) GEOS_LOG_RANK( #var " = " << var )
133 
139 #if defined(GEOS_DEVICE_COMPILE)
140 #define GEOS_ERROR_IF( EXP, msg ) LVARRAY_ERROR_IF( EXP, msg )
141 #else
142 #define GEOS_ERROR_IF( EXP, msg ) LVARRAY_ERROR_IF( EXP, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
143 #endif
144 
151 #define GEOS_THROW_IF( EXP, msg, TYPE ) LVARRAY_THROW_IF( EXP, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
152 
157 #define GEOS_ERROR( msg ) GEOS_ERROR_IF( true, msg )
158 
164 #define GEOS_THROW( msg, TYPE ) GEOS_THROW_IF( true, msg, TYPE )
165 
171 #define GEOS_ASSERT_MSG( EXP, msg ) LVARRAY_ASSERT_MSG( EXP, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
172 
177 #define GEOS_ASSERT( EXP ) GEOS_ASSERT_MSG( EXP, "" )
178 
184 #define GEOS_WARNING_IF( EXP, msg ) LVARRAY_WARNING_IF( EXP, msg )
185 
190 #define GEOS_WARNING( msg ) LVARRAY_WARNING( msg )
191 
197 #define GEOS_INFO_IF( EXP, msg ) LVARRAY_INFO_IF( EXP, msg )
198 
203 #define GEOS_INFO( msg ) LVARRAY_INFO( msg )
204 
211 #define GEOS_ERROR_IF_EQ_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_EQ_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
212 
220 #define GEOS_THROW_IF_EQ_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_EQ_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
221 
227 #define GEOS_ERROR_IF_EQ( lhs, rhs ) GEOS_ERROR_IF_EQ_MSG( lhs, rhs, "" )
228 
235 #define GEOS_THROW_IF_EQ( lhs, rhs, TYPE ) GEOS_THROW_IF_EQ_MSG( lhs, rhs, "", TYPE )
236 
243 #define GEOS_ERROR_IF_NE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_NE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
244 
252 #define GEOS_THROW_IF_NE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_NE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
253 
259 #define GEOS_ERROR_IF_NE( lhs, rhs ) GEOS_ERROR_IF_NE_MSG( lhs, rhs, "" )
260 
267 #define GEOS_THROW_IF_NE( lhs, rhs, TYPE ) GEOS_THROW_IF_NE_MSG( lhs, rhs, "", TYPE )
268 
275 #define GEOS_ERROR_IF_GT_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_GT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
276 
284 #define GEOS_THROW_IF_GT_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_GT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
285 
291 #define GEOS_ERROR_IF_GT( lhs, rhs ) GEOS_ERROR_IF_GT_MSG( lhs, rhs, "" )
292 
299 #define GEOS_THROW_IF_GT( lhs, rhs, TYPE ) GEOS_ERROR_IF_GT_MSG( lhs, rhs, "", TYPE )
300 
307 #define GEOS_ERROR_IF_GE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_GE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
308 
316 #define GEOS_THROW_IF_GE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_GE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
317 
323 #define GEOS_ERROR_IF_GE( lhs, rhs ) GEOS_ERROR_IF_GE_MSG( lhs, rhs, "" )
324 
331 #define GEOS_THROW_IF_GE( lhs, rhs, TYPE ) GEOS_ERROR_IF_GE_MSG( lhs, rhs, "", TYPE )
332 
339 #define GEOS_ERROR_IF_LT_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_LT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
340 
348 #define GEOS_THROW_IF_LT_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_LT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
349 
355 #define GEOS_ERROR_IF_LT( lhs, rhs ) GEOS_ERROR_IF_LT_MSG( lhs, rhs, "" )
356 
363 #define GEOS_THROW_IF_LT( lhs, rhs, TYPE ) GEOS_ERROR_IF_LT_MSG( lhs, rhs, "", TYPE )
364 
371 #define GEOS_ERROR_IF_LE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_LE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
372 
380 #define GEOS_THROW_IF_LE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_LE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg, TYPE )
381 
387 #define GEOS_ERROR_IF_LE( lhs, rhs ) GEOS_ERROR_IF_LE_MSG( lhs, rhs, "" )
388 
395 #define GEOS_THROW_IF_LE( lhs, rhs, TYPE ) GEOS_ERROR_IF_LE_MSG( lhs, rhs, "", TYPE )
396 
403 #define GEOS_ASSERT_EQ_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_EQ_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
404 
410 #define GEOS_ASSERT_EQ( lhs, rhs ) GEOS_ASSERT_EQ_MSG( lhs, rhs, "" )
411 
418 #define GEOS_ASSERT_NE_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_NE_MSG( lhs, rhs, msg )
419 
425 #define GEOS_ASSERT_NE( lhs, rhs ) LVARRAY_ASSERT_NE( lhs, rhs )
426 
433 #define GEOS_ASSERT_GT_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_GT_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
434 
440 #define GEOS_ASSERT_GT( lhs, rhs ) GEOS_ASSERT_GT_MSG( lhs, rhs, "" )
441 
448 #define GEOS_ASSERT_GE_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_GE_MSG( lhs, rhs, "***** Rank " << ::geos::logger::internal::rankString << ": " << msg )
449 
455 #define GEOS_ASSERT_GE( lhs, rhs ) GEOS_ASSERT_GE_MSG( lhs, rhs, "" )
456 
462 #define GEOS_LOG_LEVEL_FN( minLevel, fn ) \
463  do { \
464  if( this->getLogLevel() >= minLevel ) \
465  { \
466  fn; \
467  } \
468  } while( false )
469 
476 #define GEOS_LOG_LEVEL( minLevel, msg ) GEOS_LOG_IF( this->getLogLevel() >= minLevel, msg );
477 
484 #define GEOS_LOG_LEVEL_RANK_0( minLevel, msg ) GEOS_LOG_RANK_0_IF( this->getLogLevel() >= minLevel, msg )
485 
492 #define GEOS_LOG_LEVEL_BY_RANK( minLevel, msg ) GEOS_LOG_RANK_IF( this->getLogLevel() >= minLevel, msg )
493 
494 
495 namespace geos
496 {
497 
501 struct InputError : public std::runtime_error
502 {
507  InputError( std::string const & what ):
508  std::runtime_error( what )
509  {}
510 
515  InputError( char const * const what ):
516  std::runtime_error( what )
517  {}
518 
524  InputError( std::exception const & subException, std::string const & msgToInsert );
525 };
526 
530 struct SimulationError : public std::runtime_error
531 {
536  SimulationError( std::string const & what ):
537  std::runtime_error( what )
538  {}
539 
544  SimulationError( char const * const what ):
545  std::runtime_error( what )
546  {}
547 
554  SimulationError( std::exception const & subException, std::string const & msgToInsert );
555 };
556 
563 struct BadTypeError : public std::runtime_error
564 {
569  BadTypeError( std::string const & what ):
570  std::runtime_error( what )
571  {}
572 };
573 
577 class NotAnError : public std::exception
578 {};
579 
580 namespace logger
581 {
582 
583 namespace internal
584 {
585 
586 extern int rank;
587 
588 extern std::string rankString;
589 
590 extern int n_ranks;
591 
592 extern std::ostream * rankStream;
593 
594 #if defined(GEOS_USE_MPI)
595 extern MPI_Comm comm;
596 #endif
597 } // namespace internal
598 
599 #if defined(GEOS_USE_MPI)
605 void InitializeLogger( MPI_Comm comm, const std::string & rank_output_dir="" );
606 #endif
607 
612 void InitializeLogger( const std::string & rank_output_dir="" );
613 
618 
619 } // namespace logger
620 
621 } // namespace geos
622 
623 #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:578
std::string string
String type.
Definition: DataTypes.hpp:91
Exception class used to report errors from type conversion.
Definition: Logger.hpp:564
BadTypeError(std::string const &what)
Constructor.
Definition: Logger.hpp:569
Exception class used to report errors in user input.
Definition: Logger.hpp:502
InputError(char const *const what)
Constructor.
Definition: Logger.hpp:515
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:507
Exception class used to report errors in user input.
Definition: Logger.hpp:531
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:536
SimulationError(char const *const what)
Constructor.
Definition: Logger.hpp:544