From 646ff7d9933b550b65b8377b3fa1b9bd85056cb3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 28 Jun 2019 13:07:21 +0200 Subject: Add ability to specify custom diag_record writer --- libbutl/diagnostics.cxx | 27 +++++++++++++++++++-------- libbutl/diagnostics.mxx | 5 +++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/libbutl/diagnostics.cxx b/libbutl/diagnostics.cxx index 43d931f..d9a979e 100644 --- a/libbutl/diagnostics.cxx +++ b/libbutl/diagnostics.cxx @@ -155,6 +155,23 @@ namespace butl diag_mutex.unlock (); } + static void + default_writer (const diag_record& r) + { + r.os.put ('\n'); + diag_stream_lock () << r.os.str (); + + // We can endup flushing the result of several writes. The last one may + // possibly be incomplete, but that's not a problem as it will also be + // followed by the flush() call. + // + // @@ Strange: why not just hold the lock for both write and flush? + // + diag_stream->flush (); + } + + void (*diag_record::writer) (const diag_record&) = &default_writer; + void diag_record:: flush () const { @@ -162,14 +179,8 @@ namespace butl { if (epilogue_ == nullptr) { - os.put ('\n'); - diag_stream_lock () << os.str (); - - // We can endup flushing the result of several writes. The last one may - // possibly be incomplete, but that's not a problem as it will also be - // followed by the flush() call. - // - diag_stream->flush (); + if (writer != nullptr) + writer (*this); empty_ = true; } diff --git a/libbutl/diagnostics.mxx b/libbutl/diagnostics.mxx index 43f13ad..c9ade44 100644 --- a/libbutl/diagnostics.mxx +++ b/libbutl/diagnostics.mxx @@ -179,6 +179,11 @@ LIBBUTL_MODEXPORT namespace butl diag_record (const diag_record&) = delete; diag_record& operator= (const diag_record&) = delete; + // Diagnostics writer. The default implementation writes the record text + // to diag_stream. If it is NULL, then the record text is ignored. + // + static void (*writer) (const diag_record&); + protected: #ifdef __cpp_lib_uncaught_exceptions const int uncaught_; -- cgit v1.1