logging: include file name suffixes in XLOG() category names
authorAdam Simpkins <simpkins@fb.com>
Wed, 10 Jan 2018 04:08:38 +0000 (20:08 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 10 Jan 2018 04:26:36 +0000 (20:26 -0800)
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
folly/experimental/logging/xlog.cpp

index 32e9643891ef9edba4ec981d7f90e085da0d7b62..48e59df72a7ea465d749f4fe56ea7fbb75d10732 100644 (file)
@@ -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"));
+}
index e5c8e5b781e12a64b65972828ddfd705aaa8d837..868021160f818d015e1c8f16a98c46b9bb933d5a 100644 (file)
@@ -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;
 }