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 private static final int PER_CHUNK_SIZE = 1024; 34 35 private static ProtoLogImpl sServiceInstance = null; 36 37 static { ProtoLogGroup.values()38 addLogGroupEnum(ProtoLogGroup.values()); 39 } 40 41 /** 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)42 public static void d(IProtoLogGroup group, int messageHash, int paramsMask, 43 @Nullable String messageString, 44 Object... args) { 45 getSingleInstance() 46 .log(LogLevel.DEBUG, group, messageHash, paramsMask, messageString, args); 47 } 48 49 /** 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)50 public static void v(IProtoLogGroup group, int messageHash, int paramsMask, 51 @Nullable String messageString, 52 Object... args) { 53 getSingleInstance().log(LogLevel.VERBOSE, group, messageHash, paramsMask, messageString, 54 args); 55 } 56 57 /** 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)58 public static void i(IProtoLogGroup group, int messageHash, int paramsMask, 59 @Nullable String messageString, 60 Object... args) { 61 getSingleInstance().log(LogLevel.INFO, group, messageHash, paramsMask, messageString, args); 62 } 63 64 /** 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)65 public static void w(IProtoLogGroup group, int messageHash, int paramsMask, 66 @Nullable String messageString, 67 Object... args) { 68 getSingleInstance().log(LogLevel.WARN, group, messageHash, paramsMask, messageString, args); 69 } 70 71 /** 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)72 public static void e(IProtoLogGroup group, int messageHash, int paramsMask, 73 @Nullable String messageString, 74 Object... args) { 75 getSingleInstance() 76 .log(LogLevel.ERROR, group, messageHash, paramsMask, messageString, args); 77 } 78 79 /** 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)80 public static void wtf(IProtoLogGroup group, int messageHash, int paramsMask, 81 @Nullable String messageString, 82 Object... args) { 83 getSingleInstance().log(LogLevel.WTF, group, messageHash, paramsMask, messageString, args); 84 } 85 86 /** Returns true iff logging is enabled for the given {@code IProtoLogGroup}. */ isEnabled(IProtoLogGroup group)87 public static boolean isEnabled(IProtoLogGroup group) { 88 return group.isLogToLogcat() 89 || (group.isLogToProto() && getSingleInstance().isProtoEnabled()); 90 } 91 92 /** 93 * Returns the single instance of the ProtoLogImpl singleton class. 94 */ getSingleInstance()95 public static synchronized ProtoLogImpl getSingleInstance() { 96 if (sServiceInstance == null) { 97 sServiceInstance = new ProtoLogImpl( 98 new File(LOG_FILENAME) 99 , BUFFER_CAPACITY 100 , new ProtoLogViewerConfigReader() 101 , PER_CHUNK_SIZE); 102 } 103 return sServiceInstance; 104 } 105 106 @VisibleForTesting setSingleInstance(@ullable ProtoLogImpl instance)107 public static synchronized void setSingleInstance(@Nullable ProtoLogImpl instance) { 108 sServiceInstance = instance; 109 } 110 ProtoLogImpl(File logFile, int bufferCapacity, ProtoLogViewerConfigReader viewConfigReader, int perChunkSize)111 public ProtoLogImpl(File logFile, int bufferCapacity, 112 ProtoLogViewerConfigReader viewConfigReader, int perChunkSize) { 113 super(logFile, VIEWER_CONFIG_FILENAME, bufferCapacity, viewConfigReader, perChunkSize); 114 } 115 } 116 117