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