blob: c877b05457958b405e912b2712bdc935be9f471f [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
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070029LogEntryPrinter::LogEntryPrinter(int out)
30 :m_out(out)
31{
32 // Initialize the EventTagMap, which is how we know the names of the numeric event tags.
33 // If this fails, we can't print well, but something will print.
34 m_tags = android_openEventTagMap(NULL);
35
36 // Printing format
37 m_format = android_log_format_new();
38 android_log_setPrintFormat(m_format, FORMAT_THREADTIME);
39}
40
41LogEntryPrinter::~LogEntryPrinter()
42{
43 if (m_tags != NULL) {
44 android_closeEventTagMap(m_tags);
45 }
46 android_log_format_free(m_format);
47}
48
49void
50LogEntryPrinter::OnLogEvent(const log_msg& msg)
51{
52 status_t err;
53 AndroidLogEntry entry;
54 char buf[1024];
55
56 err = android_log_processBinaryLogBuffer(&(const_cast<log_msg*>(&msg)->entry_v1),
57 &entry, m_tags, buf, sizeof(buf));
58 if (err == NO_ERROR) {
59 android_log_printLogLine(m_format, m_out, &entry);
60 } else {
61 printf("log entry: %s\n", buf);
62 fflush(stdout);
63 }
64}
65
Bookatz906a35c2017-09-20 15:26:44 -070066} // namespace statsd
67} // namespace os
68} // namespace android
69