satellite-log.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Magister Solutions Ltd.
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: Frans Laakso <frans.laakso@magister.fi>
19  */
20 
21 #include "satellite-log.h"
22 
23 #include "../utils/satellite-env-variables.h"
24 
25 #include <ns3/log.h>
26 #include <ns3/singleton.h>
27 #include <ns3/string.h>
28 
29 NS_LOG_COMPONENT_DEFINE("SatLog");
30 
31 namespace ns3
32 {
33 
34 NS_OBJECT_ENSURE_REGISTERED(SatLog);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId("ns3::SatLog").SetParent<Object>().AddConstructor<SatLog>();
40  return tid;
41 }
42 
43 TypeId
45 {
46  NS_LOG_FUNCTION(this);
47 
48  return GetTypeId();
49 }
50 
52 {
53  NS_LOG_FUNCTION(this);
54 
55  ObjectBase::ConstructSelf(AttributeConstructionList());
56 }
57 
59 {
60  NS_LOG_FUNCTION(this);
61 
62  Reset();
63 }
64 
65 void
67 {
68  NS_LOG_FUNCTION(this);
69 
70  Reset();
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION(this);
77 
78  if (!m_container.empty())
79  {
80  WriteToFile();
81 
82  m_container.clear();
83  }
84 }
85 
86 Ptr<SatOutputFileStreamStringContainer>
87 SatLog::CreateLog(LogType_t logType, std::string fileTag)
88 {
89  NS_LOG_FUNCTION(this);
90 
91  std::stringstream filename;
92  std::string dataPath = Singleton<SatEnvVariables>::Get()->GetOutputPath();
93 
94  filename << dataPath << "/log" << fileTag;
95 
96  key_t key = std::make_pair(logType, fileTag);
97 
98  std::pair<container_t::iterator, bool> result = m_container.insert(std::make_pair(
99  key,
100  CreateObject<SatOutputFileStreamStringContainer>(filename.str().c_str(), std::ios::out)));
101 
102  if (result.second == false)
103  {
104  NS_FATAL_ERROR("SatLog::CreateLog failed");
105  }
106 
107  NS_LOG_INFO("Created type " << logType << " log with file tag " << fileTag);
108 
109  return result.first->second;
110 }
111 
112 Ptr<SatOutputFileStreamStringContainer>
113 SatLog::FindLog(LogType_t logType, std::string fileTag)
114 {
115  NS_LOG_FUNCTION(this);
116 
117  key_t key = std::make_pair(logType, fileTag);
118 
119  container_t::iterator iter = m_container.find(key);
120 
121  if (iter == m_container.end())
122  {
123  return CreateLog(logType, fileTag);
124  }
125 
126  return iter->second;
127 }
128 
129 void
131 {
132  NS_LOG_FUNCTION(this);
133 
134  container_t::iterator iter;
135 
136  for (iter = m_container.begin(); iter != m_container.end(); iter++)
137  {
138  iter->second->WriteContainerToFile();
139  }
140 }
141 
142 void
143 SatLog::AddToLog(LogType_t logType, std::string fileTag, std::string message)
144 {
145  NS_LOG_FUNCTION(this);
146 
147  if (logType != LOG_CUSTOM)
148  {
149  fileTag = GetFileTag(logType);
150  }
151 
152  Ptr<SatOutputFileStreamStringContainer> log = FindLog(logType, fileTag);
153 
154  NS_LOG_INFO("Type: " << logType << ", file tag: " << fileTag << ", message: " << message);
155 
156  if (log != NULL)
157  {
158  log->AddToContainer(message);
159  }
160 }
161 
162 std::string
164 {
165  std::string fileTag = "";
166 
167  switch (logType)
168  {
169  case LOG_GENERIC: {
170  fileTag = "";
171  break;
172  }
173  case LOG_INFO: {
174  fileTag = "_info";
175  break;
176  }
177  case LOG_WARNING: {
178  fileTag = "_warning";
179  break;
180  }
181  case LOG_ERROR: {
182  fileTag = "_error";
183  break;
184  }
185  case LOG_CUSTOM: {
186  NS_FATAL_ERROR("SatLog::GetFileTag - This function should not be called for custom logs");
187  break;
188  }
189  default: {
190  NS_FATAL_ERROR("SatLog::GetFileTag - Invalid log type");
191  break;
192  }
193  }
194  return fileTag;
195 }
196 
197 } // namespace ns3
SatLog()
Constructor.
void DoDispose()
Do needed dispose actions.
void WriteToFile()
Write the contents of a container matching to the key into a file.
Ptr< SatOutputFileStreamStringContainer > CreateLog(LogType_t logType, std::string fileTag)
Function for creating a log.
std::pair< LogType_t, std::string > key_t
typedef for container key
Definition: satellite-log.h:72
std::string GetFileTag(LogType_t logType)
Function for getting the file tag for predefined log types.
void Reset()
Function for resetting the variables.
~SatLog()
Destructor.
Ptr< SatOutputFileStreamStringContainer > FindLog(LogType_t logType, std::string fileTag)
Function for finding a log based on the key.
void AddToLog(LogType_t logType, std::string fileTag, std::string message)
Function for adding a line to a specific log.
container_t m_container
Map for containers.
static TypeId GetTypeId(void)
NS-3 type id function.
TypeId GetInstanceTypeId(void) const
NS-3 instance type id function.
LogType_t
Enum for log types.
Definition: satellite-log.h:61
@ LOG_ERROR
LOG_ERROR.
Definition: satellite-log.h:65
@ LOG_WARNING
LOG_WARNING.
Definition: satellite-log.h:64
@ LOG_CUSTOM
LOG_CUSTOM.
Definition: satellite-log.h:66
@ LOG_GENERIC
LOG_GENERIC.
Definition: satellite-log.h:62
@ LOG_INFO
LOG_INFO.
Definition: satellite-log.h:63
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.