7#include <userver/logging/log_filepath.hpp>
11namespace utils::impl {
13class SourceLocation
final {
14 struct EmplaceEnabler
final {
15 constexpr explicit EmplaceEnabler()
noexcept =
default;
19 static constexpr SourceLocation Current(
20 EmplaceEnabler = EmplaceEnabler{},
22 std::string_view function_name =
__builtin_FUNCTION(),
23 std::uint_least32_t line =
__builtin_LINE())
noexcept {
24 return SourceLocation{line, file_name, function_name};
28 static constexpr SourceLocation Custom(
29 std::uint_least32_t line, std::string_view file_name,
30 std::string_view function_name)
noexcept {
31 return SourceLocation{line, file_name, function_name};
34 constexpr std::uint_least32_t GetLine()
const noexcept {
return line_; }
36 constexpr std::string_view GetLineString()
const noexcept {
37 return std::string_view(&line_string_[kMaxLineDigits - line_digits_],
41 constexpr std::string_view GetFileName()
const noexcept {
return file_name_; }
43 constexpr std::string_view GetFunctionName()
const noexcept {
44 return function_name_;
48 static_assert(
sizeof(std::uint_least32_t) ==
sizeof(std::uint32_t));
50 static constexpr std::size_t kMaxLineDigits = 8;
52 constexpr SourceLocation(std::uint_least32_t line, std::string_view file_name,
53 std::string_view function_name)
noexcept
55 line_digits_(DigitsBase10(line)),
56 file_name_(file_name),
57 function_name_(function_name) {
61 static constexpr std::size_t DigitsBase10(std::uint32_t line)
noexcept {
62 static_assert(
sizeof(line) == 4);
72 + (line >= 1000000000);
75 constexpr void FillLineString()
noexcept {
76 constexpr std::size_t kMaxSupportedLine = 99999999;
77 if (line_ > kMaxSupportedLine) {
80 line_ = kMaxSupportedLine;
81 line_digits_ = kMaxLineDigits;
84 line_string_[kMaxLineDigits - 1] =
'0';
85 for (std::size_t index = kMaxLineDigits - 1; line > 0;
86 line /= 10, --index) {
87 line_string_[index] =
'0' + (line % 10);
92 std::uint32_t line_digits_;
93 char line_string_[kMaxLineDigits]{};
94 std::string_view file_name_;
95 std::string_view function_name_;