]> Creatis software - bbtk.git/blob - kernel/src/bbtkMessageManager.cxx
bfed13a58aaedc77471dd869cd49f0011ed5af0d
[bbtk.git] / kernel / src / bbtkMessageManager.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkMessageManager.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/10/17 08:18:13 $
6   Version:   $Revision: 1.12 $
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 = "wx";
90     mMessageLevel[key] = 0;
91     mMessageHelp[key] = "Widgets related messages";
92     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
93     key = "gui";
94     mMessageLevel[key] = 0;
95     mMessageHelp[key] = "Graphical user interface related messages";
96     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
97     key = "object";
98     mMessageLevel[key] = 0;
99     mMessageHelp[key] = "object memory related messages";
100     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
101     key = "package";
102     mMessageLevel[key] = 0;
103     mMessageHelp[key] = "Packages related messages";
104     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
105     key = "connection";
106     mMessageLevel[key] = 0;
107     mMessageHelp[key] = "Connections related messages";
108     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
109     key = "modified";
110     mMessageLevel[key] = 0;
111     mMessageHelp[key] = "Modified related messages";
112     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
113    }
114
115
116
117
118   MessageManager::~MessageManager() 
119   {
120     //      std::cout << "~MessageManager"<<std::endl;
121   }
122   
123
124
125   MessageManager* MessageManager::GetInstance() 
126   { 
127     static MessageManager* m = 0;
128     if (!m) m = new MessageManager();
129     return m; 
130   }
131
132   void MessageManager::RegisterMessageType(std::string key, 
133                                                   std::string help,
134                                                   unsigned char default_level) 
135   {
136     GetInstance()->mMessageLevel[key] = default_level;
137     GetInstance()->mMessageHelp[key] = help;
138     if (GetInstance()->mMaxMessageLength<key.length()) 
139       GetInstance()->mMaxMessageLength = key.length();
140   }
141
142
143
144   void MessageManager::SetMessageLevel(std::string key, 
145                                               unsigned char level) 
146   {
147     std::map<std::string,int>::iterator i;
148     i = GetInstance()->mMessageLevel.find(key);
149     if (i!=GetInstance()->mMessageLevel.end()) 
150       {
151         (*i).second = level;
152       }
153     else 
154       {
155         bbtkWarning("MessageManager::SetMessageLevel : message type=<"
156                     <<key<<"> unregistered");
157       }
158     
159   }
160   
161   
162   
163   int MessageManager::GetMessageLevel(std::string key) 
164   {
165     int l = GetInstance()->mMessageLevel["all"];
166     std::map<std::string,int>::iterator i = 
167       GetInstance()->mMessageLevel.find(key);
168     if (i!=GetInstance()->mMessageLevel.end()) {
169       if ( (*i).second > l ) l = (*i).second;      
170     }
171     int m = GetInstance()->mMessageLevel["max"];
172     if (l>m) l=m;
173     return l;
174   }
175
176
177
178   void MessageManager::PrintInfo() 
179   {
180     bbtkMessage("Help",1,"================ Messages =================" 
181                 << bbtkendl);
182     bbtkMessage("Help",1, "Kind");
183     for (int k=0;
184          k<(int)(GetInstance()->mMaxMessageLength-2);
185          k++) 
186       {
187         bbtkMessageCont("Help",1," "); 
188       }
189     bbtkMessageCont("Help",1,"Level  Nature" << bbtkendl);
190     std::map<std::string,int>::iterator i;
191     std::map<std::string,std::string>::iterator j;  
192     for (i=GetInstance()->mMessageLevel.begin(),
193            j=GetInstance()->mMessageHelp.begin();
194          i!=GetInstance()->mMessageLevel.end();++i,++j) {
195       bbtkMessage("Help",1, (*i).first);
196       for (int k=0;
197            k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
198            k++) {
199         bbtkMessageCont("Help",1," ");
200       }
201       bbtkMessageCont("Help",1, (*i).second << "\t" 
202                       << (*j).second << bbtkendl);
203     }
204     bbtkMessage("Help",1,"===========================================" 
205                 << bbtkendl);
206   }
207   
208
209 }