1 /*
2  * Copyright (C) 2020 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.internal.protolog;
18 
19 import android.annotation.Nullable;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 import com.android.internal.protolog.common.IProtoLogGroup;
23 
24 import java.io.File;
25 
26 /**
27  * A service for the ProtoLog logging system.
28  */
29 public class ProtoLogImpl extends BaseProtoLogImpl {
30     private static final int BUFFER_CAPACITY = 1024 * 1024;
31     private static final String LOG_FILENAME = "/data/misc/wmtrace/wm_log.winscope";
32     private static final String VIEWER_CONFIG_FILENAME = "/system/etc/protolog.conf.json.gz";
33 
34     private static ProtoLogImpl sServiceInstance = null;
35 
36     static {
ProtoLogGroup.values()37         addLogGroupEnum(ProtoLogGroup.values());
38     }
39 
40     /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
d(IProtoLogGroup group, int messageHash, int paramsMask, @Nullable String messageString, Object... args)41     public static void d(IProtoLogGroup group, int messageHash, int paramsMask,
42             @Nullable String messageString,
43             Object... args) {
44         getSingleInstance()
45                 .log(LogLevel.DEBUG, group, messageHash, paramsMask, messageString, args);
46     }
47 
48     /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
v(IProtoLogGroup group, int messageHash, int paramsMask, @Nullable String messageString, Object... args)49     public static void v(IProtoLogGroup group, int messageHash, int paramsMask,
50             @Nullable String messageString,
51             Object... args) {
52         getSingleInstance().log(LogLevel.VERBOSE, group, messageHash, paramsMask, messageString,
53                 args);
54     }
55 
56     /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
i(IProtoLogGroup group, int messageHash, int paramsMask, @Nullable String messageString, Object... args)57     public static void i(IProtoLogGroup group, int messageHash, int paramsMask,
58             @Nullable String messageString,
59             Object... args) {
60         getSingleInstance().log(LogLevel.INFO, group, messageHash, paramsMask, messageString, args);
61     }
62 
63     /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
w(IProtoLogGroup group, int messageHash, int paramsMask, @Nullable String messageString, Object... args)64     public static void w(IProtoLogGroup group, int messageHash, int paramsMask,
65             @Nullable String messageString,
66             Object... args) {
67         getSingleInstance().log(LogLevel.WARN, group, messageHash, paramsMask, messageString, args);
68     }
69 
70     /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
e(IProtoLogGroup group, int messageHash, int paramsMask, @Nullable String messageString, Object... args)71     public static void e(IProtoLogGroup group, int messageHash, int paramsMask,
72             @Nullable String messageString,
73             Object... args) {
74         getSingleInstance()
75                 .log(LogLevel.ERROR, group, messageHash, paramsMask, messageString, args);
76     }
77 
78     /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
wtf(IProtoLogGroup group, int messageHash, int paramsMask, @Nullable String messageString, Object... args)79     public static void wtf(IProtoLogGroup group, int messageHash, int paramsMask,
80             @Nullable String messageString,
81             Object... args) {
82         getSingleInstance().log(LogLevel.WTF, group, messageHash, paramsMask, messageString, args);
83     }
84 
85     /** Returns true iff logging is enabled for the given {@code IProtoLogGroup}. */
isEnabled(IProtoLogGroup group)86     public static boolean isEnabled(IProtoLogGroup group) {
87         return group.isLogToLogcat()
88                 || (group.isLogToProto() && getSingleInstance().isProtoEnabled());
89     }
90 
91     /**
92      * Returns the single instance of the ProtoLogImpl singleton class.
93      */
getSingleInstance()94     public static synchronized ProtoLogImpl getSingleInstance() {
95         if (sServiceInstance == null) {
96             sServiceInstance = new ProtoLogImpl(
97                     new File(LOG_FILENAME), BUFFER_CAPACITY, new ProtoLogViewerConfigReader());
98         }
99         return sServiceInstance;
100     }
101 
102     @VisibleForTesting
setSingleInstance(@ullable ProtoLogImpl instance)103     public static synchronized void setSingleInstance(@Nullable ProtoLogImpl instance) {
104         sServiceInstance = instance;
105     }
106 
ProtoLogImpl(File logFile, int bufferCapacity, ProtoLogViewerConfigReader viewConfigReader)107     public ProtoLogImpl(File logFile, int bufferCapacity,
108             ProtoLogViewerConfigReader viewConfigReader) {
109         super(logFile, VIEWER_CONFIG_FILENAME, bufferCapacity, viewConfigReader);
110     }
111 }
112 
113