2021-04-24 08:26:53 +03:00
|
|
|
/*
|
2021-05-30 16:28:59 +03:00
|
|
|
* Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
|
2021-04-24 08:26:53 +03:00
|
|
|
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-04-25 06:01:26 +03:00
|
|
|
#include <AK/Format.h>
|
2021-04-24 08:26:53 +03:00
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class SourceLocation {
|
|
|
|
public:
|
2022-07-11 22:53:29 +03:00
|
|
|
[[nodiscard]] constexpr StringView function_name() const { return { m_function, __builtin_strlen(m_function) }; }
|
|
|
|
[[nodiscard]] constexpr StringView filename() const { return { m_file, __builtin_strlen(m_file) }; }
|
2021-04-24 08:26:53 +03:00
|
|
|
[[nodiscard]] constexpr u32 line_number() const { return m_line; }
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
[[nodiscard]] static constexpr SourceLocation current(char const* const file = __builtin_FILE(), u32 line = __builtin_LINE(), char const* const function = __builtin_FUNCTION())
|
2021-04-24 08:26:53 +03:00
|
|
|
{
|
|
|
|
return SourceLocation(file, line, function);
|
|
|
|
}
|
|
|
|
|
2021-04-25 01:15:57 +03:00
|
|
|
constexpr SourceLocation() = default;
|
2023-09-22 02:58:21 +03:00
|
|
|
constexpr SourceLocation(SourceLocation const&) = default;
|
|
|
|
SourceLocation& operator=(SourceLocation& other) = default;
|
2021-04-25 01:15:57 +03:00
|
|
|
|
2021-04-24 08:26:53 +03:00
|
|
|
private:
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr SourceLocation(char const* const file, u32 line, char const* const function)
|
2021-04-24 08:26:53 +03:00
|
|
|
: m_function(function)
|
|
|
|
, m_file(file)
|
|
|
|
, m_line(line)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-09-22 02:58:21 +03:00
|
|
|
char const* m_function { nullptr };
|
|
|
|
char const* m_file { nullptr };
|
|
|
|
u32 m_line { 0 };
|
2021-04-24 08:26:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-25 06:01:26 +03:00
|
|
|
template<>
|
|
|
|
struct AK::Formatter<AK::SourceLocation> : AK::Formatter<FormatString> {
|
2021-11-16 03:15:21 +03:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, AK::SourceLocation location)
|
2021-04-25 06:01:26 +03:00
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
return AK::Formatter<FormatString>::format(builder, "[\x1b[34m{}\x1b[0m @ {}:{}]"sv, location.function_name(), location.filename(), location.line_number());
|
2021-04-25 06:01:26 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-04-24 08:26:53 +03:00
|
|
|
using AK::SourceLocation;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|