src/common/ascii-writer.cc
changeset 6068 a2127017ecb4
parent 6067 ccbdc2b19ea5
parent 6062 f62b76f5c92a
child 6069 c21754b56036
equal deleted inserted replaced
6067:ccbdc2b19ea5 6068:a2127017ecb4
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2009 INRIA
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License version 2 as
       
     7  * published by the Free Software Foundation;
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    17  *
       
    18  * Author: Guillaume Seguin <guillaume@segu.in>
       
    19  */
       
    20 #include "ascii-writer.h"
       
    21 
       
    22 #include "ns3/log.h"
       
    23 #include "ns3/simulator.h"
       
    24 #include "ns3/simulation-singleton.h"
       
    25 #include "ns3/packet.h"
       
    26 
       
    27 #include <fstream>
       
    28 #include <map>
       
    29 
       
    30 
       
    31 NS_LOG_COMPONENT_DEFINE ("AsciiWriter");
       
    32 
       
    33 namespace ns3 {
       
    34 
       
    35 typedef std::map<std::ostream*, Ptr<AsciiWriter> > AsciiWritersMap;
       
    36 
       
    37 Ptr<AsciiWriter>
       
    38 AsciiWriter::Get (std::ostream &os)
       
    39 {
       
    40   AsciiWritersMap *map = SimulationSingleton<AsciiWritersMap>::Get ();
       
    41   Ptr<AsciiWriter> writer = (*map)[&os];
       
    42   if (writer == 0)
       
    43     {
       
    44       // don't call Create<> because constructor is private
       
    45       writer = Ptr<AsciiWriter> (new AsciiWriter (&os), false);
       
    46       (*map)[&os] = writer;
       
    47     }
       
    48   return writer;
       
    49 }
       
    50 
       
    51 AsciiWriter::AsciiWriter (std::ostream *os)
       
    52   : m_writer (os)
       
    53 {
       
    54   NS_LOG_FUNCTION (this);
       
    55 }
       
    56 
       
    57 AsciiWriter::~AsciiWriter (void)
       
    58 {
       
    59   NS_LOG_FUNCTION (this);
       
    60 }
       
    61 
       
    62 void
       
    63 AsciiWriter::WritePacket (enum Type type, std::string message, Ptr<const Packet> packet)
       
    64 {
       
    65   std::string typeString;
       
    66   switch (type)
       
    67     {
       
    68     case ENQUEUE:
       
    69       typeString = "+";
       
    70       break;
       
    71     case DEQUEUE:
       
    72       typeString = "-";
       
    73       break;
       
    74     case RX:
       
    75       typeString = "r";
       
    76       break;
       
    77     case TX:
       
    78       typeString = "t";
       
    79       break;
       
    80     case DROP:
       
    81       typeString = "d";
       
    82       break;
       
    83     }
       
    84   NS_LOG_FUNCTION (this << typeString << message);
       
    85   *m_writer << typeString << " " << Simulator::Now ().GetSeconds () << " "
       
    86             << message << " " << *packet << std::endl;
       
    87 }
       
    88 
       
    89 } // namespace ns3