]> Creatis software - crea.git/blob - src/creaMessageManager.cxx
*** empty log message ***
[crea.git] / src / creaMessageManager.cxx
1    /*=========================================================================
2                                                                                 
3   Program:   crea
4   Module:    $RCSfile: creaMessageManager.cxx,v $
5   Language:  C++
6   Date:      $Date: 2009/02/26 12:04:10 $
7   Version:   $Revision: 1.2 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 /**
19  * \file
20  * \brief class MessageManager : Manages the messages displayed by crea (code)
21  */
22 #include "creaMessageManager.h"
23
24 namespace crea 
25 {
26
27   //===========================================================================
28   MessageManager::MessageManager() 
29     : mMaxMessageLength(8),
30       mSendToCout(true)
31   {
32     std::string key;
33
34     key ="all";
35     mMessageMap[key] = new MessageType(0,"Minimum level for all kind of messages");
36     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
37
38     key ="max";
39     mMessageMap[key] = new MessageType(9,"Maximum level for all kind of messages");
40     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
41
42     key ="info";
43     mMessageMap[key] = new MessageType(1,"Information messages");
44     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
45
46     key ="warning";
47     mMessageMap[key] = new MessageType(1,"Warning messages");
48     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
49
50    /*
51     key ="max";
52     mMessageLevel[key] = 9;
53     mMessageHelp[key] = "Maximum level for all kind of messages";
54     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
55     key = "error";
56     mMessageLevel[key] = 0;
57     mMessageHelp[key] = "Error messages";
58     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
59     key = "warning";
60     mMessageLevel[key] = 0;
61     mMessageHelp[key] = "Warning messages";
62     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
63     key = "debug";
64     mMessageLevel[key] = 0;
65     mMessageHelp[key] = "Debug messages";
66     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
67     key = "info";
68     mMessageLevel[key] = 1;
69     mMessageHelp[key] = "Information messages";
70     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
71     */
72
73
74    }
75   //===========================================================================
76
77
78   //===========================================================================
79   MessageManager::~MessageManager() 
80   {
81     //      std::cout << "~MessageManager"<<std::endl;
82     MessageMapType::iterator i;
83     for (i=mMessageMap.begin(); i!=mMessageMap.end(); ++i)
84       {
85         delete i->second;
86       }
87   }
88   //===========================================================================
89
90
91   //===========================================================================
92   MessageManager* MessageManager::GetInstance() 
93   { 
94     static MessageManager* m = 0;
95     if (!m) m = new MessageManager();
96     return m; 
97   }
98   //===========================================================================
99
100   //===========================================================================
101   void MessageManager::RegisterMessageType(const std::string& key, 
102                                            const std::string& help,
103                                            unsigned char default_level) 
104   {
105     GetInstance()->mMessageMap[key] = new MessageType(default_level,help);
106
107     if (GetInstance()->mMaxMessageLength<key.length()) 
108       GetInstance()->mMaxMessageLength = key.length();
109   }
110   //===========================================================================
111
112   //===========================================================================
113   void MessageManager::SetMessageLevel(const std::string& key, 
114                                        unsigned char level) 
115   {
116     MessageMapType::iterator i;
117     i = GetInstance()->mMessageMap.find(key);
118     if (i!=GetInstance()->mMessageMap.end()) 
119       {
120         (*i).second->Level = level;
121       }
122     else 
123       {
124         creaWarning("MessageManager::SetMessageLevel : message type=<"
125                     <<key<<"> unregistered");
126       }
127     
128   }
129   //===========================================================================  
130   
131   //===========================================================================
132   int MessageManager::GetMessageLevel(const std::string& key) 
133   {
134     int l = GetInstance()->mMessageMap["all"]->Level;
135     MessageMapType::iterator i = GetInstance()->mMessageMap.find(key);
136     if (i!=GetInstance()->mMessageMap.end()) {
137       if ( (*i).second->Level > l ) l = (*i).second->Level;      
138     }
139     int m = GetInstance()->mMessageMap["max"]->Level;
140     if (l>m) l=m;
141     return l;
142   }
143   //===========================================================================
144
145   //===========================================================================
146   void MessageManager::SendMessagesToCout(bool v)
147   {
148     GetInstance()->mSendToCout = v;
149   }
150   //===========================================================================
151
152   //===========================================================================
153   void MessageManager::SendMessage(const std::string& key, const std::string& mess)
154   {
155     if (GetInstance()->mSendToCout)
156       {
157         std::cout << mess;
158       }
159     GetInstance()->mMessageMap[key]->Signal(mess);
160   }
161   //===========================================================================
162
163   //===========================================================================
164   void MessageManager::AddMessageObserver( const std::string& key, MessageCallbackType callback )
165   {
166     GetInstance()->mMessageMap[key]->Signal.connect(callback);
167   }
168   //===========================================================================
169  
170   //===========================================================================
171   void MessageManager::PrintInfo() 
172   {
173     creaMessage("info",1,"================ Messages =================" 
174                 << creaendl);
175     creaMessage("info",1, "Kind");
176     for (int k=0;
177          k<(int)(GetInstance()->mMaxMessageLength-2);
178          k++) 
179       {
180         creaMessageCont("info",1," "); 
181       }
182     creaMessageCont("info",1,"Level  Nature" << creaendl);
183     MessageMapType::iterator i;
184     for (i=GetInstance()->mMessageMap.begin();
185          i!=GetInstance()->mMessageMap.end();
186          ++i) 
187       {
188         creaMessage("info",1, (*i).first);
189         for (int k=0;
190              k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
191              k++) {
192           creaMessageCont("info",1," ");
193         }
194         creaMessageCont("info",1, (*i).second->Level << "\t" 
195                         << (*i).second->Help << creaendl);
196     }
197     creaMessage("info",1,"===========================================" 
198                 << creaendl);
199   }
200     //===========================================================================
201
202 }