Commit 9db54edd authored by Xavier Deguillard's avatar Xavier Deguillard Committed by Facebook GitHub Bot

logging: fix prefix stripping on Windows

Summary:
Windows paths can both use forward slashes as well as backslashes, and also
contain colons that folly uses to separate prefixes to strip.

The xlogStripFilename unfortunately didn't handle both of these cases leading
to log categories to always contain the full path instead of a suffix path.

Reviewed By: yfeldblum

Differential Revision: D24353208

fbshipit-source-id: ff17afe4b46f36d46bf468a5e3cf9ba9ba8412b8
parent 308bab0e
......@@ -484,6 +484,20 @@ TEST(Xlog, xlogStripFilename) {
xlogStripFilename("/my/project/src/test.cpp", "/my/project"),
"src/test.cpp") == 0,
"incorrect xlogStripFilename() behavior");
if (kIsWindows) {
EXPECT_STREQ(
"c\\d.txt", xlogStripFilename("Z:\\a\\b\\c\\d.txt", "Z:\\a\\b"));
EXPECT_STREQ("c\\d.txt", xlogStripFilename("Z:\\a\\b\\c\\d.txt", "Z:/a/b"));
EXPECT_STREQ("c/d.txt", xlogStripFilename("Z:/a/b/c/d.txt", "Z:\\a\\b"));
EXPECT_STREQ("c/d.txt", xlogStripFilename("Z:/a/b/c/d.txt", "Z:/a/b"));
EXPECT_STREQ(
"c\\d.txt", xlogStripFilename("Z:\\a\\b\\c\\d.txt", "C:/x/y:Z:/a/b"));
EXPECT_STREQ(
"c\\d.txt", xlogStripFilename("Z:\\a\\b\\c\\d.txt", "Z:/x/y:Z:/a/b"));
}
}
TEST(Xlog, XCheckPrecedence) {
......
......@@ -698,7 +698,8 @@ constexpr const char* xlogStripFilenameRecursive(
// However, in order to maintain compatibility with pre-C++14 compilers we
// have implemented it recursively to adhere to C++11 restrictions for
// constexpr functions.
return (prefixes[prefixIdx] == ':' || prefixes[prefixIdx] == '\0')
return ((prefixes[prefixIdx] == ':' && filename[filenameIdx] != ':') ||
prefixes[prefixIdx] == '\0')
? ((match && filenameIdx > 0 &&
(xlogIsDirSeparator(prefixes[filenameIdx - 1]) ||
xlogIsDirSeparator(filename[filenameIdx])))
......@@ -708,7 +709,10 @@ constexpr const char* xlogStripFilenameRecursive(
? filename
: xlogStripFilenameRecursive(
filename, prefixes, prefixIdx + 1, 0, true)))
: ((match && (prefixes[prefixIdx] == filename[filenameIdx]))
: ((match &&
((prefixes[prefixIdx] == filename[filenameIdx]) ||
(xlogIsDirSeparator(prefixes[prefixIdx]) &&
xlogIsDirSeparator(filename[filenameIdx]))))
? xlogStripFilenameRecursive(
filename, prefixes, prefixIdx + 1, filenameIdx + 1, true)
: xlogStripFilenameRecursive(
......
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