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