From 18e39e00f524cde8e75b7abefd61061d30561e4f Mon Sep 17 00:00:00 2001 From: Dario Sassi Date: Wed, 11 Dec 2013 18:32:43 +0000 Subject: [PATCH] Include : aggiunti Logger e funzioni per Ini file. --- EGkGeoObj.h | 2 +- EGkGeomDB.h | 6 +- EGnScan.h | 2 +- EGnStringConverter.h | 27 ++--- EGnStringUtils.h | 2 + EGnVersion.h | 1 - EgkGdbExecutor.h | 3 +- EgnCmdParser.h | 3 +- EgnGetModuleVer.h | 31 ++++++ EgtILogger.h | 57 +++++++++++ EgtIniFile.h | 58 +++++++++++ EgtLogger.h | 231 +++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 404 insertions(+), 19 deletions(-) create mode 100644 EgnGetModuleVer.h create mode 100644 EgtILogger.h create mode 100644 EgtIniFile.h create mode 100644 EgtLogger.h diff --git a/EGkGeoObj.h b/EGkGeoObj.h index 088af5d..3cfcf4a 100644 --- a/EGkGeoObj.h +++ b/EGkGeoObj.h @@ -13,11 +13,11 @@ #pragma once -#include #include "/EgtDev/Include/EGnScan.h" #include "/EgtDev/Include/EGkPoint3d.h" #include "/EgtDev/Include/EGkFrame3d.h" #include "/EgtDev/Include/EGkGeoObjType.h" +#include //----------------------- Macro per import/export ---------------------------- #undef EGK_EXPORT diff --git a/EGkGeomDB.h b/EGkGeomDB.h index 6eef7cc..6d667cf 100644 --- a/EGkGeomDB.h +++ b/EGkGeomDB.h @@ -13,9 +13,10 @@ #pragma once -#include #include "/EgtDev/Include/EGkGdbConst.h" #include "/EgtDev/Include/EGkGeoObj.h" +#include "/EgtDev/Include/EGtILogger.h" +#include //----------------------- Macro per import/export ---------------------------- #undef EGK_EXPORT @@ -30,7 +31,8 @@ class _declspec( novtable) IGeomDB { public : virtual ~IGeomDB( void) {} - virtual bool Init( void) = 0 ; + virtual bool Init( ILogger* pLogger) = 0 ; + virtual bool ReInit( void) = 0 ; virtual bool Clear( void) = 0 ; virtual bool Load( std::ifstream& osIn) = 0 ; virtual bool Save( std::ofstream& osOut) const = 0 ; diff --git a/EGnScan.h b/EGnScan.h index 5268b7f..1c7336a 100644 --- a/EGnScan.h +++ b/EGnScan.h @@ -30,7 +30,7 @@ class EGN_EXPORT CScan public : CScan( void) ; ~CScan( void) ; - bool Initialize( std::ifstream& InFile) ; + bool Init( std::ifstream& InFile) ; bool Terminate( void) ; bool GetLine( std::string& sLine) ; int GetCurrLineNbr( void) { return m_nLineNbr ; } diff --git a/EGnStringConverter.h b/EGnStringConverter.h index 7884cf4..deb7f9b 100644 --- a/EGnStringConverter.h +++ b/EGnStringConverter.h @@ -11,6 +11,9 @@ // //---------------------------------------------------------------------------- +#pragma once + + //----------------------------------------------------------------------------- #include @@ -120,10 +123,10 @@ inline bool IsTextUtf8( const char* psz) //----------------------------------------------------------------------------- template< int t_nBufferLength = 128 > -class CAtoWEX +class AtoWEX { public : - CAtoWEX( const char* psz) : + AtoWEX( const char* psz) : m_psz( m_szBuffer) { if ( IsTextUtf8( psz)) @@ -131,13 +134,13 @@ class CAtoWEX else Init( psz, CP_ACP) ; } - CAtoWEX( const char* psz, unsigned int nCodePage) : + AtoWEX( const char* psz, unsigned int nCodePage) : m_psz( m_szBuffer) { Init( psz, nCodePage) ; } - ~CAtoWEX( void) + ~AtoWEX( void) { SmartFreeMemory( m_psz, m_szBuffer, t_nBufferLength) ; } @@ -176,27 +179,27 @@ class CAtoWEX wchar_t m_szBuffer[t_nBufferLength] ; } ; -typedef CAtoWEX<> CAtoW ; -#define stringtoW(s) LPWSTR( CAtoW( s.c_str())) +typedef AtoWEX<> AtoW ; +#define stringtoW(s) LPWSTR( AtoW( s.c_str())) //----------------------------------------------------------------------------- template < int t_nBufferLength = 128 > -class CWtoAEX +class WtoAEX { public : - CWtoAEX( const wchar_t* psz) : + WtoAEX( const wchar_t* psz) : m_psz( m_szBuffer) { Init( psz, CP_UTF8) ; } - CWtoAEX( const wchar_t* psz, unsigned int nCodePage) : + WtoAEX( const wchar_t* psz, unsigned int nCodePage) : m_psz( m_szBuffer) { Init( psz, nCodePage) ; } - ~CWtoAEX( void) + ~WtoAEX( void) { SmartFreeMemory( m_psz, m_szBuffer, t_nBufferLength) ; } @@ -240,5 +243,5 @@ class CWtoAEX char m_szBuffer[t_nBufferLength] ; } ; -typedef CWtoAEX<> CWtoA ; -#define wstringtoA(ws) LPSTR( CWtoA( ws.c_str())) +typedef WtoAEX<> WtoA ; +#define wstringtoA(ws) LPSTR( WtoA( ws.c_str())) diff --git a/EGnStringUtils.h b/EGnStringUtils.h index e73e706..5c7ba39 100644 --- a/EGnStringUtils.h +++ b/EGnStringUtils.h @@ -69,6 +69,7 @@ inline bool FromString( const std::string& sVal, int& nVal) { const char* pStart ; char* pStop ; + errno = 0 ; pStart = sVal.c_str() ; nVal = strtol( pStart, &pStop, 10) ; return ( pStop != pStart && *pStop == '\0' && errno == 0) ; } @@ -83,6 +84,7 @@ inline bool FromString( const std::string& sVal, double& dVal) { const char* pStart ; char* pStop ; + errno = 0 ; pStart = sVal.c_str() ; dVal = strtod( pStart, &pStop) ; return ( pStop != pStart && *pStop == '\0' && errno == 0) ; } diff --git a/EGnVersion.h b/EGnVersion.h index 63782f5..c430772 100644 --- a/EGnVersion.h +++ b/EGnVersion.h @@ -14,7 +14,6 @@ #pragma once - //----------------------- Macro per import/export ---------------------------- #undef EGN_EXPORT #if defined( I_AM_EGN) // da definirsi solo nella DLL diff --git a/EgkGdbExecutor.h b/EgkGdbExecutor.h index f5a66cb..1377ff9 100644 --- a/EgkGdbExecutor.h +++ b/EgkGdbExecutor.h @@ -14,6 +14,7 @@ #pragma once #include "/EgtDev/Include/EgnCmdExecutor.h" +#include "/EgtDev/Include/EgtILogger.h" #include "/EgtDev/Include/EgkGeomDB.h" //----------------------- Macro per import/export ---------------------------- @@ -29,7 +30,7 @@ class _declspec( novtable) IGdbExecutor : public ICmdExecutor { public : virtual ~IGdbExecutor( void) {} - virtual bool Set( IGeomDB* pGdb) = 0 ; + virtual bool Init( IGeomDB* pGdb, ILogger* pLogger) = 0 ; } ; //---------------------------------------------------------------------------- diff --git a/EgnCmdParser.h b/EgnCmdParser.h index 83129ea..27aaf8c 100644 --- a/EgnCmdParser.h +++ b/EgnCmdParser.h @@ -15,6 +15,7 @@ #include "/EgtDev/Include/EgnStringBase.h" #include "/EgtDev/Include/EgnCmdExecutor.h" +#include "/EgtDev/Include/EgtILogger.h" //----------------------- Macro per import/export ----------------------------- #undef EGN_EXPORT @@ -29,7 +30,7 @@ class _declspec( novtable) ICmdParser { public : virtual ~ICmdParser( void) {} - virtual bool Initialize( std::wstring sCmdFile, ICmdExecutor* pCmdExec) = 0 ; + virtual bool Init( std::string sCmdFile, ICmdExecutor* pCmdExec, ILogger* pLogger) = 0 ; virtual bool Run( void) = 0 ; } ; diff --git a/EgnGetModuleVer.h b/EgnGetModuleVer.h new file mode 100644 index 0000000..e36d144 --- /dev/null +++ b/EgnGetModuleVer.h @@ -0,0 +1,31 @@ +//---------------------------------------------------------------------------- +// EgalTech 2013-2013 +//---------------------------------------------------------------------------- +// File : EgnGetModuleVer.h Data : 10.12.13 Versione : 1.4a4 +// Contenuto : Prototipo funzione calcolo versione del modulo richiesto. +// +// +// +// Modifiche : 10.12.13 DS Creazione modulo. +// 11.12.13 DS Agg. funzione per calcolo direttorio. +// +//---------------------------------------------------------------------------- + +#pragma once + +#include +#include + + +//----------------------- Macro per import/export ---------------------------- +#undef EGN_EXPORT +#if defined( I_AM_EGN) // da definirsi solo nella DLL + #define EGN_EXPORT __declspec( dllexport) +#else + #define EGN_EXPORT __declspec( dllimport) +#endif + +//----------------------------------------------------------------------------- +EGN_EXPORT bool GetModuleVersion( HINSTANCE hModule, std::string& sVer) ; +EGN_EXPORT bool GetModuleDirectory( HINSTANCE hModule, std::string& sDir) ; +EGN_EXPORT bool SetCurrentDirectory( const std::string& sDir) ; diff --git a/EgtILogger.h b/EgtILogger.h new file mode 100644 index 0000000..0575cea --- /dev/null +++ b/EgtILogger.h @@ -0,0 +1,57 @@ +//---------------------------------------------------------------------------- +// EgalTech 2013-2013 +//---------------------------------------------------------------------------- +// File : EgtILogger.h Data : 09.12.13 Versione : 1.4a4 +// Contenuto : Interfaccia per logger. +// +// +// +// Modifiche : 09.12.13 DS Creazione modulo. +// +// +//---------------------------------------------------------------------------- + +#pragma once + +//---------------------------------------------------------------------------- +#define LOG_DATETIME( pLog, szText) if ( (pLog) != nullptr) \ + (pLog)->Log( LL_INFO, LI_DATETIME, nullptr, 0, nullptr, szText) ; +#define LOG_INFO( pLog, szText) if ( (pLog) != nullptr) \ + (pLog)->Log( LL_INFO, 0, nullptr, 0, nullptr, szText) ; +#define LOG_WARN( pLog, szText) if ( (pLog) != nullptr) \ + ((pLog)->Log( LL_WARN, 0, nullptr, 0, nullptr, szText) ; +#define LOG_ERROR( pLog, szText) if ( (pLog) != nullptr) \ + (pLog)->Log( LL_ERROR, 0, nullptr, 0, nullptr, szText) ; +#define LOG_DEBUG( pLog, szText) if ( (pLog) != nullptr) \ + (pLog)->Log( LL_DEBUG, __FILE__, __LINE__, __FUNCTION__, szText) ; +#define LOG( pLog, nLev, nItem, szText) if ( (pLog) != nullptr) \ + (pLog)->Log( nLev, nItem, __FILE__, __LINE__, __FUNCTION__, szText) ; + +//---------------------------------------------------------------------------- +enum LogLevel +{ + LL_DEBUG = 1, + LL_INFO, + LL_WARN, + LL_ERROR +} ; + +//---------------------------------------------------------------------------- +enum LogItem +{ + LI_FILENAME = 0x1, + LI_LINENUMBER = 0x2, + LI_FUNCTION = 0x4, + LI_DATETIME = 0x8, + LI_THREADID = 0x10, + LI_LOGGERNAME = 0x20, + LI_LOGGERLEVEL = 0x40 +} ; + +//---------------------------------------------------------------------------- +class _declspec( novtable) ILogger +{ + public : + virtual void Log( LogLevel nLevel, const char* szFile, int nLine, const char* szFunc, const char* szText) = 0 ; + virtual void Log( LogLevel nLevel, int nItem, const char* szFile, int nLine, const char* szFunc, const char* szText) = 0 ; +} ; diff --git a/EgtIniFile.h b/EgtIniFile.h new file mode 100644 index 0000000..f2400c6 --- /dev/null +++ b/EgtIniFile.h @@ -0,0 +1,58 @@ +//---------------------------------------------------------------------------- +// EgalTech 2013-2013 +//---------------------------------------------------------------------------- +// File : EgtIniFile.h Data : 10.12.13 Versione : 1.4a4 +// Contenuto : Funzioni per leggere e scrivere stringhe UTF-8 in file INI. +// +// +// +// Modifiche : 10.12.13 DS Creazione modulo. +// +// +//---------------------------------------------------------------------------- + +#pragma once + +#include "/EgtDev/Include/EGnStringUtils.h" +#include "/EgtDev/Include/EGnStringConverter.h" +#include + +//---------------------------------------------------------------------------- +inline std::string +GetPrivateProfileStringUtf8( const char* szSection, const char* szKey, const char* szDefault, const char* szIniFile) +{ + int nChar ; + wchar_t szBuffW[_MAX_PATH] ; + + + // leggo la stringa dal file INI come ANSI/UTF-8 in WChar e metto in Char + nChar = GetPrivateProfileStringW( AtoW( szSection), AtoW( szKey), NULL, szBuffW, _MAX_PATH, AtoW( szIniFile)) ; + + if ( nChar > 0) { + return LPSTR( WtoAEX<>( szBuffW, CP_ACP)) ; + } + else + return szDefault ; +} + +//---------------------------------------------------------------------------- +inline bool +WritePrivateProfileStringUtf8( const char* szSection, const char* szKey, const char* szVal, const char* szIniFile) +{ + // salvo la stringa nel file INI come ANSI/UTF-8 ma scritta in variabile WChar + return WritePrivateProfileStringW( AtoW( szSection), AtoW( szKey), AtoWEX<>( szVal, CP_ACP), AtoW( szIniFile)) != 0 ; +} + +//---------------------------------------------------------------------------- +inline int +GetPrivateProfileInt( const char* szSection, const char* szKey, int nDefault, const char* szIniFile) +{ + return GetPrivateProfileIntW( AtoW( szSection), AtoW( szKey), nDefault, AtoW( szIniFile)) ; +} + +//---------------------------------------------------------------------------- +inline bool +WritePrivateProfileInt( const char* szSection, const char* szKey, int nVal, const char* szIniFile) +{ + return WritePrivateProfileStringUtf8( szSection, szKey, ToString( nVal).c_str(), szIniFile) ; +} diff --git a/EgtLogger.h b/EgtLogger.h new file mode 100644 index 0000000..0aa85c7 --- /dev/null +++ b/EgtLogger.h @@ -0,0 +1,231 @@ +//---------------------------------------------------------------------------- +// EgalTech 2013-2013 +//---------------------------------------------------------------------------- +// File : EgtLogger.h Data : 06.12.13 Versione : 1.4a4 +// Contenuto : Classi per logger. +// +// +// +// Modifiche : 06.12.13 DS Creazione modulo. +// +// +//---------------------------------------------------------------------------- + +#pragma once + +#include "/EgtDev/Include/EgtILogger.h" +#include +#include +#include +#include + +//---------------------------------------------------------------------------- +namespace egtlogger +{ + //---------------------------------------------------------------------------- + class IntraProcessLock + { + public : + IntraProcessLock( bool bThreadSafe) + : m_bThreadSafe( bThreadSafe) + { + if ( m_bThreadSafe) + InitializeCriticalSection( &m_cs) ; + } + + ~IntraProcessLock( void) + { + if ( m_bThreadSafe) + DeleteCriticalSection( &m_cs) ; + } + + void Lock( void) + { + if ( m_bThreadSafe) + EnterCriticalSection( &m_cs) ; + } + + void Unlock( void) + { + if ( m_bThreadSafe) + LeaveCriticalSection( &m_cs) ; + } + + private : + bool m_bThreadSafe ; + CRITICAL_SECTION m_cs ; + } ; + + //---------------------------------------------------------------------------- + class Logger : public ILogger + { + private : + struct StreamInfo + { + std::ostream* pStream ; + bool bOwned ; + LogLevel nLevel ; + + StreamInfo( std::ostream* pStream, bool bOwned, LogLevel nLevel) + { + this->pStream = pStream ; + this->bOwned = bOwned ; + this->nLevel = nLevel ; + } + } ; + + public : + Logger( LogLevel nLevel, const char* szName, bool bThreadSafe = false, + int nLoggableItems = LI_DATETIME | LI_LOGGERNAME | LI_LOGGERLEVEL | LI_FUNCTION | LI_LINENUMBER) + : m_nLevel( nLevel), m_name( szName), m_threadProtect( bThreadSafe), m_loggableItem( nLoggableItems) + { + } + + ~Logger( void) + { + ClearOutputStreams() ; + } + + void AddOutputStream( std::ostream& os, bool bOwn, LogLevel nLevel) + { + AddOutputStream( &os, bOwn, nLevel) ; + } + + void AddOutputStream( std::ostream& os, bool bOwn) + { + AddOutputStream( os, bOwn, m_nLevel) ; + } + + void AddOutputStream( std::ostream* os, bool bOwn) + { + AddOutputStream( os, bOwn, m_nLevel) ; + } + + void AddOutputStream( std::ostream* os, bool bOwn, LogLevel nLevel) + { + StreamInfo si( os, bOwn, nLevel) ; + m_outputStreams.push_back( si) ; + } + + void ClearOutputStreams( void) + { + std::vector::iterator iter ; + for ( iter = m_outputStreams.begin(); iter < m_outputStreams.end() ; ++iter) { + if ( iter->bOwned) + delete iter->pStream ; + } + + m_outputStreams.clear() ; + } + + void Log( LogLevel nLevel, const char* szFile, int nLine, const char* szFunc, const char* szText) + { + Log( nLevel, m_loggableItem, szFile, nLine, szFunc, szText) ; + } + + void Log( LogLevel nLevel, int nItem, const char* szFile, int nLine, const char* szFunc, const char* szText) + { + m_threadProtect.Lock() ; + + std::vector::iterator iter ; + for ( iter = m_outputStreams.begin() ; iter < m_outputStreams.end() ; ++iter) { + if ( nLevel < iter->nLevel) + continue ; + + bool bWritten = false ; + std::ostream* pStream = iter->pStream ; + if ( pStream == nullptr) + continue ; + + if ( nItem & LI_DATETIME) + bWritten = write_datetime( bWritten, pStream) ; + + if ( nItem & LI_THREADID) + bWritten = write( GetCurrentThreadId(), bWritten, pStream) ; + + if ( nItem & LI_LOGGERNAME) + bWritten = write( m_name.c_str(), bWritten, pStream) ; + + if ( nItem & LI_LOGGERLEVEL) { + char strLevel[4] ; + LogLevelToString( nLevel, strLevel) ; + bWritten = write( strLevel, bWritten, pStream) ; + } + + if ( nItem & LI_FUNCTION) + bWritten = write( szFunc, bWritten, pStream) ; + + if ( nItem & LI_FILENAME) + bWritten = write( szFile, bWritten, pStream) ; + + if ( nItem & LI_LINENUMBER) + bWritten = write( nLine, bWritten, pStream) ; + + bWritten = write( szText, bWritten, pStream) ; + + if ( bWritten) { + (*pStream) << std::endl ; + pStream->flush() ; + } + } + + m_threadProtect.Unlock() ; + } + + private : + int m_loggableItem ; + LogLevel m_nLevel ; + std::string m_name ; + std::vector m_outputStreams ; + IntraProcessLock m_threadProtect ; + + template + bool write( T data, bool bWritten, std::ostream* strm) + { + if ( bWritten) + (*strm) << " " ; + (*strm) << data ; + return true ; + } + + bool write_datetime( bool bWritten, std::ostream* strm) + { + if ( bWritten) + (*strm) << " " ; + + time_t szClock ; + tm newTime ; + + time( &szClock) ; + localtime_s( &newTime, &szClock) ; + + char strDate[10] = { '\0'} ; + char strTime[10] = { '\0'} ; + + _strdate_s( strDate, 10) ; + _strtime_s( strTime, 10) ; + + (*strm) << strDate << " " << strTime ; + + return true ; + } + + void LogLevelToString( LogLevel nLevel, char* strLevel) + { + switch ( nLevel) { + case LL_ERROR : + strcpy_s( strLevel, 4, "ERR") ; + break ; + case LL_WARN : + strcpy_s( strLevel, 4, "WRN") ; + break ; + case LL_INFO : + strcpy_s( strLevel, 4, "INF") ; + break ; + case LL_DEBUG : + strcpy_s( strLevel, 4, "DBG") ; + break ; + } + } + } ; +}