1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include <string>
#include <sstream>
// gestion des logs
#define LOG_FCT_HIGH_LEVEL 0x0001
#define LOG_FCT_LOW_LEVEL 0x0002
#define LOG_FCT_SYNCHRO 0x0004
#define LOG_PATH 0x0008
#define LOG_DETAIL 0x0010
#define LOG_SQL 0x0020
#define LOG_LBL_FCT_HIGH_LEVEL _T("FCT_HIGH_LEVEL")
#define LOG_LBL_FCT_LOW_LEVEL _T("FCT_LOW_LEVEL")
#define LOG_LBL_FCT_SYNCHRO _T("FCT_SYNCHRO")
#define LOG_LBL_PATH _T("PATH")
#define LOG_LBL_DETAIL _T("DETAIL")
#define LOG_LBL_SQL _T("SQL")
#define LOG_DELIMITER '@'
typedef unsigned short type_tag;
unsigned short ConvHexDec( wxString hex )
{
std::string sHex = wx2std(hex);
unsigned short x;
std::istringstream iss( sHex );
iss >> std::hex >> x;
return x;
}
//! converts wxString to std::string
std::string wx2std(const wxString& input, wxMBConv* conv)
{
if (input.empty())
return "";
if (!conv)
conv = wxConvCurrent;
return std::string(input.mb_str(*conv));
}
wxString LogTagString( type_tag tag )
{
wxString s;
s.Empty();
if( (tag & LOG_FCT_HIGH_LEVEL) != 0 )
s << LOG_LBL_FCT_HIGH_LEVEL << _T(", ");
if( (tag & LOG_FCT_LOW_LEVEL) != 0 )
s << LOG_LBL_FCT_LOW_LEVEL << _T(", ");
if( (tag & LOG_FCT_SYNCHRO) != 0 )
s << LOG_LBL_FCT_SYNCHRO << _T(", ");
if( (tag & LOG_PATH) != 0 )
s << LOG_LBL_PATH << _T(", ");
if( (tag & LOG_DETAIL) != 0 )
s << LOG_LBL_DETAIL << _T(", ");
if( (tag & LOG_SQL) != 0 )
s << LOG_LBL_SQL << _T(", ");
if( !s.IsEmpty() )
s = s.Left( s.Len() -2 ); // désallocation de s avant sa réaffectation ?
return( s );
}
class LogTextCtrlLevel : public wxLogTextCtrl
{
public:
type_tag tag;
bool bShowTag;
LogTextCtrlLevel(wxTextCtrl *textctrl, type_tag _tag, bool _bShowTag)
: tag(_tag) , bShowTag(_bShowTag) , wxLogTextCtrl(textctrl) { }
void DoLog(wxLogLevel level, const wxChar *msg, time_t timestamp);
};
void LogTextCtrlLevel::DoLog(wxLogLevel level, const wxChar *msg, time_t timestamp)
{
// recherche du filtre en début du message
wxString log = wxString(msg).AfterFirst( wxChar(LOG_DELIMITER) );
if( log.IsEmpty() || wxString(msg).Find(log) != 5 ) {
// log du message sans tag
wxLogTextCtrl::DoLog( level, msg, timestamp );
} else {
// conversion des tags en valeur
wxString s = msg[0];
s << msg[1] << msg[2] << msg[3];
type_tag i = ConvHexDec( s );
// affichage des tags
if( bShowTag )
log = LogTagString(i) + _T(" -> ") + log; // désallocation de log avant sa réaffectation ?
// comparaison des tags
if( (tag & i) != 0 ) {
// affichage de la log
wxLogTextCtrl::DoLog( level, log.GetData(), timestamp );
} /*else { // à commenter
wxLogTextCtrl::DoLog( level, log.GetData(), timestamp );
wxLogTextCtrl::DoLog( level, log.GetData(), timestamp );
}*/
}
}
/// ***********************************************************************
void MainFrame::InitLog()
{
// création du TextCtrl qui contient l'affichage des logs
m_Log = new wxTextCtrl(this,wxID_ANY, wxEmptyString,
wxPoint(0,0), wxSize(600,150),
wxTE_MULTILINE | wxTE_READONLY );
// redirection de toutes les logs vers le fichier de log
wxLogStream* logger = new wxLogStream(new std::ofstream("app.log", std::ios_base::out|std::ios_base::app));
// activation de la log cible
m_logOld = wxLog::SetActiveTarget( logger ); // ?
// ajout d'un chainage des log vers le TextCtrl
wxLogChain *logger2 = new wxLogChain( new LogTextCtrlLevel( m_Log, LOG_DETAIL | LOG_SQL, true ) );
wxLog::SetTimestamp( wxT("%Y-%M-%d %X") );
/// Exemple de log
wxLogMessage( wxT("%.4x%c Essai Log affiche"), (type_tag) LOG_PATH | LOG_SQL, LOG_DELIMITER );
wxLogMessage( wxT("%.4x%c Essai Log non affiche"), (type_tag) LOG_FCT_LOW_LEVEL | LOG_PATH, LOG_DELIMITER );
} |
Partager