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