1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Tim King, Aina Niemetz |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Wrappers to handle memory management of ostreams. |
14 |
|
* |
15 |
|
* This file contains wrappers to handle special cases of managing memory |
16 |
|
* related to ostreams. |
17 |
|
*/ |
18 |
|
|
19 |
|
#include "smt/managed_ostreams.h" |
20 |
|
|
21 |
|
#include <ostream> |
22 |
|
|
23 |
|
#include "base/check.h" |
24 |
|
#include "options/open_ostream.h" |
25 |
|
#include "options/option_exception.h" |
26 |
|
#include "options/smt_options.h" |
27 |
|
#include "smt/update_ostream.h" |
28 |
|
|
29 |
|
namespace cvc5 { |
30 |
|
|
31 |
30279 |
ManagedOstream::ManagedOstream() : d_managed(NULL) {} |
32 |
|
|
33 |
60558 |
ManagedOstream::~ManagedOstream() { |
34 |
30279 |
manage(NULL); |
35 |
30279 |
Assert(d_managed == NULL); |
36 |
30279 |
} |
37 |
|
|
38 |
|
void ManagedOstream::set(const std::string& filename) { |
39 |
|
std::pair<bool, std::ostream*> pair = open(filename); |
40 |
|
initialize(pair.second); |
41 |
|
manage(pair.first ? pair.second : NULL); |
42 |
|
} |
43 |
|
|
44 |
|
std::pair<bool, std::ostream*> ManagedOstream::open(const std::string& filename) |
45 |
|
const { |
46 |
|
OstreamOpener opener(getName()); |
47 |
|
addSpecialCases(&opener); |
48 |
|
return opener.open(filename); |
49 |
|
} |
50 |
|
|
51 |
30279 |
void ManagedOstream::manage(std::ostream* new_managed_value) { |
52 |
30279 |
if(d_managed == new_managed_value){ |
53 |
|
// This is a no-op. |
54 |
|
} else { |
55 |
|
Assert(d_managed != new_managed_value); |
56 |
|
std::ostream* old_managed_value = d_managed; |
57 |
|
d_managed = new_managed_value; |
58 |
|
if(old_managed_value != NULL){ |
59 |
|
delete old_managed_value; |
60 |
|
} |
61 |
|
} |
62 |
30279 |
} |
63 |
|
|
64 |
20186 |
ManagedDumpOStream::~ManagedDumpOStream() { |
65 |
10093 |
if(Dump.getStreamPointer() == getManagedOstream()) { |
66 |
|
Dump.setStream(&null_os); |
67 |
|
} |
68 |
10093 |
} |
69 |
|
|
70 |
|
std::string ManagedDumpOStream::defaultSource() const{ |
71 |
10092 |
return options::dumpToFileName(); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
void ManagedDumpOStream::initialize(std::ostream* outStream) { |
76 |
|
#ifdef CVC5_DUMPING |
77 |
|
DumpOstreamUpdate dumpGetStream; |
78 |
|
dumpGetStream.apply(outStream); |
79 |
|
#else /* CVC5_DUMPING */ |
80 |
|
throw OptionException( |
81 |
|
"The dumping feature was disabled in this build of cvc5."); |
82 |
|
#endif /* CVC5_DUMPING */ |
83 |
|
} |
84 |
|
|
85 |
|
void ManagedDumpOStream::addSpecialCases(OstreamOpener* opener) const { |
86 |
|
opener->addSpecialCase("-", &DumpOutC::dump_cout); |
87 |
|
} |
88 |
|
|
89 |
20186 |
ManagedRegularOutputChannel::~ManagedRegularOutputChannel() { |
90 |
|
// Set all ostream that may still be using the old value of this channel |
91 |
|
// to null_os. Consult RegularOutputChannelListener for the list of |
92 |
|
// channels. |
93 |
10093 |
if(options::err() == getManagedOstream()){ |
94 |
|
Options::current().set(options::err, &null_os); |
95 |
|
} |
96 |
10093 |
} |
97 |
|
|
98 |
|
std::string ManagedRegularOutputChannel::defaultSource() const { |
99 |
10092 |
return options::regularChannelName(); |
100 |
|
} |
101 |
|
|
102 |
|
void ManagedRegularOutputChannel::initialize(std::ostream* outStream) { |
103 |
|
OptionsErrOstreamUpdate optionsErrOstreamUpdate; |
104 |
|
optionsErrOstreamUpdate.apply(outStream); |
105 |
|
} |
106 |
|
|
107 |
|
void ManagedRegularOutputChannel::addSpecialCases(OstreamOpener* opener) |
108 |
|
const { |
109 |
|
opener->addSpecialCase("stdout", &std::cout); |
110 |
|
opener->addSpecialCase("stderr", &std::cerr); |
111 |
|
} |
112 |
|
|
113 |
20186 |
ManagedDiagnosticOutputChannel::~ManagedDiagnosticOutputChannel() { |
114 |
|
// Set all ostreams that may still be using the old value of this channel |
115 |
|
// to null_os. Consult DiagnosticOutputChannelListener for the list of |
116 |
|
// channels. |
117 |
10093 |
if(options::err() == getManagedOstream()){ |
118 |
|
Options::current().set(options::err, &null_os); |
119 |
|
} |
120 |
|
|
121 |
10093 |
if(Debug.getStreamPointer() == getManagedOstream()) { |
122 |
|
Debug.setStream(&null_os); |
123 |
|
} |
124 |
10093 |
if(Warning.getStreamPointer() == getManagedOstream()){ |
125 |
|
Warning.setStream(&null_os); |
126 |
|
} |
127 |
10093 |
if (CVC5Message.getStreamPointer() == getManagedOstream()) |
128 |
|
{ |
129 |
|
CVC5Message.setStream(&null_os); |
130 |
|
} |
131 |
10093 |
if(Notice.getStreamPointer() == getManagedOstream()){ |
132 |
|
Notice.setStream(&null_os); |
133 |
|
} |
134 |
10093 |
if(Chat.getStreamPointer() == getManagedOstream()){ |
135 |
|
Chat.setStream(&null_os); |
136 |
|
} |
137 |
10093 |
if(Trace.getStreamPointer() == getManagedOstream()){ |
138 |
|
Trace.setStream(&null_os); |
139 |
|
} |
140 |
10093 |
} |
141 |
|
|
142 |
|
|
143 |
|
std::string ManagedDiagnosticOutputChannel::defaultSource() const { |
144 |
10092 |
return options::diagnosticChannelName(); |
145 |
|
} |
146 |
|
void ManagedDiagnosticOutputChannel::initialize(std::ostream* outStream) { |
147 |
|
DebugOstreamUpdate debugOstreamUpdate; |
148 |
|
debugOstreamUpdate.apply(outStream); |
149 |
|
WarningOstreamUpdate warningOstreamUpdate; |
150 |
|
warningOstreamUpdate.apply(outStream); |
151 |
|
MessageOstreamUpdate messageOstreamUpdate; |
152 |
|
messageOstreamUpdate.apply(outStream); |
153 |
|
NoticeOstreamUpdate noticeOstreamUpdate; |
154 |
|
noticeOstreamUpdate.apply(outStream); |
155 |
|
ChatOstreamUpdate chatOstreamUpdate; |
156 |
|
chatOstreamUpdate.apply(outStream); |
157 |
|
TraceOstreamUpdate traceOstreamUpdate; |
158 |
|
traceOstreamUpdate.apply(outStream); |
159 |
|
OptionsErrOstreamUpdate optionsErrOstreamUpdate; |
160 |
|
optionsErrOstreamUpdate.apply(outStream); |
161 |
|
} |
162 |
|
|
163 |
|
void ManagedDiagnosticOutputChannel::addSpecialCases(OstreamOpener* opener) |
164 |
|
const { |
165 |
|
opener->addSpecialCase("stdout", &std::cout); |
166 |
|
opener->addSpecialCase("stderr", &std::cerr); |
167 |
|
} |
168 |
|
|
169 |
96228 |
} // namespace cvc5 |