33// / @file userver/logging/log_filepath.hpp
44// / @brief Short source path calculator
55
6- #include < string_view>
6+ #include < userver/compiler/impl/constexpr.hpp>
7+ #include < userver/utils/string_literal.hpp>
8+ #include < userver/utils/zstring_view.hpp>
79
810USERVER_NAMESPACE_BEGIN
911
@@ -12,12 +14,12 @@ USERVER_NAMESPACE_BEGIN
1214
1315// / @ingroup userver_universal
1416// /
15- // / @brief Short std::string_view with source path for logging.
17+ // / @brief Short @ref utils::zstring_view with source path for logging.
1618// / @hideinitializer
1719// We need user's filename here, not ours
1820// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
1921#define USERVER_FILEPATH \
20- std::string_view { __builtin_FILE () }
22+ USERVER_NAMESPACE::utils::zstring_view { __builtin_FILE () }
2123
2224#else
2325
@@ -30,8 +32,8 @@ namespace logging::impl {
3032#define USERVER_LOG_FILEPATH_STRINGIZE (X ) USERVER_LOG_FILEPATH_STRINGIZE_AUX(X)
3133
3234// May have different macro values for different translation units, hence static
33- static constexpr std::size_t PathBaseSize (std::string_view path) noexcept {
34- constexpr std::string_view kSourcePathPrefixes [] = {
35+ static constexpr std::size_t PathBaseSize (utils::zstring_view path) noexcept {
36+ constexpr utils::StringLiteral kSourcePathPrefixes [] = {
3537#ifdef USERVER_LOG_PREFIX_PATH_BASE
3638 USERVER_LOG_FILEPATH_STRINGIZE (USERVER_LOG_PREFIX_PATH_BASE),
3739#endif
@@ -43,7 +45,7 @@ static constexpr std::size_t PathBaseSize(std::string_view path) noexcept {
4345#endif
4446 };
4547
46- for (const std::string_view base : kSourcePathPrefixes ) {
48+ for (const utils::StringLiteral base : kSourcePathPrefixes ) {
4749 if (path.substr (0 , base.size ()) == base) {
4850 std::size_t base_size = path.find_first_not_of (' /' , base.size ());
4951 if (base_size == std::string_view::npos) {
@@ -56,10 +58,11 @@ static constexpr std::size_t PathBaseSize(std::string_view path) noexcept {
5658 return 0 ;
5759}
5860
59- // TODO: consteval
60- static constexpr std::string_view CutFilePath (const char * path) noexcept {
61- const std::string_view path_view = path;
62- return path_view.substr (impl::PathBaseSize (path_view));
61+ // May have different macro values for different translation units, hence static. `consteval` breaks the behavior of
62+ // utils::SourceLocation.
63+ static constexpr utils::zstring_view CutFilePath (utils::zstring_view path_view) noexcept {
64+ path_view.remove_prefix (impl::PathBaseSize (path_view));
65+ return path_view;
6366}
6467
6568#undef USERVER_LOG_FILEPATH_STRINGIZE
0 commit comments