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