[112] | 1 | #ifndef __XMLIO_LOGGER__ |
---|
| 2 | #define __XMLIO_LOGGER__ |
---|
| 3 | |
---|
| 4 | #include <csignal> |
---|
| 5 | |
---|
| 6 | #ifdef __GNUC__ |
---|
| 7 | #include <execinfo.h> |
---|
| 8 | #include <cxxabi.h> |
---|
| 9 | #endif // __GNUC__ |
---|
| 10 | |
---|
[122] | 11 | using std::string; |
---|
[112] | 12 | |
---|
| 13 | namespace XMLIOSERVER |
---|
| 14 | { |
---|
[122] | 15 | class ILogger : public std::ostringstream |
---|
[112] | 16 | { |
---|
[122] | 17 | private : |
---|
[112] | 18 | |
---|
[128] | 19 | ILogger(void) |
---|
[122] | 20 | { signal(SIGSEGV, SigHandler); } |
---|
[112] | 21 | |
---|
[128] | 22 | private : /* static */ |
---|
| 23 | |
---|
[122] | 24 | static ILogger LOGGER; |
---|
[112] | 25 | |
---|
[128] | 26 | public : /* static */ |
---|
[122] | 27 | |
---|
| 28 | static ILogger& GetLogger(void) { return (ILogger::LOGGER); } |
---|
| 29 | |
---|
[128] | 30 | static void Flush(std::ostream& out = std::clog) |
---|
| 31 | { ILogger::GetLogger().flush(out); } |
---|
| 32 | |
---|
[112] | 33 | static void ShowBTrace(std::ostream& out = std::clog) |
---|
| 34 | { |
---|
| 35 | #ifdef __GNUC__ |
---|
| 36 | int status = 0; |
---|
| 37 | string value; |
---|
| 38 | void *stack_addrs[20] = {NULL}; |
---|
| 39 | size_t stack_depth = backtrace(stack_addrs, 20); |
---|
| 40 | char **stack_strings = backtrace_symbols(stack_addrs, stack_depth); |
---|
| 41 | |
---|
| 42 | out << "Trace : " << std::endl; |
---|
| 43 | for (size_t i = 2, j = 0; i < stack_depth; i++) |
---|
| 44 | { |
---|
| 45 | value.assign(stack_strings[i]); |
---|
| 46 | size_t bpos = value.find ('('), epos = value.find ('+'); |
---|
| 47 | if ((bpos != string::npos) and (epos != string::npos)) |
---|
| 48 | { |
---|
| 49 | char* demangled_name = abi::__cxa_demangle(value.substr(bpos+1, epos-bpos-1).c_str(), 0, 0, &status); |
---|
| 50 | if (status == 0) out << " " << ++j << " -> " << (demangled_name) << std::endl; |
---|
| 51 | else out << " " << ++j << " -> " << value.substr(bpos+1, epos-bpos-1) << std::endl; |
---|
| 52 | free(demangled_name); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | free(stack_strings); |
---|
| 57 | #endif // __GNUC__ |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | static void SigHandler(int _sigValue) |
---|
| 61 | { |
---|
| 62 | std::cerr << "============= Erreur =============" << std::endl; |
---|
| 63 | std::cerr << "Signal " << _sigValue << " reçu !" << std::endl; |
---|
| 64 | ShowBTrace(std::cerr); |
---|
| 65 | std::cerr << "==================================" << std::endl; |
---|
| 66 | exit(EXIT_FAILURE); |
---|
| 67 | } |
---|
| 68 | |
---|
[128] | 69 | public : |
---|
[112] | 70 | |
---|
[128] | 71 | void flush(std::ostream& out = std::clog) |
---|
| 72 | { out << str() << std::endl; str(""); } |
---|
| 73 | |
---|
| 74 | ~ILogger(void) { this->flush(); } |
---|
| 75 | |
---|
[138] | 76 | }; // class ILogger |
---|
[112] | 77 | |
---|
| 78 | // Initialisation de la classe de Logging |
---|
[122] | 79 | ILogger ILogger::LOGGER; |
---|
[112] | 80 | |
---|
| 81 | ///////////////////////////////////////////////////////////////////// |
---|
[120] | 82 | unsigned int Indent = 0; |
---|
| 83 | const char* Increm = " "; |
---|
[112] | 84 | |
---|
| 85 | std::ostream& NIndent(std::ostream& out) |
---|
| 86 | { |
---|
| 87 | static unsigned int LineNB = 1; |
---|
| 88 | out<< LineNB++ << ". "; |
---|
| 89 | for(unsigned int i = 0;i < Indent; i++) out << Increm; |
---|
| 90 | return(out); |
---|
| 91 | } |
---|
| 92 | |
---|
[137] | 93 | std::ostream& IncIndent(std::ostream& out) |
---|
| 94 | { Indent++; return (NIndent(out)); } |
---|
| 95 | |
---|
| 96 | std::ostream& DecEndl (std::ostream& out) |
---|
| 97 | { Indent--; return (out); } |
---|
[112] | 98 | ///////////////////////////////////////////////////////////////////// |
---|
| 99 | |
---|
[114] | 100 | } // namespace XMLIOSERVER |
---|
[112] | 101 | |
---|
[128] | 102 | #define ERROR(MSG)\ |
---|
| 103 | (ILogger::GetLogger() << __FILE__ << ":" << __LINE__ << ": error : " << MSG << std::endl) |
---|
| 104 | #define WARNING(MSG)\ |
---|
| 105 | (ILogger::GetLogger() << __FILE__ << ":" << __LINE__ << ": warning : " << MSG << std::endl) |
---|
| 106 | #define INFO(MSG)\ |
---|
| 107 | (ILogger::GetLogger() << __FILE__ << ":" << __LINE__ << ": info : " << MSG << std::endl) |
---|
[112] | 108 | // A compléter. |
---|
| 109 | |
---|
| 110 | #endif // __XMLIO_LOGGER__ |
---|