1 /*
2  * Copyright (C) 2020 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.internal.protolog.common;
18 
19 import android.util.Log;
20 
21 /**
22  * ProtoLog API - exposes static logging methods. Usage of this API is similar
23  * to {@code android.utils.Log} class. Instead of plain text log messages each call consists of
24  * a messageString, which is a format string for the log message (has to be a string literal or
25  * a concatenation of string literals) and a vararg array of parameters for the formatter.
26  *
27  * The syntax for the message string depends on
28  * {@link android.text.TextUtils#formatSimple(String, Object...)}}.
29  * Supported conversions:
30  * %b - boolean
31  * %d %x - integral type (Short, Integer or Long)
32  * %f - floating point type (Float or Double)
33  * %s - string
34  * %% - a literal percent character
35  * The width and precision modifiers are supported, argument_index and flags are not.
36  *
37  * Methods in this class are stubs, that are replaced by optimised versions by the ProtoLogTool
38  * during build.
39  */
40 public class ProtoLog {
41 
42     // Needs to be set directly otherwise the protologtool tries to transform the method call
43     public static boolean REQUIRE_PROTOLOGTOOL = true;
44 
45     /**
46      * DEBUG level log.
47      *
48      * @param group         {@code IProtoLogGroup} controlling this log call.
49      * @param messageString constant format string for the logged message.
50      * @param args          parameters to be used with the format string.
51      */
d(IProtoLogGroup group, String messageString, Object... args)52     public static void d(IProtoLogGroup group, String messageString, Object... args) {
53         // Stub, replaced by the ProtoLogTool.
54         if (REQUIRE_PROTOLOGTOOL) {
55             throw new UnsupportedOperationException(
56                     "ProtoLog calls MUST be processed with ProtoLogTool");
57         }
58         if (group.isLogToLogcat()) {
59             Log.d(group.getTag(), String.format(messageString, args));
60         }
61     }
62 
63     /**
64      * VERBOSE level log.
65      *
66      * @param group         {@code IProtoLogGroup} controlling this log call.
67      * @param messageString constant format string for the logged message.
68      * @param args          parameters to be used with the format string.
69      */
v(IProtoLogGroup group, String messageString, Object... args)70     public static void v(IProtoLogGroup group, String messageString, Object... args) {
71         // Stub, replaced by the ProtoLogTool.
72         if (REQUIRE_PROTOLOGTOOL) {
73             throw new UnsupportedOperationException(
74                     "ProtoLog calls MUST be processed with ProtoLogTool");
75         }
76         if (group.isLogToLogcat()) {
77             Log.v(group.getTag(), String.format(messageString, args));
78         }
79     }
80 
81     /**
82      * INFO level log.
83      *
84      * @param group         {@code IProtoLogGroup} controlling this log call.
85      * @param messageString constant format string for the logged message.
86      * @param args          parameters to be used with the format string.
87      */
i(IProtoLogGroup group, String messageString, Object... args)88     public static void i(IProtoLogGroup group, String messageString, Object... args) {
89         // Stub, replaced by the ProtoLogTool.
90         if (REQUIRE_PROTOLOGTOOL) {
91             throw new UnsupportedOperationException(
92                     "ProtoLog calls MUST be processed with ProtoLogTool");
93         }
94         if (group.isLogToLogcat()) {
95             Log.i(group.getTag(), String.format(messageString, args));
96         }
97     }
98 
99     /**
100      * WARNING level log.
101      *
102      * @param group         {@code IProtoLogGroup} controlling this log call.
103      * @param messageString constant format string for the logged message.
104      * @param args          parameters to be used with the format string.
105      */
w(IProtoLogGroup group, String messageString, Object... args)106     public static void w(IProtoLogGroup group, String messageString, Object... args) {
107         // Stub, replaced by the ProtoLogTool.
108         if (REQUIRE_PROTOLOGTOOL) {
109             throw new UnsupportedOperationException(
110                     "ProtoLog calls MUST be processed with ProtoLogTool");
111         }
112         if (group.isLogToLogcat()) {
113             Log.w(group.getTag(), String.format(messageString, args));
114         }
115     }
116 
117     /**
118      * ERROR level log.
119      *
120      * @param group         {@code IProtoLogGroup} controlling this log call.
121      * @param messageString constant format string for the logged message.
122      * @param args          parameters to be used with the format string.
123      */
e(IProtoLogGroup group, String messageString, Object... args)124     public static void e(IProtoLogGroup group, String messageString, Object... args) {
125         // Stub, replaced by the ProtoLogTool.
126         if (REQUIRE_PROTOLOGTOOL) {
127             throw new UnsupportedOperationException(
128                     "ProtoLog calls MUST be processed with ProtoLogTool");
129         }
130         if (group.isLogToLogcat()) {
131             Log.e(group.getTag(), String.format(messageString, args));
132         }
133     }
134 
135     /**
136      * WHAT A TERRIBLE FAILURE level log.
137      *
138      * @param group         {@code IProtoLogGroup} controlling this log call.
139      * @param messageString constant format string for the logged message.
140      * @param args          parameters to be used with the format string.
141      */
wtf(IProtoLogGroup group, String messageString, Object... args)142     public static void wtf(IProtoLogGroup group, String messageString, Object... args) {
143         // Stub, replaced by the ProtoLogTool.
144         if (REQUIRE_PROTOLOGTOOL) {
145             throw new UnsupportedOperationException(
146                     "ProtoLog calls MUST be processed with ProtoLogTool");
147         }
148         if (group.isLogToLogcat()) {
149             Log.wtf(group.getTag(), String.format(messageString, args));
150         }
151     }
152 }
153