-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathappenderattachableimpl.cpp
170 lines (150 loc) · 4.28 KB
/
appenderattachableimpl.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <log4cxx/logstring.h>
#include <log4cxx/helpers/appenderattachableimpl.h>
#include <log4cxx/appender.h>
#include <log4cxx/spi/loggingevent.h>
#include <algorithm>
#include <log4cxx/helpers/pool.h>
#include <mutex>
using namespace LOG4CXX_NS;
using namespace LOG4CXX_NS::helpers;
using namespace LOG4CXX_NS::spi;
IMPLEMENT_LOG4CXX_OBJECT(AppenderAttachableImpl)
struct AppenderAttachableImpl::priv_data
{
/** Array of appenders. */
AppenderList appenderList;
mutable std::mutex m_mutex;
};
AppenderAttachableImpl::AppenderAttachableImpl(Pool& pool)
: m_priv()
{
}
AppenderAttachableImpl::~AppenderAttachableImpl()
{
}
void AppenderAttachableImpl::addAppender(const AppenderPtr newAppender)
{
// Null values for newAppender parameter are strictly forbidden.
if (!newAppender)
{
return;
}
if (!m_priv)
m_priv = std::make_unique<AppenderAttachableImpl::priv_data>();
std::lock_guard<std::mutex> lock( m_priv->m_mutex );
AppenderList::iterator it = std::find(
m_priv->appenderList.begin(), m_priv->appenderList.end(), newAppender);
if (it == m_priv->appenderList.end())
{
m_priv->appenderList.push_back(newAppender);
}
}
int AppenderAttachableImpl::appendLoopOnAppenders(
const spi::LoggingEventPtr& event,
Pool& p)
{
int numberAppended = 0;
if (m_priv)
{
// FallbackErrorHandler::error() may modify our list of appenders
// while we are iterating over them (if it holds the same logger).
// So, make a local copy of the appenders that we want to iterate over
// before actually iterating over them.
AppenderList allAppenders = getAllAppenders();
for (auto appender : allAppenders)
{
appender->doAppend(event, p);
numberAppended++;
}
}
return numberAppended;
}
AppenderList AppenderAttachableImpl::getAllAppenders() const
{
AppenderList result;
if (m_priv)
{
std::lock_guard<std::mutex> lock( m_priv->m_mutex );
result = m_priv->appenderList;
}
return result;
}
AppenderPtr AppenderAttachableImpl::getAppender(const LogString& name) const
{
AppenderPtr result;
if (m_priv && !name.empty())
{
std::lock_guard<std::mutex> lock( m_priv->m_mutex );
for (auto appender : m_priv->appenderList)
{
if (name == appender->getName())
{
result = appender;
break;
}
}
}
return result;
}
bool AppenderAttachableImpl::isAttached(const AppenderPtr appender) const
{
bool result = false;
if (m_priv && appender)
{
std::lock_guard<std::mutex> lock( m_priv->m_mutex );
result = std::find(m_priv->appenderList.begin(), m_priv->appenderList.end(), appender) != m_priv->appenderList.end();
}
return result;
}
void AppenderAttachableImpl::removeAllAppenders()
{
if (m_priv)
{
for (auto a : getAllAppenders())
a->close();
std::lock_guard<std::mutex> lock( m_priv->m_mutex );
m_priv->appenderList.clear();
}
}
void AppenderAttachableImpl::removeAppender(const AppenderPtr appender)
{
if (m_priv && appender)
{
std::lock_guard<std::mutex> lock( m_priv->m_mutex );
auto it = std::find(m_priv->appenderList.begin(), m_priv->appenderList.end(), appender);
if (it != m_priv->appenderList.end())
{
m_priv->appenderList.erase(it);
}
}
}
void AppenderAttachableImpl::removeAppender(const LogString& name)
{
if (m_priv && !name.empty())
{
std::lock_guard<std::mutex> lock( m_priv->m_mutex );
auto it = std::find_if(m_priv->appenderList.begin(), m_priv->appenderList.end()
, [&name](const AppenderPtr& appender) -> bool
{
return name == appender->getName();
});
if (it != m_priv->appenderList.end())
m_priv->appenderList.erase(it);
}
}