2 * Copyright 2004-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include <folly/experimental/logging/LogCategory.h>
20 #include <folly/ExceptionString.h>
21 #include <folly/experimental/logging/LogHandler.h>
22 #include <folly/experimental/logging/LogMessage.h>
23 #include <folly/experimental/logging/LogName.h>
24 #include <folly/experimental/logging/LoggerDB.h>
28 LogCategory::LogCategory(LoggerDB* db)
29 : effectiveLevel_{LogLevel::ERROR},
30 level_{static_cast<uint32_t>(LogLevel::ERROR)},
35 LogCategory::LogCategory(StringPiece name, LogCategory* parent)
36 : effectiveLevel_{parent->getEffectiveLevel()},
37 level_{static_cast<uint32_t>(LogLevel::MAX_LEVEL) | FLAG_INHERIT},
39 name_{LogName::canonicalize(name)},
41 nextSibling_{parent_->firstChild_} {
42 parent_->firstChild_ = this;
45 void LogCategory::processMessage(const LogMessage& message) const {
46 // Make a copy of any attached LogHandlers, so we can release the handlers_
47 // lock before holding them.
49 // In the common case there will only be a small number of handlers. Use a
50 // std::array in this case to avoid a heap allocation for the vector.
51 const std::shared_ptr<LogHandler>* handlers = nullptr;
52 size_t numHandlers = 0;
53 constexpr uint32_t kSmallOptimizationSize = 5;
54 std::array<std::shared_ptr<LogHandler>, kSmallOptimizationSize> handlersArray;
55 std::vector<std::shared_ptr<LogHandler>> handlersVector;
57 auto lockedHandlers = handlers_.rlock();
58 numHandlers = lockedHandlers->size();
59 if (numHandlers <= kSmallOptimizationSize) {
60 for (size_t n = 0; n < numHandlers; ++n) {
61 handlersArray[n] = (*lockedHandlers)[n];
63 handlers = handlersArray.data();
65 handlersVector = *lockedHandlers;
66 handlers = handlersVector.data();
70 for (size_t n = 0; n < numHandlers; ++n) {
72 handlers[n]->handleMessage(message, this);
73 } catch (const std::exception& ex) {
74 // Use LoggerDB::internalWarning() to report the error, but continue
75 // trying to log the message to any other handlers attached to ourself or
76 // one of our parent categories.
77 LoggerDB::internalWarning(
80 "log handler for category \"",
82 "\" threw an error: ",
83 folly::exceptionStr(ex));
87 // Propagate the message up to our parent LogCategory.
89 // Maybe in the future it might be worth adding a flag to control if a
90 // LogCategory should propagate messages to its parent or not. (This would
91 // be similar to log4j's "additivity" flag.)
92 // For now I don't have a strong use case for this.
94 parent_->processMessage(message);
98 void LogCategory::addHandler(std::shared_ptr<LogHandler> handler) {
99 auto handlers = handlers_.wlock();
100 handlers->emplace_back(std::move(handler));
103 void LogCategory::clearHandlers() {
104 std::vector<std::shared_ptr<LogHandler>> emptyHandlersList;
105 // Swap out the handlers list with the handlers_ lock held.
107 auto handlers = handlers_.wlock();
108 handlers->swap(emptyHandlersList);
110 // Destroy emptyHandlersList now that the handlers_ lock is released.
111 // This way we don't hold the handlers_ lock while invoking any of the
112 // LogHandler destructors.
115 void LogCategory::setLevel(LogLevel level, bool inherit) {
116 // We have to set the level through LoggerDB, since we require holding
117 // the LoggerDB lock to iterate through our children in case our effective
119 db_->setLevel(this, level, inherit);
122 void LogCategory::setLevelLocked(LogLevel level, bool inherit) {
123 // Clamp the value to MIN_LEVEL and MAX_LEVEL.
125 // This makes sure that UNINITIALIZED is always less than any valid level
126 // value, and that level values cannot conflict with our flag bits.
127 if (level > LogLevel::MAX_LEVEL) {
128 level = LogLevel::MAX_LEVEL;
129 } else if (level < LogLevel::MIN_LEVEL) {
130 level = LogLevel::MIN_LEVEL;
133 // Make sure the inherit flag is always off for the root logger.
137 auto newValue = static_cast<uint32_t>(level);
139 newValue |= FLAG_INHERIT;
142 // Update the stored value
143 uint32_t oldValue = level_.exchange(newValue, std::memory_order_acq_rel);
145 // Break out early if the value has not changed.
146 if (oldValue == newValue) {
150 // Update the effective log level
151 LogLevel newEffectiveLevel;
153 newEffectiveLevel = std::min(level, parent_->getEffectiveLevel());
155 newEffectiveLevel = level;
157 updateEffectiveLevel(newEffectiveLevel);
160 void LogCategory::updateEffectiveLevel(LogLevel newEffectiveLevel) {
161 auto oldEffectiveLevel =
162 effectiveLevel_.exchange(newEffectiveLevel, std::memory_order_acq_rel);
163 // Break out early if the value did not change.
164 if (newEffectiveLevel == oldEffectiveLevel) {
168 // Update all of the values in xlogLevels_
169 for (auto* levelPtr : xlogLevels_) {
170 levelPtr->store(newEffectiveLevel, std::memory_order_release);
173 // Update all children loggers
174 LogCategory* child = firstChild_;
175 while (child != nullptr) {
176 child->parentLevelUpdated(newEffectiveLevel);
177 child = child->nextSibling_;
181 void LogCategory::parentLevelUpdated(LogLevel parentEffectiveLevel) {
182 uint32_t levelValue = level_.load(std::memory_order_acquire);
183 auto inherit = (levelValue & FLAG_INHERIT);
188 auto myLevel = static_cast<LogLevel>(levelValue & ~FLAG_INHERIT);
189 auto newEffectiveLevel = std::min(myLevel, parentEffectiveLevel);
190 updateEffectiveLevel(newEffectiveLevel);
193 void LogCategory::registerXlogLevel(std::atomic<LogLevel>* levelPtr) {
194 xlogLevels_.push_back(levelPtr);