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