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 #include "common/format/Format.hpp"
26 #include <mutex>
27 
28 namespace geos
29 {
30 
37 {
38 
43  enum class Attribute
44  {
45  InputFile,
46  InputLine,
47  DataPath,
48  DetectionLoc,
49  Signal,
50  };
51 
54 
65 
71 
77  ErrorContext( string formattedContext, map< Attribute, std::string > attributes ):
78  m_formattedContext( formattedContext ),
79  m_attributes( attributes ) {};
80 
87  ErrorContext( string formattedContext, map< Attribute, std::string > attributes, integer priority ):
88  m_formattedContext( formattedContext ),
89  m_attributes( attributes ),
90  m_priority( priority ) {};
91 
99  { m_priority = priority; return *this; }
100 
107 
108 };
109 
114 enum class MsgType
115 {
116  Error,
117  ExternalError,
118  Warning,
119  Exception,
120  Undefined
121 };
122 
127 {
129  MsgType m_type = MsgType::Undefined;
135  std::set< int > m_ranksInfo;
141  std::vector< ErrorContext > m_contextsInfo;
143  std::vector< std::string > m_sourceCallStack;
145  bool m_isValidStackTrace = false;
146 };
147 
152 {
153 public:
154 
164  MsgType msgType,
165  std::string_view msgContent,
166  integer rank );
167 
174 
181  DiagnosticMsgBuilder & addToMsg( std::exception const & e, bool toEnd = false );
182 
189  DiagnosticMsgBuilder & addToMsg( std::string_view msg, bool toEnd = false );
190 
197  template< typename ... Args >
198  DiagnosticMsgBuilder & addContextInfo( Args && ... args )
199  {
200  ( this->addContextInfoImpl( ErrorContext( args ) ), ... );
201  return *this;
202  }
203 
210 
219  DiagnosticMsgBuilder & addSignal( integer sig, bool toEnd = false );
220 
228 
235 
242 
249 
256 
261 
262 private:
263 
269  m_errorMsg( msg ){}
270 
275  DiagnosticMsgBuilder & addContextInfoImpl( ErrorContext && ctxInfo );
276 
278  DiagnosticMsg & m_errorMsg;
279 };
280 
285 {
286 
287 public:
288 
297 
302  void createFile();
303 
308  void enableFileOutput( bool value )
309  { m_writeYaml = value; }
310 
314  bool isOutputFileEnabled() const
315  { return m_writeYaml; }
316 
323  { m_filename = filename; }
324 
329  { return m_filename; }
330 
336  static std::string toString( MsgType type );
337 
341  std::ostream const & getErrorStream() const
342  { return m_stream; }
343 
348  { return m_getCurrentExceptionMsg;}
349 
359  std::string_view msgContent,
360  integer rank );
361 
367  { return DiagnosticMsgBuilder::modify( m_getCurrentExceptionMsg ); }
368 
374 
381  void flushErrorMsg( DiagnosticMsg & errMsg );
382 
388  static void formatMsgForLog( DiagnosticMsg const & errMsg, std::ostream & os );
389 
394  void writeToLogStream( DiagnosticMsg & errMsg );
395 
396 private:
397 
399  DiagnosticMsg m_getCurrentExceptionMsg;
401  bool m_writeYaml = false;
403  std::string_view m_filename = "errors.yaml";
405  std::ostream & m_stream = std::cout;
407  std::mutex m_errorHandlerAsciiMutex;
409  std::mutex m_errorHandlerYamlMutex;
410 
415  void writeToYamlStream( DiagnosticMsg & errMsg );
416 
423  void streamMultilineYamlAttribute( std::string_view msg, std::ofstream & yamlFile,
424  std::string_view indent );
425 };
426 
427 
428 
429 } /* namespace geos */
430 
431 #endif
#define GEOS_HOST
Marks a host-only function.
Definition: GeosxMacros.hpp:45
Builder class for constructing DiagnosticMsg objects.
DiagnosticMsgBuilder & addRank(integer rank)
Add a rank on which the error has been raised.
DiagnosticMsgBuilder & addContextInfo(Args &&... args)
Adds one or more context elements to the error.
DiagnosticMsgBuilder & addDetectionLocation(string_view detectionLocation)
Add where the detection occured.
DiagnosticMsgBuilder & setCause(std::string_view cause)
Set the cause of the error.
DiagnosticMsgBuilder & setCodeLocation(std::string_view msgFile, integer msgLine)
Set the source code location values (file and line where the error is detected)
DiagnosticMsgBuilder & addCallStackInfo(std::string_view stacktrace)
Add the stack trace information about the error.
DiagnosticMsgBuilder & addToMsg(std::exception const &e, bool toEnd=false)
Append exception text to the message.
DiagnosticMsgBuilder & addSignal(integer sig, bool toEnd=false)
Add the signal to the DiagnosticMsg.
static DiagnosticMsgBuilder init(DiagnosticMsg &msg, MsgType msgType, std::string_view msgContent, integer rank)
Initialize a new DiagnosticMsg.
DiagnosticMsg & getDiagnosticMsg()
DiagnosticMsgBuilder & addToMsg(std::string_view msg, bool toEnd=false)
Append text to the message.
DiagnosticMsgBuilder & setType(MsgType msgType)
Set the type of the error, (amoung one of the MsgType)
static DiagnosticMsgBuilder modify(DiagnosticMsg &errorMsg)
Modify an existing DiagnosticMsg.
Logger for formatting and outputting diagnostics.
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".
std::ostream const & getErrorStream() const
DiagnosticMsgBuilder modifyCurrentExceptionMessage()
Modify/Continue building the current exception message.
void createFile()
Create the YAML file or overwrite the contents if a YAML file of the same name already exists And wri...
static void formatMsgForLog(DiagnosticMsg const &errMsg, std::ostream &os)
Format all information in ErrorMsg and write it to the specified output stream.
DiagnosticMsgBuilder initCurrentExceptionMessage(MsgType msgType, std::string_view msgContent, integer rank)
Start building a new exception message.
void writeToLogStream(DiagnosticMsg &errMsg)
Write the ErrorMsg into the log stream output stream.
std::string_view getOutputFilename()
void enableFileOutput(bool value)
Enable the YAML file output, which is false by default.
static GEOS_HOST ErrorLogger & global()
bool isOutputFileEnabled() const
DiagnosticMsg const & getCurrentExceptionMsg() const
void flushErrorMsg(DiagnosticMsg &errMsg)
Write all the information retrieved about the diagnostic message into the instance outputs (stream sp...
void flushCurrentExceptionMessage()
Write all the information retrieved about the current exception message into the instance outputs (st...
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
Struct to construct the diagnostic message object.
std::set< int > m_ranksInfo
the rank(s) on which the diagnostic occured
std::vector< ErrorContext > m_contextsInfo
Additional information about the diagnostic in the input file.
std::string m_file
the source location file
integer m_line
the source location line (default is 0)
MsgType m_type
Type of diagnostic (Warning, Error or Exception)
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)
bool m_isValidStackTrace
Indicates whether the stored call stack trace is valid and usable.
std::string m_msg
the message that can be completed
ErrorContext(string formattedContext, map< Attribute, std::string > attributes)
Construct to initialize ErrorContext.
integer m_priority
Priority level assigned to an error context.
static std::string attributeToString(Attribute attribute)
Convert a value from the Attribute enumeration to a string.
string m_formattedContext
String containing the target object name followed by the the file and line declaring it.
ErrorContext & setPriority(integer priority)
Set the priority value of the current error context information. This way the different context infor...
ErrorContext(string formattedContext, map< Attribute, std::string > attributes, integer priority)
Construct to initialize ErrorContext given a string containing the context and his priority.
map< Attribute, std::string > m_attributes
The map contains contextual information about the error It could be something like "file" = "/path/to...
Geos Exception used in GEOS_THROW.