Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1 | /* |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2006-2017 The Android Open Source Project |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 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 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 | |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 18 | #include <dirent.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 20 | #include <error.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 22 | #include <getopt.h> |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 23 | #include <math.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 24 | #include <sched.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 25 | #include <stdarg.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 28 | #include <string.h> |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 29 | #include <sys/cdefs.h> |
Jaegeuk Kim | 5327d93 | 2019-07-04 18:09:38 -0700 | [diff] [blame] | 30 | #include <sys/ioctl.h> |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 31 | #include <sys/resource.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 33 | #include <sys/types.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 36 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 37 | #include <memory> |
Elliott Hughes | addd852 | 2019-08-08 08:53:59 -0700 | [diff] [blame] | 38 | #include <regex> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 39 | #include <string> |
Wei Wang | c27d481 | 2018-09-05 11:05:57 -0700 | [diff] [blame] | 40 | #include <utility> |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 41 | #include <vector> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 43 | #include <android-base/file.h> |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 44 | #include <android-base/macros.h> |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 45 | #include <android-base/parseint.h> |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 46 | #include <android-base/properties.h> |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 47 | #include <android-base/stringprintf.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 48 | #include <android-base/strings.h> |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 49 | #include <android-base/unique_fd.h> |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 50 | #include <android/log.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 51 | #include <log/event_tag_map.h> |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 52 | #include <log/log_id.h> |
Tom Cherry | c5c6d7d | 2020-04-17 09:38:55 -0700 | [diff] [blame^] | 53 | #include <log/log_read.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 54 | #include <log/logprint.h> |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 55 | #include <private/android_logger.h> |
Suren Baghdasaryan | 0284333 | 2018-12-21 12:30:16 -0800 | [diff] [blame] | 56 | #include <processgroup/sched_policy.h> |
Elliott Hughes | b9e53b4 | 2016-02-17 11:58:01 -0800 | [diff] [blame] | 57 | #include <system/thread_defs.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | #define DEFAULT_MAX_ROTATED_LOGS 4 |
| 60 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 61 | using android::base::Join; |
| 62 | using android::base::ParseByteCount; |
| 63 | using android::base::ParseUint; |
| 64 | using android::base::Split; |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 65 | using android::base::StringPrintf; |
| 66 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 67 | class Logcat { |
| 68 | public: |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 69 | int Run(int argc, char** argv); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 70 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 71 | private: |
| 72 | void RotateLogs(); |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 73 | void ProcessBuffer(struct log_msg* buf); |
| 74 | void PrintDividers(log_id_t log_id, bool print_dividers); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 75 | void SetupOutputAndSchedulingPolicy(bool blocking); |
| 76 | int SetLogFormat(const char* format_string); |
| 77 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 78 | // Used for all options |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 79 | android::base::unique_fd output_fd_{dup(STDOUT_FILENO)}; |
| 80 | std::unique_ptr<AndroidLogFormat, decltype(&android_log_format_free)> logformat_{ |
| 81 | android_log_format_new(), &android_log_format_free}; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 82 | |
| 83 | // For logging to a file and log rotation |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 84 | const char* output_file_name_ = nullptr; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 85 | size_t log_rotate_size_kb_ = 0; // 0 means "no log rotation" |
| 86 | size_t max_rotated_logs_ = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded" |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 87 | size_t out_byte_count_ = 0; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 88 | |
| 89 | // For binary log buffers |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 90 | int print_binary_ = 0; |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 91 | std::unique_ptr<EventTagMap, decltype(&android_closeEventTagMap)> event_tag_map_{ |
| 92 | nullptr, &android_closeEventTagMap}; |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 93 | bool has_opened_event_tag_map_ = false; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 94 | |
| 95 | // For the related --regex, --max-count, --print |
| 96 | std::unique_ptr<std::regex> regex_; |
| 97 | size_t max_count_ = 0; // 0 means "infinite" |
| 98 | size_t print_count_ = 0; |
| 99 | bool print_it_anyways_ = false; |
| 100 | |
| 101 | // For PrintDividers() |
| 102 | log_id_t last_printed_id_ = LOG_ID_MAX; |
| 103 | bool printed_start_[LOG_ID_MAX] = {}; |
| 104 | |
| 105 | bool debug_ = false; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
Jaegeuk Kim | 5327d93 | 2019-07-04 18:09:38 -0700 | [diff] [blame] | 108 | #ifndef F2FS_IOC_SET_PIN_FILE |
| 109 | #define F2FS_IOCTL_MAGIC 0xf5 |
| 110 | #define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32) |
| 111 | #endif |
| 112 | |
| 113 | static int openLogFile(const char* pathname, size_t sizeKB) { |
Mike Ma | a7fb095 | 2020-01-17 18:01:03 -0800 | [diff] [blame] | 114 | int fd = open(pathname, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP); |
Jaegeuk Kim | 5327d93 | 2019-07-04 18:09:38 -0700 | [diff] [blame] | 115 | if (fd < 0) { |
| 116 | return fd; |
| 117 | } |
| 118 | |
| 119 | // no need to check errors |
| 120 | __u32 set = 1; |
| 121 | ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set); |
| 122 | fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, (sizeKB << 10)); |
| 123 | return fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 126 | void Logcat::RotateLogs() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 127 | // Can't rotate logs if we're not outputting to a file |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 128 | if (!output_file_name_) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 130 | output_fd_.reset(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 132 | // Compute the maximum number of digits needed to count up to |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 133 | // maxRotatedLogs in decimal. eg: |
| 134 | // maxRotatedLogs == 30 |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 135 | // -> log10(30) == 1.477 |
| 136 | // -> maxRotationCountDigits == 2 |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 137 | int max_rotation_count_digits = |
| 138 | max_rotated_logs_ > 0 ? (int)(floor(log10(max_rotated_logs_) + 1)) : 0; |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 139 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 140 | for (int i = max_rotated_logs_; i > 0; i--) { |
| 141 | std::string file1 = |
| 142 | StringPrintf("%s.%.*d", output_file_name_, max_rotation_count_digits, i); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 144 | std::string file0; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 145 | if (!(i - 1)) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 146 | file0 = output_file_name_; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | } else { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 148 | file0 = StringPrintf("%s.%.*d", output_file_name_, max_rotation_count_digits, i - 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 151 | if (!file0.length() || !file1.length()) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 152 | perror("while rotating log files"); |
| 153 | break; |
| 154 | } |
| 155 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 156 | int err = rename(file0.c_str(), file1.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | |
| 158 | if (err < 0 && errno != ENOENT) { |
| 159 | perror("while rotating log files"); |
| 160 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 163 | output_fd_.reset(openLogFile(output_file_name_, log_rotate_size_kb_)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 165 | if (!output_fd_.ok()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 166 | error(EXIT_FAILURE, errno, "Couldn't open output file"); |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 167 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 168 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 169 | out_byte_count_ = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 172 | void Logcat::ProcessBuffer(struct log_msg* buf) { |
Mathias Agopian | 5084452 | 2010-03-17 16:10:26 -0700 | [diff] [blame] | 173 | int bytesWritten = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 174 | int err; |
| 175 | AndroidLogEntry entry; |
| 176 | char binaryMsgBuf[1024]; |
| 177 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 178 | bool is_binary = |
| 179 | buf->id() == LOG_ID_EVENTS || buf->id() == LOG_ID_STATS || buf->id() == LOG_ID_SECURITY; |
| 180 | |
| 181 | if (is_binary) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 182 | if (!event_tag_map_ && !has_opened_event_tag_map_) { |
| 183 | event_tag_map_.reset(android_openEventTagMap(nullptr)); |
| 184 | has_opened_event_tag_map_ = true; |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 185 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 186 | err = android_log_processBinaryLogBuffer(&buf->entry, &entry, event_tag_map_.get(), |
Tom Cherry | 441054a | 2019-10-15 16:53:11 -0700 | [diff] [blame] | 187 | binaryMsgBuf, sizeof(binaryMsgBuf)); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 188 | // printf(">>> pri=%d len=%d msg='%s'\n", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | // entry.priority, entry.messageLen, entry.message); |
| 190 | } else { |
Tom Cherry | 441054a | 2019-10-15 16:53:11 -0700 | [diff] [blame] | 191 | err = android_log_processLogBuffer(&buf->entry, &entry); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 193 | if (err < 0 && !debug_) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 195 | if (android_log_shouldPrintLine(logformat_.get(), std::string(entry.tag, entry.tagLen).c_str(), |
| 196 | entry.priority)) { |
| 197 | bool match = !regex_ || |
| 198 | std::regex_search(entry.message, entry.message + entry.messageLen, *regex_); |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 199 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 200 | print_count_ += match; |
| 201 | if (match || print_it_anyways_) { |
| 202 | bytesWritten = android_log_printLogLine(logformat_.get(), output_fd_.get(), &entry); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 203 | |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 204 | if (bytesWritten < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 205 | error(EXIT_FAILURE, 0, "Output error."); |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 206 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 207 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 210 | out_byte_count_ += bytesWritten; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 212 | if (log_rotate_size_kb_ > 0 && (out_byte_count_ / 1024) >= log_rotate_size_kb_) { |
| 213 | RotateLogs(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 217 | void Logcat::PrintDividers(log_id_t log_id, bool print_dividers) { |
| 218 | if (log_id == last_printed_id_ || print_binary_) { |
| 219 | return; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 220 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 221 | if (!printed_start_[log_id] || print_dividers) { |
| 222 | if (dprintf(output_fd_.get(), "--------- %s %s\n", |
| 223 | printed_start_[log_id] ? "switch to" : "beginning of", |
| 224 | android_log_id_to_name(log_id)) < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 225 | error(EXIT_FAILURE, errno, "Output error"); |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | last_printed_id_ = log_id; |
| 229 | printed_start_[log_id] = true; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 232 | void Logcat::SetupOutputAndSchedulingPolicy(bool blocking) { |
| 233 | if (!output_file_name_) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 234 | |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 235 | if (blocking) { |
| 236 | // Lower priority and set to batch scheduling if we are saving |
| 237 | // the logs into files and taking continuous content. |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 238 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 239 | fprintf(stderr, "failed to set background scheduling policy\n"); |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 242 | struct sched_param param = {}; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 243 | if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) { |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 244 | fprintf(stderr, "failed to set to batch scheduler\n"); |
| 245 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 246 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 247 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { |
| 248 | fprintf(stderr, "failed set to priority\n"); |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 249 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 250 | } |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 251 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 252 | output_fd_.reset(openLogFile(output_file_name_, log_rotate_size_kb_)); |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 253 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 254 | if (!output_fd_.ok()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 255 | error(EXIT_FAILURE, errno, "Couldn't open output file"); |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | struct stat statbuf; |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 259 | if (fstat(output_fd_.get(), &statbuf) == -1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 260 | error(EXIT_FAILURE, errno, "Couldn't get output file stat"); |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 263 | if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 264 | error(EXIT_FAILURE, 0, "Invalid output file stat."); |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 267 | out_byte_count_ = statbuf.st_size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 270 | // clang-format off |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 271 | static void show_help() { |
| 272 | const char* cmd = getprogname(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 273 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 274 | fprintf(stderr, "Usage: %s [options] [filterspecs]\n", cmd); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 275 | |
Tom Cherry | 4169732 | 2019-11-21 10:31:06 -0800 | [diff] [blame] | 276 | fprintf(stderr, R"init( |
| 277 | General options: |
| 278 | -b, --buffer=<buffer> Request alternate ring buffer(s): |
| 279 | main system radio events crash default all |
| 280 | Additionally, 'kernel' for userdebug and eng builds, and |
| 281 | 'security' for Device Owner installations. |
| 282 | Multiple -b parameters or comma separated list of buffers are |
| 283 | allowed. Buffers are interleaved. |
| 284 | Default -b main,system,crash,kernel. |
| 285 | -L, --last Dump logs from prior to last reboot from pstore. |
| 286 | -c, --clear Clear (flush) the entire log and exit. |
| 287 | if -f is specified, clear the specified file and its related rotated |
| 288 | log files instead. |
| 289 | if -L is specified, clear pstore log instead. |
| 290 | -d Dump the log and then exit (don't block). |
| 291 | --pid=<pid> Only print logs from the given pid. |
| 292 | --wrap Sleep for 2 hours or when buffer about to wrap whichever |
| 293 | comes first. Improves efficiency of polling by providing |
| 294 | an about-to-wrap wakeup. |
| 295 | |
| 296 | Formatting: |
| 297 | -v, --format=<format> Sets log print format verb and adverbs, where <format> is one of: |
| 298 | brief help long process raw tag thread threadtime time |
| 299 | Modifying adverbs can be added: |
| 300 | color descriptive epoch monotonic printable uid usec UTC year zone |
| 301 | Multiple -v parameters or comma separated list of format and format |
| 302 | modifiers are allowed. |
| 303 | -D, --dividers Print dividers between each log buffer. |
| 304 | -B, --binary Output the log in binary. |
| 305 | |
| 306 | Outfile files: |
| 307 | -f, --file=<file> Log to file instead of stdout. |
| 308 | -r, --rotate-kbytes=<n> Rotate log every <n> kbytes. Requires -f option. |
| 309 | -n, --rotate-count=<count> Sets max number of rotated logs to <count>, default 4. |
| 310 | --id=<id> If the signature <id> for logging to file changes, then clear the |
| 311 | associated files and continue. |
| 312 | |
| 313 | Logd control: |
| 314 | These options send a control message to the logd daemon on device, print its return message if |
| 315 | applicable, then exit. They are incompatible with -L, as these attributes do not apply to pstore. |
| 316 | -g, --buffer-size Get the size of the ring buffers within logd. |
| 317 | -G, --buffer-size=<size> Set size of a ring buffer in logd. May suffix with K or M. |
| 318 | This can individually control each buffer's size with -b. |
| 319 | -S, --statistics Output statistics. |
| 320 | --pid can be used to provide pid specific stats. |
| 321 | -p, --prune Print prune white and ~black list. Service is specified as UID, |
| 322 | UID/PID or /PID. Weighed for quicker pruning if prefix with ~, |
| 323 | otherwise weighed for longevity if unadorned. All other pruning |
| 324 | activity is oldest first. Special case ~! represents an automatic |
| 325 | quicker pruning for the noisiest UID as determined by the current |
| 326 | statistics. |
| 327 | -P, --prune='<list> ...' Set prune white and ~black list, using same format as listed above. |
| 328 | Must be quoted. |
| 329 | |
| 330 | Filtering: |
| 331 | -s Set default filter to silent. Equivalent to filterspec '*:S' |
| 332 | -e, --regex=<expr> Only print lines where the log message matches <expr> where <expr> is |
| 333 | an ECMAScript regular expression. |
| 334 | -m, --max-count=<count> Quit after printing <count> lines. This is meant to be paired with |
| 335 | --regex, but will work on its own. |
| 336 | --print This option is only applicable when --regex is set and only useful if |
| 337 | --max-count is also provided. |
| 338 | With --print, logcat will print all messages even if they do not |
| 339 | match the regex. Logcat will quit after printing the max-count number |
| 340 | of lines that match the regex. |
| 341 | -t <count> Print only the most recent <count> lines (implies -d). |
| 342 | -t '<time>' Print the lines since specified time (implies -d). |
| 343 | -T <count> Print only the most recent <count> lines (does not imply -d). |
| 344 | -T '<time>' Print the lines since specified time (not imply -d). |
| 345 | count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...' |
| 346 | 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format. |
| 347 | )init"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 349 | fprintf(stderr, "\nfilterspecs are a series of \n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 350 | " <tag>[:priority]\n\n" |
| 351 | "where <tag> is a log component tag (or * for all) and priority is:\n" |
Mark Salyzyn | bba894a | 2015-03-09 09:32:56 -0700 | [diff] [blame] | 352 | " V Verbose (default for <tag>)\n" |
| 353 | " D Debug (default for '*')\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 354 | " I Info\n" |
| 355 | " W Warn\n" |
| 356 | " E Error\n" |
| 357 | " F Fatal\n" |
Mark Salyzyn | bba894a | 2015-03-09 09:32:56 -0700 | [diff] [blame] | 358 | " S Silent (suppress all output)\n" |
| 359 | "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n" |
| 360 | "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n" |
| 361 | "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n" |
| 362 | "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n" |
| 363 | "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n" |
Mark Salyzyn | 649fc60 | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 364 | "or defaults to \"threadtime\"\n\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 367 | static void show_format_help() { |
| 368 | fprintf(stderr, |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 369 | "-v <format>, --format=<format> options:\n" |
| 370 | " Sets log print format verb and adverbs, where <format> is:\n" |
| 371 | " brief long process raw tag thread threadtime time\n" |
| 372 | " and individually flagged modifying adverbs can be added:\n" |
| 373 | " color descriptive epoch monotonic printable uid usec UTC year zone\n" |
| 374 | "\nSingle format verbs:\n" |
| 375 | " brief — Display priority/tag and PID of the process issuing the message.\n" |
| 376 | " long — Display all metadata fields, separate messages with blank lines.\n" |
| 377 | " process — Display PID only.\n" |
| 378 | " raw — Display the raw log message, with no other metadata fields.\n" |
| 379 | " tag — Display the priority/tag only.\n" |
Mark Salyzyn | c74f8d9 | 2017-05-15 13:40:33 -0700 | [diff] [blame] | 380 | " thread — Display priority, PID and TID of process issuing the message.\n" |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 381 | " threadtime — Display the date, invocation time, priority, tag, and the PID\n" |
| 382 | " and TID of the thread issuing the message. (the default format).\n" |
| 383 | " time — Display the date, invocation time, priority/tag, and PID of the\n" |
| 384 | " process issuing the message.\n" |
| 385 | "\nAdverb modifiers can be used in combination:\n" |
| 386 | " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n" |
| 387 | " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n" |
| 388 | " descriptive — events logs only, descriptions from event-log-tags database.\n" |
| 389 | " epoch — Display time as seconds since Jan 1 1970.\n" |
| 390 | " monotonic — Display time as cpu seconds since last boot.\n" |
| 391 | " printable — Ensure that any binary logging content is escaped.\n" |
| 392 | " uid — If permitted, display the UID or Android ID of logged process.\n" |
| 393 | " usec — Display time down the microsecond precision.\n" |
| 394 | " UTC — Display time as UTC.\n" |
| 395 | " year — Add the year to the displayed time.\n" |
| 396 | " zone — Add the local timezone to the displayed time.\n" |
| 397 | " \"<zone>\" — Print using this public named timezone (experimental).\n\n" |
| 398 | ); |
| 399 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 400 | // clang-format on |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 401 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 402 | int Logcat::SetLogFormat(const char* format_string) { |
| 403 | AndroidLogPrintFormat format = android_log_formatFromString(format_string); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 404 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 405 | // invalid string? |
| 406 | if (format == FORMAT_OFF) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 408 | return android_log_setPrintFormat(logformat_.get(), format); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Wei Wang | c27d481 | 2018-09-05 11:05:57 -0700 | [diff] [blame] | 411 | static std::pair<unsigned long, const char*> format_of_size(unsigned long value) { |
| 412 | static const char multipliers[][3] = {{""}, {"Ki"}, {"Mi"}, {"Gi"}}; |
| 413 | size_t i; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 414 | for (i = 0; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 415 | (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024); |
| 416 | value /= 1024, ++i) |
| 417 | ; |
Wei Wang | c27d481 | 2018-09-05 11:05:57 -0700 | [diff] [blame] | 418 | return std::make_pair(value, multipliers[i]); |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 421 | static char* parseTime(log_time& t, const char* cp) { |
| 422 | char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q"); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 423 | if (ep) return ep; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 424 | ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q"); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 425 | if (ep) return ep; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 426 | return t.strptime(cp, "%s.%q"); |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 427 | } |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 428 | |
Mark Salyzyn | 3196106 | 2016-08-04 07:43:46 -0700 | [diff] [blame] | 429 | // Find last logged line in <outputFileName>, or <outputFileName>.1 |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 430 | static log_time lastLogTime(const char* outputFileName) { |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 431 | log_time retval(log_time::EPOCH); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 432 | if (!outputFileName) return retval; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 433 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 434 | std::string directory; |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 435 | const char* file = strrchr(outputFileName, '/'); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 436 | if (!file) { |
| 437 | directory = "."; |
| 438 | file = outputFileName; |
| 439 | } else { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 440 | directory = std::string(outputFileName, file - outputFileName); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 441 | ++file; |
| 442 | } |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 443 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 444 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()), |
| 445 | closedir); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 446 | if (!dir.get()) return retval; |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 447 | |
Mark Salyzyn | 3196106 | 2016-08-04 07:43:46 -0700 | [diff] [blame] | 448 | log_time now(android_log_clockid()); |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 449 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 450 | size_t len = strlen(file); |
| 451 | log_time modulo(0, NS_PER_SEC); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 452 | struct dirent* dp; |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 453 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 454 | while (!!(dp = readdir(dir.get()))) { |
| 455 | if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) || |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 456 | (dp->d_name[len] && ((dp->d_name[len] != '.') || |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 457 | (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) { |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 458 | continue; |
| 459 | } |
| 460 | |
| 461 | std::string file_name = directory; |
| 462 | file_name += "/"; |
| 463 | file_name += dp->d_name; |
| 464 | std::string file; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 465 | if (!android::base::ReadFileToString(file_name, &file)) continue; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 466 | |
| 467 | bool found = false; |
| 468 | for (const auto& line : android::base::Split(file, "\n")) { |
| 469 | log_time t(log_time::EPOCH); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 470 | char* ep = parseTime(t, line.c_str()); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 471 | if (!ep || (*ep != ' ')) continue; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 472 | // determine the time precision of the logs (eg: msec or usec) |
| 473 | for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) { |
| 474 | if (t.tv_nsec % (mod * 10)) { |
| 475 | modulo.tv_nsec = mod; |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | // We filter any times later than current as we may not have the |
| 480 | // year stored with each log entry. Also, since it is possible for |
| 481 | // entries to be recorded out of order (very rare) we select the |
| 482 | // maximum we find just in case. |
| 483 | if ((t < now) && (t > retval)) { |
| 484 | retval = t; |
| 485 | found = true; |
| 486 | } |
| 487 | } |
| 488 | // We count on the basename file to be the definitive end, so stop here. |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 489 | if (!dp->d_name[len] && found) break; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 490 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 491 | if (retval == log_time::EPOCH) return retval; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 492 | // tail_time prints matching or higher, round up by the modulo to prevent |
| 493 | // a replay of the last entry we have just checked. |
| 494 | retval += modulo; |
| 495 | return retval; |
| 496 | } |
| 497 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 498 | void ReportErrorName(const std::string& name, bool allow_security, |
| 499 | std::vector<std::string>* errors) { |
| 500 | if (allow_security || name != "security") { |
| 501 | errors->emplace_back(name); |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 502 | } |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 503 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 504 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 505 | int Logcat::Run(int argc, char** argv) { |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 506 | bool hasSetLogFormat = false; |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 507 | bool clearLog = false; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 508 | bool security_buffer_selected = |
| 509 | false; // Do not report errors on the security buffer unless it is explicitly named. |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 510 | bool getLogSize = false; |
| 511 | bool getPruneList = false; |
| 512 | bool printStatistics = false; |
| 513 | bool printDividers = false; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 514 | unsigned long setLogSize = 0; |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 515 | const char* setPruneList = nullptr; |
| 516 | const char* setId = nullptr; |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 517 | int mode = 0; |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 518 | std::string forceFilters; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 519 | size_t tail_lines = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 520 | log_time tail_time(log_time::EPOCH); |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 521 | size_t pid = 0; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 522 | bool got_t = false; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 523 | unsigned id_mask = 0; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 524 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 525 | if (argc == 2 && !strcmp(argv[1], "--help")) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 526 | show_help(); |
| 527 | return EXIT_SUCCESS; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 530 | // meant to catch comma-delimited values, but cast a wider |
| 531 | // net for stability dealing with possible mistaken inputs. |
| 532 | static const char delimiters[] = ",:; \t\n\r\f"; |
| 533 | |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 534 | optind = 0; |
| 535 | while (true) { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 536 | int option_index = 0; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 537 | // list of long-argument only strings for later comparison |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 538 | static const char pid_str[] = "pid"; |
Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 539 | static const char debug_str[] = "debug"; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 540 | static const char id_str[] = "id"; |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 541 | static const char wrap_str[] = "wrap"; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 542 | static const char print_str[] = "print"; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 543 | // clang-format off |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 544 | static const struct option long_options[] = { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 545 | { "binary", no_argument, nullptr, 'B' }, |
| 546 | { "buffer", required_argument, nullptr, 'b' }, |
| 547 | { "buffer-size", optional_argument, nullptr, 'g' }, |
| 548 | { "clear", no_argument, nullptr, 'c' }, |
| 549 | { debug_str, no_argument, nullptr, 0 }, |
| 550 | { "dividers", no_argument, nullptr, 'D' }, |
| 551 | { "file", required_argument, nullptr, 'f' }, |
| 552 | { "format", required_argument, nullptr, 'v' }, |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 553 | // hidden and undocumented reserved alias for --regex |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 554 | { "grep", required_argument, nullptr, 'e' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 555 | // hidden and undocumented reserved alias for --max-count |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 556 | { "head", required_argument, nullptr, 'm' }, |
Mark Salyzyn | e74e51d | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 557 | { "help", no_argument, nullptr, 'h' }, |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 558 | { id_str, required_argument, nullptr, 0 }, |
| 559 | { "last", no_argument, nullptr, 'L' }, |
| 560 | { "max-count", required_argument, nullptr, 'm' }, |
| 561 | { pid_str, required_argument, nullptr, 0 }, |
| 562 | { print_str, no_argument, nullptr, 0 }, |
| 563 | { "prune", optional_argument, nullptr, 'p' }, |
| 564 | { "regex", required_argument, nullptr, 'e' }, |
| 565 | { "rotate-count", required_argument, nullptr, 'n' }, |
| 566 | { "rotate-kbytes", required_argument, nullptr, 'r' }, |
| 567 | { "statistics", no_argument, nullptr, 'S' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 568 | // hidden and undocumented reserved alias for -t |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 569 | { "tail", required_argument, nullptr, 't' }, |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 570 | // support, but ignore and do not document, the optional argument |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 571 | { wrap_str, optional_argument, nullptr, 0 }, |
| 572 | { nullptr, 0, nullptr, 0 } |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 573 | }; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 574 | // clang-format on |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 575 | |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 576 | int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options, |
| 577 | &option_index); |
| 578 | if (c == -1) break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 579 | |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 580 | switch (c) { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 581 | case 0: |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 582 | // only long options |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 583 | if (long_options[option_index].name == pid_str) { |
Steven Moreland | b0e867a | 2019-08-02 10:13:57 -0700 | [diff] [blame] | 584 | if (pid != 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 585 | error(EXIT_FAILURE, 0, "Only one --pid argument can be provided."); |
Steven Moreland | b0e867a | 2019-08-02 10:13:57 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 588 | if (!ParseUint(optarg, &pid) || pid < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 589 | error(EXIT_FAILURE, 0, "%s %s out of range.", |
| 590 | long_options[option_index].name, optarg); |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 591 | } |
| 592 | break; |
| 593 | } |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 594 | if (long_options[option_index].name == wrap_str) { |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 595 | mode |= ANDROID_LOG_WRAP | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 596 | // ToDo: implement API that supports setting a wrap timeout |
| 597 | size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 598 | if (optarg && (!ParseUint(optarg, &dummy) || dummy < 1)) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 599 | error(EXIT_FAILURE, 0, "%s %s out of range.", |
| 600 | long_options[option_index].name, optarg); |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 601 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 602 | if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) { |
| 603 | fprintf(stderr, "WARNING: %s %u seconds, ignoring %zu\n", |
| 604 | long_options[option_index].name, ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, |
| 605 | dummy); |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 606 | } |
| 607 | break; |
| 608 | } |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 609 | if (long_options[option_index].name == print_str) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 610 | print_it_anyways_ = true; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 611 | break; |
| 612 | } |
Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 613 | if (long_options[option_index].name == debug_str) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 614 | debug_ = true; |
Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 615 | break; |
| 616 | } |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 617 | if (long_options[option_index].name == id_str) { |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 618 | setId = (optarg && optarg[0]) ? optarg : nullptr; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 619 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 620 | break; |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 621 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 622 | case 's': |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 623 | // default to all silent |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 624 | android_log_addFilterRule(logformat_.get(), "*:s"); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 625 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 626 | |
| 627 | case 'c': |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 628 | clearLog = true; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 629 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 630 | |
Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 631 | case 'L': |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 632 | mode |= ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 633 | break; |
Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 634 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 635 | case 'd': |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 636 | mode |= ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 637 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 638 | |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 639 | case 't': |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 640 | got_t = true; |
Tom Cherry | 907b2d0 | 2020-03-23 13:40:10 -0700 | [diff] [blame] | 641 | mode |= ANDROID_LOG_NONBLOCK; |
Chih-Hung Hsieh | 502f486 | 2018-09-13 11:08:41 -0700 | [diff] [blame] | 642 | FALLTHROUGH_INTENDED; |
Mark Salyzyn | 5d3d1f1 | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 643 | case 'T': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 644 | if (strspn(optarg, "0123456789") != strlen(optarg)) { |
| 645 | char* cp = parseTime(tail_time, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 646 | if (!cp) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 647 | error(EXIT_FAILURE, 0, "-%c '%s' not in time format.", c, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 648 | } |
| 649 | if (*cp) { |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 650 | char ch = *cp; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 651 | *cp = '\0'; |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 652 | fprintf(stderr, "WARNING: -%c '%s' '%c%s' time truncated\n", c, optarg, ch, |
| 653 | cp + 1); |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 654 | *cp = ch; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 655 | } |
| 656 | } else { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 657 | if (!ParseUint(optarg, &tail_lines) || tail_lines < 1) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 658 | fprintf(stderr, "WARNING: -%c %s invalid, setting to 1\n", c, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 659 | tail_lines = 1; |
| 660 | } |
| 661 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 662 | break; |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 663 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 664 | case 'D': |
| 665 | printDividers = true; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 666 | break; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 667 | |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 668 | case 'e': |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 669 | regex_.reset(new std::regex(optarg)); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 670 | break; |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 671 | |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 672 | case 'm': { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 673 | if (!ParseUint(optarg, &max_count_) || max_count_ < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 674 | error(EXIT_FAILURE, 0, "-%c '%s' isn't an integer greater than zero.", c, |
| 675 | optarg); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 676 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 677 | } break; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 678 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 679 | case 'g': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 680 | if (!optarg) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 681 | getLogSize = true; |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 682 | break; |
| 683 | } |
Chih-Hung Hsieh | 502f486 | 2018-09-13 11:08:41 -0700 | [diff] [blame] | 684 | FALLTHROUGH_INTENDED; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 685 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 686 | case 'G': { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 687 | if (!ParseByteCount(optarg, &setLogSize) || setLogSize < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 688 | error(EXIT_FAILURE, 0, "-G must be specified as <num><multiplier>."); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 689 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 690 | } break; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 691 | |
| 692 | case 'p': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 693 | if (!optarg) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 694 | getPruneList = true; |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 695 | break; |
| 696 | } |
Chih-Hung Hsieh | 502f486 | 2018-09-13 11:08:41 -0700 | [diff] [blame] | 697 | FALLTHROUGH_INTENDED; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 698 | |
| 699 | case 'P': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 700 | setPruneList = optarg; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 701 | break; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 702 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 703 | case 'b': |
| 704 | for (const auto& buffer : Split(optarg, delimiters)) { |
| 705 | if (buffer == "default") { |
| 706 | id_mask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) | (1 << LOG_ID_CRASH); |
| 707 | } else if (buffer == "all") { |
| 708 | id_mask = -1; |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 709 | } else { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 710 | log_id_t log_id = android_name_to_log_id(buffer.c_str()); |
| 711 | if (log_id >= LOG_ID_MAX) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 712 | error(EXIT_FAILURE, 0, "Unknown buffer '%s' listed for -b.", |
| 713 | buffer.c_str()); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 714 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 715 | if (log_id == LOG_ID_SECURITY) { |
| 716 | security_buffer_selected = true; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 717 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 718 | id_mask |= (1 << log_id); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 719 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 720 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 721 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 722 | |
| 723 | case 'B': |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 724 | print_binary_ = 1; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 725 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 726 | |
| 727 | case 'f': |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 728 | if ((tail_time == log_time::EPOCH) && !tail_lines) { |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 729 | tail_time = lastLogTime(optarg); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 730 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 731 | // redirect output to a file |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 732 | output_file_name_ = optarg; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 733 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 734 | |
Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 735 | case 'r': |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 736 | if (!ParseUint(optarg, &log_rotate_size_kb_) || log_rotate_size_kb_ < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 737 | error(EXIT_FAILURE, 0, "Invalid parameter '%s' to -r.", optarg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 738 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 739 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 740 | |
Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 741 | case 'n': |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 742 | if (!ParseUint(optarg, &max_rotated_logs_) || max_rotated_logs_ < 1) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 743 | error(EXIT_FAILURE, 0, "Invalid parameter '%s' to -n.", optarg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 744 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 745 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 746 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 747 | case 'v': |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 748 | if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 749 | show_format_help(); |
| 750 | return EXIT_SUCCESS; |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 751 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 752 | for (const auto& arg : Split(optarg, delimiters)) { |
| 753 | int err = SetLogFormat(arg.c_str()); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 754 | if (err < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 755 | error(EXIT_FAILURE, 0, "Invalid parameter '%s' to -v.", arg.c_str()); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 756 | } |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 757 | if (err) hasSetLogFormat = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 758 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 759 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 760 | |
| 761 | case 'Q': |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 762 | #define LOGCAT_FILTER "androidboot.logcat=" |
| 763 | #define CONSOLE_PIPE_OPTION "androidboot.consolepipe=" |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 764 | #define CONSOLE_OPTION "androidboot.console=" |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 765 | #define QEMU_PROPERTY "ro.kernel.qemu" |
| 766 | #define QEMU_CMDLINE "qemu.cmdline" |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 767 | // This is a *hidden* option used to start a version of logcat |
| 768 | // in an emulated device only. It basically looks for |
| 769 | // androidboot.logcat= on the kernel command line. If |
| 770 | // something is found, it extracts a log filter and uses it to |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 771 | // run the program. The logcat output will go to consolepipe if |
| 772 | // androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise, |
| 773 | // it goes to androidboot.console (e.g. tty) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 774 | { |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 775 | // if not in emulator, exit quietly |
| 776 | if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 777 | return EXIT_SUCCESS; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 778 | } |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 779 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 780 | std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, ""); |
| 781 | if (cmdline.empty()) { |
| 782 | android::base::ReadFileToString("/proc/cmdline", &cmdline); |
| 783 | } |
| 784 | |
| 785 | const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER); |
| 786 | // if nothing found or invalid filters, exit quietly |
| 787 | if (!logcatFilter) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 788 | return EXIT_SUCCESS; |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | const char* p = logcatFilter + strlen(LOGCAT_FILTER); |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 792 | const char* q = strpbrk(p, " \t\n\r"); |
| 793 | if (!q) q = p + strlen(p); |
| 794 | forceFilters = std::string(p, q); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 795 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 796 | // redirect our output to the emulator console pipe or console |
| 797 | const char* consolePipe = |
| 798 | strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION); |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 799 | const char* console = |
| 800 | strstr(cmdline.c_str(), CONSOLE_OPTION); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 801 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 802 | if (consolePipe) { |
| 803 | p = consolePipe + strlen(CONSOLE_PIPE_OPTION); |
| 804 | } else if (console) { |
| 805 | p = console + strlen(CONSOLE_OPTION); |
| 806 | } else { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 807 | return EXIT_FAILURE; |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 810 | q = strpbrk(p, " \t\n\r"); |
| 811 | int len = q ? q - p : strlen(p); |
| 812 | std::string devname = "/dev/" + std::string(p, len); |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 813 | std::string pipePurpose("pipe:logcat"); |
| 814 | if (consolePipe) { |
| 815 | // example: "qemu_pipe,pipe:logcat" |
| 816 | // upon opening of /dev/qemu_pipe, the "pipe:logcat" |
| 817 | // string with trailing '\0' should be written to the fd |
Chih-Hung Hsieh | e5d975c | 2017-08-03 13:56:49 -0700 | [diff] [blame] | 818 | size_t pos = devname.find(','); |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 819 | if (pos != std::string::npos) { |
| 820 | pipePurpose = devname.substr(pos + 1); |
| 821 | devname = devname.substr(0, pos); |
| 822 | } |
| 823 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 824 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 825 | fprintf(stderr, "logcat using %s\n", devname.c_str()); |
| 826 | |
| 827 | int fd = open(devname.c_str(), O_WRONLY | O_CLOEXEC); |
| 828 | if (fd < 0) { |
| 829 | break; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 830 | } |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 831 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 832 | if (consolePipe) { |
| 833 | // need the trailing '\0' |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 834 | if (!android::base::WriteFully(fd, pipePurpose.c_str(), |
| 835 | pipePurpose.size() + 1)) { |
| 836 | close(fd); |
| 837 | return EXIT_FAILURE; |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 838 | } |
| 839 | } |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 840 | // close output and error channels, replace with console |
Erwin Jansen | 4f138b2 | 2019-11-27 12:58:58 -0800 | [diff] [blame] | 841 | dup2(fd, output_fd_.get()); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 842 | dup2(fd, STDERR_FILENO); |
| 843 | close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 844 | } |
| 845 | break; |
| 846 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 847 | case 'S': |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 848 | printStatistics = true; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 849 | break; |
| 850 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 851 | case ':': |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 852 | error(EXIT_FAILURE, 0, "Option '%s' needs an argument.", argv[optind - 1]); |
| 853 | break; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 854 | |
Mark Salyzyn | e74e51d | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 855 | case 'h': |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 856 | show_help(); |
| 857 | show_format_help(); |
| 858 | return EXIT_SUCCESS; |
Mark Salyzyn | e74e51d | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 859 | |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 860 | case '?': |
| 861 | error(EXIT_FAILURE, 0, "Unknown option '%s'.", argv[optind - 1]); |
| 862 | break; |
| 863 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 864 | default: |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 865 | error(EXIT_FAILURE, 0, "Unknown getopt_long() result '%c'.", c); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 869 | if (max_count_ && got_t) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 870 | error(EXIT_FAILURE, 0, "Cannot use -m (--max-count) and -t together."); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 871 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 872 | if (print_it_anyways_ && (!regex_ || !max_count_)) { |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 873 | // One day it would be nice if --print -v color and --regex <expr> |
| 874 | // could play with each other and show regex highlighted content. |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 875 | fprintf(stderr, |
| 876 | "WARNING: " |
| 877 | "--print ignored, to be used in combination with\n" |
| 878 | " " |
| 879 | "--regex <expr> and --max-count <N>\n"); |
| 880 | print_it_anyways_ = false; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 881 | } |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 882 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 883 | // If no buffers are specified, default to using these buffers. |
| 884 | if (id_mask == 0) { |
| 885 | id_mask = (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) | (1 << LOG_ID_CRASH) | |
| 886 | (1 << LOG_ID_KERNEL); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 887 | } |
| 888 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 889 | if (log_rotate_size_kb_ != 0 && !output_file_name_) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 890 | error(EXIT_FAILURE, 0, "-r requires -f as well."); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 893 | if (setId != 0) { |
| 894 | if (!output_file_name_) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 895 | error(EXIT_FAILURE, 0, "--id='%s' requires -f as well.", setId); |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 898 | std::string file_name = StringPrintf("%s.id", output_file_name_); |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 899 | std::string file; |
| 900 | bool file_ok = android::base::ReadFileToString(file_name, &file); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 901 | android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR, |
| 902 | getuid(), getgid()); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 903 | if (!file_ok || !file.compare(setId)) setId = nullptr; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 904 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 905 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 906 | if (!hasSetLogFormat) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 907 | const char* logFormat = getenv("ANDROID_PRINTF_LOG"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 908 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 909 | if (!!logFormat) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 910 | for (const auto& arg : Split(logFormat, delimiters)) { |
| 911 | int err = SetLogFormat(arg.c_str()); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 912 | // environment should not cause crash of logcat |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 913 | if (err < 0) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 914 | fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg.c_str()); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 915 | } |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 916 | if (err > 0) hasSetLogFormat = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 917 | } |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 918 | } |
| 919 | if (!hasSetLogFormat) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 920 | SetLogFormat("threadtime"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 924 | if (forceFilters.size()) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 925 | int err = android_log_addFilterString(logformat_.get(), forceFilters.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 926 | if (err < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 927 | error(EXIT_FAILURE, 0, "Invalid filter expression in logcat args."); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 928 | } |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 929 | } else if (argc == optind) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 930 | // Add from environment variable |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 931 | const char* env_tags_orig = getenv("ANDROID_LOG_TAGS"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 932 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 933 | if (!!env_tags_orig) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 934 | int err = android_log_addFilterString(logformat_.get(), env_tags_orig); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 935 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 936 | if (err < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 937 | error(EXIT_FAILURE, 0, "Invalid filter expression in ANDROID_LOG_TAGS."); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | } else { |
| 941 | // Add from commandline |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 942 | for (int i = optind ; i < argc ; i++) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 943 | int err = android_log_addFilterString(logformat_.get(), argv[i]); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 944 | if (err < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 945 | error(EXIT_FAILURE, 0, "Invalid filter expression '%s'.", argv[i]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
Tom Cherry | 9156c53 | 2019-11-14 08:56:39 -0800 | [diff] [blame] | 950 | if (mode & ANDROID_LOG_PSTORE) { |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 951 | if (output_file_name_) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 952 | error(EXIT_FAILURE, 0, "-c is ambiguous with both -f and -L specified."); |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 953 | } |
| 954 | if (setLogSize || getLogSize || printStatistics || getPruneList || setPruneList) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 955 | error(EXIT_FAILURE, 0, "-L is incompatible with -g/-G, -S, and -p/-P."); |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 956 | } |
Tom Cherry | 9156c53 | 2019-11-14 08:56:39 -0800 | [diff] [blame] | 957 | if (clearLog) { |
| 958 | unlink("/sys/fs/pstore/pmsg-ramoops-0"); |
| 959 | return EXIT_SUCCESS; |
| 960 | } |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | if (output_file_name_) { |
Tom Cherry | 9156c53 | 2019-11-14 08:56:39 -0800 | [diff] [blame] | 964 | if (setLogSize || getLogSize || printStatistics || getPruneList || setPruneList) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 965 | error(EXIT_FAILURE, 0, "-f is incompatible with -g/-G, -S, and -p/-P."); |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | if (clearLog || setId) { |
| 969 | int max_rotation_count_digits = |
| 970 | max_rotated_logs_ > 0 ? (int)(floor(log10(max_rotated_logs_) + 1)) : 0; |
| 971 | |
| 972 | for (int i = max_rotated_logs_; i >= 0; --i) { |
| 973 | std::string file; |
| 974 | |
| 975 | if (!i) { |
| 976 | file = output_file_name_; |
| 977 | } else { |
| 978 | file = StringPrintf("%s.%.*d", output_file_name_, max_rotation_count_digits, i); |
| 979 | } |
| 980 | |
| 981 | int err = unlink(file.c_str()); |
| 982 | |
| 983 | if (err < 0 && errno != ENOENT) { |
| 984 | fprintf(stderr, "failed to delete log file '%s': %s\n", file.c_str(), |
| 985 | strerror(errno)); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | if (clearLog) { |
| 991 | return EXIT_SUCCESS; |
Tom Cherry | 9156c53 | 2019-11-14 08:56:39 -0800 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 995 | std::unique_ptr<logger_list, decltype(&android_logger_list_free)> logger_list{ |
| 996 | nullptr, &android_logger_list_free}; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 997 | if (tail_time != log_time::EPOCH) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 998 | logger_list.reset(android_logger_list_alloc_time(mode, tail_time, pid)); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 999 | } else { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1000 | logger_list.reset(android_logger_list_alloc(mode, tail_lines, pid)); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1001 | } |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1002 | // We have three orthogonal actions below to clear, set log size and |
| 1003 | // get log size. All sharing the same iteration loop. |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1004 | std::vector<std::string> open_device_failures; |
| 1005 | std::vector<std::string> clear_failures; |
| 1006 | std::vector<std::string> set_size_failures; |
| 1007 | std::vector<std::string> get_size_failures; |
| 1008 | |
| 1009 | for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { |
| 1010 | if (!(id_mask & (1 << i))) continue; |
| 1011 | const char* buffer_name = android_log_id_to_name(static_cast<log_id_t>(i)); |
| 1012 | |
| 1013 | auto logger = android_logger_open(logger_list.get(), static_cast<log_id_t>(i)); |
| 1014 | if (logger == nullptr) { |
| 1015 | ReportErrorName(buffer_name, security_buffer_selected, &open_device_failures); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1016 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1017 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1018 | |
Tom Cherry | c30a3ee | 2019-12-04 14:37:38 -0800 | [diff] [blame] | 1019 | if (clearLog) { |
| 1020 | if (android_logger_clear(logger)) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1021 | ReportErrorName(buffer_name, security_buffer_selected, &clear_failures); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1022 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1025 | if (setLogSize) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1026 | if (android_logger_set_log_size(logger, setLogSize)) { |
| 1027 | ReportErrorName(buffer_name, security_buffer_selected, &set_size_failures); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1028 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1031 | if (getLogSize) { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1032 | long size = android_logger_get_log_size(logger); |
| 1033 | long readable = android_logger_get_log_readable_size(logger); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1034 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1035 | if (size < 0 || readable < 0) { |
| 1036 | ReportErrorName(buffer_name, security_buffer_selected, &get_size_failures); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1037 | } else { |
Wei Wang | c27d481 | 2018-09-05 11:05:57 -0700 | [diff] [blame] | 1038 | auto size_format = format_of_size(size); |
| 1039 | auto readable_format = format_of_size(readable); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1040 | std::string str = android::base::StringPrintf( |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1041 | "%s: ring buffer is %lu %sB (%lu %sB consumed)," |
| 1042 | " max entry is %d B, max payload is %d B\n", |
| 1043 | buffer_name, size_format.first, size_format.second, readable_format.first, |
| 1044 | readable_format.second, (int)LOGGER_ENTRY_MAX_LEN, |
| 1045 | (int)LOGGER_ENTRY_MAX_PAYLOAD); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1046 | TEMP_FAILURE_RETRY(write(output_fd_.get(), str.data(), str.length())); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1047 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1048 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1049 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1050 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1051 | // report any errors in the above loop and exit |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1052 | if (!open_device_failures.empty()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1053 | error(EXIT_FAILURE, 0, "Unable to open log device%s '%s'.", |
| 1054 | open_device_failures.size() > 1 ? "s" : "", Join(open_device_failures, ",").c_str()); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1055 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1056 | if (!clear_failures.empty()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1057 | error(EXIT_FAILURE, 0, "failed to clear the '%s' log%s.", Join(clear_failures, ",").c_str(), |
| 1058 | clear_failures.size() > 1 ? "s" : ""); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1059 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1060 | if (!set_size_failures.empty()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1061 | error(EXIT_FAILURE, 0, "failed to set the '%s' log size%s.", |
| 1062 | Join(set_size_failures, ",").c_str(), set_size_failures.size() > 1 ? "s" : ""); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1063 | } |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1064 | if (!get_size_failures.empty()) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1065 | error(EXIT_FAILURE, 0, "failed to get the readable '%s' log size%s.", |
| 1066 | Join(get_size_failures, ",").c_str(), get_size_failures.size() > 1 ? "s" : ""); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1067 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1068 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1069 | if (setPruneList) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1070 | size_t len = strlen(setPruneList); |
Tom Cherry | ed860ff | 2019-12-06 11:01:51 -0800 | [diff] [blame] | 1071 | if (android_logger_set_prune_list(logger_list.get(), setPruneList, len)) { |
| 1072 | error(EXIT_FAILURE, 0, "Failed to set the prune list."); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1073 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1074 | return EXIT_SUCCESS; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 1077 | if (printStatistics || getPruneList) { |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1078 | std::string buf(8192, '\0'); |
| 1079 | size_t ret_length = 0; |
| 1080 | int retry = 32; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1081 | |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1082 | for (; retry >= 0; --retry) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1083 | if (getPruneList) { |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1084 | android_logger_get_prune_list(logger_list.get(), buf.data(), buf.size()); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1085 | } else { |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1086 | android_logger_get_statistics(logger_list.get(), buf.data(), buf.size()); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1087 | } |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1088 | |
| 1089 | ret_length = atol(buf.c_str()); |
| 1090 | if (ret_length < 3) { |
| 1091 | error(EXIT_FAILURE, 0, "Failed to read data."); |
| 1092 | } |
| 1093 | |
| 1094 | if (ret_length < buf.size()) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1095 | break; |
| 1096 | } |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1097 | |
| 1098 | buf.resize(ret_length + 1); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1101 | if (retry < 0) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1102 | error(EXIT_FAILURE, 0, "Failed to read data."); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1105 | buf.resize(ret_length); |
| 1106 | if (buf.back() == '\f') { |
| 1107 | buf.pop_back(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Tom Cherry | e17b4f6 | 2019-12-06 13:33:11 -0800 | [diff] [blame] | 1110 | // Remove the byte count prefix |
| 1111 | const char* cp = buf.c_str(); |
| 1112 | while (isdigit(*cp)) ++cp; |
| 1113 | if (*cp == '\n') ++cp; |
| 1114 | |
| 1115 | size_t len = strlen(cp); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1116 | TEMP_FAILURE_RETRY(write(output_fd_.get(), cp, len)); |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1117 | return EXIT_SUCCESS; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1120 | if (getLogSize || setLogSize || clearLog) return EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1121 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1122 | SetupOutputAndSchedulingPolicy(!(mode & ANDROID_LOG_NONBLOCK)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1123 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1124 | while (!max_count_ || print_count_ < max_count_) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1125 | struct log_msg log_msg; |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1126 | int ret = android_logger_list_read(logger_list.get(), &log_msg); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1127 | if (!ret) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1128 | error(EXIT_FAILURE, 0, R"init(Unexpected EOF! |
Tom Cherry | 4169732 | 2019-11-21 10:31:06 -0800 | [diff] [blame] | 1129 | |
Tom Cherry | 26d712e | 2020-02-14 10:01:57 -0800 | [diff] [blame] | 1130 | This means that either the device shut down, logd crashed, or this instance of logcat was unable to read log |
Tom Cherry | 4169732 | 2019-11-21 10:31:06 -0800 | [diff] [blame] | 1131 | messages as quickly as they were being produced. |
| 1132 | |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1133 | If you have enabled significant logging, look into using the -G option to increase log buffer sizes.)init"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | if (ret < 0) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1137 | if (ret == -EAGAIN) break; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1138 | |
| 1139 | if (ret == -EIO) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1140 | error(EXIT_FAILURE, 0, "Unexpected EOF!"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1141 | } |
| 1142 | if (ret == -EINVAL) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1143 | error(EXIT_FAILURE, 0, "Unexpected length."); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1144 | } |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1145 | error(EXIT_FAILURE, errno, "Logcat read failure"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1148 | if (log_msg.id() > LOG_ID_MAX) { |
Tom Cherry | 72a4e08 | 2019-12-06 10:25:37 -0800 | [diff] [blame] | 1149 | error(EXIT_FAILURE, 0, "Unexpected log id (%d) over LOG_ID_MAX (%d).", log_msg.id(), |
| 1150 | LOG_ID_MAX); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1151 | } |
| 1152 | |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1153 | PrintDividers(log_msg.id(), printDividers); |
| 1154 | |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1155 | if (print_binary_) { |
| 1156 | TEMP_FAILURE_RETRY(write(output_fd_.get(), &log_msg, log_msg.len())); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1157 | } else { |
Tom Cherry | 6f061e8 | 2019-10-29 07:05:24 -0700 | [diff] [blame] | 1158 | ProcessBuffer(&log_msg); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1159 | } |
| 1160 | } |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1161 | return EXIT_SUCCESS; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
Tom Cherry | 98c6c33 | 2019-10-30 13:51:03 -0700 | [diff] [blame] | 1164 | int main(int argc, char** argv) { |
Tom Cherry | d162f14 | 2019-10-24 17:35:26 -0700 | [diff] [blame] | 1165 | Logcat logcat; |
| 1166 | return logcat.Run(argc, argv); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1167 | } |