gKitGL
|
00001 00002 #ifndef _GK_LOGGER_H 00003 #define _GK_LOGGER_H 00004 00005 #include <cstdio> 00006 #include <string> 00007 #include <set> 00008 #include <vector> 00009 00010 00011 namespace gk { 00012 00013 //! affiche une seule fois chaque message. 00014 class Log 00015 { 00016 // non copyable 00017 Log( const Log& ); 00018 Log& operator= ( const Log& ); 00019 00020 std::set<std::string> m_slots; 00021 FILE *m_output; 00022 unsigned int m_level; 00023 00024 public: 00025 00026 //! plusieurs types de messages. 00027 enum { 00028 ERROR= 0, //!< pas bon ! 00029 WARNING, //!< oops 00030 MESSAGE //!< information 00031 }; 00032 00033 //! constructeur par defaut. 00034 Log( ); 00035 //! destructeur. 00036 ~Log( ); 00037 00038 //! utilsation interne. filtre un message formate / printf. 00039 //! utiliser les macros MESSAGE(), WARNING() et ERROR() a la place. 00040 void write( const unsigned int type, const char *file, const int line, const char *function, const char *format, ...); 00041 00042 //! redirige les messages vers un fichier texte. 00043 int setOutputFile( const char *filename ); 00044 00045 //! selectionne le type de message a afficher, dans l'ordre ERROR, WARNING, MESSAGE. 00046 int setOutputLevel( const unsigned int level ); 00047 00048 //! singleton. 00049 static 00050 Log& manager() 00051 { 00052 static Log logger; 00053 return logger; 00054 } 00055 }; 00056 00057 #ifndef NDEBUG 00058 #define MESSAGE(format, ...) gk::Log::manager().write(gk::Log::MESSAGE, __FILE__, __LINE__, __FUNCTION__, format, ## __VA_ARGS__ ) 00059 #define WARNING(format, ...) gk::Log::manager().write(gk::Log::WARNING, __FILE__, __LINE__, __FUNCTION__, format, ## __VA_ARGS__ ) 00060 #define ERROR(format, ...) gk::Log::manager().write(gk::Log::ERROR, __FILE__, __LINE__, __FUNCTION__, format, ## __VA_ARGS__ ) 00061 00062 #else 00063 // supprime toutes les sorties en mode release 00064 #define MESSAGE(format, ...) 00065 #define WARNING(format, ...) 00066 #define ERROR(format, ...) 00067 #endif 00068 00069 } // namespace 00070 00071 #endif