1 /* 2 * Copyright (C) 2015 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 org.chromium.latency.walt; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.support.v4.content.LocalBroadcastManager; 24 import android.util.Log; 25 26 /** 27 * A very simple logger that keeps its data in a StringBuilder. We need on screen log because the 28 * USB port is often taken and we don't have easy access to adb log. 29 */ 30 public class SimpleLogger { 31 private static final String LOG_INTENT = "log-message"; 32 public static final String TAG = "WaltLogger"; 33 34 private static final Object LOCK = new Object(); 35 private static SimpleLogger instance; 36 37 private StringBuilder sb = new StringBuilder(); 38 private LocalBroadcastManager broadcastManager; 39 40 public static SimpleLogger getInstance(Context context) { 41 synchronized (LOCK) { 42 if (instance == null) { 43 instance = new SimpleLogger(context.getApplicationContext()); 44 } 45 return instance; 46 } 47 } 48 49 private SimpleLogger(Context context) { 50 broadcastManager = LocalBroadcastManager.getInstance(context); 51 } 52 53 public synchronized void log(String msg) { 54 Log.i(TAG, msg); 55 sb.append(msg); 56 sb.append('\n'); 57 if (broadcastManager != null) { 58 Intent intent = new Intent(LOG_INTENT); 59 intent.putExtra("message", msg); 60 broadcastManager.sendBroadcast(intent); 61 } 62 } 63 64 public void registerReceiver(BroadcastReceiver broadcastReceiver) { 65 broadcastManager.registerReceiver(broadcastReceiver, new IntentFilter(LOG_INTENT)); 66 } 67 68 public void unregisterReceiver(BroadcastReceiver broadcastReceiver) { 69 broadcastManager.unregisterReceiver(broadcastReceiver); 70 } 71 72 public String getLogText() { 73 return sb.toString(); 74 } 75 76 public void clear() { 77 sb = new StringBuilder(); 78 } 79 80 } 81