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
|
#ifndef LOGGER_HPP
#define LOGGER_HPP
// Output Type (Flags)
#define LOG_FILE 0x00000001
#define LOG_SCREEN 0x00000002
#define LOG_CONSOLE 0x00000004
#define LOG_ALL LOG_FILE | LOG_SCREEN | LOG_CONSOLE
// Msg Type (1 = Log / 0 = Don't)
#define LOG_OK 1
#define LOG_WARNING 1
#define LOG_ERROR 1
#define LOG_MISC 1
// 0 -> __FUNCTION__ macro supported ; 1 -> Define your own macro .
#if 0
#define __FUNCTION__ "__FUNCION__ isn't available"
#endif
// The actual logging macro.
#define Log(flag,type) Logger<type == 1>(#type,flag,__FILE__,__FUNCTION__,__LINE__)
#include <sstream>
#include <iostream>
#include <fstream>
#include <boost/algorithm/string/replace.hpp>
// Template, general declaration
template<bool DoLog> class Logger;
// Template version, if we actually want to log something .
template<> class Logger<true>
{
public :
Logger(const char * Type,unsigned const char Flag,const char * File,const char * Function,unsigned int Line)
: m_Type(Type) ,
m_Flag(Flag) ,
m_File(File) ,
m_Function(Function) ,
m_Line(Line) {}
~Logger()
{
if ( m_Flag & LOG_FILE )
{
boost::algorithm::replace_all(m_Function,"<","<");
boost::algorithm::replace_all(m_Function,">;",">");
m_FileStream << "<EventLog>" << "\n" ;
m_FileStream << "<Type>" << m_Type << "</Type>" << "\n" ;
m_FileStream << "<File>" << m_File << "</File>" << "\n" ;
m_FileStream << "<Line>" << m_Line << "</Line>" << "\n" ;
m_FileStream << "<Function>" << m_Function << "</Function>" << "\n" ;
m_FileStream << "<Msg>" << m_Msg.str() << "</Msg>" << "\n" ;
m_FileStream << "</EventLog>" << "\n" << "\n" << std::flush ;
}
if ( m_Flag & LOG_SCREEN )
{ /* TODO */ }
if ( m_Flag & LOG_CONSOLE )
{ /* TODO */ }
}
static void Init()
{
m_FileStream << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" << "\n" ;
m_FileStream << "<?xml-stylesheet type=\"text/xsl\" href=\"Log.txt\" ?>" << "\n" ;
m_FileStream << "<RuntimeLog>" << "\n" << "\n" ;
m_FileStream << "<LogHeader>" << "\n" ;
/* TODO : Find what i need in the LogHeader . */
m_FileStream << "</LogHeader>" << "\n" << "\n" << std::flush ;
}
static void Close()
{
m_FileStream << "</RuntimeLog>" << std::flush ;
m_FileStream.close();
}
template<typename T> Logger & operator << (T const & Msg) { m_Msg << Msg; return * this; }
private:
std::stringstream m_Msg;
unsigned const char m_Flag ;
const char * m_File ;
const int m_Line ;
const char * m_Type ;
std::string m_Function ;
static std::ofstream m_FileStream ;
};
// Template version, if we do NOT want to log .
template<> class Logger<false>
{
public :
Logger(const char * Type,unsigned const char Flag,const char * File,const char * Function,unsigned int Line){}
template<typename T> Logger & operator << (T const & Msg) { return * this; }
};
#endif |
Partager