A Discrete-Event Network Simulator
API
file-aggregator.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 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  * Author: Mitch Watrous (watrous@u.washington.edu)
18  */
19 
20 #include "file-aggregator.h"
21 
22 #include "ns3/abort.h"
23 #include "ns3/log.h"
24 
25 #include <fstream>
26 #include <iostream>
27 #include <string>
28 
29 namespace ns3
30 {
31 
32 NS_LOG_COMPONENT_DEFINE("FileAggregator");
33 
34 NS_OBJECT_ENSURE_REGISTERED(FileAggregator);
35 
36 TypeId
38 {
39  static TypeId tid =
40  TypeId("ns3::FileAggregator").SetParent<DataCollectionObject>().SetGroupName("Stats");
41 
42  return tid;
43 }
44 
45 FileAggregator::FileAggregator(const std::string& outputFileName, FileType fileType)
46  : m_outputFileName(outputFileName),
47  m_fileType(fileType),
48  m_hasHeadingBeenSet(false),
49  m_1dFormat("%e"),
50  m_2dFormat("%e %e"),
51  m_3dFormat("%e %e %e"),
52  m_4dFormat("%e %e %e %e"),
53  m_5dFormat("%e %e %e %e %e"),
54  m_6dFormat("%e %e %e %e %e %e"),
55  m_7dFormat("%e %e %e %e %e %e %e"),
56  m_8dFormat("%e %e %e %e %e %e %e %e"),
57  m_9dFormat("%e %e %e %e %e %e %e %e %e"),
58  m_10dFormat("%e %e %e %e %e %e %e %e %e %e")
59 {
60  NS_LOG_FUNCTION(this << outputFileName << fileType);
61 
62  // Set the values separator.
63  switch (m_fileType)
64  {
65  case COMMA_SEPARATED:
66  m_separator = ",";
67  break;
68  case TAB_SEPARATED:
69  m_separator = "\t";
70  break;
71  default:
72  // Space separated.
73  m_separator = " ";
74  break;
75  }
76 
78 }
79 
81 {
82  NS_LOG_FUNCTION(this);
83  m_file.close();
84 }
85 
86 void
88 {
89  NS_LOG_FUNCTION(this << fileType);
90  m_fileType = fileType;
91 }
92 
93 void
94 FileAggregator::SetHeading(const std::string& heading)
95 {
96  NS_LOG_FUNCTION(this << heading);
98  {
99  m_heading = heading;
100  m_hasHeadingBeenSet = true;
101 
102  // Print the heading to the file.
103  m_file << m_heading << std::endl;
104  }
105 }
106 
107 void
108 FileAggregator::Set1dFormat(const std::string& format)
109 {
110  NS_LOG_FUNCTION(this << format);
111  m_1dFormat = format;
112 }
113 
114 void
115 FileAggregator::Set2dFormat(const std::string& format)
116 {
117  NS_LOG_FUNCTION(this << format);
118  m_2dFormat = format;
119 }
120 
121 void
122 FileAggregator::Set3dFormat(const std::string& format)
123 {
124  NS_LOG_FUNCTION(this << format);
125  m_3dFormat = format;
126 }
127 
128 void
129 FileAggregator::Set4dFormat(const std::string& format)
130 {
131  NS_LOG_FUNCTION(this << format);
132  m_4dFormat = format;
133 }
134 
135 void
136 FileAggregator::Set5dFormat(const std::string& format)
137 {
138  NS_LOG_FUNCTION(this << format);
139  m_5dFormat = format;
140 }
141 
142 void
143 FileAggregator::Set6dFormat(const std::string& format)
144 {
145  NS_LOG_FUNCTION(this << format);
146  m_6dFormat = format;
147 }
148 
149 void
150 FileAggregator::Set7dFormat(const std::string& format)
151 {
152  NS_LOG_FUNCTION(this << format);
153  m_7dFormat = format;
154 }
155 
156 void
157 FileAggregator::Set8dFormat(const std::string& format)
158 {
159  NS_LOG_FUNCTION(this << format);
160  m_8dFormat = format;
161 }
162 
163 void
164 FileAggregator::Set9dFormat(const std::string& format)
165 {
166  NS_LOG_FUNCTION(this << format);
167  m_9dFormat = format;
168 }
169 
170 void
171 FileAggregator::Set10dFormat(const std::string& format)
172 {
173  NS_LOG_FUNCTION(this << format);
174  m_10dFormat = format;
175 }
176 
177 void
178 FileAggregator::Write1d(std::string context, double v1)
179 {
180  NS_LOG_FUNCTION(this << context << v1);
181 
182  if (m_enabled)
183  {
184  // Write the 1D data point to the file.
185  if (m_fileType == FORMATTED)
186  {
187  // Initially, have the C-style string in the buffer, which
188  // is terminated by a null character, be of length zero.
189  char buffer[500];
190  int maxBufferSize = 500;
191  buffer[0] = 0;
192 
193  // Format the value.
194  int charWritten = snprintf(buffer, maxBufferSize, m_1dFormat.c_str(), v1);
195  if (charWritten < 0)
196  {
197  NS_LOG_DEBUG("Error writing value to output file");
198  }
199 
200  // Write the formatted value.
201  m_file << buffer << std::endl;
202  }
203  else
204  {
205  // Write the value.
206  m_file << v1 << std::endl;
207  }
208  }
209 }
210 
211 void
212 FileAggregator::Write2d(std::string context, double v1, double v2)
213 {
214  NS_LOG_FUNCTION(this << context << v1 << v2);
215 
216  if (m_enabled)
217  {
218  // Write the 2D data point to the file.
219  if (m_fileType == FORMATTED)
220  {
221  // Initially, have the C-style string in the buffer, which
222  // is terminated by a null character, be of length zero.
223  char buffer[500];
224  int maxBufferSize = 500;
225  buffer[0] = 0;
226 
227  // Format the values.
228  int charWritten = snprintf(buffer, maxBufferSize, m_2dFormat.c_str(), v1, v2);
229  if (charWritten < 0)
230  {
231  NS_LOG_DEBUG("Error writing values to output file");
232  }
233 
234  // Write the formatted values.
235  m_file << buffer << std::endl;
236  }
237  else
238  {
239  // Write the values with the proper separator.
240  m_file << v1 << m_separator << v2 << std::endl;
241  }
242  }
243 }
244 
245 void
246 FileAggregator::Write3d(std::string context, double v1, double v2, double v3)
247 {
248  NS_LOG_FUNCTION(this << context << v1 << v2 << v3);
249 
250  if (m_enabled)
251  {
252  // Write the 3D data point to the file.
253  if (m_fileType == FORMATTED)
254  {
255  // Initially, have the C-style string in the buffer, which
256  // is terminated by a null character, be of length zero.
257  char buffer[500];
258  int maxBufferSize = 500;
259  buffer[0] = 0;
260 
261  // Format the values.
262  int charWritten = snprintf(buffer, maxBufferSize, m_3dFormat.c_str(), v1, v2, v3);
263  if (charWritten < 0)
264  {
265  NS_LOG_DEBUG("Error writing values to output file");
266  }
267 
268  // Write the formatted values.
269  m_file << buffer << std::endl;
270  }
271  else
272  {
273  // Write the values with the proper separator.
274  m_file << v1 << m_separator << v2 << m_separator << v3 << std::endl;
275  }
276  }
277 }
278 
279 void
280 FileAggregator::Write4d(std::string context, double v1, double v2, double v3, double v4)
281 {
282  NS_LOG_FUNCTION(this << context << v1 << v2 << v3 << v4);
283 
284  if (m_enabled)
285  {
286  // Write the 4D data point to the file.
287  if (m_fileType == FORMATTED)
288  {
289  // Initially, have the C-style string in the buffer, which
290  // is terminated by a null character, be of length zero.
291  char buffer[500];
292  int maxBufferSize = 500;
293  buffer[0] = 0;
294 
295  // Format the values.
296  int charWritten = snprintf(buffer, maxBufferSize, m_4dFormat.c_str(), v1, v2, v3, v4);
297  if (charWritten < 0)
298  {
299  NS_LOG_DEBUG("Error writing values to output file");
300  }
301 
302  // Write the formatted values.
303  m_file << buffer << std::endl;
304  }
305  else
306  {
307  // Write the values with the proper separator.
308  m_file << v1 << m_separator << v2 << m_separator << v3 << m_separator << v4
309  << std::endl;
310  }
311  }
312 }
313 
314 void
315 FileAggregator::Write5d(std::string context, double v1, double v2, double v3, double v4, double v5)
316 {
317  NS_LOG_FUNCTION(this << context << v1 << v2 << v3 << v4 << v5);
318 
319  if (m_enabled)
320  {
321  // Write the 5D data point to the file.
322  if (m_fileType == FORMATTED)
323  {
324  // Initially, have the C-style string in the buffer, which
325  // is terminated by a null character, be of length zero.
326  char buffer[500];
327  int maxBufferSize = 500;
328  buffer[0] = 0;
329 
330  // Format the values.
331  int charWritten =
332  snprintf(buffer, maxBufferSize, m_5dFormat.c_str(), v1, v2, v3, v4, v5);
333  if (charWritten < 0)
334  {
335  NS_LOG_DEBUG("Error writing values to output file");
336  }
337 
338  // Write the formatted values.
339  m_file << buffer << std::endl;
340  }
341  else
342  {
343  // Write the values with the proper separator.
344  m_file << v1 << m_separator << v2 << m_separator << v3 << m_separator << v4
345  << m_separator << v5 << std::endl;
346  }
347  }
348 }
349 
350 void
351 FileAggregator::Write6d(std::string context,
352  double v1,
353  double v2,
354  double v3,
355  double v4,
356  double v5,
357  double v6)
358 {
359  NS_LOG_FUNCTION(this << context << v1 << v2 << v3 << v4 << v5 << v6);
360 
361  if (m_enabled)
362  {
363  // Write the 6D data point to the file.
364  if (m_fileType == FORMATTED)
365  {
366  // Initially, have the C-style string in the buffer, which
367  // is terminated by a null character, be of length zero.
368  char buffer[500];
369  int maxBufferSize = 500;
370  buffer[0] = 0;
371 
372  // Format the values.
373  int charWritten =
374  snprintf(buffer, maxBufferSize, m_6dFormat.c_str(), v1, v2, v3, v4, v5, v6);
375  if (charWritten < 0)
376  {
377  NS_LOG_DEBUG("Error writing values to output file");
378  }
379 
380  // Write the formatted values.
381  m_file << buffer << std::endl;
382  }
383  else
384  {
385  // Write the values with the proper separator.
386  m_file << v1 << m_separator << v2 << m_separator << v3 << m_separator << v4
387  << m_separator << v5 << m_separator << v6 << std::endl;
388  }
389  }
390 }
391 
392 void
393 FileAggregator::Write7d(std::string context,
394  double v1,
395  double v2,
396  double v3,
397  double v4,
398  double v5,
399  double v6,
400  double v7)
401 {
402  NS_LOG_FUNCTION(this << context << v1 << v2 << v3 << v4 << v5 << v6 << v7);
403 
404  if (m_enabled)
405  {
406  // Write the 7D data point to the file.
407  if (m_fileType == FORMATTED)
408  {
409  // Initially, have the C-style string in the buffer, which
410  // is terminated by a null character, be of length zero.
411  char buffer[500];
412  int maxBufferSize = 500;
413  buffer[0] = 0;
414 
415  // Format the values.
416  int charWritten =
417  snprintf(buffer, maxBufferSize, m_7dFormat.c_str(), v1, v2, v3, v4, v5, v6, v7);
418  if (charWritten < 0)
419  {
420  NS_LOG_DEBUG("Error writing values to output file");
421  }
422 
423  // Write the formatted values.
424  m_file << buffer << std::endl;
425  }
426  else
427  {
428  // Write the values with the proper separator.
429  m_file << v1 << m_separator << v2 << m_separator << v3 << m_separator << v4
430  << m_separator << v5 << m_separator << v6 << m_separator << v7 << std::endl;
431  }
432  }
433 }
434 
435 void
436 FileAggregator::Write8d(std::string context,
437  double v1,
438  double v2,
439  double v3,
440  double v4,
441  double v5,
442  double v6,
443  double v7,
444  double v8)
445 {
446  NS_LOG_FUNCTION(this << context << v1 << v2 << v3 << v4 << v5 << v6 << v7 << v8);
447 
448  if (m_enabled)
449  {
450  // Write the 8D data point to the file.
451  if (m_fileType == FORMATTED)
452  {
453  // Initially, have the C-style string in the buffer, which
454  // is terminated by a null character, be of length zero.
455  char buffer[500];
456  int maxBufferSize = 500;
457  buffer[0] = 0;
458 
459  // Format the values.
460  int charWritten =
461  snprintf(buffer, maxBufferSize, m_8dFormat.c_str(), v1, v2, v3, v4, v5, v6, v7, v8);
462  if (charWritten < 0)
463  {
464  NS_LOG_DEBUG("Error writing values to output file");
465  }
466 
467  // Write the formatted values.
468  m_file << buffer << std::endl;
469  }
470  else
471  {
472  // Write the values with the proper separator.
473  m_file << v1 << m_separator << v2 << m_separator << v3 << m_separator << v4
474  << m_separator << v5 << m_separator << v6 << m_separator << v7 << m_separator
475  << v8 << std::endl;
476  }
477  }
478 }
479 
480 void
481 FileAggregator::Write9d(std::string context,
482  double v1,
483  double v2,
484  double v3,
485  double v4,
486  double v5,
487  double v6,
488  double v7,
489  double v8,
490  double v9)
491 {
492  NS_LOG_FUNCTION(this << context << v1 << v2 << v3 << v4 << v5 << v6 << v7 << v8 << v9);
493  if (m_enabled)
494  {
495  // Write the 9D data point to the file.
496  if (m_fileType == FORMATTED)
497  {
498  // Initially, have the C-style string in the buffer, which
499  // is terminated by a null character, be of length zero.
500  char buffer[500];
501  int maxBufferSize = 500;
502  buffer[0] = 0;
503 
504  // Format the values.
505  int charWritten = snprintf(buffer,
506  maxBufferSize,
507  m_9dFormat.c_str(),
508  v1,
509  v2,
510  v3,
511  v4,
512  v5,
513  v6,
514  v7,
515  v8,
516  v9);
517  if (charWritten < 0)
518  {
519  NS_LOG_DEBUG("Error writing values to output file");
520  }
521 
522  // Write the formatted values.
523  m_file << buffer << std::endl;
524  }
525  else
526  {
527  // Write the values with the proper separator.
528  m_file << v1 << m_separator << v2 << m_separator << v3 << m_separator << v4
529  << m_separator << v5 << m_separator << v6 << m_separator << v7 << m_separator
530  << v8 << m_separator << v9 << std::endl;
531  }
532  }
533 }
534 
535 void
536 FileAggregator::Write10d(std::string context,
537  double v1,
538  double v2,
539  double v3,
540  double v4,
541  double v5,
542  double v6,
543  double v7,
544  double v8,
545  double v9,
546  double v10)
547 {
548  NS_LOG_FUNCTION(this << context << v1 << v2 << v3 << v4 << v5 << v6 << v7 << v8 << v9 << v10);
549  if (m_enabled)
550  {
551  // Write the 10D data point to the file.
552  if (m_fileType == FORMATTED)
553  {
554  // Initially, have the C-style string in the buffer, which
555  // is terminated by a null character, be of length zero.
556  char buffer[500];
557  int maxBufferSize = 500;
558  buffer[0] = 0;
559 
560  // Format the values.
561  int charWritten = snprintf(buffer,
562  maxBufferSize,
563  m_10dFormat.c_str(),
564  v1,
565  v2,
566  v3,
567  v4,
568  v5,
569  v6,
570  v7,
571  v8,
572  v9,
573  v10);
574  if (charWritten < 0)
575  {
576  NS_LOG_DEBUG("Error writing values to output file");
577  }
578 
579  // Write the formatted values.
580  m_file << buffer << std::endl;
581  }
582  else
583  {
584  // Write the values with the proper separator.
585  m_file << v1 << m_separator << v2 << m_separator << v3 << m_separator << v4
586  << m_separator << v5 << m_separator << v6 << m_separator << v7 << m_separator
587  << v8 << m_separator << v9 << m_separator << v10 << std::endl;
588  }
589  }
590 }
591 
592 } // namespace ns3
Base class for data collection framework objects.
bool m_enabled
Object's activation state.
~FileAggregator() override
void Set4dFormat(const std::string &format)
Sets the 4D format string for the C-style sprintf() function.
void Write7d(std::string context, double v1, double v2, double v3, double v4, double v5, double v6, double v7)
Writes 7 values to the file.
void Write3d(std::string context, double v1, double v2, double v3)
Writes 3 values to the file.
std::string m_separator
Printed between values in the file.
void Set6dFormat(const std::string &format)
Sets the 6D format string for the C-style sprintf() function.
std::string m_5dFormat
Format string for 5D C-style sprintf() function.
FileType m_fileType
Determines the kind of file written by the aggregator.
std::string m_4dFormat
Format string for 4D C-style sprintf() function.
void Set8dFormat(const std::string &format)
Sets the 8D format string for the C-style sprintf() function.
void Write1d(std::string context, double v1)
Writes 1 value to the file.
void Set7dFormat(const std::string &format)
Sets the 7D format string for the C-style sprintf() function.
void Write9d(std::string context, double v1, double v2, double v3, double v4, double v5, double v6, double v7, double v8, double v9)
Writes 9 values to the file.
void Write4d(std::string context, double v1, double v2, double v3, double v4)
Writes 4 values to the file.
void Set2dFormat(const std::string &format)
Sets the 2D format string for the C-style sprintf() function.
void SetFileType(FileType fileType)
Set the file type to create, which determines the separator to use when printing values to the file.
void Set9dFormat(const std::string &format)
Sets the 9D format string for the C-style sprintf() function.
std::string m_10dFormat
Format string for 10D C-style sprintf() function.
std::ofstream m_file
Used to write values to the file.
void Write2d(std::string context, double v1, double v2)
Writes 2 values to the file.
std::string m_9dFormat
Format string for 9D C-style sprintf() function.
void Write5d(std::string context, double v1, double v2, double v3, double v4, double v5)
Writes 5 values to the file.
void Set3dFormat(const std::string &format)
Sets the 3D format string for the C-style sprintf() function.
void Set1dFormat(const std::string &format)
Sets the 1D format string for the C-style sprintf() function.
bool m_hasHeadingBeenSet
Indicates if the heading line for the file has been set.
std::string m_7dFormat
Format string for 7D C-style sprintf() function.
std::string m_heading
Heading line for the outputfile.
void Set10dFormat(const std::string &format)
Sets the 10D format string for the C-style sprintf() function.
FileAggregator(const std::string &outputFileName, FileType fileType=SPACE_SEPARATED)
void Write6d(std::string context, double v1, double v2, double v3, double v4, double v5, double v6)
Writes 6 values to the file.
void Set5dFormat(const std::string &format)
Sets the 5D format string for the C-style sprintf() function.
std::string m_3dFormat
Format string for 3D C-style sprintf() function.
void SetHeading(const std::string &heading)
Sets the heading string that will be printed on the first line of the file.
std::string m_1dFormat
Format string for 1D C-style sprintf() function.
std::string m_outputFileName
The file name.
std::string m_6dFormat
Format string for 6D C-style sprintf() function.
std::string m_8dFormat
Format string for 8D C-style sprintf() function.
void Write8d(std::string context, double v1, double v2, double v3, double v4, double v5, double v6, double v7, double v8)
Writes 8 values to the file.
FileType
The type of file written by the aggregator.
std::string m_2dFormat
Format string for 2D C-style sprintf() function.
void Write10d(std::string context, double v1, double v2, double v3, double v4, double v5, double v6, double v7, double v8, double v9, double v10)
Writes 10 values to the file.
static TypeId GetTypeId()
Get the type ID.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.