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

Commit 2f8dccac authored by Thomas Stuart's avatar Thomas Stuart Committed by Android (Google) Code Review
Browse files

Merge "fix log spewing in Telecom" into tm-dev

parents 57983d37 dbf18ee3
Loading
Loading
Loading
Loading
+40 −7
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.os.SystemClock;
import android.telecom.Logging.EventManager;
import android.telecom.Logging.EventManager.TimedEventPair;

import com.android.internal.annotations.VisibleForTesting;

import java.util.HashMap;
import java.util.Map;

@@ -34,6 +36,9 @@ public class LogUtils {
    private static final String LOGUTILS_TAG = "LogUtils";

    public static final boolean SYSTRACE_DEBUG = false; /* STOP SHIP if true */
    private static boolean sInitializedLoggerListeners = false; // used to gate re-init of listeners
    private static int sInitializedCounter = 0; /* For testing purposes only */
    private static final Object sLock = new Object(); // Coarse lock for all of LogUtils

    public static class EventTimer {
        private long mLastElapsedMillis;
@@ -263,6 +268,14 @@ public class LogUtils {
    }

    public static void initLogging(Context context) {
        android.telecom.Log.d(LOGUTILS_TAG, "initLogging: attempting to acquire LogUtils sLock");
        synchronized (sLock) {
            android.telecom.Log.d(LOGUTILS_TAG, "initLogging: grabbed LogUtils sLock");
            if (!sInitializedLoggerListeners) {
                android.telecom.Log.d(LOGUTILS_TAG,
                        "initLogging: called for first time. Registering the EventListener &"
                                + " SessionListener.");

                android.telecom.Log.setTag(TAG);
                android.telecom.Log.setSessionContext(context);
                for (EventManager.TimedEventPair p : Events.Timings.sTimedEvents) {
@@ -271,5 +284,25 @@ public class LogUtils {
                android.telecom.Log.registerEventListener(LogUtils::eventRecordAdded);
                // Store analytics about recently completed Sessions.
                android.telecom.Log.registerSessionListener(Analytics::addSessionTiming);

                // Ensure LogUtils#initLogging(Context) is called once throughout the entire
                // lifecycle of not only TelecomSystem, but the Testing Framework.
                sInitializedLoggerListeners = true;
                sInitializedCounter++; /* For testing purposes only */
            } else {
                android.telecom.Log.d(LOGUTILS_TAG, "initLogging: called again. Doing nothing.");
            }
        }
    }

    /**
     * Needed in order to test if the registerEventListener & registerSessionListener are ever
     * re-initialized in the entire process of the Testing Framework or TelecomSystem.
     *
     * @return the number of times initLogging(Context) listeners have been initialized
     */
    @VisibleForTesting
    public static int getInitializedCounter() {
        return sInitializedCounter;
    }
}
+65 −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.telecom.tests;

import static org.junit.Assert.assertTrue;

import android.test.suitebuilder.annotation.SmallTest;

import com.android.server.telecom.LogUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class LogUtilsTest extends TelecomTestCase {


    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @Override
    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Tests LogUtils#initLogging(Context) listeners cannot be initialized more than once by calling
     * the init function multiple times.  If the listeners are ever re-initialized, log spewing
     * will occur.
     *
     * Note, LogUtils will already be initialized at the start of the testing framework,
     * so you cannot assume it is 0 at the start of this testing class.
     */
    @SmallTest
    @Test
    public void testLogUtilsIsNotReInitialized() {

        // assert the listeners of LogUtils are never re-initialized
        assertTrue(LogUtils.getInitializedCounter() <= 1);
        // call initLogging an arbitrary amount of times...
        LogUtils.initLogging(mContext);
        LogUtils.initLogging(mContext);
        LogUtils.initLogging(mContext);
        // assert the listeners of LogUtils are never re-initialized
        assertTrue(LogUtils.getInitializedCounter() <= 1);
    }
}