1 /* 2 * Copyright (C) 2018 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 package com.android.tradefed.log; 17 18 import com.android.ddmlib.Log.LogLevel; 19 import com.android.tradefed.config.IConfiguration; 20 import com.android.tradefed.config.Option; 21 import com.android.tradefed.log.LogUtil.CLog; 22 23 import java.util.Collection; 24 import java.util.HashMap; 25 import java.util.HashSet; 26 import java.util.Map; 27 import java.util.Set; 28 29 /** 30 * A base implementation for {@link ILeveledLogOutput} that allows to filtering some tags based on 31 * their name or components. 32 */ 33 public abstract class BaseLeveledLogOutput implements ILeveledLogOutput { 34 35 @Option( 36 name = "include-log-tag", 37 description = "List of tags that should be included in the display" 38 ) 39 private Set<String> mIncludeTag = new HashSet<>(); 40 41 @Option( 42 name = "class-verbosity", 43 description = "Sets the verbosity of a particular class tag. For example: StubTest INFO." 44 ) 45 private Map<String, LogLevel> mClassVerbosity = new HashMap<>(); 46 47 @Option( 48 name = "component-verbosity", 49 description = 50 "Sets the verbosity of a group of Tradefed components." 51 + "For example: target_preparer WARN." 52 ) 53 private Map<String, LogLevel> mComponentVerbosity = new HashMap<>(); 54 55 private Map<String, LogLevel> mVerbosityMap = new HashMap<>(); 56 57 /** Initialize the components filters based on the invocation {@link IConfiguration}. */ initFilters(IConfiguration config)58 public final void initFilters(IConfiguration config) { 59 initComponentVerbosity(mComponentVerbosity, mVerbosityMap, config); 60 mVerbosityMap.putAll(mClassVerbosity); 61 } 62 63 /** 64 * Whether or not a particular statement should be displayed base on its tag. 65 * 66 * @param forceStdout Whether or not to force the output to stdout. 67 * @param invocationLogLevel The current logLevel for the information. 68 * @param messageLogLevel The message evaluated log level. 69 * @param tag The logging tag of the message considered. 70 * @return True if it should be displayed, false otherwise. 71 */ shouldDisplay( boolean forceStdout, LogLevel invocationLogLevel, LogLevel messageLogLevel, String tag)72 public final boolean shouldDisplay( 73 boolean forceStdout, 74 LogLevel invocationLogLevel, 75 LogLevel messageLogLevel, 76 String tag) { 77 if (forceStdout) { 78 return true; 79 } 80 // If we have a specific verbosity use it. Otherwise use the invocation verbosity. 81 if (mVerbosityMap.get(tag) != null) { 82 if (messageLogLevel.getPriority() >= mVerbosityMap.get(tag).getPriority()) { 83 return true; 84 } 85 } else if (messageLogLevel.getPriority() >= invocationLogLevel.getPriority()) { 86 return true; 87 } 88 return false; 89 } 90 initComponentVerbosity( Map<String, LogLevel> components, Map<String, LogLevel> receiver, IConfiguration config)91 private void initComponentVerbosity( 92 Map<String, LogLevel> components, 93 Map<String, LogLevel> receiver, 94 IConfiguration config) { 95 for (String component : components.keySet()) { 96 Collection<Object> objs = config.getAllConfigurationObjectsOfType(component); 97 if (objs == null) { 98 continue; 99 } 100 for (Object o : objs) { 101 String objTag = CLog.parseClassName(o.getClass().getName()); 102 receiver.put(objTag, components.get(component)); 103 } 104 } 105 // Add components we have less control over (ddmlib for example) to ensure they don't flood 106 // us. This will still write to the log. 107 mVerbosityMap.put("ddms", LogLevel.WARN); 108 } 109 110 /** {@inheritDoc} */ 111 @Override clone()112 public abstract ILeveledLogOutput clone(); 113 } 114