blob: 63465b094da2489048c7699fb19ed110b2ddfc01 [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
2 * Copyright (C) 2017 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 <LogEntryPrinter.h>
18
19#include <log/event_tag_map.h>
20#include <log/logprint.h>
21#include <utils/Errors.h>
22
23using namespace android;
24
Bookatz906a35c2017-09-20 15:26:44 -070025namespace android {
26namespace os {
27namespace statsd {
28
Yao Chenef99c4f2017-09-22 16:26:54 -070029LogEntryPrinter::LogEntryPrinter(int out) : m_out(out) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070030 // Initialize the EventTagMap, which is how we know the names of the numeric event tags.
31 // If this fails, we can't print well, but something will print.
32 m_tags = android_openEventTagMap(NULL);
33
34 // Printing format
35 m_format = android_log_format_new();
36 android_log_setPrintFormat(m_format, FORMAT_THREADTIME);
37}
38
Yao Chenef99c4f2017-09-22 16:26:54 -070039LogEntryPrinter::~LogEntryPrinter() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070040 if (m_tags != NULL) {
41 android_closeEventTagMap(m_tags);
42 }
43 android_log_format_free(m_format);
44}
45
Yao Chenef99c4f2017-09-22 16:26:54 -070046void LogEntryPrinter::OnLogEvent(const log_msg& msg) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070047 status_t err;
48 AndroidLogEntry entry;
49 char buf[1024];
50
Yao Chenef99c4f2017-09-22 16:26:54 -070051 err = android_log_processBinaryLogBuffer(&(const_cast<log_msg*>(&msg)->entry_v1), &entry,
52 m_tags, buf, sizeof(buf));
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070053 if (err == NO_ERROR) {
54 android_log_printLogLine(m_format, m_out, &entry);
55 } else {
56 printf("log entry: %s\n", buf);
57 fflush(stdout);
58 }
59}
60
Yao Chenef99c4f2017-09-22 16:26:54 -070061} // namespace statsd
62} // namespace os
63} // namespace android