2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
8 # This software is governed by the CeCILL-B license under French law and
9 # abiding by the rules of distribution of free software. You can use,
10 # modify and/ or redistribute the software under the terms of the CeCILL-B
11 # license as circulated by CEA, CNRS and INRIA at the following URL
12 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
13 # or in the file LICENSE.txt.
15 # As a counterpart to the access to the source code and rights to copy,
16 # modify and redistribute granted by the license, users are provided only
17 # with a limited warranty and the software's author, the holder of the
18 # economic rights, and the successive licensors have only limited
21 # The fact that you are presently reading this means that you have had
22 # knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------ */
25 /*=========================================================================
28 Module: $RCSfile: creaMessageManager.cxx,v $
30 Date: $Date: 2012/11/15 09:07:32 $
31 Version: $Revision: 1.3 $
33 This software is distributed WITHOUT ANY WARRANTY; without even
34 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
35 PURPOSE. See the above copyright notices for more information.
37 =========================================================================*/
40 * \brief class MessageManager : Manages the messages displayed by crea (code)
42 #include "creaMessageManager.h"
47 //===========================================================================
48 MessageManager::MessageManager()
49 : mMaxMessageLength(8),
55 mMessageMap[key] = new MessageType(0,"Minimum level for all kind of messages");
56 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
59 mMessageMap[key] = new MessageType(9,"Maximum level for all kind of messages");
60 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
63 mMessageMap[key] = new MessageType(1,"Information messages");
64 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
67 mMessageMap[key] = new MessageType(1,"Warning messages");
68 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
72 mMessageLevel[key] = 9;
73 mMessageHelp[key] = "Maximum level for all kind of messages";
74 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
76 mMessageLevel[key] = 0;
77 mMessageHelp[key] = "Error messages";
78 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
80 mMessageLevel[key] = 0;
81 mMessageHelp[key] = "Warning messages";
82 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
84 mMessageLevel[key] = 0;
85 mMessageHelp[key] = "Debug messages";
86 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
88 mMessageLevel[key] = 1;
89 mMessageHelp[key] = "Information messages";
90 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
95 //===========================================================================
98 //===========================================================================
99 MessageManager::~MessageManager()
101 // std::cout << "~MessageManager"<<std::endl;
102 MessageMapType::iterator i;
103 for (i=mMessageMap.begin(); i!=mMessageMap.end(); ++i)
108 //===========================================================================
111 //===========================================================================
112 MessageManager* MessageManager::GetInstance()
114 static MessageManager* m = 0;
115 if (!m) m = new MessageManager();
118 //===========================================================================
120 //===========================================================================
121 void MessageManager::RegisterMessageType(const std::string& key,
122 const std::string& help,
123 unsigned char default_level)
125 GetInstance()->mMessageMap[key] = new MessageType(default_level,help);
127 if (GetInstance()->mMaxMessageLength<key.length())
128 GetInstance()->mMaxMessageLength = key.length();
130 //===========================================================================
132 //===========================================================================
133 void MessageManager::SetMessageLevel(const std::string& key,
136 MessageMapType::iterator i;
137 i = GetInstance()->mMessageMap.find(key);
138 if (i!=GetInstance()->mMessageMap.end())
140 (*i).second->Level = level;
144 creaWarning("MessageManager::SetMessageLevel : message type=<"
145 <<key<<"> unregistered");
149 //===========================================================================
151 //===========================================================================
152 int MessageManager::GetMessageLevel(const std::string& key)
154 int l = GetInstance()->mMessageMap["all"]->Level;
155 MessageMapType::iterator i = GetInstance()->mMessageMap.find(key);
156 if (i!=GetInstance()->mMessageMap.end()) {
157 if ( (*i).second->Level > l ) l = (*i).second->Level;
159 int m = GetInstance()->mMessageMap["max"]->Level;
163 //===========================================================================
165 //===========================================================================
166 void MessageManager::SendMessagesToCout(bool v)
168 GetInstance()->mSendToCout = v;
170 //===========================================================================
172 //===========================================================================
173 void MessageManager::SendMessage(const std::string& key, const std::string& mess)
175 if (GetInstance()->mSendToCout)
179 GetInstance()->mMessageMap[key]->Signal(mess);
181 //===========================================================================
183 //===========================================================================
184 void MessageManager::AddMessageObserver( const std::string& key, MessageCallbackType callback )
186 GetInstance()->mMessageMap[key]->Signal.connect(callback);
188 //===========================================================================
190 //===========================================================================
191 void MessageManager::PrintInfo()
193 creaMessage("info",1,"================ Messages ================="
195 creaMessage("info",1, "Kind");
197 k<(int)(GetInstance()->mMaxMessageLength-2);
200 creaMessageCont("info",1," ");
202 creaMessageCont("info",1,"Level Nature" << creaendl);
203 MessageMapType::iterator i;
204 for (i=GetInstance()->mMessageMap.begin();
205 i!=GetInstance()->mMessageMap.end();
208 creaMessage("info",1, (*i).first);
210 k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
212 creaMessageCont("info",1," ");
214 creaMessageCont("info",1, (*i).second->Level << "\t"
215 << (*i).second->Help << creaendl);
217 creaMessage("info",1,"==========================================="
220 //===========================================================================