1 /*
2  * Copyright (C) 2011 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.compat.annotation.UnsupportedAppUsage;
20 import android.os.Build;
21 
22 import java.io.Writer;
23 
24 /** @hide */
25 @android.ravenwood.annotation.RavenwoodKeepWholeClass
26 public class LogWriter extends Writer {
27     private final int mPriority;
28     private final String mTag;
29     private final int mBuffer;
30     private StringBuilder mBuilder = new StringBuilder(128);
31 
32     /**
33      * Create a new Writer that sends to the log with the given priority
34      * and tag.
35      *
36      * @param priority The desired log priority:
37      * {@link android.util.Log#VERBOSE Log.VERBOSE},
38      * {@link android.util.Log#DEBUG Log.DEBUG},
39      * {@link android.util.Log#INFO Log.INFO},
40      * {@link android.util.Log#WARN Log.WARN}, or
41      * {@link android.util.Log#ERROR Log.ERROR}.
42      * @param tag A string tag to associate with each printed log statement.
43      */
44     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
LogWriter(int priority, String tag)45     public LogWriter(int priority, String tag) {
46         mPriority = priority;
47         mTag = tag;
48         mBuffer = Log.LOG_ID_MAIN;
49     }
50 
51     /**
52      * @hide
53      * Same as above, but buffer is one of the LOG_ID_ constants from android.util.Log.
54      */
LogWriter(int priority, String tag, int buffer)55     public LogWriter(int priority, String tag, int buffer) {
56         mPriority = priority;
57         mTag = tag;
58         mBuffer = buffer;
59     }
60 
close()61     @Override public void close() {
62         flushBuilder();
63     }
64 
flush()65     @Override public void flush() {
66         flushBuilder();
67     }
68 
write(char[] buf, int offset, int count)69     @Override public void write(char[] buf, int offset, int count) {
70         for(int i = 0; i < count; i++) {
71             char c = buf[offset + i];
72             if ( c == '\n') {
73                 flushBuilder();
74             }
75             else {
76                 mBuilder.append(c);
77             }
78         }
79     }
80 
flushBuilder()81     private void flushBuilder() {
82         if (mBuilder.length() > 0) {
83             Log.println_native(mBuffer, mPriority, mTag, mBuilder.toString());
84             mBuilder.delete(0, mBuilder.length());
85         }
86     }
87 }
88