proxygen
xlog.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include <folly/logging/xlog.h>
17 #include <folly/Synchronized.h>
18 
19 using folly::StringPiece;
20 
21 namespace folly {
22 
23 namespace {
39 StringPiece stripBuckOutPrefix(StringPiece filename) {
40  size_t idx = 0;
41  while (true) {
42  auto end = filename.find('/', idx);
43  if (end == StringPiece::npos) {
44  // We were unable to find where the buck-out prefix should end.
45  return filename;
46  }
47 
48  auto component = filename.subpiece(idx, end - idx);
49  if (component.find('#') != StringPiece::npos) {
50  return filename.subpiece(end + 1);
51  }
52  idx = end + 1;
53  }
54 }
55 } // namespace
56 
58  // Buck mangles the directory layout for header files. Rather than including
59  // them from their original location, it moves them into deep directories
60  // inside buck-out, and includes them from there.
61  //
62  // If this path looks like a buck header directory, try to strip off the
63  // buck-specific portion.
64  if (filename.startsWith("buck-out/")) {
65  filename = stripBuckOutPrefix(filename);
66  }
67 
68  return filename;
69 }
70 
71 template <bool IsInHeaderFile>
73  folly::StringPiece categoryName,
74  bool isOverridden) {
75  auto currentLevel = level_.load(std::memory_order_acquire);
76  if (UNLIKELY(currentLevel == ::folly::LogLevel::UNINITIALIZED)) {
77  return LoggerDB::get().xlogInit(
78  isOverridden ? categoryName : getXlogCategoryNameForFile(categoryName),
79  &level_,
80  nullptr);
81  }
82  return currentLevel;
83 }
84 
85 template <bool IsInHeaderFile>
87  folly::StringPiece categoryName,
88  bool isOverridden) {
90  isOverridden ? categoryName : getXlogCategoryNameForFile(categoryName),
91  &category_,
92  &isInitialized_);
93 }
94 
95 #ifdef __INCLUDE_LEVEL__
97  folly::StringPiece categoryName,
98  bool isOverridden,
99  XlogFileScopeInfo* fileScopeInfo) {
100  auto currentLevel = fileScopeInfo->level.load(std::memory_order_acquire);
101  if (UNLIKELY(currentLevel == ::folly::LogLevel::UNINITIALIZED)) {
102  return LoggerDB::get().xlogInit(
103  isOverridden ? categoryName : getXlogCategoryNameForFile(categoryName),
104  &fileScopeInfo->level,
105  &fileScopeInfo->category);
106  }
107  return currentLevel;
108 }
109 #endif
110 
111 // Explicitly instantiations of XlogLevelInfo and XlogCategoryInfo
112 // If __INCLUDE_LEVEL__ is not available only the "true" variants ever get
113 // used, because we cannot determine if we are ever in the .cpp file being
114 // compiled or not.
115 template class XlogLevelInfo<true>;
116 template class XlogCategoryInfo<true>;
117 } // namespace folly
LogCategory * init(folly::StringPiece categoryName, bool isOverridden)
Definition: xlog.cpp:86
LogCategory * xlogInitCategory(folly::StringPiece categoryName, LogCategory **xlogCategory, std::atomic< bool > *isInitialized)
Definition: LoggerDB.cpp:623
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
StringPiece getXlogCategoryNameForFile(StringPiece filename)
Definition: xlog.cpp:57
LogLevel xlogInit(folly::StringPiece categoryName, std::atomic< LogLevel > *xlogCategoryLevel, LogCategory **xlogCategory)
Definition: LoggerDB.cpp:597
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
LogLevel loadLevelFull(folly::StringPiece categoryName, bool isOverridden)
Definition: xlog.cpp:72
LogLevel
Definition: LogLevel.h:38
bool startsWith(const const_range_type &other) const
Definition: Range.h:828
static const size_type npos
Definition: Range.h:197
Range< const char * > StringPiece
#define UNLIKELY(x)
Definition: Likely.h:48
static LoggerDB & get()
Definition: LoggerDB.cpp:112