Statistics
| Branch: | Revision:

root / src / envir / fileoutscalarmgr.h @ 81ad8b66

History | View | Annotate | Download (3.24 KB)

1
//==========================================================================
2
//  FILEOUTPUTSCALARMGR.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17

    
18
#ifndef __FILEOUTPUTSCALARMGR_H
19
#define __FILEOUTPUTSCALARMGR_H
20

    
21
#include <stdio.h>
22
#include "envirdefs.h"
23
#include "envirext.h"
24
#include "simutil.h"
25
#include "stringutil.h"
26
#include "runattributes.h"
27

    
28
NAMESPACE_BEGIN
29

    
30
/**
31
 * A cOutputScalarManager that uses a line-oriented text file as output.
32
 *
33
 * @ingroup Envir
34
 */
35
class ENVIR_API cFileOutputScalarManager : public cOutputScalarManager
36
{
37
  protected:
38
    sRunData run;      // holds data of the current run
39
    opp_string fname;  // output file name
40
    FILE *f;           // file ptr of output file
41
    int prec;          // number of significant digits when writing doubles
42

    
43
  protected:
44
    void openFile();
45
    void closeFile();
46
    void init();
47
    void writeStatisticField(const char *name, long value);
48
    void writeStatisticField(const char *name, double value);
49
    void recordNumericIterationVariable(const char *name, const char *value); // i.e. write *if* numeric
50

    
51
  public:
52
    /** @name Constructors, destructor */
53
    //@{
54

    
55
    /**
56
     * Constructor.
57
     */
58
    explicit cFileOutputScalarManager();
59

    
60
    /**
61
     * Destructor.
62
     */
63
    virtual ~cFileOutputScalarManager();
64
    //@}
65

    
66
    /** @name Controlling the beginning and end of collecting data. */
67
    //@{
68

    
69
    /**
70
     * Opens collecting. Called at the beginning of a simulation run.
71
     */
72
    virtual void startRun();
73

    
74
    /**
75
     * Closes collecting. Called at the end of a simulation run.
76
     */
77
    virtual void endRun();
78

    
79
    /** @name Scalar statistics */
80
    //@{
81

    
82
    /**
83
     * Records a double scalar result into the scalar result file.
84
     */
85
    void recordScalar(cComponent *component, const char *name, double value, opp_string_map *attributes=NULL);
86

    
87
    /**
88
     * Records a histogram or statistic object into the scalar result file.
89
     * This operation may invoke the transform() method on the histogram object.
90
     */
91
    void recordStatistic(cComponent *component, const char *name, cStatistic *statistic, opp_string_map *attributes=NULL);
92

    
93
    /**
94
     * Returns the file name.
95
     */
96
    const char *getFileName() const;
97

    
98
    /**
99
     * Calls fflush().
100
     */
101
    virtual void flush();
102
    //@}
103
};
104

    
105
inline void cFileOutputScalarManager::writeStatisticField(const char *name, long value)
106
{
107
    if (fprintf(f, "field %s %ld\n", QUOTE(name), value) < 0)
108
        throw cRuntimeError("Cannot write output scalar file `%s'", fname.c_str());
109
}
110

    
111
inline void cFileOutputScalarManager::writeStatisticField(const char *name, double value)
112
{
113
    if (fprintf(f, "field %s %.*g\n", QUOTE(name), prec, value) < 0)
114
        throw cRuntimeError("Cannot write output scalar file `%s'", fname.c_str());
115
}
116

    
117
NAMESPACE_END
118

    
119
#endif
120