Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2012 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.utils;
     18 
     19 import com.android.SdkConstants;
     20 import com.android.annotations.NonNull;
     21 
     22 import java.io.PrintStream;
     23 import java.util.Formatter;
     24 
     25 
     26 /**
     27  * An implementation of {@link ILogger} that prints to {@link System#out} and {@link System#err}.
     28  * <p/>
     29  *
     30  */
     31 public class StdLogger implements ILogger {
     32 
     33     private final Level mLevel;
     34 
     35     public enum Level {
     36         VERBOSE(0),
     37         INFO(1),
     38         WARNING(2),
     39         ERROR(3);
     40 
     41         private final int mLevel;
     42 
     43         Level(int level) {
     44             mLevel = level;
     45         }
     46     }
     47 
     48     /**
     49      * Creates the {@link StdLogger} with a given log {@link Level}.
     50      * @param level the log Level.
     51      */
     52     public StdLogger(@NonNull Level level) {
     53         if (level == null) {
     54             throw new IllegalArgumentException("level cannot be null");
     55         }
     56 
     57         mLevel = level;
     58     }
     59 
     60     /**
     61      * Returns the logger's log {@link Level}.
     62      * @return the log level.
     63      */
     64     public Level getLevel() {
     65         return mLevel;
     66     }
     67 
     68     /**
     69      * Prints an error message.
     70      * <p/>
     71      * The message will be tagged with "Error" on the output so the caller does not
     72      * need to put such a prefix in the format string.
     73      * <p/>
     74      * The output is done on {@link System#err}.
     75      * <p/>
     76      * This is always displayed, independent of the logging {@link Level}.
     77      *
     78      * @param t is an optional {@link Throwable} or {@link Exception}. If non-null, it's
     79      *          message will be printed out.
     80      * @param errorFormat is an optional error format. If non-null, it will be printed
     81      *          using a {@link Formatter} with the provided arguments.
     82      * @param args provides the arguments for errorFormat.
     83      */
     84     @Override
     85     public void error(Throwable t, String errorFormat, Object... args) {
     86         if (errorFormat != null) {
     87             String msg = String.format("Error: " + errorFormat, args);
     88 
     89             printMessage(msg, System.err);
     90         }
     91         if (t != null) {
     92             System.err.println(String.format("Error: %1$s", t.getMessage()));
     93         }
     94     }
     95 
     96     /**
     97      * Prints a warning message.
     98      * <p/>
     99      * The message will be tagged with "Warning" on the output so the caller does not
    100      * need to put such a prefix in the format string.
    101      * <p/>
    102      * The output is done on {@link System#out}.
    103      * <p/>
    104      * This is displayed only if the logging {@link Level} is {@link Level#WARNING} or higher.
    105      *
    106      * @param msgFormat is a string format to be used with a {@link Formatter}. Cannot be null.
    107      * @param args provides the arguments for warningFormat.
    108      */
    109     @Override
    110     public void warning(@NonNull String warningFormat, Object... args) {
    111         if (mLevel.mLevel > Level.WARNING.mLevel) {
    112             return;
    113         }
    114 
    115         String msg = String.format("Warning: " + warningFormat, args);
    116 
    117         printMessage(msg, System.out);
    118     }
    119 
    120     /**
    121      * Prints an info message.
    122      * <p/>
    123      * The output is done on {@link System#out}.
    124      * <p/>
    125      * This is displayed only if the logging {@link Level} is {@link Level#INFO} or higher.
    126      *
    127      * @param msgFormat is a string format to be used with a {@link Formatter}. Cannot be null.
    128      * @param args provides the arguments for msgFormat.
    129      */
    130     @Override
    131     public void info(@NonNull String msgFormat, Object... args) {
    132         if (mLevel.mLevel > Level.INFO.mLevel) {
    133             return;
    134         }
    135 
    136         String msg = String.format(msgFormat, args);
    137 
    138         printMessage(msg, System.out);
    139     }
    140 
    141     /**
    142      * Prints a verbose message.
    143      * <p/>
    144      * The output is done on {@link System#out}.
    145      * <p/>
    146      * This is displayed only if the logging {@link Level} is {@link Level#VERBOSE} or higher.
    147      *
    148      * @param msgFormat is a string format to be used with a {@link Formatter}. Cannot be null.
    149      * @param args provides the arguments for msgFormat.
    150      */
    151     @Override
    152     public void verbose(@NonNull String msgFormat, Object... args) {
    153         if (mLevel.mLevel > Level.VERBOSE.mLevel) {
    154             return;
    155         }
    156 
    157         String msg = String.format(msgFormat, args);
    158 
    159         printMessage(msg, System.out);
    160     }
    161 
    162     private void printMessage(String msg, PrintStream stream) {
    163         if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_WINDOWS &&
    164                 !msg.endsWith("\r\n") &&
    165                 msg.endsWith("\n")) {
    166             // remove last \n so that println can use \r\n as needed.
    167             msg = msg.substring(0, msg.length() - 1);
    168         }
    169 
    170         stream.print(msg);
    171 
    172         if (!msg.endsWith("\n")) {
    173             stream.println();
    174         }
    175     }
    176 
    177 }
    178