]> Creatis software - crea.git/blob - src/creaMessageManager.cxx
*** empty log message ***
[crea.git] / src / creaMessageManager.cxx
1    /*=========================================================================
2                                                                                 
3   Program:   crea
4   Module:    $RCSfile: creaMessageManager.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/09/26 14:09:55 $
7   Version:   $Revision: 1.1 $
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 crea (code)
21  */
22 #include "creaMessageManager.h"
23
24 namespace crea 
25 {
26
27   MessageManager::MessageManager() 
28     : mMaxMessageLength(8)//, mAllLevel(0), mMaxLevel(9)
29     
30   {
31     std::string key;
32     key ="all";
33     mMessageLevel[key] = 0;
34     mMessageHelp[key] = "Minimum level for all kind of messages";
35     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
36     key ="max";
37     mMessageLevel[key] = 9;
38     mMessageHelp[key] = "Maximum level for all kind of messages";
39     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
40     key = "error";
41     mMessageLevel[key] = 0;
42     mMessageHelp[key] = "Error messages";
43     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
44     key = "warning";
45     mMessageLevel[key] = 0;
46     mMessageHelp[key] = "Warning messages";
47     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
48     key = "debug";
49     mMessageLevel[key] = 0;
50     mMessageHelp[key] = "Debug messages";
51     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
52     key = "info";
53     mMessageLevel[key] = 1;
54     mMessageHelp[key] = "Information messages";
55     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
56     /*
57     key ="Kernel";
58     mMessageLevel[key] = 0;
59     mMessageHelp[key] = "Messages generated by the core classes of the lib";
60     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
61     key ="process";
62     mMessageLevel[key] = 0;
63     mMessageHelp[key] = "Messages related to box processing";
64     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
65     key = "Help";
66     mMessageLevel[key] = 1;
67     mMessageHelp[key] = "Help messages";
68     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
69     key = "Error";
70     mMessageLevel[key] = 0;
71     mMessageHelp[key] = "Error messages";
72     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
73     key = "Warning";
74     mMessageLevel[key] = 1;
75     mMessageHelp[key] = "Warning messages";
76     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
77     key = "Output";
78     mMessageLevel[key] = 1;
79     mMessageHelp[key] = "Output messages";
80     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
81     key = "debug";
82     mMessageLevel[key] = 0;
83     mMessageHelp[key] = "Debug messages";
84     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
85     key = "Config";
86     mMessageLevel[key] = 0;
87     mMessageHelp[key] = "Configuration related messages";
88     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
89     key = "data";
90     mMessageLevel[key] = 0;
91     mMessageHelp[key] = "Data related messages";
92     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
93     key = "wx";
94     mMessageLevel[key] = 0;
95     mMessageHelp[key] = "Widgets related messages";
96     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
97     key = "gui";
98     mMessageLevel[key] = 0;
99     mMessageHelp[key] = "Graphical user interface related messages";
100     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
101     key = "object";
102     mMessageLevel[key] = 0;
103     mMessageHelp[key] = "object memory related messages";
104     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
105     key = "package";
106     mMessageLevel[key] = 0;
107     mMessageHelp[key] = "Packages related messages";
108     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
109     key = "connection";
110     mMessageLevel[key] = 0;
111     mMessageHelp[key] = "Connections related messages";
112     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
113     key = "modified";
114     mMessageLevel[key] = 0;
115     mMessageHelp[key] = "Modified related messages";
116     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
117     */
118    }
119
120
121
122
123   MessageManager::~MessageManager() 
124   {
125     //      std::cout << "~MessageManager"<<std::endl;
126   }
127   
128
129
130   MessageManager* MessageManager::GetInstance() 
131   { 
132     static MessageManager* m = 0;
133     if (!m) m = new MessageManager();
134     return m; 
135   }
136
137   void MessageManager::RegisterMessageType(std::string key, 
138                                                   std::string help,
139                                                   unsigned char default_level) 
140   {
141     GetInstance()->mMessageLevel[key] = default_level;
142     GetInstance()->mMessageHelp[key] = help;
143     if (GetInstance()->mMaxMessageLength<key.length()) 
144       GetInstance()->mMaxMessageLength = key.length();
145   }
146
147
148
149   void MessageManager::SetMessageLevel(std::string key, 
150                                               unsigned char level) 
151   {
152     std::map<std::string,int>::iterator i;
153     i = GetInstance()->mMessageLevel.find(key);
154     if (i!=GetInstance()->mMessageLevel.end()) 
155       {
156         (*i).second = level;
157       }
158     else 
159       {
160         creaWarning("MessageManager::SetMessageLevel : message type=<"
161                     <<key<<"> unregistered");
162       }
163     
164   }
165   
166   
167   
168   int MessageManager::GetMessageLevel(std::string key) 
169   {
170     int l = GetInstance()->mMessageLevel["all"];
171     std::map<std::string,int>::iterator i = 
172       GetInstance()->mMessageLevel.find(key);
173     if (i!=GetInstance()->mMessageLevel.end()) {
174       if ( (*i).second > l ) l = (*i).second;      
175     }
176     int m = GetInstance()->mMessageLevel["max"];
177     if (l>m) l=m;
178     return l;
179   }
180
181
182
183   void MessageManager::PrintInfo() 
184   {
185     creaMessage("info",1,"================ Messages =================" 
186                 << creaendl);
187     creaMessage("info",1, "Kind");
188     for (int k=0;
189          k<(int)(GetInstance()->mMaxMessageLength-2);
190          k++) 
191       {
192         creaMessageCont("info",1," "); 
193       }
194     creaMessageCont("info",1,"Level  Nature" << creaendl);
195     std::map<std::string,int>::iterator i;
196     std::map<std::string,std::string>::iterator j;  
197     for (i=GetInstance()->mMessageLevel.begin(),
198            j=GetInstance()->mMessageHelp.begin();
199          i!=GetInstance()->mMessageLevel.end();++i,++j) {
200       creaMessage("info",1, (*i).first);
201       for (int k=0;
202            k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
203            k++) {
204         creaMessageCont("info",1," ");
205       }
206       creaMessageCont("info",1, (*i).second << "\t" 
207                       << (*j).second << creaendl);
208     }
209     creaMessage("info",1,"===========================================" 
210                 << creaendl);
211   }
212   
213
214 }