Include : aggiunti Logger e funzioni per Ini file.

This commit is contained in:
Dario Sassi
2013-12-11 18:32:43 +00:00
parent 609dac7878
commit 18e39e00f5
12 changed files with 404 additions and 19 deletions
+1 -1
View File
@@ -13,11 +13,11 @@
#pragma once
#include <string>
#include "/EgtDev/Include/EGnScan.h"
#include "/EgtDev/Include/EGkPoint3d.h"
#include "/EgtDev/Include/EGkFrame3d.h"
#include "/EgtDev/Include/EGkGeoObjType.h"
#include <string>
//----------------------- Macro per import/export ----------------------------
#undef EGK_EXPORT
+4 -2
View File
@@ -13,9 +13,10 @@
#pragma once
#include <string>
#include "/EgtDev/Include/EGkGdbConst.h"
#include "/EgtDev/Include/EGkGeoObj.h"
#include "/EgtDev/Include/EGtILogger.h"
#include <string>
//----------------------- 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 ;
+1 -1
View File
@@ -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 ; }
+15 -12
View File
@@ -11,6 +11,9 @@
//
//----------------------------------------------------------------------------
#pragma once
//-----------------------------------------------------------------------------
#include <windows.h>
@@ -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()))
+2
View File
@@ -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) ; }
-1
View File
@@ -14,7 +14,6 @@
#pragma once
//----------------------- Macro per import/export ----------------------------
#undef EGN_EXPORT
#if defined( I_AM_EGN) // da definirsi solo nella DLL
+2 -1
View File
@@ -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 ;
} ;
//----------------------------------------------------------------------------
+2 -1
View File
@@ -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 ;
} ;
+31
View File
@@ -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 <windows.h>
#include <string>
//----------------------- 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) ;
+57
View File
@@ -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 ;
} ;
+58
View File
@@ -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 <Winbase.h>
//----------------------------------------------------------------------------
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) ;
}
+231
View File
@@ -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 <iostream>
#include <vector>
#include <Windows.h>
#include <time.h>
//----------------------------------------------------------------------------
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<StreamInfo>::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<StreamInfo>::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<int>( GetCurrentThreadId(), bWritten, pStream) ;
if ( nItem & LI_LOGGERNAME)
bWritten = write<const char*>( m_name.c_str(), bWritten, pStream) ;
if ( nItem & LI_LOGGERLEVEL) {
char strLevel[4] ;
LogLevelToString( nLevel, strLevel) ;
bWritten = write<const char*>( strLevel, bWritten, pStream) ;
}
if ( nItem & LI_FUNCTION)
bWritten = write<const char*>( szFunc, bWritten, pStream) ;
if ( nItem & LI_FILENAME)
bWritten = write<const char*>( szFile, bWritten, pStream) ;
if ( nItem & LI_LINENUMBER)
bWritten = write<int>( nLine, bWritten, pStream) ;
bWritten = write<const char*>( 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<StreamInfo> m_outputStreams ;
IntraProcessLock m_threadProtect ;
template <class T>
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 ;
}
}
} ;
}