Home | History | Annotate | Download | only in sdkman2
      1 /*
      2  * Copyright (C) 2011 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.sdkuilib.internal.repository.sdkman2;
     18 
     19 import com.android.sdklib.ISdkLog;
     20 import com.android.sdkuilib.internal.tasks.ILogUiProvider;
     21 
     22 
     23 /**
     24  * Adapter that transform log from an {@link ILogUiProvider} to an {@link ISdkLog}.
     25  */
     26 public final class SdkLogAdapter implements ILogUiProvider {
     27 
     28     private ISdkLog mSdkLog;
     29     private String mLastLogMsg;
     30 
     31     /**
     32      * Creates a new adapter to output log on the given {@code sdkLog}.
     33      *
     34      * @param sdkLog The logger to output to. Must not be null.
     35      */
     36     public SdkLogAdapter(ISdkLog sdkLog) {
     37         mSdkLog = sdkLog;
     38     }
     39 
     40     /**
     41      * Sets the description in the current task dialog.
     42      * This method can be invoked from a non-UI thread.
     43      */
     44     public void setDescription(final String description) {
     45         if (acceptLog(description)) {
     46             mSdkLog.printf("%1$s", description);    //$NON-NLS-1$
     47         }
     48     }
     49 
     50     /**
     51      * Logs a "normal" information line.
     52      * This method can be invoked from a non-UI thread.
     53      */
     54     public void log(String log) {
     55         if (acceptLog(log)) {
     56             mSdkLog.printf("  %1$s", log);          //$NON-NLS-1$
     57         }
     58     }
     59 
     60     /**
     61      * Logs an "error" information line.
     62      * This method can be invoked from a non-UI thread.
     63      */
     64     public void logError(String log) {
     65         if (acceptLog(log)) {
     66             mSdkLog.error(null, "  %1$s", log);     //$NON-NLS-1$
     67         }
     68     }
     69 
     70     /**
     71      * Logs a "verbose" information line, that is extra details which are typically
     72      * not that useful for the end-user and might be hidden until explicitly shown.
     73      * This method can be invoked from a non-UI thread.
     74      */
     75     public void logVerbose(String log) {
     76         if (acceptLog(log)) {
     77             mSdkLog.printf("    %1$s", log);        //$NON-NLS-1$
     78         }
     79     }
     80 
     81     // ----
     82 
     83     /**
     84      * Filter messages displayed in the log: <br/>
     85      * - Messages with a % are typical part of a progress update and shouldn't be in the log. <br/>
     86      * - Messages that are the same as the same output message should be output a second time.
     87      *
     88      * @param msg The potential log line to print.
     89      * @return True if the log line should be printed, false otherwise.
     90      */
     91     private boolean acceptLog(String msg) {
     92         if (msg == null) {
     93             return false;
     94         }
     95 
     96         msg = msg.trim();
     97         if (msg.indexOf('%') != -1) {
     98             return false;
     99         }
    100 
    101         if (msg.equals(mLastLogMsg)) {
    102             return false;
    103         }
    104 
    105         mLastLogMsg = msg;
    106         return true;
    107     }
    108 }
    109