/src/logging-log4cxx/src/main/cpp/fallbackerrorhandler.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include <log4cxx/logstring.h> |
19 | | #include <log4cxx/appender.h> |
20 | | #include <log4cxx/logger.h> |
21 | | #include <log4cxx/varia/fallbackerrorhandler.h> |
22 | | #include <log4cxx/helpers/loglog.h> |
23 | | #include <log4cxx/helpers/stringhelper.h> |
24 | | #include <log4cxx/spi/loggingevent.h> |
25 | | #include <log4cxx/hierarchy.h> |
26 | | #include <log4cxx/logmanager.h> |
27 | | |
28 | | using namespace LOG4CXX_NS; |
29 | | using namespace LOG4CXX_NS::helpers; |
30 | | using namespace LOG4CXX_NS::spi; |
31 | | using namespace LOG4CXX_NS::varia; |
32 | | |
33 | | IMPLEMENT_LOG4CXX_OBJECT(FallbackErrorHandler) |
34 | | |
35 | | struct FallbackErrorHandler::FallbackErrorHandlerPrivate |
36 | | { |
37 | | AppenderWeakPtr backup; |
38 | | AppenderWeakPtr primary; |
39 | | std::vector<LoggerPtr> loggers; |
40 | | bool errorReported = false; |
41 | | }; |
42 | | |
43 | | FallbackErrorHandler::FallbackErrorHandler() |
44 | 0 | : m_priv(std::make_unique<FallbackErrorHandlerPrivate>()) |
45 | 0 | { |
46 | 0 | } Unexecuted instantiation: log4cxx::varia::FallbackErrorHandler::FallbackErrorHandler() Unexecuted instantiation: log4cxx::varia::FallbackErrorHandler::FallbackErrorHandler() |
47 | | |
48 | 0 | FallbackErrorHandler::~FallbackErrorHandler() {} |
49 | | |
50 | | void FallbackErrorHandler::setLogger(const LoggerPtr& logger) |
51 | 0 | { |
52 | 0 | if (LogLog::isDebugEnabled()) |
53 | 0 | { |
54 | 0 | LogLog::debug(((LogString) LOG4CXX_STR("FB: Adding logger [")) |
55 | 0 | + logger->getName() + LOG4CXX_STR("].")); |
56 | 0 | } |
57 | 0 | m_priv->loggers.push_back(logger); |
58 | 0 | } |
59 | | |
60 | | void FallbackErrorHandler::error(const LogString& message, |
61 | | const std::exception& e, |
62 | | int errorCode) const |
63 | 0 | { |
64 | 0 | error(message, e, errorCode, 0); |
65 | 0 | } |
66 | | |
67 | | void FallbackErrorHandler::error(const LogString& message, |
68 | | const std::exception& e, |
69 | | int, const spi::LoggingEventPtr&) const |
70 | 0 | { |
71 | 0 | if (LogLog::isDebugEnabled()) |
72 | 0 | { |
73 | 0 | LogLog::debug(((LogString) LOG4CXX_STR("FB: The following error reported: ")) |
74 | 0 | + message, e); |
75 | 0 | LogLog::debug(LOG4CXX_STR("FB: INITIATING FALLBACK PROCEDURE.")); |
76 | 0 | } |
77 | |
|
78 | 0 | AppenderPtr primaryLocked = m_priv->primary.lock(); |
79 | 0 | AppenderPtr backupLocked = m_priv->backup.lock(); |
80 | |
|
81 | 0 | if ( !primaryLocked || !backupLocked ) |
82 | 0 | { |
83 | 0 | return; |
84 | 0 | } |
85 | | |
86 | 0 | for (LoggerPtr l : m_priv->loggers) |
87 | 0 | { |
88 | 0 | if (LogLog::isDebugEnabled()) |
89 | 0 | { |
90 | 0 | LogLog::debug(LOG4CXX_STR("FB: Replacing [") |
91 | 0 | + primaryLocked->getName() + LOG4CXX_STR("] with [") |
92 | 0 | + backupLocked->getName() + LOG4CXX_STR("] in logger [") |
93 | 0 | + l->getName() + LOG4CXX_STR("].")); |
94 | 0 | } |
95 | 0 | if (!l->replaceAppender(primaryLocked, backupLocked)) |
96 | 0 | { |
97 | 0 | LogLog::debug(LOG4CXX_STR("FB: Failed to replace [") |
98 | 0 | + primaryLocked->getName() + LOG4CXX_STR("] with [") |
99 | 0 | + backupLocked->getName() + LOG4CXX_STR("] in logger [") |
100 | 0 | + l->getName() + LOG4CXX_STR("].")); |
101 | 0 | } |
102 | 0 | } |
103 | 0 | m_priv->errorReported = true; |
104 | 0 | } |
105 | | |
106 | | void FallbackErrorHandler::setAppender(const AppenderPtr& primary1) |
107 | 0 | { |
108 | 0 | if (LogLog::isDebugEnabled()) |
109 | 0 | { |
110 | 0 | LogLog::debug(((LogString) LOG4CXX_STR("FB: Setting primary appender to [")) |
111 | 0 | + primary1->getName() + LOG4CXX_STR("].")); |
112 | 0 | } |
113 | 0 | m_priv->primary = primary1; |
114 | 0 | } |
115 | | |
116 | | void FallbackErrorHandler::setBackupAppender(const AppenderPtr& backup1) |
117 | 0 | { |
118 | 0 | if (LogLog::isDebugEnabled()) |
119 | 0 | { |
120 | 0 | LogLog::debug(((LogString) LOG4CXX_STR("FB: Setting backup appender to [")) |
121 | 0 | + backup1->getName() + LOG4CXX_STR("].")); |
122 | 0 | } |
123 | 0 | m_priv->backup = backup1; |
124 | | |
125 | | // Make sure that we keep a reference to the appender around, since otherwise |
126 | | // the appender would be lost if it has no loggers that use it. |
127 | 0 | auto repository = LogManager::getLoggerRepository(); |
128 | 0 | if (auto hierarchy = dynamic_cast<Hierarchy*>(repository.get())) |
129 | 0 | { |
130 | 0 | hierarchy->addAppender(backup1); |
131 | 0 | } |
132 | |
|
133 | 0 | } |
134 | | |
135 | | void FallbackErrorHandler::activateOptions(Pool&) |
136 | 0 | { |
137 | 0 | } |
138 | | |
139 | | void FallbackErrorHandler::setOption(const LogString&, const LogString&) |
140 | 0 | { |
141 | 0 | } |
142 | | |
143 | | bool FallbackErrorHandler::errorReported() const |
144 | 0 | { |
145 | 0 | return m_priv->errorReported; |
146 | 0 | } |