]> Creatis software - bbtk.git/blob - kernel/src/bbtkMessageManager.cxx
=== MAJOR RELEASE ====
[bbtk.git] / kernel / src / bbtkMessageManager.cxx
1    /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkMessageManager.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/04/18 12:59:15 $
7   Version:   $Revision: 1.5 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18 /**
19  * \file
20  * \brief class MessageManager : Manages the messages displayed by bbtk (code)
21  */
22 #include "bbtkMessageManager.h"
23
24 namespace bbtk 
25 {
26
27   MessageManager::MessageManager() 
28     : mMaxMessageLength(8), mAllLevel(0) 
29     
30   {
31     std::string key;
32     key ="Kernel";
33     mMessageLevel[key] = 0;
34     mMessageHelp[key] = "Messages generated by the core classes of the lib";
35     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
36     key ="Process";
37     mMessageLevel[key] = 0;
38     mMessageHelp[key] = "Messages related to box processing";
39     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
40     key = "Help";
41     mMessageLevel[key] = 1;
42     mMessageHelp[key] = "Help messages";
43     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
44     key = "Error";
45     mMessageLevel[key] = 0;
46     mMessageHelp[key] = "Error messages";
47     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
48     key = "Warning";
49     mMessageLevel[key] = 1;
50     mMessageHelp[key] = "Warning messages";
51     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
52     key = "Output";
53     mMessageLevel[key] = 1;
54     mMessageHelp[key] = "Output messages";
55     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
56     key = "Debug";
57     mMessageLevel[key] = 0;
58     mMessageHelp[key] = "Debug messages";
59     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
60     key = "Config";
61     mMessageLevel[key] = 0;
62     mMessageHelp[key] = "Configuration related messages";
63     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
64     key = "data";
65     mMessageLevel[key] = 0;
66     mMessageHelp[key] = "Data related messages";
67     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
68     key = "Wx";
69     mMessageLevel[key] = 0;
70     mMessageHelp[key] = "Widgets related messages";
71     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
72     key = "gui";
73     mMessageLevel[key] = 0;
74     mMessageHelp[key] = "Graphical user interface related messages";
75     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
76     key = "object";
77     mMessageLevel[key] = 0;
78     mMessageHelp[key] = "object memory related messages";
79     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
80     key = "package";
81     mMessageLevel[key] = 0;
82     mMessageHelp[key] = "Packages related messages";
83     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
84    }
85
86
87
88
89   MessageManager::~MessageManager() 
90   {
91     //      std::cout << "~MessageManager"<<std::endl;
92   }
93   
94
95
96   MessageManager* MessageManager::GetInstance() 
97   { 
98     static MessageManager* m = 0;
99     if (!m) m = new MessageManager();
100     return m; 
101   }
102
103   void MessageManager::RegisterMessageType(std::string key, 
104                                                   std::string help,
105                                                   unsigned char default_level) 
106   {
107     GetInstance()->mMessageLevel[key] = default_level;
108     GetInstance()->mMessageHelp[key] = help;
109     if (GetInstance()->mMaxMessageLength<key.length()) 
110       GetInstance()->mMaxMessageLength = key.length();
111   }
112
113
114
115   void MessageManager::SetMessageLevel(std::string key, 
116                                               unsigned char level) 
117   {
118     std::map<std::string,int>::iterator i;
119     if (key==std::string("All")) {
120       GetInstance()->mAllLevel = level;
121       
122       for (i=GetInstance()->mMessageLevel.begin();
123            i!=GetInstance()->mMessageLevel.end();
124            ++i) 
125         (*i).second = level;
126       
127     }
128     else {
129       i = GetInstance()->mMessageLevel.find(key);
130       if (i!=GetInstance()->mMessageLevel.end()) {
131         (*i).second = level;
132       }
133       else {
134         bbtkWarning("MessageManager::SetMessageLevel : message type=<"
135                     <<key<<"> unregistered");
136       }
137     }
138   }
139
140
141
142   int MessageManager::GetMessageLevel(std::string key) 
143   {
144     int l = GetInstance()->mAllLevel;
145     std::map<std::string,int>::iterator i = 
146       GetInstance()->mMessageLevel.find(key);
147     if (i!=GetInstance()->mMessageLevel.end()) {
148       if ( (*i).second > l ) l = (*i).second;      
149     }
150     return l;
151   }
152
153
154
155   void MessageManager::PrintInfo() 
156   {
157     bbtkMessage("Help",1,"================ Messages =================" 
158                 << bbtkendl);
159     bbtkMessage("Help",1, "Kind");
160     for (int k=0;
161          k<(int)(GetInstance()->mMaxMessageLength-8);
162          k++) {
163       bbtkMessageCont("Help",1," "); 
164     }
165     bbtkMessageCont("Help",1,"Level  Nature" << bbtkendl);
166     std::map<std::string,int>::iterator i;
167     std::map<std::string,std::string>::iterator j;  
168     for (i=GetInstance()->mMessageLevel.begin(),
169            j=GetInstance()->mMessageHelp.begin();
170          i!=GetInstance()->mMessageLevel.end();++i,++j) {
171       bbtkMessage("Help",1, (*i).first);
172       for (int k=0;
173            k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
174            k++) {
175         bbtkMessageCont("Help",1," ");
176       }
177       bbtkMessageCont("Help",1, (*i).second << "\t" 
178                       << (*j).second << bbtkendl);
179     }
180     bbtkMessage("Help",1,"===========================================" 
181                 << bbtkendl);
182   }
183   
184
185 }