A Discrete-Event Network Simulator
API
output-stream-wrapper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #include "output-stream-wrapper.h"
19 
20 #include "ns3/abort.h"
21 #include "ns3/fatal-impl.h"
22 #include "ns3/log.h"
23 
24 #include <fstream>
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("OutputStreamWrapper");
30 
31 OutputStreamWrapper::OutputStreamWrapper(std::string filename, std::ios::openmode filemode)
32  : m_destroyable(true)
33 {
34  NS_LOG_FUNCTION(this << filename << filemode);
35  auto os = new std::ofstream();
36  os->open(filename, filemode);
37  m_ostream = os;
39  NS_ABORT_MSG_UNLESS(os->is_open(),
40  "AsciiTraceHelper::CreateFileStream(): "
41  << "Unable to Open " << filename << " for mode " << filemode);
42 }
43 
45  : m_ostream(os),
46  m_destroyable(false)
47 {
48  NS_LOG_FUNCTION(this << os);
50  NS_ABORT_MSG_UNLESS(m_ostream->good(), "Output stream is not valid for writing.");
51 }
52 
54 {
55  NS_LOG_FUNCTION(this);
57  if (m_destroyable)
58  {
59  delete m_ostream;
60  }
61  m_ostream = nullptr;
62 }
63 
64 std::ostream*
66 {
67  NS_LOG_FUNCTION(this);
68  return m_ostream;
69 }
70 
71 } // namespace ns3
OutputStreamWrapper(std::string filename, std::ios::openmode filemode)
Constructor.
std::ostream * GetStream()
Return a pointer to an ostream previously set in the wrapper.
bool m_destroyable
Can be destroyed.
std::ostream * m_ostream
The output stream.
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
void UnregisterStream(std::ostream *stream)
Unregister a stream for flushing on abnormal exit.
Definition: fatal-impl.cc:143
void RegisterStream(std::ostream *stream)
Register a stream to be flushed on abnormal exit.
Definition: fatal-impl.cc:136
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.