Bläddra i källkod

Use new format functions in remaining DevTools. (#3755)

* AK: Add formatter for JsonValue.

* Inspector: Use new format functions.

* Profiler: Use new format functions.

* UserspaceEmulator: Use new format functions.
Paul Scharnofske 4 år sedan
förälder
incheckning
d94f674bbb

+ 8 - 0
AK/JsonValue.h

@@ -261,6 +261,14 @@ private:
     } m_value;
 };
 
+template<>
+struct Formatter<JsonValue> : Formatter<StringView> {
+    void format(TypeErasedFormatParams& params, FormatBuilder& builder, const JsonValue& value)
+    {
+        Formatter<StringView>::format(params, builder, value.to_string());
+    }
+};
+
 }
 
 using AK::JsonValue;

+ 3 - 3
DevTools/Inspector/RemoteObjectGraphModel.cpp

@@ -110,9 +110,9 @@ GUI::Variant RemoteObjectGraphModel::data(const GUI::ModelIndex& index, GUI::Mod
             return m_layout_icon;
         return m_object_icon;
     }
-    if (role == GUI::ModelRole::Display) {
-        return String::format("%s{%p}", remote_object->class_name.characters(), remote_object->address);
-    }
+    if (role == GUI::ModelRole::Display)
+        return String::formatted("{}({:p})", remote_object->class_name, remote_object->address);
+
     return {};
 }
 

+ 3 - 3
DevTools/Inspector/RemoteObjectPropertyModel.cpp

@@ -77,9 +77,9 @@ GUI::Variant RemoteObjectPropertyModel::data(const GUI::ModelIndex& index, GUI::
         case Column::Value: {
             auto data = path->resolve(m_object.json);
             if (data.is_array())
-                return String::format("<Array with %d element%s", data.as_array().size(), data.as_array().size() == 1 ? ">" : "s>");
+                return String::formatted("<Array with {} element{}", data.as_array().size(), data.as_array().size() == 1 ? ">" : "s>");
             if (data.is_object())
-                return String::format("<Object with %d entr%s", data.as_object().size(), data.as_object().size() == 1 ? "y>" : "ies>");
+                return String::formatted("<Object with {} entr{}", data.as_object().size(), data.as_object().size() == 1 ? "y>" : "ies>");
             return data;
         }
         }
@@ -189,7 +189,7 @@ GUI::ModelIndex RemoteObjectPropertyModel::parent_index(const GUI::ModelIndex& i
         return create_index(index_in_parent, 0, cpath);
     }
 
-    dbg() << "No cached path found for path " << path.to_string();
+    dbgln("No cached path found for path {}", path.to_string());
     return {};
 }
 

+ 4 - 4
DevTools/Inspector/RemoteProcess.cpp

@@ -145,7 +145,7 @@ void RemoteProcess::update()
 
     m_socket->on_ready_to_read = [this] {
         if (m_socket->eof()) {
-            dbg() << "Disconnected from PID " << m_pid;
+            dbgln("Disconnected from PID {}", m_pid);
             m_socket->close();
             return;
         }
@@ -166,13 +166,13 @@ void RemoteProcess::update()
         }
 
         ASSERT(data.size() == length);
-        dbg() << "Got data size " << length << " and read that many bytes";
+        dbgln("Got data size {} and read that many bytes", length);
 
         auto json_value = JsonValue::from_string(data);
         ASSERT(json_value.has_value());
         ASSERT(json_value.value().is_object());
 
-        dbg() << "Got JSON response " << json_value.value().to_string();
+        dbgln("Got JSON response {}", json_value.value());
 
         auto& response = json_value.value().as_object();
 
@@ -193,7 +193,7 @@ void RemoteProcess::update()
 
     auto success = m_socket->connect(Core::SocketAddress::local(String::format("/tmp/rpc/%d", m_pid)));
     if (!success) {
-        fprintf(stderr, "Couldn't connect to PID %d\n", m_pid);
+        warnln("Couldn't connect to PID {}", m_pid);
         exit(1);
     }
 }

+ 2 - 2
DevTools/Inspector/main.cpp

@@ -44,7 +44,7 @@ using namespace Inspector;
 
 [[noreturn]] static void print_usage_and_exit()
 {
-    printf("usage: Inspector <pid>\n");
+    outln("usage: Inspector <pid>");
     exit(0);
 }
 
@@ -117,7 +117,7 @@ int main(int argc, char** argv)
 
     remote_process.on_update = [&] {
         if (!remote_process.process_name().is_null())
-            window->set_title(String::format("%s (%d) - Inspector", remote_process.process_name().characters(), remote_process.pid()));
+            window->set_title(String::formatted("{} ({}) - Inspector", remote_process.process_name(), remote_process.pid()));
     };
 
     auto& tree_view = splitter.add<GUI::TreeView>();

