• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.log.LogcatEchoTracker
23 import com.android.systemui.util.time.SystemClock
24 import java.util.concurrent.ConcurrentHashMap
25 import javax.inject.Inject
26 
27 @SysUISingleton
28 class TableLogBufferFactory
29 @Inject
30 constructor(
31     private val dumpManager: DumpManager,
32     private val systemClock: SystemClock,
33     private val logcatEchoTracker: LogcatEchoTracker,
34 ) {
35     private val existingBuffers = ConcurrentHashMap<String, TableLogBuffer>()
36 
37     /**
38      * Creates a new [TableLogBuffer]. This method should only be called from static contexts, where
39      * it is guaranteed only to be created one time. See [getOrCreate] for a cache-aware method of
40      * obtaining a buffer.
41      *
42      * @param name a unique table name
43      * @param maxSize the buffer max size. See [adjustMaxSize]
44      * @return a new [TableLogBuffer] registered with [DumpManager]
45      */
createnull46     fun create(name: String, maxSize: Int): TableLogBuffer {
47         val tableBuffer =
48             TableLogBuffer(adjustMaxSize(maxSize), name, systemClock, logcatEchoTracker)
49         dumpManager.registerTableLogBuffer(name, tableBuffer)
50         return tableBuffer
51     }
52 
53     /**
54      * Log buffers are retained indefinitely by [DumpManager], so that they can be represented in
55      * bugreports. Because of this, many of them are created statically in the Dagger graph.
56      *
57      * In the case where you have to create a logbuffer with a name only known at runtime, this
58      * method can be used to lazily create a table log buffer which is then cached for reuse.
59      *
60      * @return a [TableLogBuffer] suitable for reuse
61      */
getOrCreatenull62     fun getOrCreate(name: String, maxSize: Int): TableLogBuffer =
63         synchronized(existingBuffers) {
64             existingBuffers.getOrElse(name) {
65                 val buffer = create(name, maxSize)
66                 existingBuffers[name] = buffer
67                 buffer
68             }
69         }
70 }
71