Skip to content

Commit

Permalink
export variable xlog_support_buck2 v.s. preprocessor
Browse files Browse the repository at this point in the history
Summary: Let the unit-test discover whether xlog was built with support for buck2 without the build system specifically configuring the test for buck2 as well.

Reviewed By: akrieger

Differential Revision: D63302448

fbshipit-source-id: a96cf6a6c0a9a90dae0e1b0511c4740f7dd78a51
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Sep 26, 2024
1 parent ece5095 commit 1aca0fa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
3 changes: 0 additions & 3 deletions folly/logging/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@ cpp_binary(
cpp_unittest(
name = "xlog_test",
srcs = ["XlogTest.cpp"],
preprocessor_flags = [
"-DFOLLY_XLOG_SUPPORT_BUCK2",
],
deps = [
":test_handler",
":xlog_test_lib",
Expand Down
24 changes: 12 additions & 12 deletions folly/logging/test/XlogTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,18 +603,18 @@ TEST_F(XlogTest, getXlogCategoryName) {
// Buck's directory prefixes for generated source files
// should be stripped out

#ifdef FOLLY_XLOG_SUPPORT_BUCK2
EXPECT_EQ(
"dirA.dirB.Foo.h",
LogName::canonicalize(getXlogCategoryNameForFile(
"buck-out/v2/gen/fbcode/bfcdfe5fb8e2f7e1/dirA/dirB/__some_lib_rule__/"
"buck-headers/dirA/dirB/Foo.h")));
EXPECT_EQ(
"abc.xyz.def.h",
LogName::canonicalize(getXlogCategoryNameForFile(
"buck-out/v2/gen/fbcode/bfcdfe5fb8e2f7e1/dirA/dirB/__some_lib_rule__/"
"buck-private-headers/abc/xyz/def.h")));
#endif // FOLLY_XLOG_SUPPORT_BUCK2
if (folly::detail::xlog_support_buck2) {
EXPECT_EQ(
"dirA.dirB.Foo.h",
LogName::canonicalize(getXlogCategoryNameForFile(
"buck-out/v2/gen/fbcode/bfcdfe5fb8e2f7e1/dirA/dirB/__some_lib_rule__/"
"buck-headers/dirA/dirB/Foo.h")));
EXPECT_EQ(
"abc.xyz.def.h",
LogName::canonicalize(getXlogCategoryNameForFile(
"buck-out/v2/gen/fbcode/bfcdfe5fb8e2f7e1/dirA/dirB/__some_lib_rule__/"
"buck-private-headers/abc/xyz/def.h")));
}
}

TEST(Xlog, xlogStripFilename) {
Expand Down
16 changes: 12 additions & 4 deletions folly/logging/xlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@

namespace folly {

namespace detail {
#ifdef FOLLY_XLOG_SUPPORT_BUCK2
bool const xlog_support_buck2 = true;
#else
bool const xlog_support_buck2 = false;
#endif
} // namespace detail

namespace detail {
size_t& xlogEveryNThreadEntry(void const* const key) {
using Map = std::unordered_map<void const*, size_t>;
Expand Down Expand Up @@ -101,11 +109,11 @@ StringPiece getXlogCategoryNameForFile(StringPiece filename) {
//
// If this path looks like a buck header directory, try to strip off the
// buck-specific portion.
#ifdef FOLLY_XLOG_SUPPORT_BUCK2
if (filename.startsWith("buck-out/v2/")) {
filename = stripBuckV2Prefix(filename);
if (detail::xlog_support_buck2) {
if (filename.startsWith("buck-out/v2/")) {
filename = stripBuckV2Prefix(filename);
}
}
#endif // FOLLY_XLOG_SUPPORT_BUCK2

return filename;
}
Expand Down
5 changes: 5 additions & 0 deletions folly/logging/xlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
#endif

namespace folly {

namespace detail {
extern bool const xlog_support_buck2;
}

constexpr auto kLoggingMinLevel = LogLevel::FOLLY_XLOG_MIN_LEVEL;
static_assert(
!isLogLevelFatal(kLoggingMinLevel),
Expand Down

0 comments on commit 1aca0fa

Please sign in to comment.