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