VALog.h 4.47 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#ifndef __VA_LOG_H__
#define __VA_LOG_H__

#include <VACoreDefinitions.h>

#include <VACore.h>
#include <VAException.h>

#include <ITAClock.h>
#include <ITAAtomicPrimitives.h>

#include <tbb/concurrent_queue.h>

#include <VistaInterProcComm/Concurrency/VistaMutex.h>
#include <VistaInterProcComm/Concurrency/VistaThreadEvent.h>
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>

#include <iostream>
#include <list>
#include <stdarg.h>
21
#include <stdio.h>
Jonas Stienen's avatar
Jonas Stienen committed
22 23 24 25

extern VACORE_API std::ostream* VA_STDOUT;
extern VACORE_API std::ostream* VA_STDERR;

Jonas Stienen's avatar
Jonas Stienen committed
26 27
void VACORE_API VALog_setOutputStream( std::ostream* os );
void VACORE_API VALog_setErrorStream( std::ostream* os );
Jonas Stienen's avatar
Jonas Stienen committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
int VACORE_API VALog_GetLogLevel();
void VACORE_API VALog_SetLogLevel( int );

VistaMutex& VALog_getOutputStreamMutex();

#define VA_PRINT( expr )			{ if (VA_STDOUT && (VALog_GetLogLevel() >= IVACore::VA_LOG_LEVEL_QUIET ) )		{ VistaMutexLock oLock(VALog_getOutputStreamMutex() ); (*VA_STDOUT) << "[VAPrint  ] " << expr << std::endl; } }
#define VA_ERROR( module, expr )	{ if (VA_STDOUT && (VALog_GetLogLevel() >= IVACore::VA_LOG_LEVEL_QUIET ) )		{ VistaMutexLock oLock(VALog_getOutputStreamMutex() ); (*VA_STDERR) << "[VAError  ] " << module << ": " << expr << std::endl; } }
#define VA_WARN( module, expr )		{ if (VA_STDOUT && (VALog_GetLogLevel() >= IVACore::VA_LOG_LEVEL_ERROR ) )		{ VistaMutexLock oLock(VALog_getOutputStreamMutex() ); (*VA_STDOUT) << "[VAWarning] " << module << ": " << expr << std::endl; } }
#define VA_INFO( module, expr )		{ if (VA_STDOUT && (VALog_GetLogLevel() >= IVACore::VA_LOG_LEVEL_INFO ) )		{ VistaMutexLock oLock(VALog_getOutputStreamMutex() ); (*VA_STDOUT) << "[VAInfo   ] " << module << ": " << expr << std::endl; } }
#define VA_VERBOSE( module,expr )	{ if (VA_STDOUT && (VALog_GetLogLevel() >= IVACore::VA_LOG_LEVEL_VERBOSE ) )	{ VistaMutexLock oLock(VALog_getOutputStreamMutex() ); (*VA_STDOUT) << "[VAVerbose] " << module << ": " << expr << std::endl; } }
#define VA_TRACE( module, expr )	{ if (VA_STDOUT && (VALog_GetLogLevel() >= IVACore::VA_LOG_LEVEL_TRACE ) )		{ VistaMutexLock oLock(VALog_getOutputStreamMutex() ); (*VA_STDOUT) << "[VATrace  ] " << module << ": " << expr << std::endl; } }

class CVALogItem
{
public:
	double dTimestamp;
	std::string sLogger;
	std::string sMsg;

Jonas Stienen's avatar
Jonas Stienen committed
47 48 49 50 51 52 53 54 55 56 57
	inline CVALogItem()
		: dTimestamp( 0 )
	{
	};

	inline CVALogItem( double dTheTimestamp, const std::string sTheLogger, const std::string sTheMsg )
		: dTimestamp( dTheTimestamp )
		, sLogger( sTheLogger )
		, sMsg( sTheMsg )
	{
	};
Jonas Stienen's avatar
Jonas Stienen committed
58 59

	//! Grer-Operator zur Verwendung der sort() Funktion
Jonas Stienen's avatar
Jonas Stienen committed
60
	bool operator> ( CVALogItem const& rhs );
Jonas Stienen's avatar
Jonas Stienen committed
61 62

