/src/serenity/AK/SourceLocation.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org> |
3 | | * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/Format.h> |
11 | | #include <AK/StringView.h> |
12 | | #include <AK/Types.h> |
13 | | |
14 | | namespace AK { |
15 | | |
16 | | class SourceLocation { |
17 | | public: |
18 | 239M | [[nodiscard]] constexpr StringView function_name() const { return { m_function, __builtin_strlen(m_function) }; } |
19 | 7.96k | [[nodiscard]] constexpr StringView filename() const { return { m_file, __builtin_strlen(m_file) }; } |
20 | 7.96k | [[nodiscard]] constexpr u32 line_number() const { return m_line; } |
21 | | |
22 | | [[nodiscard]] static constexpr SourceLocation current(char const* const file = __builtin_FILE(), u32 line = __builtin_LINE(), char const* const function = __builtin_FUNCTION()) |
23 | 579M | { |
24 | 579M | return SourceLocation(file, line, function); |
25 | 579M | } |
26 | | |
27 | 0 | constexpr SourceLocation() = default; |
28 | | constexpr SourceLocation(SourceLocation const&) = default; |
29 | | SourceLocation& operator=(SourceLocation& other) = default; |
30 | | |
31 | | private: |
32 | | constexpr SourceLocation(char const* const file, u32 line, char const* const function) |
33 | 579M | : m_function(function) |
34 | 579M | , m_file(file) |
35 | 579M | , m_line(line) |
36 | 579M | { |
37 | 579M | } |
38 | | |
39 | | char const* m_function { nullptr }; |
40 | | char const* m_file { nullptr }; |
41 | | u32 m_line { 0 }; |
42 | | }; |
43 | | |
44 | | } |
45 | | |
46 | | template<> |
47 | | struct AK::Formatter<AK::SourceLocation> : AK::Formatter<FormatString> { |
48 | | ErrorOr<void> format(FormatBuilder& builder, AK::SourceLocation location) |
49 | 0 | { |
50 | 0 | return AK::Formatter<FormatString>::format(builder, "[\x1b[34m{}\x1b[0m @ {}:{}]"sv, location.function_name(), location.filename(), location.line_number()); |
51 | 0 | } |
52 | | }; |
53 | | |
54 | | #if USING_AK_GLOBALLY |
55 | | using AK::SourceLocation; |
56 | | #endif |