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

Commit f203694f authored by Alex Dadukin's avatar Alex Dadukin Committed by Android (Google) Code Review
Browse files

Merge "Migrate UsbDeviceLogger to EventLogger"

parents f580be5b ba6d3a26
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.usb;

import com.android.internal.util.dump.DualDumpOutputStream;
import com.android.server.utils.EventLogger;

import java.util.List;

/**
 * Writes logs to {@link DualDumpOutputStream}.
 *
 * @see EventLogger.DumpSink
 * @see DualDumpOutputStream
 */
final class DualOutputStreamDumpSink implements EventLogger.DumpSink {

    private final long mId;
    private final DualDumpOutputStream mDumpOutputStream;

    /* package */ DualOutputStreamDumpSink(DualDumpOutputStream dualDumpOutputStream, long id) {
        mDumpOutputStream = dualDumpOutputStream;
        mId = id;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void sink(String tag, List<EventLogger.Event> events) {
        mDumpOutputStream.write("USB Event Log", mId, tag);
        for (EventLogger.Event evt: events) {
            mDumpOutputStream.write("USB Event", mId, evt.toString());
        }
    }
}
+0 −143
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.usb;

import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.dump.DualDumpOutputStream;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;

/**
* Constructor UsbDeviceLogger class
*/
public class UsbDeviceLogger {
    private final Object mLock = new Object();

    // ring buffer of events to log.
    @GuardedBy("mLock")
    private final LinkedList<Event> mEvents;

    private final String mTitle;

    // the maximum number of events to keep in log
    private final int mMemSize;

    /**
     * Constructor for Event class.
     */
    public abstract static class Event {
        // formatter for timestamps
        private static final SimpleDateFormat sFormat = new SimpleDateFormat("MM-dd HH:mm:ss:SSS");

        private final Calendar mCalendar;

        Event() {
            mCalendar = Calendar.getInstance();
        }

    /**
     * Convert event to String
     * @return StringBuilder
     */
        public String toString() {
            return (new StringBuilder(String.format("%tm-%td %tH:%tM:%tS.%tL",
                    mCalendar, mCalendar, mCalendar, mCalendar, mCalendar, mCalendar)))
                    .append(" ").append(eventToString()).toString();
        }

        /**
         * Causes the string message for the event to appear in the logcat.
         * Here is an example of how to create a new event (a StringEvent), adding it to the logger
         * (an instance of UsbDeviceLoggerr) while also making it show in the logcat:
         * <pre>
         *     myLogger.log(
         *         (new StringEvent("something for logcat and logger")).printLog(MyClass.TAG) );
         * </pre>
         * @param tag the tag for the android.util.Log.v
         * @return the same instance of the event
         */
        public Event printLog(String tag) {
            Log.i(tag, eventToString());
            return this;
        }

        /**
         * Convert event to String.
         * This method is only called when the logger history is about to the dumped,
         * so this method is where expensive String conversions should be made, not when the Event
         * subclass is created.
         * Timestamp information will be automatically added, do not include it.
         * @return a string representation of the event that occurred.
         */
        public abstract String eventToString();
    }

    /**
    * Constructor StringEvent class
    */
    public static class StringEvent extends Event {
        private final String mMsg;

        public StringEvent(String msg) {
            mMsg = msg;
        }

        @Override
        public String eventToString() {
            return mMsg;
        }
    }

    /**
     * Constructor for logger.
     * @param size the maximum number of events to keep in log
     * @param title the string displayed before the recorded log
     */
    public UsbDeviceLogger(int size, String title) {
        mEvents = new LinkedList<Event>();
        mMemSize = size;
        mTitle = title;
    }

    /**
     * Constructor for logger.
     * @param evt the maximum number of events to keep in log
     */
    public synchronized void log(Event evt) {
        synchronized (mLock) {
            if (mEvents.size() >= mMemSize) {
                mEvents.removeFirst();
            }
            mEvents.add(evt);
        }
    }

