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