diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index f0062779b..d440d910a 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #ifdef _WIN32 @@ -253,13 +254,15 @@ Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsign using std::chrono::duration_cast; using std::chrono::steady_clock; + // matches from the beginning up to the last '../' or 'src/' + static const std::regex trim_source_path(R"(.*([\/\\]|^)((\.\.)|(src))[\/\\])"); static steady_clock::time_point time_origin = steady_clock::now(); Entry entry; entry.timestamp = duration_cast(steady_clock::now() - time_origin); entry.log_class = log_class; entry.log_level = log_level; - entry.filename = Common::TrimSourcePath(filename, {R"(\.\.)", "src"}).data(); + entry.filename = std::regex_replace(filename, trim_source_path, ""); entry.line_num = line_nr; entry.function = function; entry.message = std::move(message); diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index 9306c8bed..f3c64884a 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include "common/common_paths.h" #include "common/logging/log.h" @@ -210,18 +209,4 @@ std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t return std::string(buffer, len); } - -std::string TrimSourcePath(const std::string& file_path, const std::vector& roots) { - // match from beginning of path to dir sep - std::string regex_src = R"(.*([\/\\]|^)()"; - // plus the last occurrence of any root - for (auto root = roots.begin(); root < roots.end() - 1; ++root) { - regex_src += '(' + *root + ")|"; - } - regex_src += '(' + roots.back() + ')'; - // plus dir sep - regex_src += R"()[\/\\])"; - std::regex regex(regex_src); - return std::regex_replace(file_path, regex, ""); -} } // namespace Common diff --git a/src/common/string_util.h b/src/common/string_util.h index aa0d147a4..110715dce 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -64,16 +64,4 @@ bool ComparePartialString(InIt begin, InIt end, const char* other) { */ std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len); -/** - * Attempts to trim an arbitrary prefix from `path`, leaving only the part starting at `root`. It's - * intended to be used to strip a system-specific build directory from the `__FILE__` macro, - * leaving only the path relative to the sources root. - * - * @param path The input file path as a string - * @param roots The name of the root source directorys as a vector of strings. Path up to and - * including the last occurrence of these names will be stripped - * @return The trimmed path as a string - */ -std::string TrimSourcePath(const std::string& file_path, const std::vector& roots); - } // namespace Common