    /**
     * Constructor for logger.
     * @param dump the maximum number of events to keep in log
     * @param id the category of events
     */
    public synchronized void dump(DualDumpOutputStream dump, long id) {
        dump.write("USB Event Log", id, mTitle);
        for (Event evt : mEvents) {
            dump.write("USB Event", id, evt.toString());
        }
    }
}
+6 −5
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ import com.android.internal.os.SomeArgs;
import com.android.internal.util.dump.DualDumpOutputStream;
import com.android.server.FgThread;
import com.android.server.LocalServices;
import com.android.server.utils.EventLogger;
import com.android.server.wm.ActivityTaskManagerInternal;

import java.io.File;
@@ -213,7 +214,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
    private static Set<Integer> sDenyInterfaces;
    private HashMap<Long, FileDescriptor> mControlFds;

    private static UsbDeviceLogger sEventLogger;
    private static EventLogger sEventLogger;

    static {
        sDenyInterfaces = new HashSet<>();
@@ -238,7 +239,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
        public void onUEvent(UEventObserver.UEvent event) {
            if (DEBUG) Slog.v(TAG, "USB UEVENT: " + event.toString());
            if (sEventLogger != null) {
                sEventLogger.log(new UsbDeviceLogger.StringEvent("USB UEVENT: "
                sEventLogger.enqueue(new EventLogger.StringEvent("USB UEVENT: "
                        + event.toString()));
            } else {
                if (DEBUG) Slog.d(TAG, "sEventLogger == null");
@@ -395,7 +396,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
        mUEventObserver.startObserving(USB_STATE_MATCH);
        mUEventObserver.startObserving(ACCESSORY_START_MATCH);

        sEventLogger = new UsbDeviceLogger(DUMPSYS_LOG_BUFFER, "UsbDeviceManager activity");
        sEventLogger = new EventLogger(DUMPSYS_LOG_BUFFER, "UsbDeviceManager activity");
    }

    UsbProfileGroupSettingsManager getCurrentSettings() {
@@ -837,7 +838,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser

        protected void sendStickyBroadcast(Intent intent) {
            mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
            sEventLogger.log(new UsbDeviceLogger.StringEvent("USB intent: " + intent));
            sEventLogger.enqueue(new EventLogger.StringEvent("USB intent: " + intent));
        }

        private void updateUsbFunctions() {
@@ -2350,7 +2351,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser

        if (mHandler != null) {
            mHandler.dump(dump, "handler", UsbDeviceManagerProto.HANDLER);
            sEventLogger.dump(dump, UsbHandlerProto.UEVENT);
            sEventLogger.dump(new DualOutputStreamDumpSink(dump, UsbHandlerProto.UEVENT));
        }

        dump.end(token);
+6 −4
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ import com.android.internal.util.XmlUtils;
import com.android.internal.util.dump.DualDumpOutputStream;
import com.android.modules.utils.TypedXmlPullParser;
import com.android.modules.utils.TypedXmlSerializer;
import com.android.server.utils.EventLogger;

import libcore.io.IoUtils;

@@ -130,7 +131,7 @@ class UsbProfileGroupSettingsManager {
    @GuardedBy("mLock")
    private boolean mIsWriteSettingsScheduled;

    private static UsbDeviceLogger sEventLogger;
    private static EventLogger sEventLogger;

    /**
     * A package of a user.
@@ -263,7 +264,7 @@ class UsbProfileGroupSettingsManager {

        mUsbHandlerManager = usbResolveActivityManager;

        sEventLogger = new UsbDeviceLogger(DUMPSYS_LOG_BUFFER,
        sEventLogger = new EventLogger(DUMPSYS_LOG_BUFFER,
                "UsbProfileGroupSettingsManager activity");
    }

@@ -970,7 +971,7 @@ class UsbProfileGroupSettingsManager {
                    matches, mAccessoryPreferenceMap.get(new AccessoryFilter(accessory)));
        }

        sEventLogger.log(new UsbDeviceLogger.StringEvent("accessoryAttached: " + intent));
        sEventLogger.enqueue(new EventLogger.StringEvent("accessoryAttached: " + intent));
        resolveActivity(intent, matches, defaultActivity, null, accessory);
    }

@@ -1524,7 +1525,8 @@ class UsbProfileGroupSettingsManager {
            }
        }

        sEventLogger.dump(dump, UsbProfileGroupSettingsManagerProto.INTENT);
        sEventLogger.dump(new DualOutputStreamDumpSink(dump,
                UsbProfileGroupSettingsManagerProto.INTENT));
        dump.end(token);
    }