folly/folly/logging/xlog.cpp

/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed 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 <folly/logging/xlog.h>

#include <type_traits>
#include <unordered_map>

#include <folly/Synchronized.h>
#include <folly/portability/PThread.h>

namespace folly {

namespace detail {
size_t& xlogEveryNThreadEntry(void const* const key) {}
} // namespace detail

namespace {
#ifdef FOLLY_XLOG_SUPPORT_BUCK1
/**
 * buck copies header files from their original location in the source tree
 * and places them under buck-out/ with a path like
 * buck-out/<rule-name-components>/<original-path>
 *
 * We want to strip off the buck-out/<rule-name-components> portion,
 * so that the filename we use is just the original path in the source tree.
 *
 * The <rule-name-component> section should always end in a path component that
 * includes a '#': it's format is <rule-name>#<parameters>, where <parameters>
 * is a comma separated list that never includes '/'.
 *
 * Search for the first path component with a '#', and strip off everything up
 * to this component.
 */
StringPiece stripBuckOutPrefix(StringPiece filename) {
  size_t idx = 0;
  while (true) {
    auto end = filename.find('/', idx);
    if (end == StringPiece::npos) {
      // We were unable to find where the buck-out prefix should end.
      return filename;
    }

    auto component = filename.subpiece(idx, end - idx);
    if (component.find('#') != StringPiece::npos) {
      return filename.subpiece(end + 1);
    }
    idx = end + 1;
  }
}
#endif // FOLLY_XLOG_SUPPORT_BUCK1

#ifdef FOLLY_XLOG_SUPPORT_BUCK2
/**
 * buck2 copies header files from their original location in the source tree
 * and places them under buck-out/ with a path like
 * buck-out/v2/gen/cell/hash/dirA/dirB/__rule_name__/buck-headers/dirA/dirB/Foo.h
 *
 * We want to strip off everything up to and including the "buck-headers" or
 * "buck-private-headers" component.
 */
StringPiece stripBuckV2Prefix(StringPiece filename) {
  static constexpr StringPiece commonPrefix("/buck-");
  static constexpr StringPiece headerPrefix("headers/");
  static constexpr StringPiece privatePrefix("private-headers/");

  size_t idx = 0;
  while (true) {
    // Look for directory strings starting with "/buck-"
    auto end = filename.find(commonPrefix, idx);
    if (end == StringPiece::npos) {
      // We were unable to find where the buck-out prefix should end.
      return filename;
    }

    // Check if this directory is either "/buck-headers/" or
    // "/buck-private-headers/".  If so, return the path stripped to here.
    const auto remainder = filename.subpiece(end + commonPrefix.size());
    if (remainder.startsWith(headerPrefix)) {
      return remainder.subpiece(headerPrefix.size());
    }
    if (remainder.startsWith(privatePrefix)) {
      return remainder.subpiece(privatePrefix.size());
    }

    // This directory was "/buck-something-else".  Keep looking.
    idx = end + 1;
  }
}
#endif // FOLLY_XLOG_SUPPORT_BUCK2

} // namespace

StringPiece getXlogCategoryNameForFile(StringPiece filename) {}

template <bool IsInHeaderFile>
LogLevel XlogLevelInfo<IsInHeaderFile>::loadLevelFull(
    folly::StringPiece categoryName, bool isOverridden) {}

template <bool IsInHeaderFile>
LogCategory* XlogCategoryInfo<IsInHeaderFile>::init(
    folly::StringPiece categoryName, bool isOverridden) {}

LogLevel XlogLevelInfo<false>::loadLevelFull(
    folly::StringPiece categoryName,
    bool isOverridden,
    XlogFileScopeInfo* fileScopeInfo) {}

// Explicitly instantiations of XlogLevelInfo and XlogCategoryInfo
template class XlogLevelInfo<true>;
template class XlogCategoryInfo<true>;
} // namespace folly