Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d050812a authored by Ytai Ben-tsvi's avatar Ytai Ben-tsvi Committed by Android (Google) Code Review
Browse files

Merge changes I9f0a440d,I0b734cc7,I510ee307,Ia257021d into rvc-dev

* changes:
  Pretty-print parcelables when logging
  Add sysdump to SoundTriggerMiddlewareService
  SoundTriggerMiddlewareService logging decorator
  Refactor SoundTriggerMiddlewareService
parents 626b2495 032ad163
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.soundtrigger_middleware;

import android.annotation.NonNull;

import java.io.PrintWriter;

/**
 * An interface of an object that can generate a dump.
 */
interface Dumpable {
    /**
     * Generate a human-readable dump into the given writer.
     * @param pw The writer.
     */
    void dump(@NonNull PrintWriter pw);
}
+249 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.soundtrigger_middleware;

import android.annotation.NonNull;
import android.annotation.Nullable;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Map;

/**
 * A collection of pretty-print utilities for data objects.
 */
class ObjectPrinter {
    /** Default maximum elements to print in a collection. */
    static public final int kDefaultMaxCollectionLength = 16;

    /**
     * Simple version of {@link #print(Object, boolean, int)} that prints an object, without
     * recursing into sub-objects.
     *
     * @param obj The object to print.
     * @return A string representing the object.
     */
    static String print(@Nullable Object obj) {
        return print(obj, false, kDefaultMaxCollectionLength);
    }

    /**
     * Pretty-prints an object.
     *
     * @param obj                 The object to print.
     * @param deep                Whether to pretty-print sub-objects (if false, just prints them
     *                            with {@link Object#toString()}).
     * @param maxCollectionLength Whenever encountering collections, maximum number of elements to
     *                            print.
     * @return A string representing the object.
     */
    static String print(@Nullable Object obj, boolean deep, int maxCollectionLength) {
        StringBuilder builder = new StringBuilder();
        print(builder, obj, deep, maxCollectionLength);
        return builder.toString();
    }

    /**
     * This version is suitable for use inside a toString() override of an object, e.g.:
     * <pre><code>
     *     class MyObject {
     *         ...
     *         @Override
     *         String toString() {
     *             return ObjectPrinter.printPublicFields(this, ...);
     *         }
     *     }
     * </code></pre>
     *
     * @param obj                 The object to print.
     * @param deep                Whether to pretty-print sub-objects (if false, just prints them
     *                            with {@link Object#toString()}).
     * @param maxCollectionLength Whenever encountering collections, maximum number of elements to
     *                            print.
     */
    static String printPublicFields(@Nullable Object obj, boolean deep, int maxCollectionLength) {
        StringBuilder builder = new StringBuilder();
        printPublicFields(builder, obj, deep, maxCollectionLength);
        return builder.toString();
    }

    /**
     * A version of {@link #print(Object, boolean, int)} that uses a {@link StringBuilder}.
     *
     * @param builder             StringBuilder to print into.
     * @param obj                 The object to print.
     * @param deep                Whether to pretty-print sub-objects (if false, just prints them
     *                            with {@link Object#toString()}).
     * @param maxCollectionLength Whenever encountering collections, maximum number of elements to
     *                            print.
     */
    static void print(@NonNull StringBuilder builder, @Nullable Object obj, boolean deep,
            int maxCollectionLength) {
        try {
            if (obj == null) {
                builder.append("null");
                return;
            }
            if (obj instanceof Boolean) {
                builder.append(obj.toString());
                return;
            }
            if (obj instanceof Number) {
                builder.append(obj.toString());
                return;
            }
            if (obj instanceof Character) {
                builder.append('\'');
                builder.append(obj.toString());
                builder.append('\'');
                return;
            }
            if (obj instanceof String) {
                builder.append('"');
                builder.append(obj.toString());
                builder.append('"');
                return;
            }

            Class cls = obj.getClass();

            if (Collection.class.isAssignableFrom(cls)) {
                Collection collection = (Collection) obj;
                builder.append("[ ");
                int length = collection.size();
                boolean isLong = false;
                int i = 0;
                for (Object child : collection) {
                    if (i > 0) {
                        builder.append(", ");
                    }
                    if (i >= maxCollectionLength) {
                        isLong = true;
                        break;
                    }
                    print(builder, child, deep, maxCollectionLength);
                    ++i;
                }
                if (isLong) {
                    builder.append("... (+");
                    builder.append(length - maxCollectionLength);
                    builder.append(" entries)");
                }
                builder.append(" ]");
                return;
            }

            if (Map.class.isAssignableFrom(cls)) {
                Map<?, ?> map = (Map<?, ?>) obj;
                builder.append("< ");
                int length = map.size();
                boolean isLong = false;
                int i = 0;
                for (Map.Entry<?, ?> child : map.entrySet()) {
                    if (i > 0) {
                        builder.append(", ");
                    }
                    if (i >= maxCollectionLength) {
                        isLong = true;
                        break;
                    }
                    print(builder, child.getKey(), deep, maxCollectionLength);
                    builder.append(": ");
                    print(builder, child.getValue(), deep, maxCollectionLength);
                    ++i;
                }
                if (isLong) {
                    builder.append("... (+");
                    builder.append(length - maxCollectionLength);
                    builder.append(" entries)");
                }
                builder.append(" >");
                return;
            }

            if (cls.isArray()) {
                builder.append("[ ");
                int length = Array.getLength(obj);
                boolean isLong = false;
                for (int i = 0; i < length; ++i) {
                    if (i > 0) {
                        builder.append(", ");
                    }
                    if (i >= maxCollectionLength) {
                        isLong = true;
                        break;
                    }
                    print(builder, Array.get(obj, i), deep, maxCollectionLength);
                }
                if (isLong) {
                    builder.append("... (+");
                    builder.append(length - maxCollectionLength);
                    builder.append(" entries)");
                }
                builder.append(" ]");
                return;
            }

            if (!deep) {
                builder.append(obj.toString());
                return;
            }
            printPublicFields(builder, obj, deep, maxCollectionLength);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * A version of {@link #printPublicFields(Object, boolean, int)} that uses a {@link
     * StringBuilder}.
     *
     * @param obj                 The object to print.
     * @param deep                Whether to pretty-print sub-objects (if false, just prints them
     *                            with {@link Object#toString()}).
     * @param maxCollectionLength Whenever encountering collections, maximum number of elements to
     *                            print.
     */
    static void printPublicFields(@NonNull StringBuilder builder, @Nullable Object obj,
            boolean deep,
            int maxCollectionLength) {
        try {
            Class cls = obj.getClass();
            builder.append("{ ");

            boolean first = true;
            for (Field fld : cls.getDeclaredFields()) {
                int mod = fld.getModifiers();
                if ((mod & Modifier.PUBLIC) != 0 && (mod & Modifier.STATIC) == 0) {
                    if (first) {
                        first = false;
                    } else {
                        builder.append(", ");
                    }
                    builder.append(fld.getName());
                    builder.append(": ");
                    print(builder, fld.get(obj), deep, maxCollectionLength);
                }
            }
            builder.append(" }");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
+454 −0

File added.

Preview size limit exceeded, changes collapsed.

+72 −631

File changed.

Preview size limit exceeded, changes collapsed.

+784 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading