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 #include <ios>
30 #include <sstream>
31 #include <stdint.h>
32 #include <string>
33 #include <utility>
34 
35 NS_LOG_COMPONENT_DEFINE("SatLog");
36 
37 namespace ns3
38 {
39 
40 NS_OBJECT_ENSURE_REGISTERED(SatLog);
41 
42 TypeId
44 {
45  static TypeId tid = TypeId("ns3::SatLog").SetParent<Object>().AddConstructor<SatLog>();
46  return tid;
47 }
48 
49 TypeId
51 {
52  NS_LOG_FUNCTION(this);
53 
54  return GetTypeId();
55 }
56 
58 {
59  NS_LOG_FUNCTION(this);
60 
61  ObjectBase::ConstructSelf(AttributeConstructionList());
62 }
63 
65 {
66  NS_LOG_FUNCTION(this);
67 
68  Reset();
69 }
70 
71 void
73 {
74  NS_LOG_FUNCTION(this);
75 
76  Reset();
77 }
78 
79 void
81 {
82  NS_LOG_FUNCTION(this);
83 
84  if (!m_container.empty())
85  {
86  WriteToFile();
87 
88  m_container.clear();
89  }
90 }
91 
92 Ptr<SatOutputFileStreamStringContainer>
93 SatLog::CreateLog(LogType_t logType, std::string fileTag)
94 {
95  NS_LOG_FUNCTION(this);
96 
97  std::stringstream filename;
98  std::string dataPath = Singleton<SatEnvVariables>::Get()->GetOutputPath();
99 
100  filename << dataPath << "/log" << fileTag;
101 
102  key_t key = std::make_pair(logType, fileTag);
103 
104  std::pair<container_t::iterator, bool> result = m_container.insert(std::make_pair(
105  key,
106  CreateObject<SatOutputFileStreamStringContainer>(filename.str().c_str(), std::ios::out)));
107 
108  if (result.second == false)
109  {
110  NS_FATAL_ERROR("SatLog::CreateLog failed");
111  }
112 
113  NS_LOG_INFO("Created type " << logType << " log with file tag " << fileTag);
114 
115  return result.first->second;
116 }
117 
118 Ptr<SatOutputFileStreamStringContainer>
119 SatLog::FindLog(LogType_t logType, std::string fileTag)
120 {
121  NS_LOG_FUNCTION(this);
122 
123  key_t key = std::make_pair(logType, fileTag);
124 
125  container_t::iterator iter = m_container.find(key);
126 
127  if (iter == m_container.end())
128  {
129  return CreateLog(logType, fileTag);
130  }
131 
132  return iter->second;
133 }
134 
135 void
137 {
138  NS_LOG_FUNCTION(this);
139 
140  container_t::iterator iter;
141 
142  for (iter = m_container.begin(); iter != m_container.end(); iter++)
143  {
144  iter->second->WriteContainerToFile();
145  }
146 }
147 
148 void
149 SatLog::AddToLog(LogType_t logType, std::string fileTag, std::string message)
150 {
151  NS_LOG_FUNCTION(this);
152 
153  if (logType != LOG_CUSTOM)
154  {
155  fileTag = GetFileTag(logType);
156  }
157 
158  Ptr<SatOutputFileStreamStringContainer> log = FindLog(logType, fileTag);
159 
160  NS_LOG_INFO("Type: " << logType << ", file tag: " << fileTag << ", message: " << message);
161 
162  if (log != nullptr)
163  {
164  log->AddToContainer(message);
165  }
166 }
167 
168 std::string
170 {
171  std::string fileTag = "";
172 
173  switch (logType)
174  {
175  case LOG_GENERIC: {
176  fileTag = "";
177  break;
178  }
179  case LOG_INFO: {
180  fileTag = "_info";
181  break;
182  }
183  case LOG_WARNING: {
184  fileTag = "_warning";
185  break;
186  }
187  case LOG_ERROR: {
188  fileTag = "_error";
189  break;
190  }
191  case LOG_CUSTOM: {
192  NS_FATAL_ERROR("SatLog::GetFileTag - This function should not be called for custom logs");
193  break;
194  }
195  default: {
196  NS_FATAL_ERROR("SatLog::GetFileTag - Invalid log type");
197  break;
198  }
199  }
200  return fileTag;
201 }
202 
203 } // 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:74
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:63
@ LOG_ERROR
LOG_ERROR.
Definition: satellite-log.h:67
@ LOG_WARNING
LOG_WARNING.
Definition: satellite-log.h:66
@ LOG_CUSTOM
LOG_CUSTOM.
Definition: satellite-log.h:68
@ LOG_GENERIC
LOG_GENERIC.
Definition: satellite-log.h:64
@ LOG_INFO
LOG_INFO.
Definition: satellite-log.h:65
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.