+ 2 - 2
DevTools/Profiler/DisassemblyModel.cpp

@@ -167,11 +167,11 @@ GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, GUI::ModelRole
             return insn.event_count;
         }
         if (index.column() == Column::Address)
-            return String::format("%#08x", insn.address);
+            return String::formatted("{:p}", insn.address);
         if (index.column() == Column::InstructionBytes) {
             StringBuilder builder;
             for (auto ch : insn.bytes) {
-                builder.appendf("%02x ", (u8)ch);
+                builder.appendff("{:02x} ", (u8)ch);
             }
             return builder.to_string();
         }

+ 3 - 3
DevTools/Profiler/Profile.cpp

@@ -166,14 +166,14 @@ OwnPtr<Profile> Profile::load_from_perfcore_file(const StringView& path)
 {
     auto file = Core::File::construct(path);
     if (!file->open(Core::IODevice::ReadOnly)) {
-        fprintf(stderr, "Unable to open %s, error: %s\n", path.to_string().characters(), file->error_string());
+        warnln("Unable to open {}, error: {}", path, file->error_string());
         return nullptr;
     }
 
     auto json = JsonValue::from_string(file->read_all());
     ASSERT(json.has_value());
     if (!json.value().is_object()) {
-        fprintf(stderr, "Invalid perfcore format (not a JSON object)\n");
+        warnln("Invalid perfcore format (not a JSON object)");
         return nullptr;
     }
 
@@ -182,7 +182,7 @@ OwnPtr<Profile> Profile::load_from_perfcore_file(const StringView& path)
 
     MappedFile elf_file(executable_path);
     if (!elf_file.is_valid()) {
-        fprintf(stderr, "Unable to open executable '%s' for symbolication.\n", executable_path.characters());
+        warnln("Unable to open executable '{}' for symbolication.", executable_path);
         return nullptr;
     }
 

+ 2 - 2
DevTools/Profiler/main.cpp

@@ -74,7 +74,7 @@ int main(int argc, char** argv)
     auto profile = Profile::load_from_perfcore_file(path);
 
     if (!profile) {
-        fprintf(stderr, "Unable to load profile '%s'\n", path);
+        warnln("Unable to load profile '{}'", path);
         return 1;
     }
 
@@ -170,7 +170,7 @@ bool generate_profile(pid_t pid)
 
     if (profiling_enable(pid) < 0) {
         int saved_errno = errno;
-        GUI::MessageBox::show(nullptr, String::format("Unable to profile PID %d: %s", pid, strerror(saved_errno)), "Profiler", GUI::MessageBox::Type::Error);
+        GUI::MessageBox::show(nullptr, String::formatted("Unable to profile PID {}: {}", pid, strerror(saved_errno)), "Profiler", GUI::MessageBox::Type::Error);
         return false;
     }
 

+ 3 - 3
DevTools/UserspaceEmulator/Emulator.cpp

@@ -29,8 +29,8 @@
 #include "SharedBufferRegion.h"
 #include "SimpleRegion.h"
 #include "SoftCPU.h"
+#include <AK/Format.h>
 #include <AK/LexicalPath.h>
-#include <AK/LogStream.h>
 #include <Kernel/API/Syscall.h>
 #include <LibX86/ELFSymbolProvider.h>
 #include <fcntl.h>
@@ -171,7 +171,7 @@ int Emulator::exec()
         auto insn = X86::Instruction::from_stream(m_cpu, true, true);
 
         if (trace)
-            out() << (const void*)m_cpu.base_eip() << "  \033[33;1m" << insn.to_string(m_cpu.base_eip(), &symbol_provider) << "\033[0m";
+            outln("{:p}  \033[33;1m{}\033[0m", m_cpu.base_eip(), insn.to_string(m_cpu.base_eip(), &symbol_provider));
 
         (m_cpu.*insn.handler())(insn);
 
@@ -1070,7 +1070,7 @@ void Emulator::register_signal_handlers()
 int Emulator::virt$sigaction(int signum, FlatPtr act, FlatPtr oldact)
 {
     if (signum == SIGKILL) {
-        dbg() << "Attempted to sigaction() with SIGKILL";
+        dbgln("Attempted to sigaction() with SIGKILL");
         return -EINVAL;
     }
 

+ 13 - 13
DevTools/UserspaceEmulator/SoftCPU.cpp

@@ -1342,13 +1342,13 @@ void SoftCPU::DIV_RM16(const X86::Instruction& insn)
 {
     auto divisor = insn.modrm().read16(*this, insn);
     if (divisor.value() == 0) {
-        warn() << "Divide by zero";
+        warnln("Divide by zero");
         TODO();
     }
     u32 dividend = ((u32)dx().value() << 16) | ax().value();
     auto quotient = dividend / divisor.value();
     if (quotient > NumericLimits<u16>::max()) {
-        warn() << "Divide overflow";
+        warnln("Divide overflow");
         TODO();
     }
 
@@ -1363,13 +1363,13 @@ void SoftCPU::DIV_RM32(const X86::Instruction& insn)
 {
     auto divisor = insn.modrm().read32(*this, insn);
     if (divisor.value() == 0) {
-        warn() << "Divide by zero";
+        warnln("Divide by zero");
         TODO();
     }
     u64 dividend = ((u64)edx().value() << 32) | eax().value();
     auto quotient = dividend / divisor.value();
     if (quotient > NumericLimits<u32>::max()) {
-        warn() << "Divide overflow";
+        warnln("Divide overflow");
         TODO();
     }
 
@@ -1384,13 +1384,13 @@ void SoftCPU::DIV_RM8(const X86::Instruction& insn)
 {
     auto divisor = insn.modrm().read8(*this, insn);
     if (divisor.value() == 0) {
-        warn() << "Divide by zero";
+        warnln("Divide by zero");
         TODO();
     }
     u16 dividend = ax().value();
     auto quotient = dividend / divisor.value();
     if (quotient > NumericLimits<u8>::max()) {
-        warn() << "Divide overflow";
+        warnln("Divide overflow");
         TODO();
     }
 
@@ -1405,7 +1405,7 @@ void SoftCPU::ENTER32(const X86::Instruction&) { TODO_INSN(); }
 
 void SoftCPU::ESCAPE(const X86::Instruction&)
 {
-    dbg() << "FIXME: x87 floating-point support";
+    dbgln("FIXME: x87 floating-point support");
     m_emulator.dump_backtrace();
     TODO();
 }
@@ -1546,13 +1546,13 @@ void SoftCPU::IDIV_RM16(const X86::Instruction& insn)
     auto divisor_with_shadow = insn.modrm().read16(*this, insn);
     auto divisor = (i16)divisor_with_shadow.value();
     if (divisor == 0) {
-        warn() << "Divide by zero";
+        warnln("Divide by zero");
         TODO();
     }
     i32 dividend = (i32)(((u32)dx().value() << 16) | (u32)ax().value());
     i32 result = dividend / divisor;
     if (result > NumericLimits<i16>::max() || result < NumericLimits<i16>::min()) {
-        warn() << "Divide overflow";
+        warnln("Divide overflow");
         TODO();
     }
 
@@ -1566,13 +1566,13 @@ void SoftCPU::IDIV_RM32(const X86::Instruction& insn)
     auto divisor_with_shadow = insn.modrm().read32(*this, insn);
     auto divisor = (i32)divisor_with_shadow.value();
     if (divisor == 0) {
-        warn() << "Divide by zero";
+        warnln("Divide by zero");
         TODO();
     }
     i64 dividend = (i64)(((u64)edx().value() << 32) | (u64)eax().value());
     i64 result = dividend / divisor;
     if (result > NumericLimits<i32>::max() || result < NumericLimits<i32>::min()) {
-        warn() << "Divide overflow";
+        warnln("Divide overflow");
         TODO();
     }
 
@@ -1586,13 +1586,13 @@ void SoftCPU::IDIV_RM8(const X86::Instruction& insn)
     auto divisor_with_shadow = insn.modrm().read8(*this, insn);
     auto divisor = (i8)divisor_with_shadow.value();
     if (divisor == 0) {
-        warn() << "Divide by zero";
+        warnln("Divide by zero");
         TODO();
     }
     i16 dividend = ax().value();
     i16 result = dividend / divisor;
     if (result > NumericLimits<i8>::max() || result < NumericLimits<i8>::min()) {
-        warn() << "Divide overflow";
+        warnln("Divide overflow");
         TODO();
     }
 

+ 2 - 2
DevTools/UserspaceEmulator/main.cpp

@@ -26,8 +26,8 @@
 
 #include "Emulator.h"
 #include "SoftCPU.h"
+#include <AK/Format.h>
 #include <AK/LexicalPath.h>
-#include <AK/LogStream.h>
 #include <AK/MappedFile.h>
 #include <AK/StringBuilder.h>
 #include <LibCore/ArgsParser.h>
@@ -49,7 +49,7 @@ int main(int argc, char** argv, char** env)
 
     MappedFile mapped_file(executable_path);
     if (!mapped_file.is_valid()) {
-        warn() << "Unable to map " << executable_path;
+        warnln("Unable to map {}", executable_path);
         return 1;
     }