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  };
66 
75 
82 
89  { m_priority = priority; return *this; }
90 
97  };
98 
102  struct ErrorMsg
103  {
105  MsgType m_type = ErrorLogger::MsgType::Undefined;
111  std::set< int > m_ranksInfo;
117  std::vector< ErrorContext > m_contextsInfo;
119  std::vector< std::string > m_sourceCallStack;
120 
125  {}
126 
135  ErrorMsg( MsgType msgType,
136  std::string_view msgContent,
137  integer rank,
138  std::string_view msgFile,
139  integer msgLine )
140  : m_type( msgType ), m_msg( msgContent ), m_ranksInfo( {rank} ), m_file( msgFile ), m_line( msgLine ) {}
141 
149  ErrorMsg & addToMsg( std::exception const & e, bool toEnd = false );
150 
158  ErrorMsg & addToMsg( std::string_view msg, bool toEnd = false );
159 
167 
173  ErrorMsg & setType( MsgType msgType );
174 
181 
187  ErrorMsg & addRank( int rank );
188 
195 
202  template< typename ... Args >
203  ErrorMsg & addContextInfo( Args && ... args );
204 
208  bool isValidStackTrace() const
209  { return m_isValidStackTrace; }
210 
211 private:
216  void addContextInfoImpl( ErrorContext && ctxInfo );
217 
218  bool m_isValidStackTrace = false;
219  };
220 
229 
233  bool isOutputFileEnabled() const
234  { return m_writeYaml; }
235 
240  void enableFileOutput( bool value )
241  { m_writeYaml = value; }
242 
249  { m_filename = filename; }
250 
255  { return m_filename; }
256 
264  { return m_currentErrorMsg; }
265 
270  void createFile();
271 
277  static std::string toString( MsgType type );
278 
284  void flushErrorMsg( ErrorMsg & errorMsg );
285 
286 private:
288  ErrorMsg m_currentErrorMsg;
290  bool m_writeYaml = false;
292  std::string_view m_filename = "errors.yaml";
293 
299  void streamMultilineYamlAttribute( std::string_view msg, std::ofstream & yamlFile,
300  std::string_view indent );
301 };
302 
304 
305 template< typename ... Args >
307 {
308  ( this->addContextInfoImpl( ErrorContext( args ) ), ... );
309  return *this;
310 }
311 
313 
314 } /* namespace geos */
315 
316 #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.
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)