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