source: XMLIO_V2/dev/dev_rv/src/XMLIO/logger.hpp @ 115

Last change on this file since 115 was 115, checked in by hozdoba, 14 years ago

Gestion du calendrier, quelques modifications en cours avant d'arriver à une version correcte.

(calendar.old² sera supprimé par la suite)

File size: 3.7 KB
Line 
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
22using Poco::Logger;
23using Poco::PatternFormatter;
24using Poco::FormattingChannel;
25using Poco::ConsoleChannel;
26using Poco::FileChannel;
27using Poco::Message;
28
29using Poco::AutoPtr;
30
31namespace 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
51         static void ShowBTrace(std::ostream& out = std::clog)
52         {
53#ifdef __GNUC__
54            int status = 0;
55            string value;
56            void *stack_addrs[20] = {NULL};
57            size_t stack_depth = backtrace(stack_addrs, 20);
58            char **stack_strings = backtrace_symbols(stack_addrs, stack_depth);
59
60            out << "Trace : " << std::endl;
61            for (size_t i = 2, j = 0; i < stack_depth; i++)
62            {
63               value.assign(stack_strings[i]);
64               size_t bpos = value.find ('('), epos = value.find ('+');
65               if ((bpos != string::npos) and (epos != string::npos))
66               {
67                  char* demangled_name = abi::__cxa_demangle(value.substr(bpos+1, epos-bpos-1).c_str(), 0, 0, &status);
68                  if (status == 0) out << "   "  << ++j << " -> "  << (demangled_name) << std::endl;
69                  else out << "   "  << ++j << " -> "  << value.substr(bpos+1, epos-bpos-1) << std::endl;
70                  free(demangled_name);
71               }
72            }
73
74            free(stack_strings);
75#endif // __GNUC__
76         }
77
78         static void SigHandler(int _sigValue)
79         {
80            std::cerr << "============= Erreur =============" << std::endl;
81            std::cerr << "Signal " << _sigValue << " reçu !" << std::endl;
82            ShowBTrace(std::cerr);
83            std::cerr << "==================================" << std::endl;
84            exit(EXIT_FAILURE);
85         }
86
87
88         static Logger & GetConsoleLogger(void) {return (Logger::get("ConsoleLogger"));}
89
90         ~ILogger(void)
91         { /* Ne rien faire de plus */ }
92
93   }; // class XMLIOLogger
94
95   // Initialisation de la classe de Logging
96   static ILogger LOGGER;
97
98   /////////////////////////////////////////////////////////////////////
99   static unsigned int Indent = 0;
100   static const char*  Increm = "   ";
101
102   std::ostream& NIndent(std::ostream& out)
103   {
104      static unsigned int LineNB = 1;
105      out<< LineNB++ << ". ";
106      for(unsigned int i = 0;i < Indent; i++) out << Increm;
107      return(out);
108   }
109
110   std::ostream& IncIndent(std::ostream& out) { Indent++; return (NIndent(out)); }
111   std::ostream& DecEndl(std::ostream& out) { Indent--; return (out); }
112   /////////////////////////////////////////////////////////////////////
113
114} // namespace XMLIOSERVER
115
116#define   ERROR(MSG)   (XMLIOSERVER::ILogger::GetConsoleLogger().error(MSG))
117#define   WARNING(MSG) (XMLIOSERVER::ILogger::GetConsoleLogger().warning(MSG))
118#define   INFO(MSG)    (XMLIOSERVER::ILogger::GetConsoleLogger().information(MSG))
119// A compléter.
120
121#endif // __XMLIO_LOGGER__
Note: See TracBrowser for help on using the repository browser.