xlog.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. using folly::StringPiece;
  19. namespace folly {
  20. namespace {
  21. /**
  22. * buck copies header files from their original location in the source tree
  23. * and places them under buck-out/ with a path like
  24. * buck-out/<rule-name-components>/<original-path>
  25. *
  26. * We want to strip off the buck-out/<rule-name-components> portion,
  27. * so that the filename we use is just the original path in the source tree.
  28. *
  29. * The <rule-name-component> section should always end in a path component that
  30. * includes a '#': it's format is <rule-name>#<parameters>, where <parameters>
  31. * is a comma separated list that never includes '/'.
  32. *
  33. * Search for the first path component with a '#', and strip off everything up
  34. * to this component.
  35. */
  36. StringPiece stripBuckOutPrefix(StringPiece filename) {
  37. size_t idx = 0;
  38. while (true) {
  39. auto end = filename.find('/', idx);
  40. if (end == StringPiece::npos) {
  41. // We were unable to find where the buck-out prefix should end.
  42. return filename;
  43. }
  44. auto component = filename.subpiece(idx, end - idx);
  45. if (component.find('#') != StringPiece::npos) {
  46. return filename.subpiece(end + 1);
  47. }
  48. idx = end + 1;
  49. }
  50. }
  51. } // namespace
  52. StringPiece getXlogCategoryNameForFile(StringPiece filename) {
  53. // Buck mangles the directory layout for header files. Rather than including
  54. // them from their original location, it moves them into deep directories
  55. // inside buck-out, and includes them from there.
  56. //
  57. // If this path looks like a buck header directory, try to strip off the
  58. // buck-specific portion.
  59. if (filename.startsWith("buck-out/")) {
  60. filename = stripBuckOutPrefix(filename);
  61. }
  62. return filename;
  63. }
  64. template <bool IsInHeaderFile>
  65. LogLevel XlogLevelInfo<IsInHeaderFile>::loadLevelFull(
  66. folly::StringPiece categoryName,
  67. bool isOverridden) {
  68. auto currentLevel = level_.load(std::memory_order_acquire);
  69. if (UNLIKELY(currentLevel == ::folly::LogLevel::UNINITIALIZED)) {
  70. return LoggerDB::get().xlogInit(
  71. isOverridden ? categoryName : getXlogCategoryNameForFile(categoryName),
  72. &level_,
  73. nullptr);
  74. }
  75. return currentLevel;
  76. }
  77. template <bool IsInHeaderFile>
  78. LogCategory* XlogCategoryInfo<IsInHeaderFile>::init(
  79. folly::StringPiece categoryName,
  80. bool isOverridden) {
  81. return LoggerDB::get().xlogInitCategory(
  82. isOverridden ? categoryName : getXlogCategoryNameForFile(categoryName),
  83. &category_,
  84. &isInitialized_);
  85. }
  86. #ifdef __INCLUDE_LEVEL__
  87. LogLevel XlogLevelInfo<false>::loadLevelFull(
  88. folly::StringPiece categoryName,
  89. bool isOverridden,
  90. XlogFileScopeInfo* fileScopeInfo) {
  91. auto currentLevel = fileScopeInfo->level.load(std::memory_order_acquire);
  92. if (UNLIKELY(currentLevel == ::folly::LogLevel::UNINITIALIZED)) {
  93. return LoggerDB::get().xlogInit(
  94. isOverridden ? categoryName : getXlogCategoryNameForFile(categoryName),
  95. &fileScopeInfo->level,
  96. &fileScopeInfo->category);
  97. }
  98. return currentLevel;
  99. }
  100. #endif
  101. // Explicitly instantiations of XlogLevelInfo and XlogCategoryInfo
  102. // If __INCLUDE_LEVEL__ is not available only the "true" variants ever get
  103. // used, because we cannot determine if we are ever in the .cpp file being
  104. // compiled or not.
  105. template class XlogLevelInfo<true>;
  106. template class XlogCategoryInfo<true>;
  107. } // namespace folly