Jelajahi Sumber

AK+LibMain: Improve formatter for AK::Error in userspace

Print the full associated string metadata by default (if available.)
Andreas Kling 3 tahun lalu
induk
melakukan
452a5531be
3 mengubah file dengan 12 tambahan dan 6 penghapusan
  1. 1 0
      AK/Error.h
  2. 10 0
      AK/Format.h
  3. 1 6
      Userland/Libraries/LibMain/Main.cpp

+ 1 - 0
AK/Error.h

@@ -15,6 +15,7 @@
 #    include <LibC/errno_codes.h>
 #else
 #    include <errno.h>
+#    include <string.h>
 #endif
 
 namespace AK {

+ 10 - 0
AK/Format.h

@@ -18,6 +18,7 @@
 
 #ifndef KERNEL
 #    include <stdio.h>
+#    include <string.h>
 #endif
 
 namespace AK {
@@ -618,9 +619,18 @@ template<>
 struct Formatter<Error> : Formatter<FormatString> {
     ErrorOr<void> format(FormatBuilder& builder, Error const& error)
     {
+#if defined(__serenity__) && defined(KERNEL)
         if (error.is_errno())
             return Formatter<FormatString>::format(builder, "Error(errno={})", error.code());
         return Formatter<FormatString>::format(builder, "Error({})", error.string_literal());
+#else
+        if (error.is_syscall())
+            return Formatter<FormatString>::format(builder, "{}: {} (errno={})", error.string_literal(), strerror(error.code()), error.code());
+        if (error.is_errno())
+            return Formatter<FormatString>::format(builder, "{} (errno={})", strerror(error.code()), error.code());
+
+        return Formatter<FormatString>::format(builder, "{}", error.string_literal());
+#endif
     }
 };
 

+ 1 - 6
Userland/Libraries/LibMain/Main.cpp

@@ -24,12 +24,7 @@ int main(int argc, char** argv)
     });
     if (result.is_error()) {
         auto error = result.release_error();
-        if (error.is_syscall())
-            warnln("Runtime error: {}: {} (errno={})", error.string_literal(), strerror(error.code()), error.code());
-        else if (error.is_errno())
-            warnln("Runtime error: {} (errno={})", strerror(error.code()), error.code());
-        else
-            warnln("Runtime error: {}", error.string_literal());
+        warnln("Runtime error: {}", error);
         return 1;
     }
     return result.value();