blob: b692777a53e2aef053b6db82ec754fdea42434c4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
// file : butl/diagnostics.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <butl/diagnostics>
#include <mutex>
#include <iostream> // cerr
using namespace std;
namespace butl
{
ostream* diag_stream = &cerr;
static mutex diag_mutex;
diag_lock::diag_lock ()
{
diag_mutex.lock ();
}
diag_lock::~diag_lock ()
{
diag_mutex.unlock ();
}
void diag_record::
flush () const
{
if (!empty_)
{
if (epilogue_ == nullptr)
{
os.put ('\n');
{
diag_lock l;
*diag_stream << 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 ();
empty_ = true;
}
else
{
// Clear the epilogue in case it calls us back.
//
auto e (epilogue_);
epilogue_ = nullptr;
e (*this); // Can throw.
flush (); // Call ourselves to write the data in case it returns.
}
}
}
diag_record::
~diag_record () noexcept (false)
{
// Don't flush the record if this destructor was called as part of the
// stack unwinding.
//
#ifdef __cpp_lib_uncaught_exceptions
if (uncaught_ == std::uncaught_exceptions ())
flush ();
#else
// Fallback implementation. Right now this means we cannot use this
// mechanism in destructors, which is not a big deal, except for one
// place: exception_guard. Thus the ugly special check.
//
if (!std::uncaught_exception () || exception_unwinding_dtor ())
flush ();
#endif
}
}
|