]> Creatis software - bbtk.git/blob - kernel/src/bbtkMessageManager.cxx
#3202 BBTK Feature New Normal - fast algorithm for ImageBoundaries box
[bbtk.git] / kernel / src / bbtkMessageManager.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   Program:   bbtk
30   Module:    $RCSfile: bbtkMessageManager.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.17 $
34 =========================================================================*/
35
36
37
38 /**
39  * \file
40  * \brief class MessageManager : Manages the messages displayed by bbtk (code)
41  */
42 #include "bbtkMessageManager.h"
43
44 namespace bbtk 
45 {
46
47   MessageManager::MessageManager() 
48     : mMaxMessageLength(8)//, mAllLevel(0), mMaxLevel(9)
49     
50   {
51     std::string key;
52     key ="all";
53     mMessageLevel[key] = 0;
54     mMessageHelp[key] = "Minimum level for all kind of messages";
55     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
56     key ="max";
57     mMessageLevel[key] = 9;
58     mMessageHelp[key] = "Maximum level for all kind of messages";
59     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
60     key ="kernel";
61     mMessageLevel[key] = 0;
62     mMessageHelp[key] = "Messages generated by the core classes of the lib";
63     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
64     key ="process";
65     mMessageLevel[key] = 0;
66     mMessageHelp[key] = "Messages related to box processing";
67     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
68     key = "help";
69     mMessageLevel[key] = 1;
70     mMessageHelp[key] = "Help messages";
71     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
72     key = "error";
73     mMessageLevel[key] = 0;
74     mMessageHelp[key] = "Error messages";
75     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
76     key = "warning";
77     mMessageLevel[key] = 1;
78     mMessageHelp[key] = "Warning messages";
79     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
80     key = "output";
81     mMessageLevel[key] = 1;
82     mMessageHelp[key] = "Output messages";
83     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
84     key = "debug";
85     mMessageLevel[key] = 0;
86     mMessageHelp[key] = "Debug messages";
87     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
88     key = "config";
89     mMessageLevel[key] = 0;
90     mMessageHelp[key] = "Configuration related messages";
91     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
92     key = "data";
93     mMessageLevel[key] = 0;
94     mMessageHelp[key] = "Data related messages";
95     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
96     key = "widget";
97     mMessageLevel[key] = 0;
98     mMessageHelp[key] = "Widgets related messages";
99     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
100     key = "wx";
101     mMessageLevel[key] = 0;
102     mMessageHelp[key] = "wxWidgets related messages";
103     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
104     key = "qt";
105     mMessageLevel[key] = 0;
106     mMessageHelp[key] = "Qt related messages";
107     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
108     key = "gui";
109     mMessageLevel[key] = 0;
110     mMessageHelp[key] = "Graphical user interface related messages";
111     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
112     key = "object";
113     mMessageLevel[key] = 0;
114     mMessageHelp[key] = "object memory related messages";
115     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
116     key = "package";
117     mMessageLevel[key] = 0;
118     mMessageHelp[key] = "Packages related messages";
119     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
120     key = "connection";
121     mMessageLevel[key] = 0;
122     mMessageHelp[key] = "Connections related messages";
123     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
124     key = "change";
125     mMessageLevel[key] = 0;
126     mMessageHelp[key] = "Box i/o changes related messages";
127     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
128     key = "code";
129     mMessageLevel[key] = 0;
130     mMessageHelp[key] = "If positive then codes of the messages are displayed";
131     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
132     }
133
134
135
136
137   MessageManager::~MessageManager() 
138   {
139     //      std::cout << "~MessageManager"<<std::endl;
140   }
141   
142
143
144   MessageManager* MessageManager::GetInstance() 
145   { 
146     static MessageManager* m = 0;
147     if (!m) m = new MessageManager();
148     return m; 
149   }
150
151   bool MessageManager::RegisterMessageType(std::string key, 
152                                                   std::string help,
153                                                   unsigned char default_level) 
154   {
155     GetInstance()->mMessageLevel[key] = default_level;
156     GetInstance()->mMessageHelp[key] = help;
157     if (GetInstance()->mMaxMessageLength<key.length()) 
158       GetInstance()->mMaxMessageLength = key.length();
159     return true;
160   }
161   
162
163
164
165   void MessageManager::SetMessageLevel(std::string key, 
166                                               unsigned char level) 
167   {
168     std::map<std::string,int>::iterator i;
169     i = GetInstance()->mMessageLevel.find(key);
170     if (i!=GetInstance()->mMessageLevel.end()) 
171       {
172         (*i).second = level;
173       }
174     else 
175       {
176         bbtkWarning("MessageManager::SetMessageLevel : message type=<"
177                     <<key<<"> unregistered");
178       }
179     
180   }
181   
182   
183   
184   int MessageManager::GetMessageLevel(std::string key) 
185   {
186     int l = GetInstance()->mMessageLevel["all"];
187     std::map<std::string,int>::iterator i = 
188       GetInstance()->mMessageLevel.find(key);
189     if (i!=GetInstance()->mMessageLevel.end()) {
190       if ( (*i).second > l ) l = (*i).second;      
191     }
192     int m = GetInstance()->mMessageLevel["max"];
193     if (l>m) l=m;
194     return l;
195   }
196
197
198
199   void MessageManager::PrintInfo() 
200   {
201     bbtkMessage("help",1,"================ Messages =================" 
202                 << bbtkendl);
203     bbtkMessage("help",1, "Kind");
204     for (int k=0;
205          k<(int)(GetInstance()->mMaxMessageLength-2);
206          k++) 
207       {
208         bbtkMessageCont("help",1," "); 
209       }
210     bbtkMessageCont("help",1,"Level  Nature" << bbtkendl);
211     std::map<std::string,int>::iterator i;
212     std::map<std::string,std::string>::iterator j;  
213     for (i=GetInstance()->mMessageLevel.begin(),
214            j=GetInstance()->mMessageHelp.begin();
215          i!=GetInstance()->mMessageLevel.end();++i,++j) {
216       bbtkMessage("help",1, (*i).first);
217       for (int k=0;
218            k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
219            k++) {
220         bbtkMessageCont("help",1," ");
221       }
222       bbtkMessageCont("help",1, (*i).second << "\t" 
223                       << (*j).second << bbtkendl);
224     }
225     bbtkMessage("help",1,"===========================================" 
226                 << bbtkendl);
227   }
228   
229   std::string MessageManager::FormatKey(const std::string& key,
230                                         int value)
231   {
232     std::string s;
233     if ( GetInstance()->mMessageLevel["code"] != 0 )
234       {
235         s = key.substr(0,4);
236         std::stringstream ss;
237         ss << value;
238         //      s += " ";
239         s += ss.str();
240         s += "> ";
241       }
242     return s;
243   }
244 }