[112] | 1 | #ifndef __XMLIO_LOGGER__ |
---|
| 2 | #define __XMLIO_LOGGER__ |
---|
| 3 | |
---|
| 4 | // Entête Poco logging |
---|
| 5 | #include <Poco/Logger.h> |
---|
| 6 | #include <Poco/PatternFormatter.h> |
---|
| 7 | #include <Poco/FormattingChannel.h> |
---|
| 8 | #include <Poco/ConsoleChannel.h> |
---|
| 9 | #include <Poco/FileChannel.h> |
---|
| 10 | #include <Poco/Message.h> |
---|
| 11 | |
---|
| 12 | #include <Poco/AutoPtr.h> |
---|
| 13 | |
---|
| 14 | #include <csignal> |
---|
| 15 | |
---|
| 16 | #ifdef __GNUC__ |
---|
| 17 | #include <execinfo.h> |
---|
| 18 | #include <cxxabi.h> |
---|
| 19 | #endif // __GNUC__ |
---|
| 20 | |
---|
| 21 | // Classes utilisées issues de Poco |
---|
| 22 | using Poco::Logger; |
---|
| 23 | using Poco::PatternFormatter; |
---|
| 24 | using Poco::FormattingChannel; |
---|
| 25 | using Poco::ConsoleChannel; |
---|
| 26 | using Poco::FileChannel; |
---|
| 27 | using Poco::Message; |
---|
| 28 | |
---|
| 29 | using Poco::AutoPtr; |
---|
| 30 | |
---|
| 31 | namespace XMLIOSERVER |
---|
| 32 | { |
---|
| 33 | class ILogger |
---|
| 34 | { |
---|
| 35 | public : |
---|
| 36 | |
---|
[114] | 37 | ILogger() |
---|
[112] | 38 | { |
---|
| 39 | // Perte mémoire faible ici (/ still reachable: 84 bytes in 2 blocks /) |
---|
| 40 | |
---|
| 41 | // TODO Créer une sortie fichier. |
---|
| 42 | AutoPtr<PatternFormatter> pf = new PatternFormatter("[%Y-%m-%d %H:%M:%S] %t"); |
---|
| 43 | AutoPtr<ConsoleChannel> cc = new ConsoleChannel(); |
---|
| 44 | AutoPtr<FormattingChannel> pFCConsole = new FormattingChannel(pf); |
---|
| 45 | pFCConsole->setChannel(cc); |
---|
| 46 | pFCConsole->open(); |
---|
| 47 | Logger::create("ConsoleLogger", pFCConsole, Message::PRIO_INFORMATION); |
---|
| 48 | |
---|
| 49 | signal(SIGSEGV, SigHandler); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | static void ShowBTrace(std::ostream& out = std::clog) |
---|
| 54 | { |
---|
| 55 | #ifdef __GNUC__ |
---|
| 56 | int status = 0; |
---|
| 57 | string value; |
---|
| 58 | void *stack_addrs[20] = {NULL}; |
---|
| 59 | size_t stack_depth = backtrace(stack_addrs, 20); |
---|
| 60 | char **stack_strings = backtrace_symbols(stack_addrs, stack_depth); |
---|
| 61 | |
---|
| 62 | out << "Trace : " << std::endl; |
---|
| 63 | for (size_t i = 2, j = 0; i < stack_depth; i++) |
---|
| 64 | { |
---|
| 65 | value.assign(stack_strings[i]); |
---|
| 66 | size_t bpos = value.find ('('), epos = value.find ('+'); |
---|
| 67 | if ((bpos != string::npos) and (epos != string::npos)) |
---|
| 68 | { |
---|
| 69 | char* demangled_name = abi::__cxa_demangle(value.substr(bpos+1, epos-bpos-1).c_str(), 0, 0, &status); |
---|
| 70 | if (status == 0) out << " " << ++j << " -> " << (demangled_name) << std::endl; |
---|
| 71 | else out << " " << ++j << " -> " << value.substr(bpos+1, epos-bpos-1) << std::endl; |
---|
| 72 | free(demangled_name); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | free(stack_strings); |
---|
| 77 | #endif // __GNUC__ |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | static void SigHandler(int _sigValue) |
---|
| 81 | { |
---|
| 82 | std::cerr << "============= Erreur =============" << std::endl; |
---|
| 83 | std::cerr << "Signal " << _sigValue << " reçu !" << std::endl; |
---|
| 84 | ShowBTrace(std::cerr); |
---|
| 85 | std::cerr << "==================================" << std::endl; |
---|
| 86 | exit(EXIT_FAILURE); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | static Logger & GetConsoleLogger(void) {return (Logger::get("ConsoleLogger"));} |
---|
| 91 | |
---|
| 92 | ~ILogger(void) |
---|
| 93 | { /* Ne rien faire de plus */ } |
---|
| 94 | |
---|
| 95 | }; // class XMLIOLogger |
---|
| 96 | |
---|
| 97 | // Initialisation de la classe de Logging |
---|
[114] | 98 | static ILogger LOGGER; |
---|
[112] | 99 | |
---|
| 100 | ///////////////////////////////////////////////////////////////////// |
---|
| 101 | static unsigned int Indent = 0; |
---|
| 102 | static const char* Increm = " "; |
---|
| 103 | |
---|
| 104 | std::ostream& NIndent(std::ostream& out) |
---|
| 105 | { |
---|
| 106 | static unsigned int LineNB = 1; |
---|
| 107 | out<< LineNB++ << ". "; |
---|
| 108 | for(unsigned int i = 0;i < Indent; i++) out << Increm; |
---|
| 109 | return(out); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | std::ostream& IncIndent(std::ostream& out) { Indent++; return (NIndent(out)); } |
---|
| 113 | std::ostream& DecEndl(std::ostream& out) { Indent--; return (out); } |
---|
| 114 | ///////////////////////////////////////////////////////////////////// |
---|
| 115 | |
---|
[114] | 116 | } // namespace XMLIOSERVER |
---|
[112] | 117 | |
---|
| 118 | #define ERROR(MSG) (XMLIOSERVER::ILogger::GetConsoleLogger().error(MSG)) |
---|
| 119 | #define WARNING(MSG) (XMLIOSERVER::ILogger::GetConsoleLogger().warning(MSG)) |
---|
| 120 | #define INFO(MSG) (XMLIOSERVER::ILogger::GetConsoleLogger().information(MSG)) |
---|
| 121 | // A compléter. |
---|
| 122 | |
---|
| 123 | #endif // __XMLIO_LOGGER__ |
---|