811a5a4a |
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.support.v2.util; |
18 | |
19 | import android.util.Log; |
20 | |
21 | import java.io.Writer; |
22 | |
23 | /** |
24 | * Useful logging utility that is not available on all versions of Android. |
25 | */ |
26 | public class LogWriter extends Writer { |
27 | private final String mTag; |
28 | private StringBuilder mBuilder = new StringBuilder(128); |
29 | |
30 | /** |
31 | * Create a new Writer that sends to the log with the given priority |
32 | * and tag. |
33 | * |
34 | * @param priority The desired log priority: |
35 | * {@link android.util.Log#VERBOSE Log.VERBOSE}, |
36 | * {@link android.util.Log#DEBUG Log.DEBUG}, |
37 | * {@link android.util.Log#INFO Log.INFO}, |
38 | * {@link android.util.Log#WARN Log.WARN}, or |
39 | * {@link android.util.Log#ERROR Log.ERROR}. |
40 | * @param tag A string tag to associate with each printed log statement. |
41 | */ |
42 | public LogWriter(String tag) { |
43 | mTag = tag; |
44 | } |
45 | |
46 | @Override public void close() { |
47 | flushBuilder(); |
48 | } |
49 | |
50 | @Override public void flush() { |
51 | flushBuilder(); |
52 | } |
53 | |
54 | @Override public void write(char[] buf, int offset, int count) { |
55 | for(int i = 0; i < count; i++) { |
56 | char c = buf[offset + i]; |
57 | if ( c == '\n') { |
58 | flushBuilder(); |
59 | } |
60 | else { |
61 | mBuilder.append(c); |
62 | } |
63 | } |
64 | } |
65 | |
66 | private void flushBuilder() { |
67 | if (mBuilder.length() > 0) { |
68 | Log.d(mTag, mBuilder.toString()); |
69 | mBuilder.delete(0, mBuilder.length()); |
70 | } |
71 | } |
72 | } |