Home | History | Annotate | Download | only in result
      1 /*
      2  * Copyright (C) 2016 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.tradefed.result;
     18 
     19 import com.android.tradefed.invoker.IInvocationContext;
     20 import com.android.tradefed.log.LogUtil.CLog;
     21 
     22 import java.io.IOException;
     23 import java.util.List;
     24 
     25 /**
     26  * A {@link ResultForwarder} for saving logs with the global file saver.
     27  */
     28 public class LogSaverResultForwarder extends ResultForwarder {
     29 
     30     ILogSaver mLogSaver;
     31 
     32     public LogSaverResultForwarder(ILogSaver logSaver,
     33             List<ITestInvocationListener> listeners) {
     34         super(listeners);
     35         mLogSaver = logSaver;
     36         for (ITestInvocationListener listener : listeners) {
     37             if (listener instanceof ILogSaverListener) {
     38                 ((ILogSaverListener) listener).setLogSaver(mLogSaver);
     39             }
     40         }
     41     }
     42 
     43     /**
     44      * {@inheritDoc}
     45      */
     46     @Override
     47     public void invocationStarted(IInvocationContext context) {
     48         // Intentionally call invocationStarted for the log saver first.
     49         mLogSaver.invocationStarted(context);
     50         for (ITestInvocationListener listener : getListeners()) {
     51             try {
     52                 listener.invocationStarted(context);
     53             } catch (RuntimeException e) {
     54                 // don't let the listener leave the invocation in a bad state
     55                 CLog.e("Caught runtime exception from ITestInvocationListener");
     56                 CLog.e(e);
     57             }
     58         }
     59     }
     60 
     61     /**
     62      * {@inheritDoc}
     63      */
     64     @Override
     65     public void invocationEnded(long elapsedTime) {
     66         InvocationSummaryHelper.reportInvocationEnded(getListeners(), elapsedTime);
     67         // Intentionally call invocationEnded for the log saver last.
     68         mLogSaver.invocationEnded(elapsedTime);
     69     }
     70 
     71     /**
     72      * {@inheritDoc}
     73      * <p/>
     74      * Also, save the log file with the global {@link ILogSaver} and call
     75      * {@link ILogSaverListener#testLogSaved(String, LogDataType, InputStreamSource, LogFile)}
     76      * for those listeners implementing the {@link ILogSaverListener} interface.
     77      */
     78     @Override
     79     public void testLog(String dataName, LogDataType dataType, InputStreamSource dataStream) {
     80         super.testLog(dataName, dataType, dataStream);
     81         try {
     82             LogFile logFile = mLogSaver.saveLogData(dataName, dataType,
     83                     dataStream.createInputStream());
     84             for (ITestInvocationListener listener : getListeners()) {
     85                 if (listener instanceof ILogSaverListener) {
     86                     ((ILogSaverListener) listener).testLogSaved(dataName, dataType,
     87                             dataStream, logFile);
     88                 }
     89             }
     90         } catch (IOException e) {
     91             CLog.e("Failed to save log data");
     92             CLog.e(e);
     93         }
     94     }
     95 }