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