1717#include < Common/logger_useful.h>
1818
1919#include < algorithm>
20+ #include < atomic>
2021#include < cstdlib>
2122#include < cstring>
2223#include < filesystem>
@@ -37,6 +38,7 @@ namespace ErrorCodes
3738 extern const int LOGICAL_ERROR;
3839 extern const int CANNOT_ALLOCATE_MEMORY;
3940 extern const int CANNOT_MREMAP;
41+ extern const int POTENTIALLY_BROKEN_DATA_PART;
4042}
4143
4244void abortOnFailedAssertion (const String & description, void * const * trace, size_t trace_offset, size_t trace_size)
@@ -263,6 +265,25 @@ void Exception::clearThreadFramePointers()
263265 thread_frame_pointers.frame_pointers .clear ();
264266}
265267
268+ bool Exception::isErrorCodeImportant () const
269+ {
270+ const int error_code = code ();
271+ return error_code == ErrorCodes::LOGICAL_ERROR
272+ || error_code == ErrorCodes::POTENTIALLY_BROKEN_DATA_PART;
273+ }
274+
275+ Exception::~Exception ()
276+ try
277+ {
278+ if (logged != nullptr && !logged->load (std::memory_order_relaxed) && isErrorCodeImportant () && isLoggingEnabled ())
279+ {
280+ LOG_ERROR (getLogger (" ForcedCriticalErrorsLogger" ), " {}" , getExceptionMessage (*this , /* with_stacktrace=*/ true ));
281+ }
282+ }
283+ catch (...) // NOLINT(bugprone-empty-catch)
284+ {
285+ }
286+
266287static void tryLogCurrentExceptionImpl (Poco::Logger * logger, const std::string & start_of_message, LogsLevel level)
267288{
268289 if (!isLoggingEnabled ())
@@ -290,6 +311,19 @@ static void tryLogCurrentExceptionImpl(Poco::Logger * logger, const std::string
290311 catch (...) // NOLINT(bugprone-empty-catch)
291312 {
292313 }
314+
315+ // / Mark the exception as logged.
316+ try
317+ {
318+ throw ;
319+ }
320+ catch (Exception & e)
321+ {
322+ e.markAsLogged ();
323+ }
324+ catch (...) // NOLINT(bugprone-empty-catch)
325+ {
326+ }
293327}
294328
295329void tryLogCurrentException (const char * log_name, const std::string & start_of_message, LogsLevel level)
0 commit comments