-
Notifications
You must be signed in to change notification settings - Fork 299
/
MessageDispatcher.cpp
197 lines (158 loc) · 6.96 KB
/
MessageDispatcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
/*****************************************************************************
* User of this library should read the documentation
* in the messaging.h file.
******************************************************************************/
#include <sstream>
using std::ostringstream ;
#include <iostream>
using std::endl ;
#include <vector>
using std::vector ;
#include <string>
using std::string ;
#include <algorithm>
using std::remove ;
#include <sofa/helper/logging/MessageDispatcher.h>
#include <sofa/helper/logging/MessageHandler.h>
#include <sofa/helper/logging/ConsoleMessageHandler.h>
#include <sofa/helper/logging/DefaultStyleMessageFormatter.h>
using sofa::helper::logging::DefaultStyleMessageFormatter;
#include <mutex>
using std::lock_guard ;
using std::mutex;
namespace sofa::helper::logging
{
#define PUBLIC_API_ENTRY_POINT_MUTEX lock_guard<mutex> guard(getMainInstance()->getMutex()) ;
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Threading issues...
/// a mutex is serializing the access to the message API.
/// Memory management:
/// object are passed to the message info.
/// some of them are duplicated
/// other get a weak reference
std::vector<MessageHandler*> getDefaultMessageHandlers(){
std::vector<MessageHandler*> messageHandlers;
static ConsoleMessageHandler s_consoleMessageHandler(&DefaultStyleMessageFormatter::getInstance());
messageHandlers.push_back(&s_consoleMessageHandler);
return messageHandlers;
}
class MessageDispatcherImpl
{
public:
mutex m_mutex ;
mutex& getMutex()
{
return m_mutex;
}
std::vector<MessageHandler*> m_messageHandlers = getDefaultMessageHandlers();
std::vector<MessageHandler*>& getHandlers()
{
return m_messageHandlers ;
}
int addHandler(MessageHandler* o)
{
if( std::find(m_messageHandlers.begin(), m_messageHandlers.end(), o) == m_messageHandlers.end())
{
m_messageHandlers.push_back(o) ;
return (int)(m_messageHandlers.size()-1);
}
return -1;
}
int rmHandler(MessageHandler* o)
{
m_messageHandlers.erase(remove(m_messageHandlers.begin(), m_messageHandlers.end(), o), m_messageHandlers.end());
return (int)(m_messageHandlers.size()-1);
}
void clearHandlers()
{
m_messageHandlers.clear() ;
}
void process(sofa::helper::logging::Message& m)
{
for( size_t i=0 ; i<m_messageHandlers.size() ; i++ ){
m_messageHandlers[i]->process(m) ;
}
}
};
MessageDispatcherImpl* getMainInstance(){
static MessageDispatcherImpl s_messagedispatcher;
return &s_messagedispatcher;
}
std::vector<MessageHandler*>& MessageDispatcher::getHandlers()
{
PUBLIC_API_ENTRY_POINT_MUTEX ;
return getMainInstance()->getHandlers();
}
int MessageDispatcher::addHandler(MessageHandler* o){
PUBLIC_API_ENTRY_POINT_MUTEX ;
return getMainInstance()->addHandler(o);
}
int MessageDispatcher::rmHandler(MessageHandler* o){
PUBLIC_API_ENTRY_POINT_MUTEX ;
return getMainInstance()->rmHandler(o);
}
void MessageDispatcher::clearHandlers(){
PUBLIC_API_ENTRY_POINT_MUTEX ;
getMainInstance()->clearHandlers();
}
void MessageDispatcher::process(sofa::helper::logging::Message& m){
PUBLIC_API_ENTRY_POINT_MUTEX ;
getMainInstance()->process(m);
}
MessageDispatcher::LoggerStream MessageDispatcher::log(Message::Class mclass, Message::Type type,
const ComponentInfo::SPtr& cinfo, const FileInfo::SPtr& fileInfo) {
return MessageDispatcher::LoggerStream(mclass, type, cinfo, fileInfo);
}
MessageDispatcher::LoggerStream MessageDispatcher::info(Message::Class mclass,
const ComponentInfo::SPtr& cinfo, const FileInfo::SPtr& fileInfo) {
return log(mclass, Message::Info, cinfo, fileInfo);
}
MessageDispatcher::LoggerStream MessageDispatcher::deprecated(Message::Class mclass, const ComponentInfo::SPtr& cinfo, const FileInfo::SPtr& fileInfo) {
return log(mclass, Message::Deprecated, cinfo, fileInfo);
}
MessageDispatcher::LoggerStream MessageDispatcher::warning(Message::Class mclass, const ComponentInfo::SPtr& cinfo, const FileInfo::SPtr& fileInfo) {
return log(mclass, Message::Warning, cinfo, fileInfo);
}
MessageDispatcher::LoggerStream MessageDispatcher::error(Message::Class mclass, const ComponentInfo::SPtr& cinfo, const FileInfo::SPtr& fileInfo) {
return log(mclass, Message::Error, cinfo, fileInfo);
}
MessageDispatcher::LoggerStream MessageDispatcher::fatal(Message::Class mclass, const ComponentInfo::SPtr& cinfo, const FileInfo::SPtr& fileInfo) {
return log(mclass, Message::Fatal, cinfo, fileInfo);
}
MessageDispatcher::LoggerStream MessageDispatcher::advice(Message::Class mclass, const ComponentInfo::SPtr& cinfo, const FileInfo::SPtr& fileInfo) {
return log(mclass, Message::Advice, cinfo, fileInfo);
}
MessageDispatcher::LoggerStream::LoggerStream(Message::Class mclass, Message::Type type,
const ComponentInfo::SPtr& cInfo, const FileInfo::SPtr& fileInfo)
: m_message( mclass
, type
, cInfo
, fileInfo )
{
}
MessageDispatcher::LoggerStream::~LoggerStream()
{
if ( !m_message.empty() ) MessageDispatcher::process(m_message);
}
}