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