• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "event_logger.h"
18 
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <chrono>
22 #include <cstddef>
23 #include <ctime>
24 #include <string>
25 
26 namespace aidl::android::hardware::contexthub {
27 
28 namespace {
29 
30 /**
31  * Returns the time formatted in the local timezone.
32  * The format is similar to the adb logcat format, i.e. `01-31 18:22:51.275`.
33  */
formatLocalTime(int64_t ms)34 std::string formatLocalTime(int64_t ms) {
35   const std::chrono::milliseconds duration(ms);
36   const std::chrono::time_point<std::chrono::system_clock> timePoint(duration);
37   time_t time = std::chrono::system_clock::to_time_t(timePoint);
38   constexpr int kBufferSize = 50;
39   char buffer[kBufferSize];
40   std::strftime(buffer, kBufferSize, "%m-%d %H:%M:%S.", std::localtime(&time));
41   return std::string(buffer) + std::to_string(1000 + ms % 1000).substr(1);
42 }
43 
44 }  // namespace
45 
logNanoappLoad(const NanoappBinary & app,bool success)46 void EventLogger::logNanoappLoad(const NanoappBinary &app, bool success) {
47   std::lock_guard<std::mutex> lock(mQueuesMutex);
48   mNanoappLoads.kick_push({
49       .timestampMs = getTimeMs(),
50       .id = app.nanoappId,
51       .version = app.nanoappVersion,
52       .sizeBytes = app.customBinary.size(),
53       .success = success,
54   });
55 }
56 
logNanoappUnload(int64_t appId,bool success)57 void EventLogger::logNanoappUnload(int64_t appId, bool success) {
58   std::lock_guard<std::mutex> lock(mQueuesMutex);
59   mNanoappUnloads.kick_push({
60       .timestampMs = getTimeMs(),
61       .id = appId,
62       .success = success,
63   });
64 }
65 
logContextHubRestart()66 void EventLogger::logContextHubRestart() {
67   std::lock_guard<std::mutex> lock(mQueuesMutex);
68   mContextHubRestarts.kick_push(getTimeMs());
69 }
70 
logMessageToNanoapp(const ContextHubMessage & message,bool success)71 void EventLogger::logMessageToNanoapp(const ContextHubMessage &message,
72                                       bool success) {
73   std::lock_guard<std::mutex> lock(mQueuesMutex);
74   mMsgToNanoapp.kick_push({
75       .timestampMs = getTimeMs(),
76       .id = message.nanoappId,
77       .sizeBytes = message.messageBody.size(),
78       .success = success,
79   });
80 }
81 
logMessageFromNanoapp(const::chre::fbs::NanoappMessageT & message)82 void EventLogger::logMessageFromNanoapp(
83     const ::chre::fbs::NanoappMessageT &message) {
84   std::lock_guard<std::mutex> lock(mQueuesMutex);
85   mMsgFromNanoapp.kick_push({
86       .timestampMs = getTimeMs(),
87       .id = static_cast<int64_t>(message.app_id),
88       .sizeBytes = message.message.size(),
89   });
90 }
91 
logMessageFromNanoapp(const ContextHubMessage & message)92 void EventLogger::logMessageFromNanoapp(const ContextHubMessage &message) {
93   std::lock_guard<std::mutex> lock(mQueuesMutex);
94   mMsgFromNanoapp.kick_push({
95       .timestampMs = getTimeMs(),
96       .id = message.nanoappId,
97       .sizeBytes = message.messageBody.size(),
98   });
99 }
100 
dump() const101 std::string EventLogger::dump() const {
102   constexpr int kBufferSize = 100;
103   char buffer[kBufferSize];
104   std::string logs;
105   std::lock_guard<std::mutex> lock(mQueuesMutex);
106 
107   logs.append("\nNanoapp loads:\n");
108   for (const NanoappLoad &load : mNanoappLoads) {
109     // Use snprintf because std::format is not available and fmtlib {:x} format
110     // crashes.
111     if (snprintf(buffer, kBufferSize,
112                  "  %s id 0x%" PRIx64 " version 0x%" PRIx32 " size %zu"
113                  " status %s\n",
114                  formatLocalTime(load.timestampMs).c_str(), load.id,
115                  load.version, load.sizeBytes,
116                  load.success ? "ok" : "fail") > 0) {
117       logs.append(buffer);
118     }
119   }
120 
121   logs.append("\nNanoapp unloads:\n");
122   for (const NanoappUnload &unload : mNanoappUnloads) {
123     if (snprintf(buffer, kBufferSize, "  %s id 0x%" PRIx64 " status %s\n",
124                  formatLocalTime(unload.timestampMs).c_str(), unload.id,
125                  unload.success ? "ok" : "fail") > 0) {
126       logs.append(buffer);
127     }
128   }
129 
130   logs.append("\nMessages to Nanoapps:\n");
131   for (const NanoappMessage &msg : mMsgToNanoapp) {
132     if (snprintf(buffer, kBufferSize,
133                  "  %s to id 0x%" PRIx64 " size %zu status %s\n",
134                  formatLocalTime(msg.timestampMs).c_str(), msg.id,
135                  msg.sizeBytes, msg.success ? "ok" : "fail") > 0) {
136       logs.append(buffer);
137     }
138   }
139 
140   logs.append("\nMessages from Nanoapps:\n");
141   for (const NanoappMessage &msg : mMsgFromNanoapp) {
142     if (snprintf(buffer, kBufferSize, "  %s from id 0x%" PRIx64 " size %zu\n",
143                  formatLocalTime(msg.timestampMs).c_str(), msg.id,
144                  msg.sizeBytes) > 0) {
145       logs.append(buffer);
146     }
147   }
148 
149   logs.append("\nContext hub restarts:\n");
150   for (const int64_t &ms : mContextHubRestarts) {
151     if (snprintf(buffer, kBufferSize, "  %s\n", formatLocalTime(ms).c_str()) >
152         0) {
153       logs.append(buffer);
154     }
155   }
156 
157   return logs;
158 }
159 
getTimeMs() const160 int64_t EventLogger::getTimeMs() const {
161   if (mNowMs.has_value()) {
162     return mNowMs.value();
163   }
164   struct timespec now;
165   clock_gettime(CLOCK_REALTIME, &now);
166   return 1000 * now.tv_sec + static_cast<int64_t>(now.tv_nsec / 1e6);
167 }
168 
169 }  // namespace aidl::android::hardware::contexthub