1 /*
2  * Copyright 2019 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 com.android.bluetooth.audio_util;
18 
19 import android.util.Log;
20 
21 import com.android.bluetooth.Utils;
22 
23 import com.google.common.collect.EvictingQueue;
24 
25 
26 // This class is to store logs for Audio for given size.
27 public class BTAudioEventLogger {
28     private final String mTitle;
29     private final EvictingQueue<Event> mEvents;
30 
31     // Event class contain timestamp and log context.
32     private class Event {
33         private final String mTimeStamp;
34         private final String mMsg;
35 
Event(String msg)36         Event(String msg) {
37             mTimeStamp = Utils.getLocalTimeString();
38             mMsg = msg;
39         }
40 
toString()41         public String toString() {
42             return (new StringBuilder(mTimeStamp)
43                     .append(" ").append(mMsg).toString());
44         }
45     }
46 
BTAudioEventLogger(int size, String title)47     public BTAudioEventLogger(int size, String title) {
48         mEvents = EvictingQueue.create(size);
49         mTitle = title;
50     }
51 
add(String msg)52     public synchronized void add(String msg) {
53         Event event = new Event(msg);
54         mEvents.add(event);
55     }
56 
logv(String tag, String msg)57     public synchronized void logv(String tag, String msg) {
58         add(msg);
59         Log.v(tag, msg);
60     }
61 
logd(String tag, String msg)62     public synchronized void logd(String tag, String msg) {
63         logd(true, tag, msg);
64     }
65 
logd(boolean debug, String tag, String msg)66     public synchronized void logd(boolean debug, String tag, String msg) {
67         add(msg);
68         if (debug) {
69             Log.d(tag, msg);
70         }
71     }
72 
dump(StringBuilder sb)73     public synchronized void dump(StringBuilder sb) {
74         sb.append("BTAudio ").append(mTitle).append(":\n");
75         for (Event event : mEvents) {
76             sb.append("  ").append(event.toString()).append("\n");
77         }
78     }
79 }
80