Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | using namespace android; |
| 24 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace os { |
| 27 | namespace statsd { |
| 28 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 29 | LogEntryPrinter::LogEntryPrinter(int out) : m_out(out) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 30 | // 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 Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 39 | LogEntryPrinter::~LogEntryPrinter() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 40 | if (m_tags != NULL) { |
| 41 | android_closeEventTagMap(m_tags); |
| 42 | } |
| 43 | android_log_format_free(m_format); |
| 44 | } |
| 45 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 46 | void LogEntryPrinter::OnLogEvent(const log_msg& msg) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 47 | status_t err; |
| 48 | AndroidLogEntry entry; |
| 49 | char buf[1024]; |
| 50 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 51 | err = android_log_processBinaryLogBuffer(&(const_cast<log_msg*>(&msg)->entry_v1), &entry, |
| 52 | m_tags, buf, sizeof(buf)); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 53 | 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 Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 61 | } // namespace statsd |
| 62 | } // namespace os |
| 63 | } // namespace android |