]> Creatis software - bbtk.git/blob - kernel/src/bbtkMessageManager.cxx
737548d41c17e21bbf5060b261fd0f8698871ddb
[bbtk.git] / kernel / src / bbtkMessageManager.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkMessageManager.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/05/28 08:12:06 $
6   Version:   $Revision: 1.16 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  * \file
33  * \brief class MessageManager : Manages the messages displayed by bbtk (code)
34  */
35 #include "bbtkMessageManager.h"
36
37 namespace bbtk 
38 {
39
40   MessageManager::MessageManager() 
41     : mMaxMessageLength(8)//, mAllLevel(0), mMaxLevel(9)
42     
43   {
44     std::string key;
45     key ="all";
46     mMessageLevel[key] = 0;
47     mMessageHelp[key] = "Minimum level for all kind of messages";
48     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
49     key ="max";
50     mMessageLevel[key] = 9;
51     mMessageHelp[key] = "Maximum level for all kind of messages";
52     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
53     key ="kernel";
54     mMessageLevel[key] = 0;
55     mMessageHelp[key] = "Messages generated by the core classes of the lib";
56     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
57     key ="process";
58     mMessageLevel[key] = 0;
59     mMessageHelp[key] = "Messages related to box processing";
60     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
61     key = "help";
62     mMessageLevel[key] = 1;
63     mMessageHelp[key] = "Help messages";
64     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
65     key = "error";
66     mMessageLevel[key] = 0;
67     mMessageHelp[key] = "Error messages";
68     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
69     key = "warning";
70     mMessageLevel[key] = 1;
71     mMessageHelp[key] = "Warning messages";
72     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
73     key = "output";
74     mMessageLevel[key] = 1;
75     mMessageHelp[key] = "Output messages";
76     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
77     key = "debug";
78     mMessageLevel[key] = 0;
79     mMessageHelp[key] = "Debug messages";
80     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
81     key = "config";
82     mMessageLevel[key] = 0;
83     mMessageHelp[key] = "Configuration related messages";
84     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
85     key = "data";
86     mMessageLevel[key] = 0;
87     mMessageHelp[key] = "Data related messages";
88     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
89     key = "widget";
90     mMessageLevel[key] = 0;
91     mMessageHelp[key] = "Widgets related messages";
92     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
93     key = "wx";
94     mMessageLevel[key] = 0;
95     mMessageHelp[key] = "wxWidgets related messages";
96     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
97     key = "qt";
98     mMessageLevel[key] = 0;
99     mMessageHelp[key] = "Qt related messages";
100     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
101     key = "gui";
102     mMessageLevel[key] = 0;
103     mMessageHelp[key] = "Graphical user interface related messages";
104     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
105     key = "object";
106     mMessageLevel[key] = 0;
107     mMessageHelp[key] = "object memory related messages";
108     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
109     key = "package";
110     mMessageLevel[key] = 0;
111     mMessageHelp[key] = "Packages related messages";
112     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
113     key = "connection";
114     mMessageLevel[key] = 0;
115     mMessageHelp[key] = "Connections related messages";
116     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
117     key = "change";
118     mMessageLevel[key] = 0;
119     mMessageHelp[key] = "Box i/o changes related messages";
120     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
121     key = "code";
122     mMessageLevel[key] = 0;
123     mMessageHelp[key] = "If positive then codes of the messages are displayed";
124     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
125     }
126
127
128
129
130   MessageManager::~MessageManager() 
131   {
132     //      std::cout << "~MessageManager"<<std::endl;
133   }
134   
135
136
137   MessageManager* MessageManager::GetInstance() 
138   { 
139     static MessageManager* m = 0;
140     if (!m) m = new MessageManager();
141     return m; 
142   }
143
144   bool MessageManager::RegisterMessageType(std::string key, 
145                                                   std::string help,
146                                                   unsigned char default_level) 
147   {
148     GetInstance()->mMessageLevel[key] = default_level;
149     GetInstance()->mMessageHelp[key] = help;
150     if (GetInstance()->mMaxMessageLength<key.length()) 
151       GetInstance()->mMaxMessageLength = key.length();
152     return true;
153   }
154   
155
156
157
158   void MessageManager::SetMessageLevel(std::string key, 
159                                               unsigned char level) 
160   {
161     std::map<std::string,int>::iterator i;
162     i = GetInstance()->mMessageLevel.find(key);
163     if (i!=GetInstance()->mMessageLevel.end()) 
164       {
165         (*i).second = level;
166       }
167     else 
168       {
169         bbtkWarning("MessageManager::SetMessageLevel : message type=<"
170                     <<key<<"> unregistered");
171       }
172     
173   }
174   
175   
176   
177   int MessageManager::GetMessageLevel(std::string key) 
178   {
179     int l = GetInstance()->mMessageLevel["all"];
180     std::map<std::string,int>::iterator i = 
181       GetInstance()->mMessageLevel.find(key);
182     if (i!=GetInstance()->mMessageLevel.end()) {
183       if ( (*i).second > l ) l = (*i).second;      
184     }
185     int m = GetInstance()->mMessageLevel["max"];
186     if (l>m) l=m;
187     return l;
188   }
189
190
191
192   void MessageManager::PrintInfo() 
193   {
194     bbtkMessage("help",1,"================ Messages =================" 
195                 << bbtkendl);
196     bbtkMessage("help",1, "Kind");
197     for (int k=0;
198          k<(int)(GetInstance()->mMaxMessageLength-2);
199          k++) 
200       {
201         bbtkMessageCont("help",1," "); 
202       }
203     bbtkMessageCont("help",1,"Level  Nature" << bbtkendl);
204     std::map<std::string,int>::iterator i;
205     std::map<std::string,std::string>::iterator j;  
206     for (i=GetInstance()->mMessageLevel.begin(),
207            j=GetInstance()->mMessageHelp.begin();
208          i!=GetInstance()->mMessageLevel.end();++i,++j) {
209       bbtkMessage("help",1, (*i).first);
210       for (int k=0;
211            k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
212            k++) {
213         bbtkMessageCont("help",1," ");
214       }
215       bbtkMessageCont("help",1, (*i).second << "\t" 
216                       << (*j).second << bbtkendl);
217     }
218     bbtkMessage("help",1,"===========================================" 
219                 << bbtkendl);
220   }
221   
222   std::string MessageManager::FormatKey(const std::string& key,
223                                         int value)
224   {
225     std::string s;
226     if ( GetInstance()->mMessageLevel["code"] != 0 )
227       {
228         s = key.substr(0,4);
229         std::stringstream ss;
230         ss << value;
231         //      s += " ";
232         s += ss.str();
233         s += "> ";
234       }
235     return s;
236   }
237 }