GEOS
ExternalErrorHandler.hpp
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 
52 #ifndef LOGGER_EXTERNALERRORHANDLER_HPP
53 #define LOGGER_EXTERNALERRORHANDLER_HPP
54 
55 #include "ErrorHandling.hpp"
56 
57 #include <functional>
58 #include <string_view>
59 #include <string>
60 #include <memory>
61 
62 namespace geos
63 {
64 
69 {
70 public:
71 
73  using PosixId = int;
74 
79  using LineHandlingFunctor = std::function< void (std::string_view, std::string_view) >;
80 
87 
92 
99  void flush( LineHandlingFunctor const & lineProcessingFunctor, std::string_view detectionLocation );
100 
101 private:
102  struct Pipe
103  {
105  PosixId fileDescriptorsArray[2];
106 
110  void create();
111 
115  PosixId & readEnd()
116  { return fileDescriptorsArray[0]; }
117 
121  PosixId & writeEnd()
122  { return fileDescriptorsArray[1]; }
123 
132  bool setDescriptorInheritanceMode( bool inherit );
133 
141  bool redirectWriteEnd( int targetFd );
142 
146  void closePipe();
147  };
148 
150  static constexpr PosixId m_disabledPipeEnd = -1;
151 
153  static constexpr PosixId m_errorResult = -1;
154 
156  PosixId m_redirectedStream;
157 
159  PosixId m_originalStreamTarget;
160 
162  Pipe m_deviationPipe;
163 
165  std::string m_unprocessedData;
166 
175  static bool setPipeEndBlockingMode( PosixId pipeEnd, bool nonBlocking );
176 
183  static int duplicateDescriptor( int pipeEnd );
184 
190  void prepareStreamingBuffer();
191 
196  static void closePipeEnd( PosixId & pipeEnd );
197 
202  static string getLastPosixErrorString();
203 };
204 
211 {
212 public:
213 
220 
227 
232 
239  void setErrorHandling( ErrorHandlingFunctor && errorHandlingFunctor )
240  { m_processErrorFunctor = errorHandlingFunctor; }
241 
248  void enableStderrPipeDeviation( bool enable );
249 
256  void flush( std::string_view detectionLocation );
257 
264  static void defaultErrorHandling( std::string_view errorMsg, std::string_view detectionLocation );
265 
266 private:
267  std::unique_ptr< OutputStreamDeviation > m_stderrDeviation;
268 
269  ErrorHandlingFunctor m_processErrorFunctor;
270 
272 };
273 
274 } /* namespace geos */
275 
276 #endif
This file provides the infrastructure to capture external errors.
Class to handle external error capture. This class role is to capture and process external error mess...
~ExternalErrorHandler()
Destructor, disable all error piping features.
void setErrorHandling(ErrorHandlingFunctor &&errorHandlingFunctor)
Set the function that process the external errors that have been captured. The processing typically c...
void flush(std::string_view detectionLocation)
Process all awaiting captured errors that were produced externally, then clear the error stream.
void enableStderrPipeDeviation(bool enable)
Enable capture of errors piped from the std::cerr stream. Helpful to capture GLIBC errors,...
static ExternalErrorHandler & instance()
Strinct singleton pattern has been choosen since we will only have single sources of external errors ...
static void defaultErrorHandling(std::string_view errorMsg, std::string_view detectionLocation)
Not designed for direct calls, error handling function in default use if never calling setErrorHandli...
OutputStreamDeviation::LineHandlingFunctor ErrorHandlingFunctor
A functor executed for each error mesage to process, taking the message as the 1st string_view parame...
This class implements pipe redirection to allow to capture and process externally streamed messages.
std::function< void(std::string_view, std::string_view) > LineHandlingFunctor
A functor executed for each independant lines to process, taking the line as the 1st string_view para...
OutputStreamDeviation(PosixId fileNo)
Construct and enable a new pipe redirection.
void flush(LineHandlingFunctor const &lineProcessingFunctor, std::string_view detectionLocation)
Flush the buffer from the original output pipe in a string, allowing to log it where needed.
~OutputStreamDeviation()
Destroy the OutputStreamDeviation object, restoring the original pipe state.
int PosixId
Posix identifier, can be a file handle, an error number... Must be consistent with posix functions.
std::string string
String type.
Definition: DataTypes.hpp:90
std::string_view string_view
String type.
Definition: DataTypes.hpp:93