1
0
Fork 0

remove Common::TrimSourcePath

wwylele / 白疾風Today at 6:14 PM
I doubt the performance of constructing regex everytime the function is called
Is TrimSourcePath only called by logging? if so, you can move the implementation into logging, and cache the regex object into global
This function is probably too specific to be in common anyway
This commit is contained in:
BreadFish64 2019-03-10 19:18:09 -05:00
parent 605dfe80e7
commit 36e368ff99
3 changed files with 4 additions and 28 deletions

View File

@ -8,6 +8,7 @@
#include <condition_variable> #include <condition_variable>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <regex>
#include <thread> #include <thread>
#include <vector> #include <vector>
#ifdef _WIN32 #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::duration_cast;
using std::chrono::steady_clock; 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(); static steady_clock::time_point time_origin = steady_clock::now();
Entry entry; Entry entry;
entry.timestamp = duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin); entry.timestamp = duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin);
entry.log_class = log_class; entry.log_class = log_class;
entry.log_level = log_level; 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.line_num = line_nr;
entry.function = function; entry.function = function;
entry.message = std::move(message); entry.message = std::move(message);

View File

@ -7,7 +7,6 @@
#include <codecvt> #include <codecvt>
#include <cstdlib> #include <cstdlib>
#include <locale> #include <locale>
#include <regex>
#include <sstream> #include <sstream>
#include "common/common_paths.h" #include "common/common_paths.h"
#include "common/logging/log.h" #include "common/logging/log.h"
@ -210,18 +209,4 @@ std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t
return std::string(buffer, len); return std::string(buffer, len);
} }
std::string TrimSourcePath(const std::string& file_path, const std::vector<std::string>& 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 } // namespace Common

View File

@ -64,16 +64,4 @@ bool ComparePartialString(InIt begin, InIt end, const char* other) {
*/ */
std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len); 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<std::string>& roots);
} // namespace Common } // namespace Common