ITADataLog.h 4.06 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
/*
2 3 4 5
 * ----------------------------------------------------------------
 *
 *		ITA core libs
 *		(c) Copyright Institute of Technical Acoustics (ITA)
6
 *		RWTH Aachen University, Germany, 2015-2017
7 8 9 10 11 12 13 14 15 16 17
 *
 * ----------------------------------------------------------------
 *				    ____  __________  _______
 *				   //  / //__   ___/ //  _   |
 *				  //  /    //  /    //  /_|  |
 *				 //  /    //  /    //  ___   |
 *				//__/    //__/    //__/   |__|
 *
 * ----------------------------------------------------------------
 *
 */
Jonas Stienen's avatar
Jonas Stienen committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

#ifndef INCLUDE_WATCHER_ITA_DATA_LOG
#define INCLUDE_WATCHER_ITA_DATA_LOG

#include <ITABaseDefinitions.h>

#include <ITAException.h>

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

//! Basisklasse fr Logdaten
/**
 * Realisiert Formattierungsfunktionen fr Konsolen-/Dateiausgabe
 */
35
class ITALogDataBase
Jonas Stienen's avatar
Jonas Stienen committed
36 37 38 39 40 41 42
{
public:
	inline virtual ~ITALogDataBase() {};

	//! Hook: Ausgabe der Beschreibung des Datums (z.B. Felder) in einen Stream
	// Dies wird fr den Header in der Datei verwendet
	// Zum Beispiel so: [0] Index, [1] Time, ...
43
	static std::ostream& outputDesc( std::ostream& os );
Jonas Stienen's avatar
Jonas Stienen committed
44 45

	//! Hook: Ausgabe eines konkreten Datums in einen Stream (Formattierer)
46
	virtual std::ostream& outputData( std::ostream& os ) const = 0;
Jonas Stienen's avatar
Jonas Stienen committed
47 48 49 50 51 52
};

//! Gepufferter Logger fr Daten
template <class T> class ITABufferedDataLogger
{
public:
53
	ITABufferedDataLogger( int iNumItems = 100, bool bHeaderIncluded = true );
Jonas Stienen's avatar
Jonas Stienen committed
54

55
	ITABufferedDataLogger( const std::string& sOutputFilePath, int iNumItems, bool bHeaderIncluded = true );
Jonas Stienen's avatar
Jonas Stienen committed
56 57 58

	virtual ~ITABufferedDataLogger();

59 60 61
	//! Include header in exported file
	void SetIncludeHeader( bool bEnabled );

Jonas Stienen's avatar
Jonas Stienen committed
62 63 64 65 66
	//! Setzt die Ausgabedatei
	/**
	 * Falls diese angegeben wird, so werden alle geloggten Daten
	 * beim Aufruf des Destruktors in diese Datei geschrieben
	 */
67
	void setOutputFile( const std::string& sFilePath );
Jonas Stienen's avatar
Jonas Stienen committed
68 69 70 71 72 73 74

	std::string GetOutputFileName() const;

	//! Alle geloggten Eintrge lschen
	void clear();

	//! Platz fr Datenfelder reservieren
75
	void reserve( int iNumItems );
Jonas Stienen's avatar
Jonas Stienen committed
76 77

	//! Datum loggen
78
	void log( const T& oLogItem );
Jonas Stienen's avatar
Jonas Stienen committed
79 80

	//! Alle geloggten Eintrge in eine Datei schreiben
81
	void store( const std::string& sFilePath );
Jonas Stienen's avatar
Jonas Stienen committed
82 83 84 85

private:
	std::vector<T> m_vLogItems;
	std::string m_sOutputFile;
86
	bool m_bIncludeHeader;
Jonas Stienen's avatar
Jonas Stienen committed
87 88 89 90
};

// Header impl to avoid explicit definitions

91 92
template< class T > ITABufferedDataLogger< T >::ITABufferedDataLogger( int iNumItems, bool bIncludeHeader )
	: m_bIncludeHeader( bIncludeHeader )
Jonas Stienen's avatar
Jonas Stienen committed
93
{
94
	m_vLogItems.reserve( iNumItems );
95
};
Jonas Stienen's avatar
Jonas Stienen committed
96

97
template< class T > ITABufferedDataLogger< T >::ITABufferedDataLogger( const std::string& sOutputFile, int iNumItems, bool bIncludeHeader )
98
: m_sOutputFile( sOutputFile )
99
, m_bIncludeHeader( bIncludeHeader )
Jonas Stienen's avatar
Jonas Stienen committed
100
{
101
	m_vLogItems.reserve( iNumItems );
102
};
Jonas Stienen's avatar
Jonas Stienen committed
103 104 105

template< class T > ITABufferedDataLogger< T >::~ITABufferedDataLogger()
{
106 107
	if( !m_sOutputFile.empty() )
		store( m_sOutputFile );
108
};
Jonas Stienen's avatar
Jonas Stienen committed
109 110 111 112

template< class T > std::string ITABufferedDataLogger< T >::GetOutputFileName() const
{
	return m_sOutputFile;
113 114 115 116 117 118
};

template< class T > void ITABufferedDataLogger< T >::SetIncludeHeader( bool bEnabled )
{
	m_bIncludeHeader = bEnabled;
};
Jonas Stienen's avatar
Jonas Stienen committed
119

120
template< class T > void ITABufferedDataLogger< T >::setOutputFile( const std::string& sFilename )
Jonas Stienen's avatar
Jonas Stienen committed
121 122
{
	m_sOutputFile = sFilename;
123
};
Jonas Stienen's avatar
Jonas Stienen committed
124

Dipl.-Ing. Jonas Stienen's avatar
Style  
Dipl.-Ing. Jonas Stienen committed
125 126
template< class T > void ITABufferedDataLogger< T >::clear()
{
Jonas Stienen's avatar
Jonas Stienen committed
127
	m_vLogItems.clear();
128
};
Jonas Stienen's avatar
Jonas Stienen committed
129

130 131 132
template< class T > void ITABufferedDataLogger< T >::reserve( int iNumItems )
{
	m_vLogItems.reserve( iNumItems );
133
};
Jonas Stienen's avatar
Jonas Stienen committed
134

135 136 137
template< class T > void ITABufferedDataLogger< T >::log( const T& oLogItem )
{
	m_vLogItems.push_back( oLogItem );
138
};
Jonas Stienen's avatar
Jonas Stienen committed
139

140 141 142 143 144 145
template< class T > void ITABufferedDataLogger< T >::store( const std::string& sFilename )
{
	std::ofstream os( sFilename.c_str(), std::ios::out | std::ios::trunc | std::ios::binary );

	if( !os.is_open() )
		ITA_EXCEPT1( IO_ERROR, std::string( "Could not create log output file \"" ) + sFilename + std::string( "\"" ) );
Jonas Stienen's avatar
Jonas Stienen committed
146

147 148 149
	if( m_bIncludeHeader )
		T::outputDesc( os );

150 151 152
	for( int i = 0; i < ( int ) m_vLogItems.size(); i++ )
	{
		T& oItem = m_vLogItems[ i ];
153
		oItem.outputData( os );
Jonas Stienen's avatar
Jonas Stienen committed
154 155 156
	}

	os.close();
157
};
Jonas Stienen's avatar
Jonas Stienen committed
158 159

#endif // INCLUDE_WATCHER_ITA_DATA_LOG