2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
28 /*=========================================================================
31 Module: $RCSfile: creaMessageManager.cxx,v $
33 Date: $Date: 2012/11/15 10:43:26 $
34 Version: $Revision: 1.4 $
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.
40 =========================================================================*/
43 * \brief class MessageManager : Manages the messages displayed by crea (code)
45 #include "creaMessageManager.h"
50 //===========================================================================
51 MessageManager::MessageManager()
52 : mMaxMessageLength(8),
58 mMessageMap[key] = new MessageType(0,"Minimum level for all kind of messages");
59 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
62 mMessageMap[key] = new MessageType(9,"Maximum level for all kind of messages");
63 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
66 mMessageMap[key] = new MessageType(1,"Information messages");
67 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
70 mMessageMap[key] = new MessageType(1,"Warning messages");
71 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
75 mMessageLevel[key] = 9;
76 mMessageHelp[key] = "Maximum level for all kind of messages";
77 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
79 mMessageLevel[key] = 0;
80 mMessageHelp[key] = "Error messages";
81 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
83 mMessageLevel[key] = 0;
84 mMessageHelp[key] = "Warning messages";
85 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
87 mMessageLevel[key] = 0;
88 mMessageHelp[key] = "Debug messages";
89 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
91 mMessageLevel[key] = 1;
92 mMessageHelp[key] = "Information messages";
93 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
98 //===========================================================================
101 //===========================================================================
102 MessageManager::~MessageManager()
104 // std::cout << "~MessageManager"<<std::endl;
105 MessageMapType::iterator i;
106 for (i=mMessageMap.begin(); i!=mMessageMap.end(); ++i)
111 //===========================================================================
114 //===========================================================================
115 MessageManager* MessageManager::GetInstance()
117 static MessageManager* m = 0;
118 if (!m) m = new MessageManager();
121 //===========================================================================
123 //===========================================================================
124 void MessageManager::RegisterMessageType(const std::string& key,
125 const std::string& help,
126 unsigned char default_level)
128 GetInstance()->mMessageMap[key] = new MessageType(default_level,help);
130 if (GetInstance()->mMaxMessageLength<key.length())
131 GetInstance()->mMaxMessageLength = key.length();
133 //===========================================================================
135 //===========================================================================
136 void MessageManager::SetMessageLevel(const std::string& key,
139 MessageMapType::iterator i;
140 i = GetInstance()->mMessageMap.find(key);
141 if (i!=GetInstance()->mMessageMap.end())
143 (*i).second->Level = level;
147 creaWarning("MessageManager::SetMessageLevel : message type=<"
148 <<key<<"> unregistered");
152 //===========================================================================
154 //===========================================================================
155 int MessageManager::GetMessageLevel(const std::string& key)
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;
162 int m = GetInstance()->mMessageMap["max"]->Level;
166 //===========================================================================
168 //===========================================================================
169 void MessageManager::SendMessagesToCout(bool v)
171 GetInstance()->mSendToCout = v;
173 //===========================================================================
175 //===========================================================================
176 void MessageManager::SendMessage(const std::string& key, const std::string& mess)
178 if (GetInstance()->mSendToCout)
182 GetInstance()->mMessageMap[key]->Signal(mess);
184 //===========================================================================
186 //===========================================================================
187 void MessageManager::AddMessageObserver( const std::string& key, MessageCallbackType callback )
189 GetInstance()->mMessageMap[key]->Signal.connect(callback);
191 //===========================================================================
193 //===========================================================================
194 void MessageManager::PrintInfo()
196 creaMessage("info",1,"================ Messages ================="
198 creaMessage("info",1, "Kind");
200 k<(int)(GetInstance()->mMaxMessageLength-2);
203 creaMessageCont("info",1," ");
205 creaMessageCont("info",1,"Level Nature" << creaendl);
206 MessageMapType::iterator i;
207 for (i=GetInstance()->mMessageMap.begin();
208 i!=GetInstance()->mMessageMap.end();
211 creaMessage("info",1, (*i).first);
213 k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
215 creaMessageCont("info",1," ");
217 creaMessageCont("info",1, (*i).second->Level << "\t"
218 << (*i).second->Help << creaendl);
220 creaMessage("info",1,"==========================================="
223 //===========================================================================