GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/managed_ostreams.cpp Lines: 27 88 30.7 %
Date: 2021-03-23 Branches: 26 128 20.3 %

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