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