From d056738b3ff6750deb9f7184e35ba284d0a27042 Mon Sep 17 00:00:00 2001 From: Adam Simpkins Date: Tue, 9 Jan 2018 20:08:38 -0800 Subject: [PATCH] logging: include file name suffixes in XLOG() category names Summary: Update `getXlogCategoryNameForFile()` to keep the file name suffix (e.g., `.h` or `.cpp`) in the category name. Previously the code had stripped this out. However it seems worth leaving it in: - This makes it possible to independently control log levels for messages from the header file vs the cpp file. - This makes the category name logic slightly easier to explain to users. The documents I added in D6525997 already describe this new category name selection behavior. This change updates the code to match the documented behavior. Reviewed By: yfeldblum Differential Revision: D6690464 fbshipit-source-id: 9af6b549d084bd900f788a08e9213e82579b664a --- folly/experimental/logging/test/XlogTest.cpp | 30 ++++++++++++++++---- folly/experimental/logging/xlog.cpp | 8 ------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/folly/experimental/logging/test/XlogTest.cpp b/folly/experimental/logging/test/XlogTest.cpp index 32e96438..48e59df7 100644 --- a/folly/experimental/logging/test/XlogTest.cpp +++ b/folly/experimental/logging/test/XlogTest.cpp @@ -110,7 +110,7 @@ TEST(Xlog, perFileCategoryHandling) { << "unexpected file name: " << messages[0].first.getFileName(); EXPECT_EQ(LogLevel::DBG3, messages[0].first.getLevel()); EXPECT_EQ( - "folly.experimental.logging.test.XlogHeader2", + "folly.experimental.logging.test.XlogHeader2.h", messages[0].first.getCategory()->getName()); EXPECT_EQ("folly.experimental.logging.test", messages[0].second->getName()); messages.clear(); @@ -123,7 +123,7 @@ TEST(Xlog, perFileCategoryHandling) { << "unexpected file name: " << messages[0].first.getFileName(); EXPECT_EQ(LogLevel::DBG1, messages[0].first.getLevel()); EXPECT_EQ( - "folly.experimental.logging.test.XlogHeader1", + "folly.experimental.logging.test.XlogHeader1.h", messages[0].first.getCategory()->getName()); EXPECT_EQ("folly.experimental.logging.test", messages[0].second->getName()); @@ -152,7 +152,7 @@ TEST(Xlog, perFileCategoryHandling) { ASSERT_EQ(1, messages.size()); EXPECT_EQ("file1: foobar 1234", messages[0].first.getMessage()); EXPECT_EQ( - "folly.experimental.logging.test.XlogFile1", + "folly.experimental.logging.test.XlogFile1.cpp", messages[0].first.getCategory()->getName()); messages.clear(); @@ -160,7 +160,7 @@ TEST(Xlog, perFileCategoryHandling) { ASSERT_EQ(1, messages.size()); EXPECT_EQ("file2: hello world", messages[0].first.getMessage()); EXPECT_EQ( - "folly.experimental.logging.test.XlogFile2", + "folly.experimental.logging.test.XlogFile2.cpp", messages[0].first.getCategory()->getName()); messages.clear(); @@ -180,7 +180,27 @@ TEST(Xlog, perFileCategoryHandling) { EXPECT_EQ( "file1: this log check should pass now", messages[0].first.getMessage()); EXPECT_EQ( - "folly.experimental.logging.test.XlogFile1", + "folly.experimental.logging.test.XlogFile1.cpp", messages[0].first.getCategory()->getName()); messages.clear(); } + +TEST(Xlog, getXlogCategoryName) { + EXPECT_EQ("foo.cpp", getXlogCategoryNameForFile("foo.cpp")); + EXPECT_EQ("foo.h", getXlogCategoryNameForFile("foo.h")); + + // Directory separators should be translated to "." + EXPECT_EQ("src.test.foo.cpp", getXlogCategoryNameForFile("src/test/foo.cpp")); + EXPECT_EQ("src.test.foo.h", getXlogCategoryNameForFile("src/test/foo.h")); + + // Buck's directory prefixes for generated source files + // should be stripped out + EXPECT_EQ( + "myproject.generated_header.h", + getXlogCategoryNameForFile( + "buck-out/gen/myproject#headers/myproject/generated_header.h")); + EXPECT_EQ( + "foo.bar.test.h", + getXlogCategoryNameForFile( + "buck-out/gen/foo/bar#header-map,headers/foo/bar/test.h")); +} diff --git a/folly/experimental/logging/xlog.cpp b/folly/experimental/logging/xlog.cpp index e5c8e5b7..86802116 100644 --- a/folly/experimental/logging/xlog.cpp +++ b/folly/experimental/logging/xlog.cpp @@ -69,20 +69,12 @@ std::string getXlogCategoryNameForFile(StringPiece filename) { // Translate slashes to dots, to turn the directory layout into // a category hierarchy. - size_t lastDot = std::string::npos; for (size_t n = 0; n < categoryName.size(); ++n) { if (categoryName[n] == '/') { categoryName[n] = '.'; - lastDot = std::string::npos; - } else if (categoryName[n] == '.') { - lastDot = n; } } - // Strip off the filename extension, if one was present. - if (lastDot != std::string::npos) { - categoryName.resize(lastDot); - } return categoryName; } -- 2.34.1