1 /*
2 * Copyright 2023 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 #include "le_audio_log_history.h"
18
19 #include <bluetooth/log.h>
20
21 #include <cstddef>
22 #include <cstdio>
23 #include <ctime>
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 #include "common/circular_buffer.h"
29 #include "common/strings.h"
30 #include "main/shim/dumpsys.h"
31 #include "types/raw_address.h"
32
33 using namespace bluetooth;
34
35 constexpr size_t kMaxLogSize = 255;
36 constexpr size_t kLeAudioLogHistoryBufferSize = 200;
37
38 class TimestampedStringCircularBuffer
39 : public bluetooth::common::TimestampedCircularBuffer<std::string> {
40 public:
TimestampedStringCircularBuffer(size_t size)41 explicit TimestampedStringCircularBuffer(size_t size)
42 : bluetooth::common::TimestampedCircularBuffer<std::string>(size) {}
43
Push(const std::string & s)44 void Push(const std::string& s) {
45 bluetooth::common::TimestampedCircularBuffer<std::string>::Push(s.substr(0, kMaxLogSize));
46 }
47
48 template <typename... Args>
Push(Args...args)49 void Push(Args... args) {
50 char buf[kMaxLogSize];
51 std::snprintf(buf, sizeof(buf), args...);
52 bluetooth::common::TimestampedCircularBuffer<std::string>::Push(std::string(buf));
53 }
54 };
55
56 class LeAudioLogHistoryImpl;
57 LeAudioLogHistoryImpl* instance;
58
59 constexpr size_t kMaxLogHistoryTagLength = 14;
60 constexpr size_t kMaxLogHistoryMsgLength = 44;
61 const std::string kTimeFormat("%Y-%m-%d %H:%M:%S");
62
63 using Record = bluetooth::common::TimestampedEntry<std::string>;
64
65 class LeAudioLogHistoryImpl : public LeAudioLogHistory {
66 public:
~LeAudioLogHistoryImpl(void)67 ~LeAudioLogHistoryImpl(void) { history_.reset(); }
68
LeAudioLogHistoryImpl(void)69 LeAudioLogHistoryImpl(void) {
70 history_ = std::make_shared<TimestampedStringCircularBuffer>(kLeAudioLogHistoryBufferSize);
71 log::assert_that(history_ != nullptr, "assert failed: history_ != nullptr");
72 history_->Push(std::string("Initialized le_audio history"));
73 }
74
Dump(int fd)75 void Dump(int fd) {
76 #define DUMPSYS_TAG "::le_audio"
77
78 LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
79 if (history_ == nullptr) {
80 return;
81 }
82 std::vector<Record> history = history_->Pull();
83 for (auto& record : history) {
84 time_t then = record.timestamp / 1000;
85 struct tm tm;
86 localtime_r(&then, &tm);
87 auto s2 = bluetooth::common::StringFormatTime(kTimeFormat, tm);
88 LOG_DUMPSYS(fd, " %s.%03u %s", s2.c_str(), static_cast<unsigned int>(record.timestamp % 1000),
89 record.entry.c_str());
90 }
91 #undef DUMPSYS_TAG
92 }
93
AddLogHistory(const std::string & tag,int group_id,const RawAddress & addr,const std::string & msg,const std::string & extra)94 void AddLogHistory(const std::string& tag, int group_id, const RawAddress& addr,
95 const std::string& msg, const std::string& extra) {
96 add_logs_history_common(tag, group_id, addr, msg, extra);
97 }
98
AddLogHistory(const std::string & tag,int group_id,const RawAddress & addr,const std::string & msg)99 void AddLogHistory(const std::string& tag, int group_id, const RawAddress& addr,
100 const std::string& msg) {
101 AddLogHistory(tag, group_id, addr, msg, std::string());
102 }
103
104 private:
add_logs_history_common(const std::string & tag,int group_id,const RawAddress & addr,const std::string & msg,const std::string & extra)105 void add_logs_history_common(const std::string& tag, int group_id, const RawAddress& addr,
106 const std::string& msg, const std::string& extra) {
107 if (history_ == nullptr) {
108 log::error("LeAudioLogHistory has not been constructed or already destroyed !");
109 return;
110 }
111
112 history_->Push("%-*s GID %-3d %-*s: %-22s %s", kMaxLogHistoryTagLength,
113 tag.substr(0, kMaxLogHistoryTagLength).c_str(), group_id,
114 kMaxLogHistoryMsgLength, msg.substr(0, kMaxLogHistoryMsgLength).c_str(),
115 addr.ToRedactedStringForLogging().c_str(), extra.c_str());
116 }
117
118 std::shared_ptr<TimestampedStringCircularBuffer> history_{nullptr};
119 };
120
Get(void)121 LeAudioLogHistory* LeAudioLogHistory::Get(void) {
122 if (instance) {
123 return instance;
124 }
125 instance = new LeAudioLogHistoryImpl();
126 return instance;
127 }
128
DebugDump(int fd)129 void LeAudioLogHistory::DebugDump(int fd) {
130 if (instance) {
131 instance->Dump(fd);
132 }
133 }
134
Cleanup(void)135 void LeAudioLogHistory::Cleanup(void) {
136 if (!instance) {
137 return;
138 }
139 auto ptr = instance;
140 instance = nullptr;
141 delete ptr;
142 }
143