1cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/*
2cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Copyright (C) 2011 The Android Open Source Project
3cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
4cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you may not use this file except in compliance with the License.
6cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You may obtain a copy of the License at
7cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
8cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
10cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
11cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * See the License for the specific language governing permissions and
14cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * limitations under the License.
15cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
16cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
17cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpackage android.support.v4.util;
18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
19cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.Log;
20cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
21cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.Writer;
22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/**
240574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov * Helper for accessing features in {@link android.util.LogWriter}
250574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov * introduced after API level 4 in a backwards compatible fashion.
260574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov *
270574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov * @hide
28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
29cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic class LogWriter extends Writer {
30cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private final String mTag;
31cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private StringBuilder mBuilder = new StringBuilder(128);
32cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
34cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Create a new Writer that sends to the log with the given priority
35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * and tag.
36cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
37cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param tag A string tag to associate with each printed log statement.
38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public LogWriter(String tag) {
40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mTag = tag;
41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override public void close() {
44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        flushBuilder();
45cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override public void flush() {
48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        flushBuilder();
49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override public void write(char[] buf, int offset, int count) {
52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        for(int i = 0; i < count; i++) {
53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            char c = buf[offset + i];
54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if ( c == '\n') {
55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                flushBuilder();
56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            else {
58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mBuilder.append(c);
59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private void flushBuilder() {
64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBuilder.length() > 0) {
65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            Log.d(mTag, mBuilder.toString());
66cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mBuilder.delete(0, mBuilder.length());
67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
68cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
69cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
70