Selaa lähdekoodia

LibGfx/TIFF: Generated the code to output debug prints

When the `TIFF_DEBUG` flag is set, the TIFF decoder logs every tag and
their values. This is already useful but require the developer to have
the spec handy in order to decrypt each value to its signification. None
of this information is available at runtime, but this is known by the
Python generator. So by generating these debug logs, we drastically
increase their value.

As a bonus point, most of these functions should be useful when we will
display image's metadata in Serenity.
Lucas CHOLLET 1 vuosi sitten
vanhempi
commit
2691651abf

+ 0 - 26
Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp

@@ -421,32 +421,6 @@ private:
             return read_tiff_value(type, count, offset);
         }()));
 
-        if constexpr (TIFF_DEBUG) {
-            if (tiff_value.size() == 1) {
-                tiff_value[0].visit(
-                    [&](ByteBuffer& value) {
-                        dbgln("Read tag({}), type({}): size {}", tag, to_underlying(type), value.size());
-                    },
-                    [&](auto const& value) {
-                        dbgln("Read tag({}), type({}): {}", tag, to_underlying(type), value);
-                    });
-            } else {
-                dbg("Read tag({}), type({}): [", tag, to_underlying(type));
-                for (u32 i = 0; i < tiff_value.size(); ++i) {
-                    tiff_value[i].visit(
-                        [&](ByteBuffer&) {
-                            VERIFY_NOT_REACHED();
-                        },
-                        [&](auto const& value) {
-                            dbg("{}", value);
-                        });
-                    if (i != tiff_value.size() - 1)
-                        dbg(", ");
-                }
-                dbgln("]");
-            }
-        }
-
         TRY(handle_tag(m_metadata, tag, type, count, move(tiff_value)));
 
         return {};

+ 21 - 1
Userland/Libraries/LibGfx/TIFFGenerator.py

@@ -357,6 +357,9 @@ def generate_tag_handler(tag: Tag) -> str:
 
     output = fR"""    case {tag.id}:
         // {tag.name}
+
+        dbgln_if(TIFF_DEBUG, "{tag.name}({{}}): {{}}", name_for_enum_tag_value(type), format_tiff_value(value));
+
         {pre_condition}
         {check_value}
         metadata.add_entry("{tag.name}"sv, move(value));
@@ -375,6 +378,23 @@ def generate_tag_handler_file(tags: List[Tag]) -> str:
 
 namespace Gfx::TIFF {{
 
+[[maybe_unused]] static String format_tiff_value(Vector<Value> const& values) {{
+    if (values.size() == 1)
+        return MUST(String::formatted("{{}}", values[0]));
+
+    StringBuilder builder;
+    builder.append('[');
+
+    for (u32 i = 0; i < values.size(); ++i) {{
+        builder.appendff("{{}}", values[i]);
+        if (i != values.size() - 1)
+            builder.append(", "sv);
+    }}
+
+    builder.append(']');
+    return MUST(builder.to_string());
+}}
+
 {HANDLE_TAG_SIGNATURE}
 {{
     switch (tag) {{
@@ -384,7 +404,7 @@ namespace Gfx::TIFF {{
 
     output += R"""
     default:
-        dbgln_if(TIFF_DEBUG, "Unknown tag: {}", tag);
+        dbgln_if(TIFF_DEBUG, "UnknownTag({}, {}): {}", tag, name_for_enum_tag_value(type), format_tiff_value(value));
     }
 
     return {};