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 | |
---|
37 | ILogger() |
---|
38 | { |
---|
39 | // TODO Créer une sortie fichier. |
---|
40 | AutoPtr<PatternFormatter> pf = new PatternFormatter("[%Y-%m-%d %H:%M:%S] %t"); |
---|
41 | AutoPtr<ConsoleChannel> cc = new ConsoleChannel(); |
---|
42 | AutoPtr<FormattingChannel> pFCConsole = new FormattingChannel(pf); |
---|
43 | pFCConsole->setChannel(cc); |
---|
44 | pFCConsole->open(); |
---|
45 | Logger::create("ConsoleLogger", pFCConsole, Message::PRIO_INFORMATION); |
---|
46 | |
---|
47 | signal(SIGSEGV, SigHandler); |
---|
48 | } |
---|
49 | |
---|
50 | static void ShowBTrace(std::ostream& out = std::clog) |
---|
51 | { |
---|
52 | #ifdef __GNUC__ |
---|
53 | int status = 0; |
---|
54 | string value; |
---|
55 | void *stack_addrs[20] = {NULL}; |
---|
56 | size_t stack_depth = backtrace(stack_addrs, 20); |
---|
57 | char **stack_strings = backtrace_symbols(stack_addrs, stack_depth); |
---|
58 | |
---|
59 | out << "Trace : " << std::endl; |
---|
60 | for (size_t i = 2, j = 0; i < stack_depth; i++) |
---|
61 | { |
---|
62 | value.assign(stack_strings[i]); |
---|
63 | size_t bpos = value.find ('('), epos = value.find ('+'); |
---|
64 | if ((bpos != string::npos) and (epos != string::npos)) |
---|
65 | { |
---|
66 | char* demangled_name = abi::__cxa_demangle(value.substr(bpos+1, epos-bpos-1).c_str(), 0, 0, &status); |
---|
67 | if (status == 0) out << " " << ++j << " -> " << (demangled_name) << std::endl; |
---|
68 | else out << " " << ++j << " -> " << value.substr(bpos+1, epos-bpos-1) << std::endl; |
---|
69 | free(demangled_name); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | free(stack_strings); |
---|
74 | #endif // __GNUC__ |
---|
75 | } |
---|
76 | |
---|
77 | static void SigHandler(int _sigValue) |
---|
78 | { |
---|
79 | std::cerr << "============= Erreur =============" << std::endl; |
---|
80 | std::cerr << "Signal " << _sigValue << " reçu !" << std::endl; |
---|
81 | ShowBTrace(std::cerr); |
---|
82 | std::cerr << "==================================" << std::endl; |
---|
83 | exit(EXIT_FAILURE); |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | static Logger & GetConsoleLogger(void) {return (Logger::get("ConsoleLogger"));} |
---|
88 | |
---|
89 | ~ILogger(void) |
---|
90 | { /* Ne rien faire de plus */ } |
---|
91 | |
---|
92 | }; // class XMLIOLogger |
---|
93 | |
---|
94 | // Initialisation de la classe de Logging |
---|
95 | ILogger LOGGER; |
---|
96 | |
---|
97 | ///////////////////////////////////////////////////////////////////// |
---|
98 | unsigned int Indent = 0; |
---|
99 | const char* Increm = " "; |
---|
100 | |
---|
101 | std::ostream& NIndent(std::ostream& out) |
---|
102 | { |
---|
103 | static unsigned int LineNB = 1; |
---|
104 | out<< LineNB++ << ". "; |
---|
105 | for(unsigned int i = 0;i < Indent; i++) out << Increm; |
---|
106 | return(out); |
---|
107 | } |
---|
108 | |
---|
109 | std::ostream& IncIndent(std::ostream& out) { Indent++; return (NIndent(out)); } |
---|
110 | std::ostream& DecEndl(std::ostream& out) { Indent--; return (out); } |
---|
111 | ///////////////////////////////////////////////////////////////////// |
---|
112 | |
---|
113 | } // namespace XMLIOSERVER |
---|
114 | |
---|
115 | #define ERROR(MSG) (XMLIOSERVER::ILogger::GetConsoleLogger().error(MSG)) |
---|
116 | #define WARNING(MSG) (XMLIOSERVER::ILogger::GetConsoleLogger().warning(MSG)) |
---|
117 | #define INFO(MSG) (XMLIOSERVER::ILogger::GetConsoleLogger().information(MSG)) |
---|
118 | // A compléter. |
---|
119 | |
---|
120 | #endif // __XMLIO_LOGGER__ |
---|