Log.java revision b35ea0d2d494ba7668b0183dfc906cde0708bc55
1/* 2 * Copyright (C) 2014 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 17package com.android.ex.camera2.portability.debug; 18 19public class Log { 20 public static final String CAMERA_LOGTAG_PREFIX = "CAM2PORT_"; 21 private static final Log.Tag TAG = new Log.Tag("Log"); 22 23 /** 24 * This class restricts the length of the log tag to be less than the 25 * framework limit and also prepends the common tag prefix defined by 26 * {@code CAMERA_LOGTAG_PREFIX}. 27 */ 28 public static final class Tag { 29 30 // The length limit from Android framework is 23. 31 private static final int MAX_TAG_LEN = 23 - CAMERA_LOGTAG_PREFIX.length(); 32 33 final String mValue; 34 35 public Tag(String tag) { 36 final int lenDiff = tag.length() - MAX_TAG_LEN; 37 if (lenDiff > 0) { 38 w(TAG, "Tag " + tag + " is " + lenDiff + " chars longer than limit."); 39 } 40 mValue = CAMERA_LOGTAG_PREFIX + (lenDiff > 0 ? tag.substring(0, MAX_TAG_LEN) : tag); 41 } 42 43 @Override 44 public String toString() { 45 return mValue; 46 } 47 } 48 49 private interface Logger { 50 void log(Tag tag, String msg); 51 void log(Tag tag, String msg, Throwable tr); 52 } 53 54 private static final Logger SILENT_LOGGER = new Logger() { 55 @Override 56 public void log(Tag tag, String msg) { 57 // Do nothing. 58 } 59 60 @Override 61 public void log(Tag tag, String msg, Throwable tr) { 62 // Do nothing. 63 } 64 }; 65 66 private static final Logger LOGGER_D = (!CurrentConfig.get().logDebug() ? SILENT_LOGGER : new Logger() { 67 final int level = android.util.Log.DEBUG; 68 69 @Override 70 public void log(Tag tag, String msg) { 71 if (isLoggable(tag, level)) { 72 android.util.Log.d(tag.toString(), msg); 73 } 74 } 75 76 @Override 77 public void log(Tag tag, String msg, Throwable tr) { 78 if (isLoggable(tag, level)) { 79 android.util.Log.d(tag.toString(), msg, tr); 80 } 81 } 82 }); 83 84 private static final Logger LOGGER_E = (!CurrentConfig.get().logError() ? SILENT_LOGGER : new Logger() { 85 final int level = android.util.Log.ERROR; 86 87 @Override 88 public void log(Tag tag, String msg) { 89 if (isLoggable(tag, level)) { 90 android.util.Log.e(tag.toString(), msg); 91 } 92 } 93 94 @Override 95 public void log(Tag tag, String msg, Throwable tr) { 96 if (isLoggable(tag, level)) { 97 android.util.Log.e(tag.toString(), msg, tr); 98 } 99 } 100 }); 101 102 private static final Logger LOGGER_I = (!CurrentConfig.get().logInfo() ? SILENT_LOGGER : new Logger() { 103 final int level = android.util.Log.INFO; 104 105 @Override 106 public void log(Tag tag, String msg) { 107 if (isLoggable(tag, level)) { 108 android.util.Log.i(tag.toString(), msg); 109 } 110 } 111 112 @Override 113 public void log(Tag tag, String msg, Throwable tr) { 114 if (isLoggable(tag, level)) { 115 android.util.Log.i(tag.toString(), msg, tr); 116 } 117 } 118 }); 119 120 private static final Logger LOGGER_V = (!CurrentConfig.get().logVerbose() ? SILENT_LOGGER : new Logger() { 121 final int level = android.util.Log.VERBOSE; 122 123 @Override 124 public void log(Tag tag, String msg) { 125 if (isLoggable(tag, level)) { 126 android.util.Log.v(tag.toString(), msg); 127 } 128 } 129 130 @Override 131 public void log(Tag tag, String msg, Throwable tr) { 132 if (isLoggable(tag, level)) { 133 android.util.Log.v(tag.toString(), msg, tr); 134 } 135 } 136 }); 137 138 private static final Logger LOGGER_W = (!CurrentConfig.get().logWarn() ? SILENT_LOGGER : new Logger() { 139 final int level = android.util.Log.WARN; 140 141 @Override 142 public void log(Tag tag, String msg) { 143 if (isLoggable(tag, level)) { 144 android.util.Log.w(tag.toString(), msg); 145 } 146 } 147 148 @Override 149 public void log(Tag tag, String msg, Throwable tr) { 150 if (isLoggable(tag, level)) { 151 android.util.Log.w(tag.toString(), msg, tr); 152 } 153 } 154 }); 155 156 157 public static void d(Tag tag, String msg) { 158 LOGGER_D.log(tag, msg); 159 } 160 161 public static void d(Tag tag, String msg, Throwable tr) { 162 LOGGER_D.log(tag, msg, tr); 163 } 164 165 public static void e(Tag tag, String msg) { 166 LOGGER_E.log(tag, msg); 167 } 168 169 public static void e(Tag tag, String msg, Throwable tr) { 170 LOGGER_E.log(tag, msg, tr); 171 } 172 173 public static void i(Tag tag, String msg) { 174 LOGGER_I.log(tag, msg); 175 } 176 177 public static void i(Tag tag, String msg, Throwable tr) { 178 LOGGER_I.log(tag, msg, tr); 179 } 180 181 public static void v(Tag tag, String msg) { 182 LOGGER_V.log(tag, msg); 183 } 184 185 public static void v(Tag tag, String msg, Throwable tr) { 186 LOGGER_V.log(tag, msg, tr); 187 } 188 189 public static void w(Tag tag, String msg) { 190 LOGGER_W.log(tag, msg); 191 } 192 193 public static void w(Tag tag, String msg, Throwable tr) { 194 LOGGER_W.log(tag, msg, tr); 195 } 196 197 private static boolean isLoggable(Tag tag, int level) { 198 try { 199 return android.util.Log.isLoggable(tag.toString(), level); 200 } catch (IllegalArgumentException ex) { 201 e(TAG, "Tag too long:" + tag); 202 return false; 203 } 204 } 205} 206