GEOS
ErrorHandling.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 INITIALIZATION_ERROR_LOGGER_HPP
21 #define INITIALIZATION_ERROR_LOGGER_HPP
22 
23 #include "common/DataTypes.hpp"
24 
25 namespace geos
26 {
27 
33 {
34 
35 public:
40  enum class MsgType
41  {
42  Error,
43  Warning,
44  Exception,
45  Undefined
46  };
47 
53  struct ErrorContext
54  {
55 
60  enum class Attribute
61  {
62  InputFile,
63  InputLine,
64  DataPath,
65  DetectionLoc,
66  Signal,
67  };
68 
77 
84 
91  { m_priority = priority; return *this; }
92 
99  };
100 
104  struct ErrorMsg
105  {
107  MsgType m_type = ErrorLogger::MsgType::Undefined;
113  std::set< int > m_ranksInfo;
119  std::vector< ErrorContext > m_contextsInfo;
121  std::vector< std::string > m_sourceCallStack;
122 
127  {}
128 
137  ErrorMsg( MsgType msgType,
138  std::string_view msgContent,
139  integer rank,
140  std::string_view msgFile,
141  integer msgLine )
142  : m_type( msgType ), m_msg( msgContent ), m_ranksInfo( {rank} ), m_file( msgFile ), m_line( msgLine ) {}
143 
151  ErrorMsg & addToMsg( std::exception const & e, bool toEnd = false );
152 
160  ErrorMsg & addToMsg( std::string_view msg, bool toEnd = false );
161 
170  ErrorMsg & addSignalToMsg( int signal, bool toEnd = false );
171 
179 
185  ErrorMsg & setType( MsgType msgType );
186 
193 
199  ErrorMsg & addRank( int rank );
200 
207 
214  template< typename ... Args >
215  ErrorMsg & addContextInfo( Args && ... args );
216 
220  bool isValidStackTrace() const
221  { return m_isValidStackTrace; }
222 
223 private:
228  void addContextInfoImpl( ErrorContext && ctxInfo );
229 
230  bool m_isValidStackTrace = false;
231  };
232 
241 
245  bool isOutputFileEnabled() const
246  { return m_writeYaml; }
247 
252  void enableFileOutput( bool value )
253  { m_writeYaml = value; }
254 
261  { m_filename = filename; }
262 
267  { return m_filename; }
268 
276  { return m_currentErrorMsg; }
277 
282  void createFile();
283 
289  static std::string toString( MsgType type );
290 
296  void flushErrorMsg( ErrorMsg & errorMsg );
297 
298 private:
300  ErrorMsg m_currentErrorMsg;
302  bool m_writeYaml = false;
304  std::string_view m_filename = "errors.yaml";
305 
311  void streamMultilineYamlAttribute( std::string_view msg, std::ofstream & yamlFile,
312  std::string_view indent );
313 };
314 
316 
317 template< typename ... Args >
319 {
320  ( this->addContextInfoImpl( ErrorContext( args ) ), ... );
321  return *this;
322 }
323 
325 
326 } /* namespace geos */
327 
328 #endif
#define GEOS_HOST
Marks a host-only function.
Definition: GeosxMacros.hpp:45
Class to format and write different error/warning information that occured during the initialization.
static std::string toString(MsgType type)
Convert a MsgType into a string.
void setOutputFilename(std::string_view filename)
Set the name of the YAML file if specified by user default is "errors.yaml".
void createFile()
Create the YAML file or overwrite the contents if a YAML file of the same name already exists And wri...
std::string_view getOutputFilename()
ErrorMsg & currentErrorMsg()
Gives acces to the error message that is currently being constructed, potencially at various applicat...
void enableFileOutput(bool value)
Enable the YAML file output, which is false by default.
static GEOS_HOST ErrorLogger & global()
bool isOutputFileEnabled() const
void flushErrorMsg(ErrorMsg &errorMsg)
Write all the information retrieved about the error/warning message into the YAML file and reset the ...
Base template for ordered and unordered maps.
std::string string
String type.
Definition: DataTypes.hpp:90
int integer
Signed integer type.
Definition: DataTypes.hpp:81
std::string_view string_view
String type.
Definition: DataTypes.hpp:93
static std::string attributeToString(Attribute attribute)
Convert a value from the Attribute enumeration to a string.
map< Attribute, std::string > m_attributes
ErrorContext & setPriority(integer priority)
Set the priority value of the current error context information.
integer m_priority
Priority level assigned to an error context.
Struct to construct the error/warning object.
std::vector< std::string > m_sourceCallStack
the stack trace
std::string m_cause
the cause of the error (erroneous condition, failed assertion...) if identified (optional)
ErrorMsg & setCause(std::string_view cause)
Set the cause of the error.
ErrorMsg & addContextInfo(Args &&... args)
Adds one or more context elements to the error.
std::vector< ErrorContext > m_contextsInfo
Additional information about the error in the input file.
ErrorMsg & setCodeLocation(std::string_view msgFile, integer msgLine)
Set the source code location values (file and line where the error is detected)
std::set< int > m_ranksInfo
the rank(s) on which the error occured
ErrorMsg()
Construct a default Error Message.
integer m_line
the source location line corresponding to the error in the code (default is 0)
ErrorMsg(MsgType msgType, std::string_view msgContent, integer rank, std::string_view msgFile, integer msgLine)
Construct a new Error Message from parameters.
ErrorMsg & setType(MsgType msgType)
Set the type of the error.
ErrorMsg & addRank(int rank)
Add a rank on which the error has been raised.
ErrorMsg & addToMsg(std::exception const &e, bool toEnd=false)
Add text to the current error msg.
ErrorMsg & addToMsg(std::string_view msg, bool toEnd=false)
Add text to the current error msg.
ErrorMsg & addCallStackInfo(std::string_view ossStackTrace)
Add stack trace information about the error.
ErrorMsg & addSignalToMsg(int signal, bool toEnd=false)
Add text to the error msg that occured according to the specified signal.
std::string m_file
the source location file corresponding to the error in the code
std::string m_msg
the error message that can be completed
MsgType m_type
the error type (Warning, Error or Exception)