Commit d056738b authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot

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
parent 59f59178
...@@ -110,7 +110,7 @@ TEST(Xlog, perFileCategoryHandling) { ...@@ -110,7 +110,7 @@ TEST(Xlog, perFileCategoryHandling) {
<< "unexpected file name: " << messages[0].first.getFileName(); << "unexpected file name: " << messages[0].first.getFileName();
EXPECT_EQ(LogLevel::DBG3, messages[0].first.getLevel()); EXPECT_EQ(LogLevel::DBG3, messages[0].first.getLevel());
EXPECT_EQ( EXPECT_EQ(
"folly.experimental.logging.test.XlogHeader2", "folly.experimental.logging.test.XlogHeader2.h",
messages[0].first.getCategory()->getName()); messages[0].first.getCategory()->getName());
EXPECT_EQ("folly.experimental.logging.test", messages[0].second->getName()); EXPECT_EQ("folly.experimental.logging.test", messages[0].second->getName());
messages.clear(); messages.clear();
...@@ -123,7 +123,7 @@ TEST(Xlog, perFileCategoryHandling) { ...@@ -123,7 +123,7 @@ TEST(Xlog, perFileCategoryHandling) {
<< "unexpected file name: " << messages[0].first.getFileName(); << "unexpected file name: " << messages[0].first.getFileName();
EXPECT_EQ(LogLevel::DBG1, messages[0].first.getLevel()); EXPECT_EQ(LogLevel::DBG1, messages[0].first.getLevel());
EXPECT_EQ( EXPECT_EQ(
"folly.experimental.logging.test.XlogHeader1", "folly.experimental.logging.test.XlogHeader1.h",
messages[0].first.getCategory()->getName()); messages[0].first.getCategory()->getName());
EXPECT_EQ("folly.experimental.logging.test", messages[0].second->getName()); EXPECT_EQ("folly.experimental.logging.test", messages[0].second->getName());
...@@ -152,7 +152,7 @@ TEST(Xlog, perFileCategoryHandling) { ...@@ -152,7 +152,7 @@ TEST(Xlog, perFileCategoryHandling) {
ASSERT_EQ(1, messages.size()); ASSERT_EQ(1, messages.size());
EXPECT_EQ("file1: foobar 1234", messages[0].first.getMessage()); EXPECT_EQ("file1: foobar 1234", messages[0].first.getMessage());
EXPECT_EQ( EXPECT_EQ(
"folly.experimental.logging.test.XlogFile1", "folly.experimental.logging.test.XlogFile1.cpp",
messages[0].first.getCategory()->getName()); messages[0].first.getCategory()->getName());
messages.clear(); messages.clear();
...@@ -160,7 +160,7 @@ TEST(Xlog, perFileCategoryHandling) { ...@@ -160,7 +160,7 @@ TEST(Xlog, perFileCategoryHandling) {
ASSERT_EQ(1, messages.size()); ASSERT_EQ(1, messages.size());
EXPECT_EQ("file2: hello world", messages[0].first.getMessage()); EXPECT_EQ("file2: hello world", messages[0].first.getMessage());
EXPECT_EQ( EXPECT_EQ(
"folly.experimental.logging.test.XlogFile2", "folly.experimental.logging.test.XlogFile2.cpp",
messages[0].first.getCategory()->getName()); messages[0].first.getCategory()->getName());
messages.clear(); messages.clear();
...@@ -180,7 +180,27 @@ TEST(Xlog, perFileCategoryHandling) { ...@@ -180,7 +180,27 @@ TEST(Xlog, perFileCategoryHandling) {
EXPECT_EQ( EXPECT_EQ(
"file1: this log check should pass now", messages[0].first.getMessage()); "file1: this log check should pass now", messages[0].first.getMessage());
EXPECT_EQ( EXPECT_EQ(
"folly.experimental.logging.test.XlogFile1", "folly.experimental.logging.test.XlogFile1.cpp",
messages[0].first.getCategory()->getName()); messages[0].first.getCategory()->getName());
messages.clear(); 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"));
}
...@@ -69,20 +69,12 @@ std::string getXlogCategoryNameForFile(StringPiece filename) { ...@@ -69,20 +69,12 @@ std::string getXlogCategoryNameForFile(StringPiece filename) {
// Translate slashes to dots, to turn the directory layout into // Translate slashes to dots, to turn the directory layout into
// a category hierarchy. // a category hierarchy.
size_t lastDot = std::string::npos;
for (size_t n = 0; n < categoryName.size(); ++n) { for (size_t n = 0; n < categoryName.size(); ++n) {
if (categoryName[n] == '/') { if (categoryName[n] == '/') {
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; return categoryName;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment