Home | History | Annotate | Download | only in log
      1 /*
      2  * Copyright (C) 2017 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.log.ILogRegistry.EventType;
     20 
     21 import org.json.JSONObject;
     22 
     23 import java.io.IOException;
     24 import java.util.HashMap;
     25 import java.util.Map;
     26 
     27 /** TF History Logger, special log that contains only some specific events. */
     28 public class HistoryLogger extends FileLogger {
     29     private static final String TEMP_FILE_PREFIX = "tradefed_history_log_";
     30     private static final String TEMP_FILE_SUFFIX = ".txt";
     31 
     32     public HistoryLogger() {}
     33 
     34     /** Initialize the log, creating any required IO resources. */
     35     @Override
     36     public void init() throws IOException {
     37         init(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX);
     38     }
     39 
     40     /** {@inheritDoc} */
     41     @Override
     42     public void printAndPromptLog(LogLevel logLevel, String tag, String message) {
     43         throw new UnsupportedOperationException(
     44                 "printAndPromptLog is not supported by HistoryLogger");
     45     }
     46 
     47     /** {@inheritDoc} */
     48     @Override
     49     public void printLog(LogLevel logLevel, String tag, String message) {
     50         throw new UnsupportedOperationException("printLog is not supported by HistoryLogger");
     51     }
     52 
     53     /**
     54      * Call this method to log an event from a type with the associated information in the map.
     55      *
     56      * @param event the {@link EventType} of the event to log.
     57      * @param args the map of arguments to be added to the log entry to get more details on the
     58      *     event.
     59      */
     60     public void logEvent(LogLevel logLevel, EventType event, Map<String, String> args) {
     61         StringBuilder message = new StringBuilder();
     62         message.append(event);
     63         message.append(": ");
     64         if (args == null) {
     65             args = new HashMap<>();
     66         }
     67         JSONObject formattedArgs = new JSONObject(args);
     68         message.append(formattedArgs.toString());
     69         message.append("\n");
     70         internalPrintLog(logLevel, message.toString());
     71     }
     72 
     73     /** internal version of printLog(...) which prints if the event is above the urgency level. */
     74     private void internalPrintLog(LogLevel logLevel, String message) {
     75         if (logLevel.getPriority() >= getLogLevelDisplay().getPriority()) {
     76             System.out.print(message);
     77         }
     78         try {
     79             writeToLog(message);
     80         } catch (IOException e) {
     81             e.printStackTrace();
     82         }
     83     }
     84 
     85     @Override
     86     public ILeveledLogOutput clone() {
     87         FileLogger logger = new HistoryLogger();
     88         logger.setLogLevelDisplay(getLogLevelDisplay());
     89         logger.setLogLevel(getLogLevel());
     90         logger.addLogTagsDisplay(getLogTagsDisplay());
     91         return logger;
     92     }
     93 }
     94