1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.log.table 18 19 import com.android.systemui.dagger.SysUISingleton 20 import com.android.systemui.dump.DumpManager 21 import com.android.systemui.log.LogBufferHelper.Companion.adjustMaxSize 22 import com.android.systemui.util.time.SystemClock 23 import javax.inject.Inject 24 25 @SysUISingleton 26 class TableLogBufferFactory 27 @Inject 28 constructor( 29 private val dumpManager: DumpManager, 30 private val systemClock: SystemClock, 31 ) { 32 private val existingBuffers = mutableMapOf<String, TableLogBuffer>() 33 34 /** 35 * Creates a new [TableLogBuffer]. This method should only be called from static contexts, where 36 * it is guaranteed only to be created one time. See [getOrCreate] for a cache-aware method of 37 * obtaining a buffer. 38 * 39 * @param name a unique table name 40 * @param maxSize the buffer max size. See [adjustMaxSize] 41 * @return a new [TableLogBuffer] registered with [DumpManager] 42 */ createnull43 fun create( 44 name: String, 45 maxSize: Int, 46 ): TableLogBuffer { 47 val tableBuffer = TableLogBuffer(adjustMaxSize(maxSize), name, systemClock) 48 dumpManager.registerNormalDumpable(name, tableBuffer) 49 return tableBuffer 50 } 51 52 /** 53 * Log buffers are retained indefinitely by [DumpManager], so that they can be represented in 54 * bugreports. Because of this, many of them are created statically in the Dagger graph. 55 * 56 * In the case where you have to create a logbuffer with a name only known at runtime, this 57 * method can be used to lazily create a table log buffer which is then cached for reuse. 58 * 59 * @return a [TableLogBuffer] suitable for reuse 60 */ getOrCreatenull61 fun getOrCreate( 62 name: String, 63 maxSize: Int, 64 ): TableLogBuffer = 65 existingBuffers.getOrElse(name) { 66 val buffer = create(name, maxSize) 67 existingBuffers[name] = buffer 68 buffer 69 } 70 } 71