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 17package android.support.v4.util; 18 19import android.support.annotation.RestrictTo; 20import android.util.Log; 21 22import java.io.Writer; 23 24import static android.support.annotation.RestrictTo.Scope.GROUP_ID; 25 26/** 27 * Helper for accessing features in {@link android.util.LogWriter} 28 * introduced after API level 4 in a backwards compatible fashion. 29 * 30 * @hide 31 */ 32@RestrictTo(GROUP_ID) 33public class LogWriter extends Writer { 34 private final String mTag; 35 private StringBuilder mBuilder = new StringBuilder(128); 36 37 /** 38 * Create a new Writer that sends to the log with the given priority 39 * and tag. 40 * 41 * @param tag A string tag to associate with each printed log statement. 42 */ 43 public LogWriter(String tag) { 44 mTag = tag; 45 } 46 47 @Override public void close() { 48 flushBuilder(); 49 } 50 51 @Override public void flush() { 52 flushBuilder(); 53 } 54 55 @Override public void write(char[] buf, int offset, int count) { 56 for(int i = 0; i < count; i++) { 57 char c = buf[offset + i]; 58 if ( c == '\n') { 59 flushBuilder(); 60 } 61 else { 62 mBuilder.append(c); 63 } 64 } 65 } 66 67 private void flushBuilder() { 68 if (mBuilder.length() > 0) { 69 Log.d(mTag, mBuilder.toString()); 70 mBuilder.delete(0, mBuilder.length()); 71 } 72 } 73} 74