]> Creatis software - crea.git/blob - src/creaMessageManager.cxx
#3180 crea Feature New Normal Future - Set wx-config for wxWidgets 2.8
[crea.git] / src / creaMessageManager.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ 
26 */ 
27
28 /*=========================================================================
29                                                                                 
30   Program:   crea
31   Module:    $RCSfile: creaMessageManager.cxx,v $
32   Language:  C++
33   Date:      $Date: 2012/11/15 10:43:26 $
34   Version:   $Revision: 1.4 $
35                                                                                 
36      This software is distributed WITHOUT ANY WARRANTY; without even
37      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
38      PURPOSE.  See the above copyright notices for more information.
39                                                                                 
40 =========================================================================*/
41 /**
42  * \file
43  * \brief class MessageManager : Manages the messages displayed by crea (code)
44  */
45 #include "creaMessageManager.h"
46
47 namespace crea 
48 {
49
50   //===========================================================================
51   MessageManager::MessageManager() 
52     : mMaxMessageLength(8),
53       mSendToCout(true)
54   {
55     std::string key;
56
57     key ="all";
58     mMessageMap[key] = new MessageType(0,"Minimum level for all kind of messages");
59     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
60
61     key ="max";
62     mMessageMap[key] = new MessageType(9,"Maximum level for all kind of messages");
63     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
64
65     key ="info";
66     mMessageMap[key] = new MessageType(1,"Information messages");
67     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
68
69     key ="warning";
70     mMessageMap[key] = new MessageType(1,"Warning messages");
71     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
72
73    /*
74     key ="max";
75     mMessageLevel[key] = 9;
76     mMessageHelp[key] = "Maximum level for all kind of messages";
77     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
78     key = "error";
79     mMessageLevel[key] = 0;
80     mMessageHelp[key] = "Error messages";
81     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
82     key = "warning";
83     mMessageLevel[key] = 0;
84     mMessageHelp[key] = "Warning messages";
85     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
86     key = "debug";
87     mMessageLevel[key] = 0;
88     mMessageHelp[key] = "Debug messages";
89     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
90     key = "info";
91     mMessageLevel[key] = 1;
92     mMessageHelp[key] = "Information messages";
93     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
94     */
95
96
97    }
98   //===========================================================================
99
100
101   //===========================================================================
102   MessageManager::~MessageManager() 
103   {
104     //      std::cout << "~MessageManager"<<std::endl;
105     MessageMapType::iterator i;
106     for (i=mMessageMap.begin(); i!=mMessageMap.end(); ++i)
107       {
108         delete i->second;
109       }
110   }
111   //===========================================================================
112
113
114   //===========================================================================
115   MessageManager* MessageManager::GetInstance() 
116   { 
117     static MessageManager* m = 0;
118     if (!m) m = new MessageManager();
119     return m; 
120   }
121   //===========================================================================
122
123   //===========================================================================
124   void MessageManager::RegisterMessageType(const std::string& key, 
125                                            const std::string& help,
126                                            unsigned char default_level) 
127   {
128     GetInstance()->mMessageMap[key] = new MessageType(default_level,help);
129
130     if (GetInstance()->mMaxMessageLength<key.length()) 
131       GetInstance()->mMaxMessageLength = key.length();
132   }
133   //===========================================================================
134
135   //===========================================================================
136   void MessageManager::SetMessageLevel(const std::string& key, 
137                                        unsigned char level) 
138   {
139     MessageMapType::iterator i;
140     i = GetInstance()->mMessageMap.find(key);
141     if (i!=GetInstance()->mMessageMap.end()) 
142       {
143         (*i).second->Level = level;
144       }
145     else 
146       {
147         creaWarning("MessageManager::SetMessageLevel : message type=<"
148                     <<key<<"> unregistered");
149       }
150     
151   }
152   //===========================================================================  
153   
154   //===========================================================================
155   int MessageManager::GetMessageLevel(const std::string& key) 
156   {
157     int l = GetInstance()->mMessageMap["all"]->Level;
158     MessageMapType::iterator i = GetInstance()->mMessageMap.find(key);
159     if (i!=GetInstance()->mMessageMap.end()) {
160       if ( (*i).second->Level > l ) l = (*i).second->Level;      
161     }
162     int m = GetInstance()->mMessageMap["max"]->Level;
163     if (l>m) l=m;
164     return l;
165   }
166   //===========================================================================
167
168   //===========================================================================
169   void MessageManager::SendMessagesToCout(bool v)
170   {
171     GetInstance()->mSendToCout = v;
172   }
173   //===========================================================================
174
175   //===========================================================================
176   void MessageManager::SendMessage(const std::string& key, const std::string& mess)
177   {
178     if (GetInstance()->mSendToCout)
179       {
180         std::cout << mess;
181       }
182     GetInstance()->mMessageMap[key]->Signal(mess);
183   }
184   //===========================================================================
185
186   //===========================================================================
187   void MessageManager::AddMessageObserver( const std::string& key, MessageCallbackType callback )
188   {
189     GetInstance()->mMessageMap[key]->Signal.connect(callback);
190   }
191   //===========================================================================
192  
193   //===========================================================================
194   void MessageManager::PrintInfo() 
195   {
196     creaMessage("info",1,"================ Messages =================" 
197                 << creaendl);
198     creaMessage("info",1, "Kind");
199     for (int k=0;
200          k<(int)(GetInstance()->mMaxMessageLength-2);
201          k++) 
202       {
203         creaMessageCont("info",1," "); 
204       }
205     creaMessageCont("info",1,"Level  Nature" << creaendl);
206     MessageMapType::iterator i;
207     for (i=GetInstance()->mMessageMap.begin();
208          i!=GetInstance()->mMessageMap.end();
209          ++i) 
210       {
211         creaMessage("info",1, (*i).first);
212         for (int k=0;
213              k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
214              k++) {
215           creaMessageCont("info",1," ");
216         }
217         creaMessageCont("info",1, (*i).second->Level << "\t" 
218                         << (*i).second->Help << creaendl);
219     }
220     creaMessage("info",1,"===========================================" 
221                 << creaendl);
222   }
223     //===========================================================================
224
225 }