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