summaryrefslogtreecommitdiff
path: root/src/smt/managed_ostreams.cpp
blob: b09448c112686348dfc623392923b5ab7bc89b39 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/******************************************************************************
 * Top contributors (to current version):
 *   Tim King, Aina Niemetz
 *
 * This file is part of the cvc5 project.
 *
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 * in the top-level source directory and their institutional affiliations.
 * All rights reserved.  See the file COPYING in the top-level source
 * directory for licensing information.
 * ****************************************************************************
 *
 * Wrappers to handle memory management of ostreams.
 *
 * This file contains wrappers to handle special cases of managing memory
 * related to ostreams.
 */

#include "smt/managed_ostreams.h"

#include <ostream>

#include "base/check.h"
#include "options/open_ostream.h"
#include "options/option_exception.h"
#include "options/smt_options.h"
#include "smt/update_ostream.h"

namespace cvc5 {

ManagedOstream::ManagedOstream() : d_managed(NULL) {}

ManagedOstream::~ManagedOstream() {
  manage(NULL);
  Assert(d_managed == NULL);
}

void ManagedOstream::set(const std::string& filename) {
  std::pair<bool, std::ostream*> pair = open(filename);
  initialize(pair.second);
  manage(pair.first ? pair.second : NULL);
}

std::pair<bool, std::ostream*> ManagedOstream::open(const std::string& filename)
    const {
  OstreamOpener opener(getName());
  addSpecialCases(&opener);
  return opener.open(filename);
}

void ManagedOstream::manage(std::ostream* new_managed_value) {
  if(d_managed == new_managed_value){
    // This is a no-op.
  } else {
    Assert(d_managed != new_managed_value);
    std::ostream* old_managed_value = d_managed;
    d_managed = new_managed_value;
    if(old_managed_value != NULL){
      delete old_managed_value;
    }
  }
}

ManagedDumpOStream::~ManagedDumpOStream() {
  if(Dump.getStreamPointer() == getManagedOstream()) {
    Dump.setStream(&null_os);
  }
}

std::string ManagedDumpOStream::defaultSource() const{
  return options::dumpToFileName();
}


void ManagedDumpOStream::initialize(std::ostream* outStream) {
#ifdef CVC5_DUMPING
  DumpOstreamUpdate dumpGetStream;
  dumpGetStream.apply(outStream);
#else  /* CVC5_DUMPING */
  throw OptionException(
      "The dumping feature was disabled in this build of cvc5.");
#endif /* CVC5_DUMPING */
}

void ManagedDumpOStream::addSpecialCases(OstreamOpener* opener) const {
  opener->addSpecialCase("-", &DumpOutC::dump_cout);
}

ManagedRegularOutputChannel::~ManagedRegularOutputChannel() {
  // Set all ostream that may still be using the old value of this channel
  // to null_os. Consult RegularOutputChannelListener for the list of
  // channels.
  if(options::err() == getManagedOstream()){
    Options::current().base.err = &null_os;
  }
}

std::string ManagedRegularOutputChannel::defaultSource() const {
  return options::regularChannelName();
}

void ManagedRegularOutputChannel::initialize(std::ostream* outStream) {
  OptionsErrOstreamUpdate optionsErrOstreamUpdate;
  optionsErrOstreamUpdate.apply(outStream);
}

void ManagedRegularOutputChannel::addSpecialCases(OstreamOpener* opener)
    const {
  opener->addSpecialCase("stdout", &std::cout);
  opener->addSpecialCase("stderr", &std::cerr);
}

ManagedDiagnosticOutputChannel::~ManagedDiagnosticOutputChannel() {
  // Set all ostreams that may still be using the old value of this channel
  // to null_os. Consult DiagnosticOutputChannelListener for the list of
  // channels.
  if(options::err() == getManagedOstream()){
    Options::current().base.err = &null_os;
  }

  if(Debug.getStreamPointer() == getManagedOstream()) {
    Debug.setStream(&null_os);
  }
  if(Warning.getStreamPointer() == getManagedOstream()){
    Warning.setStream(&null_os);
  }
  if (CVC5Message.getStreamPointer() == getManagedOstream())
  {
    CVC5Message.setStream(&null_os);
  }
  if(Notice.getStreamPointer() == getManagedOstream()){
    Notice.setStream(&null_os);
  }
  if(Chat.getStreamPointer() == getManagedOstream()){
    Chat.setStream(&null_os);
  }
  if(Trace.getStreamPointer() == getManagedOstream()){
    Trace.setStream(&null_os);
  }
}


std::string ManagedDiagnosticOutputChannel::defaultSource() const {
  return options::diagnosticChannelName();
}
void ManagedDiagnosticOutputChannel::initialize(std::ostream* outStream) {
  DebugOstreamUpdate debugOstreamUpdate;
  debugOstreamUpdate.apply(outStream);
  WarningOstreamUpdate warningOstreamUpdate;
  warningOstreamUpdate.apply(outStream);
  MessageOstreamUpdate messageOstreamUpdate;
  messageOstreamUpdate.apply(outStream);
  NoticeOstreamUpdate noticeOstreamUpdate;
  noticeOstreamUpdate.apply(outStream);
  ChatOstreamUpdate chatOstreamUpdate;
  chatOstreamUpdate.apply(outStream);
  TraceOstreamUpdate traceOstreamUpdate;
  traceOstreamUpdate.apply(outStream);
  OptionsErrOstreamUpdate optionsErrOstreamUpdate;
  optionsErrOstreamUpdate.apply(outStream);
}

void ManagedDiagnosticOutputChannel::addSpecialCases(OstreamOpener* opener)
    const {
  opener->addSpecialCase("stdout", &std::cout);
  opener->addSpecialCase("stderr", &std::cerr);
}

}  // namespace cvc5
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback