| 1 | /* |
| 2 | * Copyright (C) 2006 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 android.util; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.SystemApi; |
| 22 | import android.compat.annotation.UnsupportedAppUsage; |
| 23 | import android.os.Build; |
| 24 | |
| 25 | /** |
| 26 | * API for sending log output to the {@link Log#LOG_ID_SYSTEM} buffer. |
| 27 | * |
| 28 | * <p>Should be used by system components. Use {@code adb logcat --buffer=system} to fetch the logs. |
| 29 | * |
| 30 | * @see Log |
| 31 | * @hide |
| 32 | */ |
| 33 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 34 | public final class Slog { |
| 35 | |
| 36 | private Slog() { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Logs {@code msg} at {@link Log#VERBOSE} level. |
| 41 | * |
| 42 | * @param tag identifies the source of a log message. It usually represents system service, |
| 43 | * e.g. {@code PackageManager}. |
| 44 | * @param msg the message to log. |
| 45 | * |
| 46 | * @see Log#v(String, String) |
| 47 | */ |
| 48 | @UnsupportedAppUsage |
| 49 | public static int v(@Nullable String tag, @NonNull String msg) { |
| 50 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.VERBOSE, tag, msg); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Logs {@code msg} at {@link Log#VERBOSE} level, attaching stack trace of the {@code tr} to |
| 55 | * the end of the log statement. |
| 56 | * |
| 57 | * @param tag identifies the source of a log message. It usually represents system service, |
| 58 | * e.g. {@code PackageManager}. |
| 59 | * @param msg the message to log. |
| 60 | * @param tr an exception to log. |
| 61 | * |
| 62 | * @see Log#v(String, String, Throwable) |
| 63 | */ |
| 64 | public static int v(@Nullable String tag, @NonNull String msg, @Nullable Throwable tr) { |
| 65 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.VERBOSE, tag, |
| 66 | msg + '\n' + Log.getStackTraceString(tr)); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Logs {@code msg} at {@link Log#DEBUG} level. |
| 71 | * |
| 72 | * @param tag identifies the source of a log message. It usually represents system service, |
| 73 | * e.g. {@code PackageManager}. |
| 74 | * @param msg the message to log. |
| 75 | * |
| 76 | * @see Log#d(String, String) |
| 77 | */ |
| 78 | @UnsupportedAppUsage |
| 79 | public static int d(@Nullable String tag, @NonNull String msg) { |
| 80 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.DEBUG, tag, msg); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Logs {@code msg} at {@link Log#DEBUG} level, attaching stack trace of the {@code tr} to |
| 85 | * the end of the log statement. |
| 86 | * |
| 87 | * @param tag identifies the source of a log message. It usually represents system service, |
| 88 | * e.g. {@code PackageManager}. |
| 89 | * @param msg the message to log. |
| 90 | * @param tr an exception to log. |
| 91 | * |
| 92 | * @see Log#d(String, String, Throwable) |
| 93 | */ |
| 94 | @UnsupportedAppUsage |
| 95 | public static int d(@Nullable String tag, @NonNull String msg, @Nullable Throwable tr) { |
| 96 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.DEBUG, tag, |
| 97 | msg + '\n' + Log.getStackTraceString(tr)); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Logs {@code msg} at {@link Log#INFO} level. |
| 102 | * |
| 103 | * @param tag identifies the source of a log message. It usually represents system service, |
| 104 | * e.g. {@code PackageManager}. |
| 105 | * @param msg the message to log. |
| 106 | * |
| 107 | * @see Log#i(String, String) |
| 108 | */ |
| 109 | @UnsupportedAppUsage |
| 110 | public static int i(@Nullable String tag, @NonNull String msg) { |
| 111 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.INFO, tag, msg); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Logs {@code msg} at {@link Log#INFO} level, attaching stack trace of the {@code tr} to |
| 116 | * the end of the log statement. |
| 117 | * |
| 118 | * @param tag identifies the source of a log message. It usually represents system service, |
| 119 | * e.g. {@code PackageManager}. |
| 120 | * @param msg the message to log. |
| 121 | * @param tr an exception to log. |
| 122 | * |
| 123 | * @see Log#i(String, String, Throwable) |
| 124 | */ |
| 125 | public static int i(@Nullable String tag, @NonNull String msg, @Nullable Throwable tr) { |
| 126 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.INFO, tag, |
| 127 | msg + '\n' + Log.getStackTraceString(tr)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Logs {@code msg} at {@link Log#WARN} level. |
| 132 | * |
| 133 | * @param tag identifies the source of a log message. It usually represents system service, |
| 134 | * e.g. {@code PackageManager}. |
| 135 | * @param msg the message to log. |
| 136 | * |
| 137 | * @see Log#w(String, String) |
| 138 | */ |
| 139 | @UnsupportedAppUsage |
| 140 | public static int w(@Nullable String tag, @NonNull String msg) { |
| 141 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.WARN, tag, msg); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Logs {@code msg} at {@link Log#WARN} level, attaching stack trace of the {@code tr} to |
| 146 | * the end of the log statement. |
| 147 | * |
| 148 | * @param tag identifies the source of a log message. It usually represents system service, |
| 149 | * e.g. {@code PackageManager}. |
| 150 | * @param msg the message to log. |
| 151 | * @param tr an exception to log. |
| 152 | * |
| 153 | * @see Log#w(String, String, Throwable) |
| 154 | */ |
| 155 | @UnsupportedAppUsage |
| 156 | public static int w(@Nullable String tag, @NonNull String msg, @Nullable Throwable tr) { |
| 157 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.WARN, tag, |
| 158 | msg + '\n' + Log.getStackTraceString(tr)); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Logs stack trace of {@code tr} at {@link Log#WARN} level. |
| 163 | * |
| 164 | * @param tag identifies the source of a log message. It usually represents system service, |
| 165 | * e.g. {@code PackageManager}. |
| 166 | * @param tr an exception to log. |
| 167 | * |
| 168 | * @see Log#w(String, Throwable) |
| 169 | */ |
| 170 | public static int w(@Nullable String tag, @Nullable Throwable tr) { |
| 171 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.WARN, tag, Log.getStackTraceString(tr)); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Logs {@code msg} at {@link Log#ERROR} level. |
| 176 | * |
| 177 | * @param tag identifies the source of a log message. It usually represents system service, |
| 178 | * e.g. {@code PackageManager}. |
| 179 | * @param msg the message to log. |
| 180 | * |
| 181 | * @see Log#e(String, String) |
| 182 | */ |
| 183 | @UnsupportedAppUsage |
| 184 | public static int e(@Nullable String tag, @NonNull String msg) { |
| 185 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.ERROR, tag, msg); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Logs {@code msg} at {@link Log#ERROR} level, attaching stack trace of the {@code tr} to |
| 190 | * the end of the log statement. |
| 191 | * |
| 192 | * @param tag identifies the source of a log message. It usually represents system service, |
| 193 | * e.g. {@code PackageManager}. |
| 194 | * @param msg the message to log. |
| 195 | * @param tr an exception to log. |
| 196 | * |
| 197 | * @see Log#e(String, String, Throwable) |
| 198 | */ |
| 199 | @UnsupportedAppUsage |
| 200 | public static int e(@Nullable String tag, @NonNull String msg, @Nullable Throwable tr) { |
| 201 | return Log.println_native(Log.LOG_ID_SYSTEM, Log.ERROR, tag, |
| 202 | msg + '\n' + Log.getStackTraceString(tr)); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Logs a condition that should never happen. |
| 207 | * |
| 208 | * <p> |
| 209 | * Similar to {@link Log#wtf(String, String)}, but will never cause the caller to crash, and |
| 210 | * will always be handled asynchronously. Primarily to be used by the system server. |
| 211 | * |
| 212 | * @param tag identifies the source of a log message. It usually represents system service, |
| 213 | * e.g. {@code PackageManager}. |
| 214 | * @param msg the message to log. |
| 215 | * |
| 216 | * @see Log#wtf(String, String) |
| 217 | */ |
| 218 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 219 | public static int wtf(@Nullable String tag, @NonNull String msg) { |
| 220 | return Log.wtf(Log.LOG_ID_SYSTEM, tag, msg, null, false, true); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Similar to {@link #wtf(String, String)}, but does not output anything to the log. |
| 225 | */ |
| 226 | public static void wtfQuiet(@Nullable String tag, @NonNull String msg) { |
| 227 | Log.wtfQuiet(Log.LOG_ID_SYSTEM, tag, msg, true); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Logs a condition that should never happen, attaching the full call stack to the log. |
| 232 | * |
| 233 | * <p> |
| 234 | * Similar to {@link Log#wtfStack(String, String)}, but will never cause the caller to crash, |
| 235 | * and will always be handled asynchronously. Primarily to be used by the system server. |
| 236 | * |
| 237 | * @param tag identifies the source of a log message. It usually represents system service, |
| 238 | * e.g. {@code PackageManager}. |
| 239 | * @param msg the message to log. |
| 240 | * |
| 241 | * @see Log#wtfStack(String, String) |
| 242 | */ |
| 243 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 244 | public static int wtfStack(@Nullable String tag, @NonNull String msg) { |
| 245 | return Log.wtf(Log.LOG_ID_SYSTEM, tag, msg, null, true, true); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Logs a condition that should never happen, attaching stack trace of the {@code tr} to the |
| 250 | * end of the log statement. |
| 251 | * |
| 252 | * <p> |
| 253 | * Similar to {@link Log#wtf(String, Throwable)}, but will never cause the caller to crash, |
| 254 | * and will always be handled asynchronously. Primarily to be used by the system server. |
| 255 | * |
| 256 | * @param tag identifies the source of a log message. It usually represents system service, |
| 257 | * e.g. {@code PackageManager}. |
| 258 | * @param tr an exception to log. |
| 259 | * |
| 260 | * @see Log#wtf(String, Throwable) |
| 261 | */ |
| 262 | public static int wtf(@Nullable String tag, @Nullable Throwable tr) { |
| 263 | return Log.wtf(Log.LOG_ID_SYSTEM, tag, tr.getMessage(), tr, false, true); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Logs a condition that should never happen, attaching stack trace of the {@code tr} to the |
| 268 | * end of the log statement. |
| 269 | * |
| 270 | * <p> |
| 271 | * Similar to {@link Log#wtf(String, String, Throwable)}, but will never cause the caller to |
| 272 | * crash, and will always be handled asynchronously. Primarily to be used by the system server. |
| 273 | * |
| 274 | * @param tag identifies the source of a log message. It usually represents system service, |
| 275 | * e.g. {@code PackageManager}. |
| 276 | * @param msg the message to log. |
| 277 | * @param tr an exception to log. |
| 278 | * |
| 279 | * @see Log#wtf(String, String, Throwable) |
| 280 | */ |
| 281 | @UnsupportedAppUsage |
| 282 | public static int wtf(@Nullable String tag, @NonNull String msg, @Nullable Throwable tr) { |
| 283 | return Log.wtf(Log.LOG_ID_SYSTEM, tag, msg, tr, false, true); |
| 284 | } |
| 285 | |
| 286 | /** @hide */ |
| 287 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 288 | public static int println(@Log.Level int priority, @Nullable String tag, @NonNull String msg) { |
| 289 | return Log.println_native(Log.LOG_ID_SYSTEM, priority, tag, msg); |
| 290 | } |
| 291 | } |