1 /*=========================================================================
4 Module: $RCSfile: creaMessageManager.cxx,v $
6 Date: $Date: 2009/02/26 12:04:10 $
7 Version: $Revision: 1.2 $
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.
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.
17 =========================================================================*/
20 * \brief class MessageManager : Manages the messages displayed by crea (code)
22 #include "creaMessageManager.h"
27 //===========================================================================
28 MessageManager::MessageManager()
29 : mMaxMessageLength(8),
35 mMessageMap[key] = new MessageType(0,"Minimum level for all kind of messages");
36 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
39 mMessageMap[key] = new MessageType(9,"Maximum level for all kind of messages");
40 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
43 mMessageMap[key] = new MessageType(1,"Information messages");
44 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
47 mMessageMap[key] = new MessageType(1,"Warning messages");
48 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
52 mMessageLevel[key] = 9;
53 mMessageHelp[key] = "Maximum level for all kind of messages";
54 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
56 mMessageLevel[key] = 0;
57 mMessageHelp[key] = "Error messages";
58 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
60 mMessageLevel[key] = 0;
61 mMessageHelp[key] = "Warning messages";
62 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
64 mMessageLevel[key] = 0;
65 mMessageHelp[key] = "Debug messages";
66 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
68 mMessageLevel[key] = 1;
69 mMessageHelp[key] = "Information messages";
70 if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
75 //===========================================================================
78 //===========================================================================
79 MessageManager::~MessageManager()
81 // std::cout << "~MessageManager"<<std::endl;
82 MessageMapType::iterator i;
83 for (i=mMessageMap.begin(); i!=mMessageMap.end(); ++i)
88 //===========================================================================
91 //===========================================================================
92 MessageManager* MessageManager::GetInstance()
94 static MessageManager* m = 0;
95 if (!m) m = new MessageManager();
98 //===========================================================================
100 //===========================================================================
101 void MessageManager::RegisterMessageType(const std::string& key,
102 const std::string& help,
103 unsigned char default_level)
105 GetInstance()->mMessageMap[key] = new MessageType(default_level,help);
107 if (GetInstance()->mMaxMessageLength<key.length())
108 GetInstance()->mMaxMessageLength = key.length();
110 //===========================================================================
112 //===========================================================================
113 void MessageManager::SetMessageLevel(const std::string& key,
116 MessageMapType::iterator i;
117 i = GetInstance()->mMessageMap.find(key);
118 if (i!=GetInstance()->mMessageMap.end())
120 (*i).second->Level = level;
124 creaWarning("MessageManager::SetMessageLevel : message type=<"
125 <<key<<"> unregistered");
129 //===========================================================================
131 //===========================================================================
132 int MessageManager::GetMessageLevel(const std::string& key)
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;
139 int m = GetInstance()->mMessageMap["max"]->Level;
143 //===========================================================================
145 //===========================================================================
146 void MessageManager::SendMessagesToCout(bool v)
148 GetInstance()->mSendToCout = v;
150 //===========================================================================
152 //===========================================================================
153 void MessageManager::SendMessage(const std::string& key, const std::string& mess)
155 if (GetInstance()->mSendToCout)
159 GetInstance()->mMessageMap[key]->Signal(mess);
161 //===========================================================================
163 //===========================================================================
164 void MessageManager::AddMessageObserver( const std::string& key, MessageCallbackType callback )
166 GetInstance()->mMessageMap[key]->Signal.connect(callback);
168 //===========================================================================
170 //===========================================================================
171 void MessageManager::PrintInfo()
173 creaMessage("info",1,"================ Messages ================="
175 creaMessage("info",1, "Kind");
177 k<(int)(GetInstance()->mMaxMessageLength-2);
180 creaMessageCont("info",1," ");
182 creaMessageCont("info",1,"Level Nature" << creaendl);
183 MessageMapType::iterator i;
184 for (i=GetInstance()->mMessageMap.begin();
185 i!=GetInstance()->mMessageMap.end();
188 creaMessage("info",1, (*i).first);
190 k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
192 creaMessageCont("info",1," ");
194 creaMessageCont("info",1, (*i).second->Level << "\t"
195 << (*i).second->Help << creaendl);
197 creaMessage("info",1,"==========================================="
200 //===========================================================================