diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txt index 2988ada7ff..8ce72a78c9 100644 --- a/AK/CMakeLists.txt +++ b/AK/CMakeLists.txt @@ -4,6 +4,7 @@ set(AK_SOURCES CircularBuffer.cpp DeprecatedFlyString.cpp DeprecatedString.cpp + Error.cpp FloatingPointStringConversions.cpp FlyString.cpp Format.cpp diff --git a/AK/Error.cpp b/AK/Error.cpp new file mode 100644 index 0000000000..df2e9a1986 --- /dev/null +++ b/AK/Error.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#ifdef KERNEL +# include +#endif + +namespace AK { + +Error Error::from_string_view_or_print_error_and_return_errno(StringView string_literal, [[maybe_unused]] int code) +{ +#ifdef KERNEL + dmesgln("{}", string_literal); + return Error::from_errno(code); +#else + return Error::from_string_view(string_literal); +#endif +} + +} diff --git a/AK/Error.h b/AK/Error.h index b1c29b74d5..ad21cf6075 100644 --- a/AK/Error.h +++ b/AK/Error.h @@ -25,6 +25,12 @@ public: ALWAYS_INLINE Error& operator=(Error&&) = default; [[nodiscard]] static Error from_errno(int code) { return Error(code); } + + // NOTE: For calling this method from within kernel code, we will simply print + // the error message and return the errno code. + // For calling this method from userspace programs, we will simply return from + // the Error::from_string_view method! + [[nodiscard]] static Error from_string_view_or_print_error_and_return_errno(StringView string_literal, int code); [[nodiscard]] static Error from_syscall(StringView syscall_name, int rc) { return Error(syscall_name, rc); } [[nodiscard]] static Error from_string_view(StringView string_literal) { return Error(string_literal); } diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index f3cc888a7f..0bf86a0d6d 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -489,6 +489,7 @@ set(AK_SOURCES ../AK/StringUtils.cpp ../AK/StringView.cpp ../AK/Time.cpp + ../AK/Error.cpp ../AK/Format.cpp ../AK/UUID.cpp ) @@ -626,6 +627,7 @@ if (ENABLE_KERNEL_COVERAGE_COLLECTION) set(KCOV_EXCLUDED_SOURCES # Make sure we don't instrument any code called from __sanitizer_cov_trace_pc # otherwise we'll end up with recursive calls to that function. + ../AK/Error.cpp ../AK/Format.cpp ../AK/StringBuilder.cpp ../Kernel/Arch/x86_64/Processor.cpp