Skip to content

Commit 1da1995

Browse files
committed
feat universal: force USERVER_FILEPATH to return utils::zstring_view
Tests: протестировано CI commit_hash:e9189656e5519909325fb753c15e51cff3c9ee87
1 parent adaeb96 commit 1da1995

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

universal/include/userver/logging/log.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ struct EntryStorage final {
231231
#define USERVER_IMPL_DYNAMIC_DEBUG_ENTRY \
232232
[]() noexcept -> const USERVER_NAMESPACE::logging::impl::StaticLogEntry& { \
233233
struct NameHolder { \
234-
static constexpr const char* Get() noexcept { return USERVER_FILEPATH.data(); } \
234+
static constexpr const char* Get() noexcept { return USERVER_FILEPATH.c_str(); } \
235235
}; \
236236
const auto& entry = USERVER_NAMESPACE::logging::impl::EntryStorage<NameHolder, __LINE__>::entry; \
237237
return entry; \

universal/include/userver/logging/log_filepath.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
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

810
USERVER_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

universal/include/userver/utils/string_literal.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include <userver/compiler/impl/constexpr.hpp>
1414
#include <userver/formats/serialize/to.hpp>
15-
#include <userver/utils/assert.hpp>
1615
#include <userver/utils/zstring_view.hpp>
1716

1817
USERVER_NAMESPACE_BEGIN

universal/include/userver/utils/zstring_view.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
#include <fmt/format.h>
1212

1313
#include <userver/formats/serialize/to.hpp>
14-
#include <userver/utils/assert.hpp>
1514

1615
USERVER_NAMESPACE_BEGIN
1716

1817
namespace utils {
1918

19+
// Forward declaration
20+
[[noreturn]] void AbortWithStacktrace(std::string_view message) noexcept;
21+
2022
/// @ingroup userver_containers
2123
///
2224
/// @brief Non-empty string view type that guarantees null-termination and has a `c_str()` member function.
@@ -48,8 +50,11 @@ class zstring_view : public std::string_view { // NOLINT(readability-identifier
4850

4951
private:
5052
constexpr zstring_view(const char* str, std::size_t len) noexcept : std::string_view{str, len} {
51-
UASSERT_MSG(str, "null not allowed");
52-
UASSERT_MSG(str[len] == 0, "Not null-terminated");
53+
#ifndef NDEBUG
54+
if (!str || str[len] != 0) {
55+
USERVER_NAMESPACE::utils::AbortWithStacktrace("`str` should be not null and should be null terminated");
56+
}
57+
#endif
5358
}
5459
};
5560

0 commit comments

Comments
 (0)