	//! Kleiner-Operation
Jonas Stienen's avatar
Jonas Stienen committed
63
	bool operator< ( CVALogItem const& rhs );
Jonas Stienen's avatar
Jonas Stienen committed
64 65 66 67 68 69

};

class CVARealtimeLogStream;

//! Realtime Logger entkoppelt die Ausgabe von Logger-Streams auf einem niederpriorisierten Thread
Jonas Stienen's avatar
Jonas Stienen committed
70 71
class CVARealtimeLogger : public VistaThreadLoop
{
Jonas Stienen's avatar
Jonas Stienen committed
72 73 74 75 76 77 78
public:
	CVARealtimeLogger();
	~CVARealtimeLogger();

	static CVARealtimeLogger* GetInstance();

	//! Streams registrieren
Jonas Stienen's avatar
Jonas Stienen committed
79
	void Register( CVARealtimeLogStream* pStream );
Jonas Stienen's avatar
Jonas Stienen committed
80 81

	//! Streams deregistrieren
Jonas Stienen's avatar
Jonas Stienen committed
82
	void Unregister( CVARealtimeLogStream* pStream );
Jonas Stienen's avatar
Jonas Stienen committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

	//! Thread anschubsen (nach Aktion)
	void Trigger();

	//! Ausgabe
	bool LoopBody();

	void PreLoop() {};
	void PostLoop() {};

private:
	VistaThreadEvent m_evTrigger;
	ITAAtomicInt m_iTriggerCnt;
	ITAAtomicBool m_bStop;
	std::list<CVARealtimeLogStream*> m_lpStreams;
	VistaMutex m_mxRegistration;
};

//! Implementierung des Echtzeit-Logger-Streams
/**
  * Nutzt den RealtimeLogger, um Stream-Informationen mittels
  * niederpriorisiertem Thread auf die Konsole oder Visual Studio Ausgabe
  * auszugeben.
  */
class CVARealtimeLogStream {
public:
Jonas Stienen's avatar
Jonas Stienen committed
109 110
	CVARealtimeLogStream( ITAClock* pClock = ITAClock::getDefaultClock() )
		: m_pClock( pClock ) {
Jonas Stienen's avatar
Jonas Stienen committed
111
		// Bei Ausgabe-Thread registrieren (Singleton)
Jonas Stienen's avatar
Jonas Stienen committed
112
		CVARealtimeLogger::GetInstance()->Register( this );
Jonas Stienen's avatar
Jonas Stienen committed
113 114 115 116
	}

	virtual ~CVARealtimeLogStream() {
		// Bei Thread deregistieren
Jonas Stienen's avatar
Jonas Stienen committed
117
		CVARealtimeLogger::GetInstance()->Unregister( this );
Jonas Stienen's avatar
Jonas Stienen committed
118 119 120 121 122 123
	}

	std::string GetName() const {
		return m_sName;
	}

Jonas Stienen's avatar
Jonas Stienen committed
124
	void SetName( const std::string& sName ) {
Jonas Stienen's avatar
Jonas Stienen committed
125 126 127 128
		m_sName = sName;
	}

	// Ausgabe
Jonas Stienen's avatar
Jonas Stienen committed
129 130
	void Printf( const char * format, ... ) {
		char buf[ 16384 ];
Jonas Stienen's avatar
Jonas Stienen committed
131
		va_list args;
Jonas Stienen's avatar
Jonas Stienen committed
132
		va_start( args, format );
133
		vsprintf( buf, format, args );
Jonas Stienen's avatar
Jonas Stienen committed
134
		va_end( args );
Jonas Stienen's avatar
Jonas Stienen committed
135

Jonas Stienen's avatar
Jonas Stienen committed
136
		m_qLog.push( CVALogItem( m_pClock->getTime(), m_sName, buf ) );
Jonas Stienen's avatar
Jonas Stienen committed
137 138 139 140 141 142 143 144 145 146 147
		// TODO: Thread zur Ausgabe anwerfen ggf.
	}

private:
	ITAClock* m_pClock;
	std::string m_sName;
	tbb::strict_ppl::concurrent_queue<CVALogItem> m_qLog;

	friend class CVARealtimeLogger;
};
#endif // __VA_LOG_H__