]> Creatis software - crea.git/blob - src/creaMessageManager.cxx
Feature #1763
[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 #
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.
14 #
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
19 #  liability. 
20 #
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 # ------------------------------------------------------------------------ */ 
24
25 /*=========================================================================
26                                                                                 
27   Program:   crea
28   Module:    $RCSfile: creaMessageManager.cxx,v $
29   Language:  C++
30   Date:      $Date: 2012/11/15 09:07:32 $
31   Version:   $Revision: 1.3 $
32                                                                                 
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.
36                                                                                 
37 =========================================================================*/
38 /**
39  * \file
40  * \brief class MessageManager : Manages the messages displayed by crea (code)
41  */
42 #include "creaMessageManager.h"
43
44 namespace crea 
45 {
46
47   //===========================================================================
48   MessageManager::MessageManager() 
49     : mMaxMessageLength(8),
50       mSendToCout(true)
51   {
52     std::string key;
53
54     key ="all";
55     mMessageMap[key] = new MessageType(0,"Minimum level for all kind of messages");
56     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
57
58     key ="max";
59     mMessageMap[key] = new MessageType(9,"Maximum level for all kind of messages");
60     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
61
62     key ="info";
63     mMessageMap[key] = new MessageType(1,"Information messages");
64     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
65
66     key ="warning";
67     mMessageMap[key] = new MessageType(1,"Warning messages");
68     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
69
70    /*
71     key ="max";
72     mMessageLevel[key] = 9;
73     mMessageHelp[key] = "Maximum level for all kind of messages";
74     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
75     key = "error";
76     mMessageLevel[key] = 0;
77     mMessageHelp[key] = "Error messages";
78     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
79     key = "warning";
80     mMessageLevel[key] = 0;
81     mMessageHelp[key] = "Warning messages";
82     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
83     key = "debug";
84     mMessageLevel[key] = 0;
85     mMessageHelp[key] = "Debug messages";
86     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
87     key = "info";
88     mMessageLevel[key] = 1;
89     mMessageHelp[key] = "Information messages";
90     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
91     */
92
93
94    }
95   //===========================================================================
96
97
98   //===========================================================================
99   MessageManager::~MessageManager() 
100   {
101     //      std::cout << "~MessageManager"<<std::endl;
102     MessageMapType::iterator i;
103     for (i=mMessageMap.begin(); i!=mMessageMap.end(); ++i)
104       {
105         delete i->second;
106       }
107   }
108   //===========================================================================
109
110
111   //===========================================================================
112   MessageManager* MessageManager::GetInstance() 
113   { 
114     static MessageManager* m = 0;
115     if (!m) m = new MessageManager();
116     return m; 
117   }
118   //===========================================================================
119
120   //===========================================================================
121   void MessageManager::RegisterMessageType(const std::string& key, 
122                                            const std::string& help,
123                                            unsigned char default_level) 
124   {
125     GetInstance()->mMessageMap[key] = new MessageType(default_level,help);
126
127     if (GetInstance()->mMaxMessageLength<key.length()) 
128       GetInstance()->mMaxMessageLength = key.length();
129   }
130   //===========================================================================
131
132   //===========================================================================
133   void MessageManager::SetMessageLevel(const std::string& key, 
134                                        unsigned char level) 
135   {
136     MessageMapType::iterator i;
137     i = GetInstance()->mMessageMap.find(key);
138     if (i!=GetInstance()->mMessageMap.end()) 
139       {
140         (*i).second->Level = level;
141       }
142     else 
143       {
144         creaWarning("MessageManager::SetMessageLevel : message type=<"
145                     <<key<<"> unregistered");
146       }
147     
148   }
149   //===========================================================================  
150   
151   //===========================================================================
152   int MessageManager::GetMessageLevel(const std::string& key) 
153   {
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;      
158     }
159     int m = GetInstance()->mMessageMap["max"]->Level;
160     if (l>m) l=m;
161     return l;
162   }
163   //===========================================================================
164
165   //===========================================================================
166   void MessageManager::SendMessagesToCout(bool v)
167   {
168     GetInstance()->mSendToCout = v;
169   }
170   //===========================================================================
171
172   //===========================================================================
173   void MessageManager::SendMessage(const std::string& key, const std::string& mess)
174   {
175     if (GetInstance()->mSendToCout)
176       {
177         std::cout << mess;
178       }
179     GetInstance()->mMessageMap[key]->Signal(mess);
180   }
181   //===========================================================================
182
183   //===========================================================================
184   void MessageManager::AddMessageObserver( const std::string& key, MessageCallbackType callback )
185   {
186     GetInstance()->mMessageMap[key]->Signal.connect(callback);
187   }
188   //===========================================================================
189  
190   //===========================================================================
191   void MessageManager::PrintInfo() 
192   {
193     creaMessage("info",1,"================ Messages =================" 
194                 << creaendl);
195     creaMessage("info",1, "Kind");
196     for (int k=0;
197          k<(int)(GetInstance()->mMaxMessageLength-2);
198          k++) 
199       {
200         creaMessageCont("info",1," "); 
201       }
202     creaMessageCont("info",1,"Level  Nature" << creaendl);
203     MessageMapType::iterator i;
204     for (i=GetInstance()->mMessageMap.begin();
205          i!=GetInstance()->mMessageMap.end();
206          ++i) 
207       {
208         creaMessage("info",1, (*i).first);
209         for (int k=0;
210              k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
211              k++) {
212           creaMessageCont("info",1," ");
213         }
214         creaMessageCont("info",1, (*i).second->Level << "\t" 
215                         << (*i).second->Help << creaendl);
216     }
217     creaMessage("info",1,"===========================================" 
218                 << creaendl);
219   }
220     //===========================================================================
221
222 }