2017-02-10 22:09:07 +01:00
|
|
|
/*
|
2017-02-10 22:09:07 +01:00
|
|
|
* Copyright (C) 2006-2017 The Android Open Source Project
|
2017-02-10 22:09:07 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-06-16 00:16:20 +02:00
|
|
|
#include "logcat.h"
|
|
|
|
|
2018-09-13 20:08:41 +02:00
|
|
|
#include <android-base/macros.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <arpa/inet.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2015-05-27 16:39:56 +02:00
|
|
|
#include <dirent.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2018-06-16 00:16:20 +02:00
|
|
|
#include <getopt.h>
|
2014-10-17 07:19:55 +02:00
|
|
|
#include <math.h>
|
2017-02-10 22:09:07 +01:00
|
|
|
#include <pthread.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <stdarg.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-04-10 14:51:39 +02:00
|
|
|
#include <sys/cdefs.h>
|
2015-06-09 08:36:34 +02:00
|
|
|
#include <sys/resource.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
2015-05-27 16:39:56 +02:00
|
|
|
#include <sys/types.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
#include <atomic>
|
2015-05-27 16:39:56 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2018-09-05 20:05:57 +02:00
|
|
|
#include <utility>
|
2017-02-10 22:09:07 +01:00
|
|
|
#include <vector>
|
2015-05-27 16:39:56 +02:00
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/file.h>
|
2017-02-21 23:31:19 +01:00
|
|
|
#include <android-base/properties.h>
|
2016-08-03 23:20:41 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/strings.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <cutils/sched_policy.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <cutils/sockets.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <log/event_tag_map.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <log/logprint.h>
|
2016-09-30 22:30:33 +02:00
|
|
|
#include <private/android_logger.h>
|
2016-02-17 20:58:01 +01:00
|
|
|
#include <system/thread_defs.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
|
2016-03-18 00:18:55 +01:00
|
|
|
#include <pcrecpp.h>
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#define DEFAULT_MAX_ROTATED_LOGS 4
|
|
|
|
|
2017-03-06 23:56:04 +01:00
|
|
|
struct log_device_t {
|
|
|
|
const char* device;
|
|
|
|
bool binary;
|
|
|
|
struct logger* logger;
|
|
|
|
struct logger_list* logger_list;
|
|
|
|
bool printed;
|
|
|
|
|
|
|
|
log_device_t* next;
|
|
|
|
|
|
|
|
log_device_t(const char* d, bool b) {
|
|
|
|
device = d;
|
|
|
|
binary = b;
|
|
|
|
next = nullptr;
|
|
|
|
printed = false;
|
|
|
|
logger = nullptr;
|
|
|
|
logger_list = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
struct android_logcat_context_internal {
|
|
|
|
// status
|
2017-02-10 22:09:07 +01:00
|
|
|
volatile std::atomic_int retval; // valid if thread_stopped set
|
|
|
|
// Arguments passed in, or copies and storage thereof if a thread.
|
2017-02-10 22:09:07 +01:00
|
|
|
int argc;
|
|
|
|
char* const* argv;
|
|
|
|
char* const* envp;
|
2017-02-10 22:09:07 +01:00
|
|
|
std::vector<std::string> args;
|
|
|
|
std::vector<const char*> argv_hold;
|
|
|
|
std::vector<std::string> envs;
|
|
|
|
std::vector<const char*> envp_hold;
|
2017-03-01 17:30:06 +01:00
|
|
|
int output_fd; // duplication of fileno(output) (below)
|
|
|
|
int error_fd; // duplication of fileno(error) (below)
|
2017-02-10 22:09:07 +01:00
|
|
|
|
|
|
|
// library
|
2017-02-10 22:09:07 +01:00
|
|
|
int fds[2]; // From popen call
|
|
|
|
FILE* output; // everything writes to fileno(output), buffer unused
|
|
|
|
FILE* error; // unless error == output.
|
|
|
|
pthread_t thr;
|
|
|
|
volatile std::atomic_bool stop; // quick exit flag
|
|
|
|
volatile std::atomic_bool thread_stopped;
|
2017-02-10 22:09:07 +01:00
|
|
|
bool stderr_null; // shell "2>/dev/null"
|
|
|
|
bool stderr_stdout; // shell "2>&1"
|
|
|
|
|
|
|
|
// global variables
|
|
|
|
AndroidLogFormat* logformat;
|
|
|
|
const char* outputFileName;
|
|
|
|
// 0 means "no log rotation"
|
|
|
|
size_t logRotateSizeKBytes;
|
|
|
|
// 0 means "unbounded"
|
|
|
|
size_t maxRotatedLogs;
|
|
|
|
size_t outByteCount;
|
|
|
|
int printBinary;
|
|
|
|
int devCount; // >1 means multiple
|
|
|
|
pcrecpp::RE* regex;
|
2017-03-06 23:56:04 +01:00
|
|
|
log_device_t* devices;
|
|
|
|
EventTagMap* eventTagMap;
|
2017-02-10 22:09:07 +01:00
|
|
|
// 0 means "infinite"
|
|
|
|
size_t maxCount;
|
|
|
|
size_t printCount;
|
2017-03-06 23:56:04 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
bool printItAnyways;
|
|
|
|
bool debug;
|
|
|
|
bool hasOpenedEventTagMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Creates a context associated with this logcat instance
|
|
|
|
android_logcat_context create_android_logcat() {
|
|
|
|
android_logcat_context_internal* context;
|
|
|
|
|
|
|
|
context = (android_logcat_context_internal*)calloc(
|
|
|
|
1, sizeof(android_logcat_context_internal));
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!context) return nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
context->fds[0] = -1;
|
|
|
|
context->fds[1] = -1;
|
2017-02-10 22:09:07 +01:00
|
|
|
context->output_fd = -1;
|
|
|
|
context->error_fd = -1;
|
|
|
|
context->maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
context->argv_hold.clear();
|
|
|
|
context->args.clear();
|
|
|
|
context->envp_hold.clear();
|
|
|
|
context->envs.clear();
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
return (android_logcat_context)context;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
// logd prefixes records with a length field
|
2009-03-04 04:32:55 +01:00
|
|
|
#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
enum helpType { HELP_FALSE, HELP_TRUE, HELP_FORMAT };
|
2016-10-18 20:30:11 +02:00
|
|
|
|
2016-03-30 21:38:29 +02:00
|
|
|
// if showHelp is set, newline required in fmt statement to transition to usage
|
2017-02-10 22:09:07 +01:00
|
|
|
static void logcat_panic(android_logcat_context_internal* context,
|
|
|
|
enum helpType showHelp, const char* fmt, ...)
|
|
|
|
__printflike(3, 4);
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static int openLogFile(const char* pathname) {
|
2012-08-13 18:55:07 +02:00
|
|
|
return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static void close_output(android_logcat_context_internal* context) {
|
|
|
|
// split output_from_error
|
|
|
|
if (context->error == context->output) {
|
2017-03-01 17:30:06 +01:00
|
|
|
context->output = nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
context->output_fd = -1;
|
|
|
|
}
|
|
|
|
if (context->error && (context->output_fd == fileno(context->error))) {
|
|
|
|
context->output_fd = -1;
|
|
|
|
}
|
|
|
|
if (context->output_fd == context->error_fd) {
|
|
|
|
context->output_fd = -1;
|
|
|
|
}
|
|
|
|
// close output channel
|
|
|
|
if (context->output) {
|
|
|
|
if (context->output != stdout) {
|
|
|
|
if (context->output_fd == fileno(context->output)) {
|
|
|
|
context->output_fd = -1;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->fds[1] == fileno(context->output)) {
|
|
|
|
context->fds[1] = -1;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
fclose(context->output);
|
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
context->output = nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
|
|
|
if (context->output_fd >= 0) {
|
|
|
|
if (context->output_fd != fileno(stdout)) {
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->fds[1] == context->output_fd) {
|
|
|
|
context->fds[1] = -1;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
close(context->output_fd);
|
|
|
|
}
|
|
|
|
context->output_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_error(android_logcat_context_internal* context) {
|
|
|
|
// split error_from_output
|
|
|
|
if (context->output == context->error) {
|
2017-03-01 17:30:06 +01:00
|
|
|
context->error = nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
context->error_fd = -1;
|
|
|
|
}
|
|
|
|
if (context->output && (context->error_fd == fileno(context->output))) {
|
|
|
|
context->error_fd = -1;
|
|
|
|
}
|
|
|
|
if (context->error_fd == context->output_fd) {
|
|
|
|
context->error_fd = -1;
|
|
|
|
}
|
|
|
|
// close error channel
|
|
|
|
if (context->error) {
|
|
|
|
if ((context->error != stderr) && (context->error != stdout)) {
|
|
|
|
if (context->error_fd == fileno(context->error)) {
|
|
|
|
context->error_fd = -1;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->fds[1] == fileno(context->error)) {
|
|
|
|
context->fds[1] = -1;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
fclose(context->error);
|
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
context->error = nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
|
|
|
if (context->error_fd >= 0) {
|
|
|
|
if ((context->error_fd != fileno(stdout)) &&
|
|
|
|
(context->error_fd != fileno(stderr))) {
|
2017-03-01 17:30:06 +01:00
|
|
|
if (context->fds[1] == context->error_fd) context->fds[1] = -1;
|
2017-02-10 22:09:07 +01:00
|
|
|
close(context->error_fd);
|
|
|
|
}
|
|
|
|
context->error_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rotateLogs(android_logcat_context_internal* context) {
|
2009-03-04 04:32:55 +01:00
|
|
|
int err;
|
|
|
|
|
|
|
|
// Can't rotate logs if we're not outputting to a file
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!context->outputFileName) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
close_output(context);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
// Compute the maximum number of digits needed to count up to
|
2017-02-10 22:09:07 +01:00
|
|
|
// maxRotatedLogs in decimal. eg:
|
|
|
|
// maxRotatedLogs == 30
|
2017-02-10 22:09:07 +01:00
|
|
|
// -> log10(30) == 1.477
|
|
|
|
// -> maxRotationCountDigits == 2
|
2014-10-17 07:19:55 +02:00
|
|
|
int maxRotationCountDigits =
|
2017-02-10 22:09:07 +01:00
|
|
|
(context->maxRotatedLogs > 0)
|
|
|
|
? (int)(floor(log10(context->maxRotatedLogs) + 1))
|
|
|
|
: 0;
|
2014-10-17 07:19:55 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
for (int i = context->maxRotatedLogs; i > 0; i--) {
|
2016-08-03 23:20:41 +02:00
|
|
|
std::string file1 = android::base::StringPrintf(
|
2017-02-10 22:09:07 +01:00
|
|
|
"%s.%.*d", context->outputFileName, maxRotationCountDigits, i);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-08-03 23:20:41 +02:00
|
|
|
std::string file0;
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!(i - 1)) {
|
2017-02-10 22:09:07 +01:00
|
|
|
file0 = android::base::StringPrintf("%s", context->outputFileName);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2017-02-10 22:09:07 +01:00
|
|
|
file0 =
|
|
|
|
android::base::StringPrintf("%s.%.*d", context->outputFileName,
|
|
|
|
maxRotationCountDigits, i - 1);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!file0.length() || !file1.length()) {
|
2015-04-10 14:51:39 +02:00
|
|
|
perror("while rotating log files");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-03 23:20:41 +02:00
|
|
|
err = rename(file0.c_str(), file1.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (err < 0 && errno != ENOENT) {
|
|
|
|
perror("while rotating log files");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
context->output_fd = openLogFile(context->outputFileName);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->output_fd < 0) {
|
|
|
|
logcat_panic(context, HELP_FALSE, "couldn't open output file");
|
|
|
|
return;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
context->output = fdopen(context->output_fd, "web");
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!context->output) {
|
2017-02-17 22:15:51 +01:00
|
|
|
logcat_panic(context, HELP_FALSE, "couldn't fdopen output file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (context->stderr_stdout) {
|
|
|
|
close_error(context);
|
|
|
|
context->error = context->output;
|
|
|
|
context->error_fd = context->output_fd;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
context->outByteCount = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
void printBinary(android_logcat_context_internal* context, struct log_msg* buf) {
|
2013-11-22 19:55:48 +01:00
|
|
|
size_t size = buf->len();
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
TEMP_FAILURE_RETRY(write(context->output_fd, buf, size));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static bool regexOk(android_logcat_context_internal* context,
|
|
|
|
const AndroidLogEntry& entry) {
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!context->regex) return true;
|
2016-03-18 00:18:55 +01:00
|
|
|
|
|
|
|
std::string messageString(entry.message, entry.messageLen);
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
return context->regex->PartialMatch(messageString);
|
2016-03-18 00:18:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static void processBuffer(android_logcat_context_internal* context,
|
|
|
|
log_device_t* dev, struct log_msg* buf) {
|
2010-03-18 00:10:26 +01:00
|
|
|
int bytesWritten = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
int err;
|
|
|
|
AndroidLogEntry entry;
|
|
|
|
char binaryMsgBuf[1024];
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
if (dev->binary) {
|
2017-02-10 22:09:07 +01:00
|
|
|
if (!context->eventTagMap && !context->hasOpenedEventTagMap) {
|
2017-03-01 17:30:06 +01:00
|
|
|
context->eventTagMap = android_openEventTagMap(nullptr);
|
2017-02-10 22:09:07 +01:00
|
|
|
context->hasOpenedEventTagMap = true;
|
2015-02-26 23:33:35 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
err = android_log_processBinaryLogBuffer(
|
|
|
|
&buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf,
|
|
|
|
sizeof(binaryMsgBuf));
|
2017-02-10 22:09:07 +01:00
|
|
|
// printf(">>> pri=%d len=%d msg='%s'\n",
|
2009-03-04 04:32:55 +01:00
|
|
|
// entry.priority, entry.messageLen, entry.message);
|
|
|
|
} else {
|
2013-11-22 19:55:48 +01:00
|
|
|
err = android_log_processLogBuffer(&buf->entry_v1, &entry);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
if ((err < 0) && !context->debug) return;
|
2010-03-01 18:11:54 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if (android_log_shouldPrintLine(
|
2017-02-10 22:09:07 +01:00
|
|
|
context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
|
2017-02-10 22:09:07 +01:00
|
|
|
entry.priority)) {
|
2017-02-10 22:09:07 +01:00
|
|
|
bool match = regexOk(context, entry);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
context->printCount += match;
|
|
|
|
if (match || context->printItAnyways) {
|
|
|
|
bytesWritten = android_log_printLogLine(context->logformat,
|
|
|
|
context->output_fd, &entry);
|
2016-03-17 22:04:52 +01:00
|
|
|
|
2016-03-30 18:38:31 +02:00
|
|
|
if (bytesWritten < 0) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE, "output error");
|
|
|
|
return;
|
2016-03-30 18:38:31 +02:00
|
|
|
}
|
2010-03-01 18:11:54 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
context->outByteCount += bytesWritten;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->logRotateSizeKBytes > 0 &&
|
|
|
|
(context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
|
|
|
|
rotateLogs(context);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static void maybePrintStart(android_logcat_context_internal* context,
|
|
|
|
log_device_t* dev, bool printDividers) {
|
2015-01-26 22:41:33 +01:00
|
|
|
if (!dev->printed || printDividers) {
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->devCount > 1 && !context->printBinary) {
|
2010-03-12 05:32:17 +01:00
|
|
|
char buf[1024];
|
2015-01-26 22:41:33 +01:00
|
|
|
snprintf(buf, sizeof(buf), "--------- %s %s\n",
|
2017-02-10 22:09:07 +01:00
|
|
|
dev->printed ? "switch to" : "beginning of", dev->device);
|
2017-02-10 22:09:07 +01:00
|
|
|
if (write(context->output_fd, buf, strlen(buf)) < 0) {
|
|
|
|
logcat_panic(context, HELP_FALSE, "output error");
|
|
|
|
return;
|
2010-03-12 05:32:17 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-26 22:41:33 +01:00
|
|
|
dev->printed = true;
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static void setupOutputAndSchedulingPolicy(
|
|
|
|
android_logcat_context_internal* context, bool blocking) {
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!context->outputFileName) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-08-04 16:53:52 +02:00
|
|
|
if (blocking) {
|
|
|
|
// Lower priority and set to batch scheduling if we are saving
|
|
|
|
// the logs into files and taking continuous content.
|
2017-02-10 22:09:07 +01:00
|
|
|
if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
|
|
|
|
fprintf(context->error,
|
|
|
|
"failed to set background scheduling policy\n");
|
2015-06-02 16:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sched_param param;
|
|
|
|
memset(¶m, 0, sizeof(param));
|
2017-02-10 22:09:07 +01:00
|
|
|
if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) {
|
2015-06-02 16:57:16 +02:00
|
|
|
fprintf(stderr, "failed to set to batch scheduler\n");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
|
|
|
|
context->error) {
|
|
|
|
fprintf(context->error, "failed set to priority\n");
|
2015-06-09 08:36:34 +02:00
|
|
|
}
|
2016-08-04 16:53:52 +02:00
|
|
|
}
|
2015-06-09 08:36:34 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
close_output(context);
|
|
|
|
|
|
|
|
context->output_fd = openLogFile(context->outputFileName);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->output_fd < 0) {
|
|
|
|
logcat_panic(context, HELP_FALSE, "couldn't open output file");
|
|
|
|
return;
|
2016-08-04 16:53:52 +02:00
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2016-08-04 16:53:52 +02:00
|
|
|
struct stat statbuf;
|
2017-02-10 22:09:07 +01:00
|
|
|
if (fstat(context->output_fd, &statbuf) == -1) {
|
|
|
|
close_output(context);
|
|
|
|
logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
|
|
|
|
return;
|
2016-08-04 16:53:52 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
|
2017-02-10 22:09:07 +01:00
|
|
|
close_output(context);
|
|
|
|
logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
|
|
|
|
return;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-08-04 16:53:52 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
context->output = fdopen(context->output_fd, "web");
|
|
|
|
|
|
|
|
context->outByteCount = statbuf.st_size;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
// clang-format off
|
2017-02-10 22:09:07 +01:00
|
|
|
static void show_help(android_logcat_context_internal* context) {
|
|
|
|
if (!context->error) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* cmd = strrchr(context->argv[0], '/');
|
|
|
|
cmd = cmd ? cmd + 1 : context->argv[0];
|
|
|
|
|
|
|
|
fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
|
|
|
|
|
|
|
|
fprintf(context->error, "options include:\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
" -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
|
|
|
|
" -f <file>, --file=<file> Log to file. Default is stdout\n"
|
2016-06-07 22:03:10 +02:00
|
|
|
" -r <kbytes>, --rotate-kbytes=<kbytes>\n"
|
|
|
|
" Rotate log every kbytes. Requires -f option\n"
|
|
|
|
" -n <count>, --rotate-count=<count>\n"
|
|
|
|
" Sets max number of rotated logs to <count>, default 4\n"
|
2016-08-03 23:20:41 +02:00
|
|
|
" --id=<id> If the signature id for logging to file changes, then clear\n"
|
|
|
|
" the fileset and continue\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
" -v <format>, --format=<format>\n"
|
2016-07-06 20:12:14 +02:00
|
|
|
" Sets log print format verb and adverbs, where <format> is:\n"
|
2016-10-18 20:30:11 +02:00
|
|
|
" brief help long process raw tag thread threadtime time\n"
|
2016-07-06 22:30:40 +02:00
|
|
|
" and individually flagged modifying adverbs can be added:\n"
|
2016-10-18 20:30:11 +02:00
|
|
|
" color descriptive epoch monotonic printable uid\n"
|
|
|
|
" usec UTC year zone\n"
|
2017-02-28 18:20:31 +01:00
|
|
|
" Multiple -v parameters or comma separated list of format and\n"
|
|
|
|
" format modifiers are allowed.\n"
|
2017-01-30 18:16:09 +01:00
|
|
|
// private and undocumented nsec, no signal, too much noise
|
|
|
|
// useful for -T or -t <timestamp> accurate testing though.
|
2016-04-12 18:11:46 +02:00
|
|
|
" -D, --dividers Print dividers between each log buffer\n"
|
|
|
|
" -c, --clear Clear (flush) the entire log and exit\n"
|
2016-06-06 23:56:00 +02:00
|
|
|
" if Log to File specified, clear fileset instead\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
" -d Dump the log and then exit (don't block)\n"
|
|
|
|
" -e <expr>, --regex=<expr>\n"
|
|
|
|
" Only print lines where the log message matches <expr>\n"
|
2017-10-31 13:44:54 +01:00
|
|
|
" where <expr> is a Perl-compatible regular expression\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
// Leave --head undocumented as alias for -m
|
|
|
|
" -m <count>, --max-count=<count>\n"
|
|
|
|
" Quit after printing <count> lines. This is meant to be\n"
|
|
|
|
" paired with --regex, but will work on its own.\n"
|
|
|
|
" --print Paired with --regex and --max-count to let content bypass\n"
|
2016-03-30 18:38:31 +02:00
|
|
|
" regex filter but still stop at number of matches.\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
// Leave --tail undocumented as alias for -t
|
|
|
|
" -t <count> Print only the most recent <count> lines (implies -d)\n"
|
|
|
|
" -t '<time>' Print most recent lines since specified time (implies -d)\n"
|
|
|
|
" -T <count> Print only the most recent <count> lines (does not imply -d)\n"
|
|
|
|
" -T '<time>' Print most recent lines since specified time (not imply -d)\n"
|
2015-08-31 17:01:33 +02:00
|
|
|
" count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
|
2015-09-01 00:53:41 +02:00
|
|
|
" 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
" -g, --buffer-size Get the size of the ring buffer.\n"
|
|
|
|
" -G <size>, --buffer-size=<size>\n"
|
|
|
|
" Set size of log ring buffer, may suffix with K or M.\n"
|
2016-11-29 12:48:11 +01:00
|
|
|
" -L, --last Dump logs from prior to last reboot\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
" -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
|
|
|
|
" 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
|
2018-10-22 20:16:07 +02:00
|
|
|
" Additionally, 'kernel' for userdebug and eng builds, and\n"
|
|
|
|
" 'security' for Device Owner installations.\n"
|
2016-04-11 23:03:48 +02:00
|
|
|
" Multiple -b parameters or comma separated list of buffers are\n"
|
|
|
|
" allowed. Buffers interleaved. Default -b main,system,crash.\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
" -B, --binary Output the log in binary.\n"
|
|
|
|
" -S, --statistics Output statistics.\n"
|
|
|
|
" -p, --prune Print prune white and ~black list. Service is specified as\n"
|
|
|
|
" UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
|
2014-04-11 22:49:43 +02:00
|
|
|
" with ~, otherwise weighed for longevity if unadorned. All\n"
|
|
|
|
" other pruning activity is oldest first. Special case ~!\n"
|
|
|
|
" represents an automatic quicker pruning for the noisiest\n"
|
|
|
|
" UID as determined by the current statistics.\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
" -P '<list> ...', --prune='<list> ...'\n"
|
|
|
|
" Set prune white and ~black list, using same format as\n"
|
|
|
|
" listed above. Must be quoted.\n"
|
2015-11-30 22:48:56 +01:00
|
|
|
" --pid=<pid> Only prints logs from the given pid.\n"
|
2016-04-12 18:11:46 +02:00
|
|
|
// Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
|
2015-11-30 22:48:56 +01:00
|
|
|
" --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
|
|
|
|
" comes first. Improves efficiency of polling by providing\n"
|
|
|
|
" an about-to-wrap wakeup.\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
fprintf(context->error, "\nfilterspecs are a series of \n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" <tag>[:priority]\n\n"
|
|
|
|
"where <tag> is a log component tag (or * for all) and priority is:\n"
|
2015-03-09 17:32:56 +01:00
|
|
|
" V Verbose (default for <tag>)\n"
|
|
|
|
" D Debug (default for '*')\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" I Info\n"
|
|
|
|
" W Warn\n"
|
|
|
|
" E Error\n"
|
|
|
|
" F Fatal\n"
|
2015-03-09 17:32:56 +01:00
|
|
|
" S Silent (suppress all output)\n"
|
|
|
|
"\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
|
|
|
|
"If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
|
|
|
|
"eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
|
|
|
|
"\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
|
|
|
|
"\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
|
2014-09-16 18:15:15 +02:00
|
|
|
"or defaults to \"threadtime\"\n\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static void show_format_help(android_logcat_context_internal* context) {
|
|
|
|
if (!context->error) return;
|
|
|
|
fprintf(context->error,
|
2016-10-18 20:30:11 +02:00
|
|
|
"-v <format>, --format=<format> options:\n"
|
|
|
|
" Sets log print format verb and adverbs, where <format> is:\n"
|
|
|
|
" brief long process raw tag thread threadtime time\n"
|
|
|
|
" and individually flagged modifying adverbs can be added:\n"
|
|
|
|
" color descriptive epoch monotonic printable uid usec UTC year zone\n"
|
|
|
|
"\nSingle format verbs:\n"
|
|
|
|
" brief — Display priority/tag and PID of the process issuing the message.\n"
|
|
|
|
" long — Display all metadata fields, separate messages with blank lines.\n"
|
|
|
|
" process — Display PID only.\n"
|
|
|
|
" raw — Display the raw log message, with no other metadata fields.\n"
|
|
|
|
" tag — Display the priority/tag only.\n"
|
2017-05-15 22:40:33 +02:00
|
|
|
" thread — Display priority, PID and TID of process issuing the message.\n"
|
2016-10-18 20:30:11 +02:00
|
|
|
" threadtime — Display the date, invocation time, priority, tag, and the PID\n"
|
|
|
|
" and TID of the thread issuing the message. (the default format).\n"
|
|
|
|
" time — Display the date, invocation time, priority/tag, and PID of the\n"
|
|
|
|
" process issuing the message.\n"
|
|
|
|
"\nAdverb modifiers can be used in combination:\n"
|
|
|
|
" color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
|
|
|
|
" \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
|
|
|
|
" descriptive — events logs only, descriptions from event-log-tags database.\n"
|
|
|
|
" epoch — Display time as seconds since Jan 1 1970.\n"
|
|
|
|
" monotonic — Display time as cpu seconds since last boot.\n"
|
|
|
|
" printable — Ensure that any binary logging content is escaped.\n"
|
|
|
|
" uid — If permitted, display the UID or Android ID of logged process.\n"
|
|
|
|
" usec — Display time down the microsecond precision.\n"
|
|
|
|
" UTC — Display time as UTC.\n"
|
|
|
|
" year — Add the year to the displayed time.\n"
|
|
|
|
" zone — Add the local timezone to the displayed time.\n"
|
|
|
|
" \"<zone>\" — Print using this public named timezone (experimental).\n\n"
|
|
|
|
);
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
// clang-format on
|
2016-10-18 20:30:11 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static int setLogFormat(android_logcat_context_internal* context,
|
|
|
|
const char* formatString) {
|
2017-02-10 22:09:07 +01:00
|
|
|
AndroidLogPrintFormat format;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
format = android_log_formatFromString(formatString);
|
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
// invalid string?
|
|
|
|
if (format == FORMAT_OFF) return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
return android_log_setPrintFormat(context->logformat, format);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-05 20:05:57 +02:00
|
|
|
static std::pair<unsigned long, const char*> format_of_size(unsigned long value) {
|
|
|
|
static const char multipliers[][3] = {{""}, {"Ki"}, {"Mi"}, {"Gi"}};
|
|
|
|
size_t i;
|
2014-05-06 16:34:59 +02:00
|
|
|
for (i = 0;
|
2017-02-10 22:09:07 +01:00
|
|
|
(i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
|
|
|
|
value /= 1024, ++i)
|
|
|
|
;
|
2018-09-05 20:05:57 +02:00
|
|
|
return std::make_pair(value, multipliers[i]);
|
2014-05-06 16:34:59 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
// String to unsigned int, returns -1 if it fails
|
|
|
|
static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
|
|
|
|
size_t max = SIZE_MAX) {
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!ptr) return false;
|
2015-06-05 23:10:12 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
char* endp;
|
2015-04-10 14:51:39 +02:00
|
|
|
errno = 0;
|
2015-06-05 23:10:12 +02:00
|
|
|
size_t ret = (size_t)strtoll(ptr, &endp, 0);
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (endp[0] || errno) return false;
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if ((ret > max) || (ret < min)) return false;
|
2015-04-10 14:51:39 +02:00
|
|
|
|
|
|
|
*val = ret;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static void logcat_panic(android_logcat_context_internal* context,
|
|
|
|
enum helpType showHelp, const char* fmt, ...) {
|
|
|
|
context->retval = EXIT_FAILURE;
|
|
|
|
if (!context->error) {
|
|
|
|
context->stop = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
va_list args;
|
2015-04-13 18:27:57 +02:00
|
|
|
va_start(args, fmt);
|
2017-02-10 22:09:07 +01:00
|
|
|
vfprintf(context->error, fmt, args);
|
2015-04-13 18:27:57 +02:00
|
|
|
va_end(args);
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2016-10-18 20:30:11 +02:00
|
|
|
switch (showHelp) {
|
2017-02-10 22:09:07 +01:00
|
|
|
case HELP_TRUE:
|
2017-02-10 22:09:07 +01:00
|
|
|
show_help(context);
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
|
|
|
case HELP_FORMAT:
|
2017-02-10 22:09:07 +01:00
|
|
|
show_format_help(context);
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
|
|
|
case HELP_FALSE:
|
|
|
|
default:
|
|
|
|
break;
|
2015-04-10 14:51:39 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
context->stop = true;
|
2015-04-10 14:51:39 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static char* parseTime(log_time& t, const char* cp) {
|
|
|
|
char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
|
2017-03-01 17:30:06 +01:00
|
|
|
if (ep) return ep;
|
2015-09-01 00:53:41 +02:00
|
|
|
ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
|
2017-03-01 17:30:06 +01:00
|
|
|
if (ep) return ep;
|
2015-09-01 00:53:41 +02:00
|
|
|
return t.strptime(cp, "%s.%q");
|
2015-08-31 17:01:33 +02:00
|
|
|
}
|
2015-05-27 16:39:56 +02:00
|
|
|
|
2016-08-04 16:43:46 +02:00
|
|
|
// Find last logged line in <outputFileName>, or <outputFileName>.1
|
2017-03-03 00:09:41 +01:00
|
|
|
static log_time lastLogTime(const char* outputFileName) {
|
2015-05-27 16:39:56 +02:00
|
|
|
log_time retval(log_time::EPOCH);
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!outputFileName) return retval;
|
2015-05-27 16:39:56 +02:00
|
|
|
|
|
|
|
std::string directory;
|
2017-03-03 00:09:41 +01:00
|
|
|
const char* file = strrchr(outputFileName, '/');
|
2015-05-27 16:39:56 +02:00
|
|
|
if (!file) {
|
|
|
|
directory = ".";
|
|
|
|
file = outputFileName;
|
|
|
|
} else {
|
2017-03-03 00:09:41 +01:00
|
|
|
directory = std::string(outputFileName, file - outputFileName);
|
2015-05-27 16:39:56 +02:00
|
|
|
++file;
|
|
|
|
}
|
2016-04-01 16:52:20 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
|
|
|
|
closedir);
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!dir.get()) return retval;
|
2016-04-01 16:52:20 +02:00
|
|
|
|
2016-08-04 16:43:46 +02:00
|
|
|
log_time now(android_log_clockid());
|
2016-04-01 16:52:20 +02:00
|
|
|
|
2015-05-27 16:39:56 +02:00
|
|
|
size_t len = strlen(file);
|
|
|
|
log_time modulo(0, NS_PER_SEC);
|
2017-02-10 22:09:07 +01:00
|
|
|
struct dirent* dp;
|
2016-04-01 16:52:20 +02:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
while (!!(dp = readdir(dir.get()))) {
|
|
|
|
if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
|
2017-02-10 22:09:07 +01:00
|
|
|
(dp->d_name[len] && ((dp->d_name[len] != '.') ||
|
2017-03-01 17:30:06 +01:00
|
|
|
(strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
|
2015-05-27 16:39:56 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_name = directory;
|
|
|
|
file_name += "/";
|
|
|
|
file_name += dp->d_name;
|
|
|
|
std::string file;
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!android::base::ReadFileToString(file_name, &file)) continue;
|
2015-05-27 16:39:56 +02:00
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
for (const auto& line : android::base::Split(file, "\n")) {
|
|
|
|
log_time t(log_time::EPOCH);
|
2017-02-10 22:09:07 +01:00
|
|
|
char* ep = parseTime(t, line.c_str());
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!ep || (*ep != ' ')) continue;
|
2015-05-27 16:39:56 +02:00
|
|
|
// determine the time precision of the logs (eg: msec or usec)
|
|
|
|
for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
|
|
|
|
if (t.tv_nsec % (mod * 10)) {
|
|
|
|
modulo.tv_nsec = mod;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We filter any times later than current as we may not have the
|
|
|
|
// year stored with each log entry. Also, since it is possible for
|
|
|
|
// entries to be recorded out of order (very rare) we select the
|
|
|
|
// maximum we find just in case.
|
|
|
|
if ((t < now) && (t > retval)) {
|
|
|
|
retval = t;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We count on the basename file to be the definitive end, so stop here.
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!dp->d_name[len] && found) break;
|
2015-05-27 16:39:56 +02:00
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
if (retval == log_time::EPOCH) return retval;
|
2015-05-27 16:39:56 +02:00
|
|
|
// tail_time prints matching or higher, round up by the modulo to prevent
|
|
|
|
// a replay of the last entry we have just checked.
|
|
|
|
retval += modulo;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* getenv(android_logcat_context_internal* context, const char* name) {
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!context->envp || !name || !*name) return nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
|
|
|
|
for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
|
|
|
|
if (strncmp(context->envp[i], name, len)) continue;
|
|
|
|
if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
|
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
return nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
} // namespace android
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
void reportErrorName(const char** current, const char* name,
|
2017-01-20 23:07:29 +01:00
|
|
|
bool blockSecurity) {
|
2017-03-01 17:30:06 +01:00
|
|
|
if (*current) return;
|
|
|
|
if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) {
|
|
|
|
*current = name;
|
2017-01-20 23:07:29 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static int __logcat(android_logcat_context_internal* context) {
|
2015-04-10 14:51:39 +02:00
|
|
|
using namespace android;
|
2009-03-04 04:32:55 +01:00
|
|
|
int err;
|
2017-02-28 18:20:31 +01:00
|
|
|
bool hasSetLogFormat = false;
|
2017-01-20 23:07:29 +01:00
|
|
|
bool clearLog = false;
|
|
|
|
bool allSelected = false;
|
|
|
|
bool getLogSize = false;
|
|
|
|
bool getPruneList = false;
|
|
|
|
bool printStatistics = false;
|
|
|
|
bool printDividers = false;
|
2014-02-11 21:29:31 +01:00
|
|
|
unsigned long setLogSize = 0;
|
2017-03-03 00:09:41 +01:00
|
|
|
const char* setPruneList = nullptr;
|
|
|
|
const char* setId = nullptr;
|
2015-01-26 19:46:44 +01:00
|
|
|
int mode = ANDROID_LOG_RDONLY;
|
2017-02-10 22:09:07 +01:00
|
|
|
std::string forceFilters;
|
2010-02-26 19:04:23 +01:00
|
|
|
log_device_t* dev;
|
2017-02-10 22:09:07 +01:00
|
|
|
struct logger_list* logger_list;
|
2015-04-10 14:51:39 +02:00
|
|
|
size_t tail_lines = 0;
|
2014-02-15 01:05:05 +01:00
|
|
|
log_time tail_time(log_time::EPOCH);
|
2015-06-05 23:10:12 +02:00
|
|
|
size_t pid = 0;
|
2016-03-17 22:04:52 +01:00
|
|
|
bool got_t = false;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
// object instantiations before goto's can happen
|
|
|
|
log_device_t unexpected("unexpected", false);
|
2017-03-01 17:30:06 +01:00
|
|
|
const char* openDeviceFail = nullptr;
|
|
|
|
const char* clearFail = nullptr;
|
|
|
|
const char* setSizeFail = nullptr;
|
|
|
|
const char* getSizeFail = nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
int argc = context->argc;
|
|
|
|
char* const* argv = context->argv;
|
|
|
|
|
|
|
|
context->output = stdout;
|
|
|
|
context->error = stderr;
|
2013-12-13 20:10:11 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
// Simulate shell stderr redirect parsing
|
|
|
|
if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
|
|
|
|
|
2017-02-17 22:15:51 +01:00
|
|
|
// Append to file not implemented, just open file
|
2017-02-10 22:09:07 +01:00
|
|
|
size_t skip = (argv[i][2] == '>') + 2;
|
|
|
|
if (!strcmp(&argv[i][skip], "/dev/null")) {
|
|
|
|
context->stderr_null = true;
|
|
|
|
} else if (!strcmp(&argv[i][skip], "&1")) {
|
|
|
|
context->stderr_stdout = true;
|
|
|
|
} else {
|
|
|
|
// stderr file redirections are not supported
|
|
|
|
fprintf(context->stderr_stdout ? stdout : stderr,
|
|
|
|
"stderr redirection to file %s unsupported, skipping\n",
|
|
|
|
&argv[i][skip]);
|
|
|
|
}
|
2017-02-17 22:15:51 +01:00
|
|
|
// Only the first one
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
const char* filename = nullptr;
|
2017-02-17 22:15:51 +01:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
// Simulate shell stdout redirect parsing
|
|
|
|
if (argv[i][0] != '>') continue;
|
|
|
|
|
|
|
|
// Append to file not implemented, just open file
|
|
|
|
filename = &argv[i][(argv[i][1] == '>') + 1];
|
|
|
|
// Only the first one
|
|
|
|
break;
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with setting up file descriptors and FILE pointers
|
2017-03-01 17:30:06 +01:00
|
|
|
if (context->error_fd >= 0) { // Is an error file descriptor supplied?
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->error_fd == context->output_fd) {
|
|
|
|
context->stderr_stdout = true;
|
2017-03-01 17:30:06 +01:00
|
|
|
} else if (context->stderr_null) { // redirection told us to close it
|
2017-02-10 22:09:07 +01:00
|
|
|
close(context->error_fd);
|
|
|
|
context->error_fd = -1;
|
2017-03-01 17:30:06 +01:00
|
|
|
} else { // All Ok, convert error to a FILE pointer
|
2017-02-10 22:09:07 +01:00
|
|
|
context->error = fdopen(context->error_fd, "web");
|
|
|
|
if (!context->error) {
|
|
|
|
context->retval = -errno;
|
|
|
|
fprintf(context->stderr_stdout ? stdout : stderr,
|
|
|
|
"Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
|
|
|
|
strerror(errno));
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
if (context->output_fd >= 0) { // Is an output file descriptor supplied?
|
|
|
|
if (filename) { // redirect to file, close supplied file descriptor.
|
2017-02-17 22:15:51 +01:00
|
|
|
close(context->output_fd);
|
|
|
|
context->output_fd = -1;
|
2017-03-01 17:30:06 +01:00
|
|
|
} else { // All Ok, convert output to a FILE pointer
|
2017-02-17 22:15:51 +01:00
|
|
|
context->output = fdopen(context->output_fd, "web");
|
|
|
|
if (!context->output) {
|
|
|
|
context->retval = -errno;
|
|
|
|
fprintf(context->stderr_stdout ? stdout : context->error,
|
|
|
|
"Failed to fdopen(output_fd=%d) %s\n",
|
|
|
|
context->output_fd, strerror(errno));
|
|
|
|
goto exit;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
if (filename) { // We supplied an output file redirected in command line
|
2017-02-17 22:15:51 +01:00
|
|
|
context->output = fopen(filename, "web");
|
|
|
|
}
|
|
|
|
// Deal with 2>&1
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->stderr_stdout) context->error = context->output;
|
2017-02-17 22:15:51 +01:00
|
|
|
// Deal with 2>/dev/null
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->stderr_null) {
|
|
|
|
context->error_fd = -1;
|
2017-03-01 17:30:06 +01:00
|
|
|
context->error = nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
2017-02-17 22:15:51 +01:00
|
|
|
// Only happens if output=stdout or output=filename
|
2017-02-10 22:09:07 +01:00
|
|
|
if ((context->output_fd < 0) && context->output) {
|
|
|
|
context->output_fd = fileno(context->output);
|
|
|
|
}
|
|
|
|
// Only happens if error=stdout || error=stderr
|
|
|
|
if ((context->error_fd < 0) && context->error) {
|
|
|
|
context->error_fd = fileno(context->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
context->logformat = android_log_format_new();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (argc == 2 && !strcmp(argv[1], "--help")) {
|
2017-02-10 22:09:07 +01:00
|
|
|
show_help(context);
|
|
|
|
context->retval = EXIT_SUCCESS;
|
|
|
|
goto exit;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-28 18:20:31 +01:00
|
|
|
// meant to catch comma-delimited values, but cast a wider
|
|
|
|
// net for stability dealing with possible mistaken inputs.
|
|
|
|
static const char delimiters[] = ",:; \t\n\r\f";
|
|
|
|
|
2018-06-16 00:16:20 +02:00
|
|
|
optind = 0;
|
|
|
|
while (true) {
|
2015-06-05 23:10:12 +02:00
|
|
|
int option_index = 0;
|
2016-03-30 18:38:31 +02:00
|
|
|
// list of long-argument only strings for later comparison
|
2015-06-05 23:10:12 +02:00
|
|
|
static const char pid_str[] = "pid";
|
2016-11-17 00:28:31 +01:00
|
|
|
static const char debug_str[] = "debug";
|
2016-08-03 23:20:41 +02:00
|
|
|
static const char id_str[] = "id";
|
2015-11-30 22:48:56 +01:00
|
|
|
static const char wrap_str[] = "wrap";
|
2016-03-30 18:38:31 +02:00
|
|
|
static const char print_str[] = "print";
|
2017-02-10 22:09:07 +01:00
|
|
|
// clang-format off
|
2015-06-05 23:10:12 +02:00
|
|
|
static const struct option long_options[] = {
|
2017-03-01 17:30:06 +01:00
|
|
|
{ "binary", no_argument, nullptr, 'B' },
|
|
|
|
{ "buffer", required_argument, nullptr, 'b' },
|
|
|
|
{ "buffer-size", optional_argument, nullptr, 'g' },
|
|
|
|
{ "clear", no_argument, nullptr, 'c' },
|
|
|
|
{ debug_str, no_argument, nullptr, 0 },
|
|
|
|
{ "dividers", no_argument, nullptr, 'D' },
|
|
|
|
{ "file", required_argument, nullptr, 'f' },
|
|
|
|
{ "format", required_argument, nullptr, 'v' },
|
2016-04-01 16:52:20 +02:00
|
|
|
// hidden and undocumented reserved alias for --regex
|
2017-03-01 17:30:06 +01:00
|
|
|
{ "grep", required_argument, nullptr, 'e' },
|
2016-03-30 18:15:09 +02:00
|
|
|
// hidden and undocumented reserved alias for --max-count
|
2017-03-01 17:30:06 +01:00
|
|
|
{ "head", required_argument, nullptr, 'm' },
|
2017-04-03 18:30:20 +02:00
|
|
|
{ "help", no_argument, nullptr, 'h' },
|
2017-03-01 17:30:06 +01:00
|
|
|
{ id_str, required_argument, nullptr, 0 },
|
|
|
|
{ "last", no_argument, nullptr, 'L' },
|
|
|
|
{ "max-count", required_argument, nullptr, 'm' },
|
|
|
|
{ pid_str, required_argument, nullptr, 0 },
|
|
|
|
{ print_str, no_argument, nullptr, 0 },
|
|
|
|
{ "prune", optional_argument, nullptr, 'p' },
|
|
|
|
{ "regex", required_argument, nullptr, 'e' },
|
|
|
|
{ "rotate-count", required_argument, nullptr, 'n' },
|
|
|
|
{ "rotate-kbytes", required_argument, nullptr, 'r' },
|
|
|
|
{ "statistics", no_argument, nullptr, 'S' },
|
2016-03-30 18:15:09 +02:00
|
|
|
// hidden and undocumented reserved alias for -t
|
2017-03-01 17:30:06 +01:00
|
|
|
{ "tail", required_argument, nullptr, 't' },
|
2015-11-30 22:48:56 +01:00
|
|
|
// support, but ignore and do not document, the optional argument
|
2017-03-01 17:30:06 +01:00
|
|
|
{ wrap_str, optional_argument, nullptr, 0 },
|
|
|
|
{ nullptr, 0, nullptr, 0 }
|
2015-06-05 23:10:12 +02:00
|
|
|
};
|
2017-02-10 22:09:07 +01:00
|
|
|
// clang-format on
|
2015-06-05 23:10:12 +02:00
|
|
|
|
2018-06-16 00:16:20 +02:00
|
|
|
int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
|
|
|
|
&option_index);
|
|
|
|
if (c == -1) break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-06-16 00:16:20 +02:00
|
|
|
switch (c) {
|
2015-06-05 23:10:12 +02:00
|
|
|
case 0:
|
2016-08-03 23:20:41 +02:00
|
|
|
// only long options
|
2015-06-05 23:10:12 +02:00
|
|
|
if (long_options[option_index].name == pid_str) {
|
|
|
|
// ToDo: determine runtime PID_MAX?
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!getSizeTArg(optarg, &pid, 1)) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
|
2018-06-16 00:16:20 +02:00
|
|
|
long_options[option_index].name, optarg);
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2015-06-05 23:10:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-11-30 22:48:56 +01:00
|
|
|
if (long_options[option_index].name == wrap_str) {
|
2017-02-10 22:09:07 +01:00
|
|
|
mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
|
2015-11-30 22:48:56 +01:00
|
|
|
ANDROID_LOG_NONBLOCK;
|
|
|
|
// ToDo: implement API that supports setting a wrap timeout
|
|
|
|
size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
|
2018-06-16 00:16:20 +02:00
|
|
|
if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
|
2018-06-16 00:16:20 +02:00
|
|
|
long_options[option_index].name, optarg);
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2015-11-30 22:48:56 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
|
|
|
|
context->error) {
|
|
|
|
fprintf(context->error,
|
2015-11-30 22:48:56 +01:00
|
|
|
"WARNING: %s %u seconds, ignoring %zu\n",
|
|
|
|
long_options[option_index].name,
|
|
|
|
ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-03-30 18:38:31 +02:00
|
|
|
if (long_options[option_index].name == print_str) {
|
2017-02-10 22:09:07 +01:00
|
|
|
context->printItAnyways = true;
|
2016-03-30 18:38:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-11-17 00:28:31 +01:00
|
|
|
if (long_options[option_index].name == debug_str) {
|
2017-02-10 22:09:07 +01:00
|
|
|
context->debug = true;
|
2016-11-17 00:28:31 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-08-03 23:20:41 +02:00
|
|
|
if (long_options[option_index].name == id_str) {
|
2018-06-16 00:16:20 +02:00
|
|
|
setId = (optarg && optarg[0]) ? optarg : nullptr;
|
2016-08-03 23:20:41 +02:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2015-06-05 23:10:12 +02:00
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
case 's':
|
2009-03-04 04:32:55 +01:00
|
|
|
// default to all silent
|
2017-02-10 22:09:07 +01:00
|
|
|
android_log_addFilterRule(context->logformat, "*:s");
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
case 'c':
|
2017-01-20 23:07:29 +01:00
|
|
|
clearLog = true;
|
2015-01-26 19:46:44 +01:00
|
|
|
mode |= ANDROID_LOG_WRONLY;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-12-15 19:01:31 +01:00
|
|
|
case 'L':
|
2017-02-10 22:09:07 +01:00
|
|
|
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
|
|
|
|
ANDROID_LOG_NONBLOCK;
|
|
|
|
break;
|
2014-12-15 19:01:31 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
case 'd':
|
2015-01-26 19:46:44 +01:00
|
|
|
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-03-12 05:32:17 +01:00
|
|
|
case 't':
|
2016-03-17 22:04:52 +01:00
|
|
|
got_t = true;
|
2015-01-26 19:46:44 +01:00
|
|
|
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
|
2018-09-13 20:08:41 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2013-12-09 22:47:00 +01:00
|
|
|
case 'T':
|
2018-06-16 00:16:20 +02:00
|
|
|
if (strspn(optarg, "0123456789") != strlen(optarg)) {
|
|
|
|
char* cp = parseTime(tail_time, optarg);
|
2014-02-15 01:05:05 +01:00
|
|
|
if (!cp) {
|
2018-06-16 00:16:20 +02:00
|
|
|
logcat_panic(context, HELP_FALSE, "-%c \"%s\" not in time format\n", c,
|
|
|
|
optarg);
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2014-02-15 01:05:05 +01:00
|
|
|
}
|
|
|
|
if (*cp) {
|
2018-06-16 00:16:20 +02:00
|
|
|
char ch = *cp;
|
2014-02-15 01:05:05 +01:00
|
|
|
*cp = '\0';
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->error) {
|
2018-06-16 00:16:20 +02:00
|
|
|
fprintf(context->error, "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
|
|
|
|
c, optarg, ch, cp + 1);
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
2018-06-16 00:16:20 +02:00
|
|
|
*cp = ch;
|
2014-02-15 01:05:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!getSizeTArg(optarg, &tail_lines, 1)) {
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->error) {
|
2018-06-16 00:16:20 +02:00
|
|
|
fprintf(context->error, "WARNING: -%c %s invalid, setting to 1\n", c,
|
|
|
|
optarg);
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
2014-02-15 01:05:05 +01:00
|
|
|
tail_lines = 1;
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2010-03-12 05:32:17 +01:00
|
|
|
|
2015-01-26 22:41:33 +01:00
|
|
|
case 'D':
|
|
|
|
printDividers = true;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2015-01-26 22:41:33 +01:00
|
|
|
|
2016-03-18 00:18:55 +01:00
|
|
|
case 'e':
|
2018-06-16 00:16:20 +02:00
|
|
|
context->regex = new pcrecpp::RE(optarg);
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2016-03-18 00:18:55 +01:00
|
|
|
|
2016-03-17 22:04:52 +01:00
|
|
|
case 'm': {
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!getSizeTArg(optarg, &context->maxCount)) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE,
|
2018-06-16 00:16:20 +02:00
|
|
|
"-%c \"%s\" isn't an integer greater than zero\n", c, optarg);
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2016-03-17 22:04:52 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
} break;
|
2016-03-17 22:04:52 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
case 'g':
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!optarg) {
|
2017-01-20 23:07:29 +01:00
|
|
|
getLogSize = true;
|
logcat: Add low hanging fruit long arguments
Add long arguments file, rotate_kbytes, rotate_count, format,
dividers, clear, buffer_size, last, buffer, binary, statistics
and prune.
Bug: 21615139
Change-Id: Ieb1bbe3d47e6711a1e91c74522630d39f8981621
2015-11-30 21:57:56 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-09-13 20:08:41 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
case 'G': {
|
2017-02-10 22:09:07 +01:00
|
|
|
char* cp;
|
2018-06-16 00:16:20 +02:00
|
|
|
if (strtoll(optarg, &cp, 0) > 0) {
|
|
|
|
setLogSize = strtoll(optarg, &cp, 0);
|
2015-04-10 14:51:39 +02:00
|
|
|
} else {
|
|
|
|
setLogSize = 0;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
switch (*cp) {
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
setLogSize *= 1024;
|
2018-09-13 20:08:41 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2017-02-10 22:09:07 +01:00
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
setLogSize *= 1024;
|
2018-09-13 20:08:41 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2017-02-10 22:09:07 +01:00
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
setLogSize *= 1024;
|
2018-09-13 20:08:41 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2017-02-10 22:09:07 +01:00
|
|
|
case '\0':
|
|
|
|
break;
|
2014-02-11 21:29:31 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
default:
|
|
|
|
setLogSize = 0;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!setLogSize) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE,
|
|
|
|
"ERROR: -G <num><multiplier>\n");
|
|
|
|
goto exit;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
} break;
|
2014-02-11 21:29:31 +01:00
|
|
|
|
|
|
|
case 'p':
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!optarg) {
|
2017-01-20 23:07:29 +01:00
|
|
|
getPruneList = true;
|
logcat: Add low hanging fruit long arguments
Add long arguments file, rotate_kbytes, rotate_count, format,
dividers, clear, buffer_size, last, buffer, binary, statistics
and prune.
Bug: 21615139
Change-Id: Ieb1bbe3d47e6711a1e91c74522630d39f8981621
2015-11-30 21:57:56 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-09-13 20:08:41 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2014-02-11 21:29:31 +01:00
|
|
|
|
|
|
|
case 'P':
|
2018-06-16 00:16:20 +02:00
|
|
|
setPruneList = optarg;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2014-02-11 21:29:31 +01:00
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
case 'b': {
|
2018-06-16 00:16:20 +02:00
|
|
|
std::unique_ptr<char, void (*)(void*)> buffers(strdup(optarg), free);
|
2017-03-03 00:09:41 +01:00
|
|
|
char* arg = buffers.get();
|
2016-04-11 23:03:48 +02:00
|
|
|
unsigned idMask = 0;
|
2017-02-28 18:20:31 +01:00
|
|
|
char* sv = nullptr; // protect against -ENOMEM above
|
2017-03-03 00:09:41 +01:00
|
|
|
while (!!(arg = strtok_r(arg, delimiters, &sv))) {
|
|
|
|
if (!strcmp(arg, "default")) {
|
2017-02-10 22:09:07 +01:00
|
|
|
idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
|
2016-04-11 23:03:48 +02:00
|
|
|
(1 << LOG_ID_CRASH);
|
2017-03-03 00:09:41 +01:00
|
|
|
} else if (!strcmp(arg, "all")) {
|
2017-01-20 23:07:29 +01:00
|
|
|
allSelected = true;
|
2016-04-11 23:03:48 +02:00
|
|
|
idMask = (unsigned)-1;
|
|
|
|
} else {
|
2017-03-03 00:09:41 +01:00
|
|
|
log_id_t log_id = android_name_to_log_id(arg);
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* name = android_log_id_to_name(log_id);
|
2016-04-11 23:03:48 +02:00
|
|
|
|
2017-03-03 00:09:41 +01:00
|
|
|
if (!!strcmp(name, arg)) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_TRUE,
|
2017-03-03 00:09:41 +01:00
|
|
|
"unknown buffer %s\n", arg);
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2015-12-04 19:59:45 +01:00
|
|
|
}
|
2017-01-20 23:07:29 +01:00
|
|
|
if (log_id == LOG_ID_SECURITY) allSelected = false;
|
2016-04-11 23:03:48 +02:00
|
|
|
idMask |= (1 << log_id);
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
2017-03-03 00:09:41 +01:00
|
|
|
arg = nullptr;
|
2015-12-04 19:59:45 +01:00
|
|
|
}
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2016-04-11 23:03:48 +02:00
|
|
|
for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* name = android_log_id_to_name((log_id_t)i);
|
2016-04-11 23:03:48 +02:00
|
|
|
log_id_t log_id = android_name_to_log_id(name);
|
2014-10-11 00:25:38 +02:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (log_id != (log_id_t)i) continue;
|
|
|
|
if (!(idMask & (1 << i))) continue;
|
2014-10-11 00:25:38 +02:00
|
|
|
|
2016-04-11 23:03:48 +02:00
|
|
|
bool found = false;
|
2017-03-06 23:56:04 +01:00
|
|
|
for (dev = context->devices; dev; dev = dev->next) {
|
2016-04-11 23:03:48 +02:00
|
|
|
if (!strcmp(name, dev->device)) {
|
|
|
|
found = true;
|
2015-12-04 19:59:45 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!dev->next) break;
|
2016-04-11 23:03:48 +02:00
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
if (found) continue;
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2017-08-25 05:14:06 +02:00
|
|
|
bool binary = !strcmp(name, "events") ||
|
|
|
|
!strcmp(name, "security") ||
|
|
|
|
!strcmp(name, "stats");
|
2016-04-11 23:03:48 +02:00
|
|
|
log_device_t* d = new log_device_t(name, binary);
|
2010-02-26 19:04:23 +01:00
|
|
|
|
2015-12-04 19:59:45 +01:00
|
|
|
if (dev) {
|
2016-04-11 23:03:48 +02:00
|
|
|
dev->next = d;
|
|
|
|
dev = d;
|
|
|
|
} else {
|
2017-03-06 23:56:04 +01:00
|
|
|
context->devices = dev = d;
|
2015-12-04 19:59:45 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
context->devCount++;
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
} break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
case 'B':
|
2017-02-10 22:09:07 +01:00
|
|
|
context->printBinary = 1;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
case 'f':
|
2017-03-01 17:30:06 +01:00
|
|
|
if ((tail_time == log_time::EPOCH) && !tail_lines) {
|
2018-06-16 00:16:20 +02:00
|
|
|
tail_time = lastLogTime(optarg);
|
2015-05-27 16:39:56 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
// redirect output to a file
|
2018-06-16 00:16:20 +02:00
|
|
|
context->outputFileName = optarg;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-06-07 22:03:10 +02:00
|
|
|
case 'r':
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!getSizeTArg(optarg, &context->logRotateSizeKBytes, 1)) {
|
|
|
|
logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -r\n", optarg);
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-06-07 22:03:10 +02:00
|
|
|
case 'n':
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!getSizeTArg(optarg, &context->maxRotatedLogs, 1)) {
|
|
|
|
logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -n\n", optarg);
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-28 18:20:31 +01:00
|
|
|
case 'v': {
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
|
2017-02-10 22:09:07 +01:00
|
|
|
show_format_help(context);
|
|
|
|
context->retval = EXIT_SUCCESS;
|
|
|
|
goto exit;
|
2016-10-18 20:30:11 +02:00
|
|
|
}
|
2018-06-16 00:16:20 +02:00
|
|
|
std::unique_ptr<char, void (*)(void*)> formats(strdup(optarg), free);
|
2017-03-03 00:09:41 +01:00
|
|
|
char* arg = formats.get();
|
2017-02-28 18:20:31 +01:00
|
|
|
char* sv = nullptr; // protect against -ENOMEM above
|
2017-03-03 00:09:41 +01:00
|
|
|
while (!!(arg = strtok_r(arg, delimiters, &sv))) {
|
|
|
|
err = setLogFormat(context, arg);
|
2017-02-28 18:20:31 +01:00
|
|
|
if (err < 0) {
|
|
|
|
logcat_panic(context, HELP_FORMAT,
|
2017-03-03 00:09:41 +01:00
|
|
|
"Invalid parameter \"%s\" to -v\n", arg);
|
2017-02-28 18:20:31 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2017-03-03 00:09:41 +01:00
|
|
|
arg = nullptr;
|
2017-02-28 18:20:31 +01:00
|
|
|
if (err) hasSetLogFormat = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-02-28 18:20:31 +01:00
|
|
|
} break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
case 'Q':
|
2017-02-21 23:31:19 +01:00
|
|
|
#define LOGCAT_FILTER "androidboot.logcat="
|
|
|
|
#define CONSOLE_PIPE_OPTION "androidboot.consolepipe="
|
2017-02-10 22:09:07 +01:00
|
|
|
#define CONSOLE_OPTION "androidboot.console="
|
2017-02-21 23:31:19 +01:00
|
|
|
#define QEMU_PROPERTY "ro.kernel.qemu"
|
|
|
|
#define QEMU_CMDLINE "qemu.cmdline"
|
2017-02-10 22:09:07 +01:00
|
|
|
// This is a *hidden* option used to start a version of logcat
|
|
|
|
// in an emulated device only. It basically looks for
|
|
|
|
// androidboot.logcat= on the kernel command line. If
|
|
|
|
// something is found, it extracts a log filter and uses it to
|
2017-02-21 23:31:19 +01:00
|
|
|
// run the program. The logcat output will go to consolepipe if
|
|
|
|
// androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise,
|
|
|
|
// it goes to androidboot.console (e.g. tty)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2017-02-21 23:31:19 +01:00
|
|
|
// if not in emulator, exit quietly
|
|
|
|
if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) {
|
|
|
|
context->retval = EXIT_SUCCESS;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, "");
|
|
|
|
if (cmdline.empty()) {
|
|
|
|
android::base::ReadFileToString("/proc/cmdline", &cmdline);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER);
|
2017-02-10 22:09:07 +01:00
|
|
|
// if nothing found or invalid filters, exit quietly
|
2017-02-21 23:31:19 +01:00
|
|
|
if (!logcatFilter) {
|
2017-02-10 22:09:07 +01:00
|
|
|
context->retval = EXIT_SUCCESS;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
const char* p = logcatFilter + strlen(LOGCAT_FILTER);
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* q = strpbrk(p, " \t\n\r");
|
|
|
|
if (!q) q = p + strlen(p);
|
|
|
|
forceFilters = std::string(p, q);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
// redirect our output to the emulator console pipe or console
|
|
|
|
const char* consolePipe =
|
|
|
|
strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION);
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* console =
|
|
|
|
strstr(cmdline.c_str(), CONSOLE_OPTION);
|
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
if (consolePipe) {
|
|
|
|
p = consolePipe + strlen(CONSOLE_PIPE_OPTION);
|
|
|
|
} else if (console) {
|
|
|
|
p = console + strlen(CONSOLE_OPTION);
|
|
|
|
} else {
|
|
|
|
context->retval = EXIT_FAILURE;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
q = strpbrk(p, " \t\n\r");
|
|
|
|
int len = q ? q - p : strlen(p);
|
|
|
|
std::string devname = "/dev/" + std::string(p, len);
|
2017-02-21 23:31:19 +01:00
|
|
|
std::string pipePurpose("pipe:logcat");
|
|
|
|
if (consolePipe) {
|
|
|
|
// example: "qemu_pipe,pipe:logcat"
|
|
|
|
// upon opening of /dev/qemu_pipe, the "pipe:logcat"
|
|
|
|
// string with trailing '\0' should be written to the fd
|
2017-08-03 22:56:49 +02:00
|
|
|
size_t pos = devname.find(',');
|
2017-02-21 23:31:19 +01:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
pipePurpose = devname.substr(pos + 1);
|
|
|
|
devname = devname.substr(0, pos);
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
cmdline.erase();
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->error) {
|
|
|
|
fprintf(context->error, "logcat using %s\n",
|
|
|
|
devname.c_str());
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
FILE* fp = fopen(devname.c_str(), "web");
|
2017-02-10 22:09:07 +01:00
|
|
|
devname.erase();
|
2017-02-10 22:09:07 +01:00
|
|
|
if (!fp) break;
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
if (consolePipe) {
|
|
|
|
// need the trailing '\0'
|
|
|
|
if(!android::base::WriteFully(fileno(fp), pipePurpose.c_str(),
|
|
|
|
pipePurpose.size() + 1)) {
|
|
|
|
fclose(fp);
|
|
|
|
context->retval = EXIT_FAILURE;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
// close output and error channels, replace with console
|
2017-02-10 22:09:07 +01:00
|
|
|
android::close_output(context);
|
|
|
|
android::close_error(context);
|
|
|
|
context->stderr_stdout = true;
|
|
|
|
context->output = fp;
|
2017-02-21 23:45:58 +01:00
|
|
|
context->output_fd = fileno(fp);
|
|
|
|
if (context->stderr_null) break;
|
|
|
|
context->stderr_stdout = true;
|
|
|
|
context->error = fp;
|
|
|
|
context->error_fd = fileno(fp);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-02-06 23:48:50 +01:00
|
|
|
case 'S':
|
2017-01-20 23:07:29 +01:00
|
|
|
printStatistics = true;
|
2014-02-06 23:48:50 +01:00
|
|
|
break;
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
case ':':
|
2018-06-16 00:16:20 +02:00
|
|
|
logcat_panic(context, HELP_TRUE, "Option -%c needs an argument\n", optopt);
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2017-04-03 18:30:20 +02:00
|
|
|
case 'h':
|
|
|
|
show_help(context);
|
|
|
|
show_format_help(context);
|
|
|
|
goto exit;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
default:
|
2018-06-16 00:16:20 +02:00
|
|
|
logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n", optopt);
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->maxCount && got_t) {
|
|
|
|
logcat_panic(context, HELP_TRUE,
|
2016-10-18 20:30:11 +02:00
|
|
|
"Cannot use -m (--max-count) and -t together\n");
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2016-03-17 22:04:52 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->printItAnyways && (!context->regex || !context->maxCount)) {
|
2016-03-30 18:38:31 +02:00
|
|
|
// One day it would be nice if --print -v color and --regex <expr>
|
|
|
|
// could play with each other and show regex highlighted content.
|
2017-02-10 22:09:07 +01:00
|
|
|
// clang-format off
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->error) {
|
|
|
|
fprintf(context->error, "WARNING: "
|
2016-03-30 18:38:31 +02:00
|
|
|
"--print ignored, to be used in combination with\n"
|
2017-02-10 22:09:07 +01:00
|
|
|
" "
|
2016-03-30 18:38:31 +02:00
|
|
|
"--regex <expr> and --max-count <N>\n");
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
|
|
|
context->printItAnyways = false;
|
2016-03-30 18:38:31 +02:00
|
|
|
}
|
2016-03-17 22:04:52 +01:00
|
|
|
|
2017-03-06 23:56:04 +01:00
|
|
|
if (!context->devices) {
|
|
|
|
dev = context->devices = new log_device_t("main", false);
|
2017-02-10 22:09:07 +01:00
|
|
|
context->devCount = 1;
|
2013-11-22 19:55:48 +01:00
|
|
|
if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
|
2015-02-27 22:41:34 +01:00
|
|
|
dev = dev->next = new log_device_t("system", false);
|
2017-02-10 22:09:07 +01:00
|
|
|
context->devCount++;
|
2010-03-01 18:11:54 +01:00
|
|
|
}
|
2014-04-07 23:58:08 +02:00
|
|
|
if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
|
2015-02-27 22:41:34 +01:00
|
|
|
dev = dev->next = new log_device_t("crash", false);
|
2017-02-10 22:09:07 +01:00
|
|
|
context->devCount++;
|
2014-05-14 21:37:22 +02:00
|
|
|
}
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!!context->logRotateSizeKBytes && !context->outputFileName) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
|
|
|
|
goto exit;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!!setId) {
|
|
|
|
if (!context->outputFileName) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_TRUE,
|
|
|
|
"--id='%s' requires -f as well\n", setId);
|
|
|
|
goto exit;
|
2016-08-03 23:20:41 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
std::string file_name = android::base::StringPrintf(
|
|
|
|
"%s.id", context->outputFileName);
|
2016-08-03 23:20:41 +02:00
|
|
|
std::string file;
|
|
|
|
bool file_ok = android::base::ReadFileToString(file_name, &file);
|
2017-02-10 22:09:07 +01:00
|
|
|
android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
|
|
|
|
getuid(), getgid());
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!file_ok || !file.compare(setId)) setId = nullptr;
|
2016-08-03 23:20:41 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!hasSetLogFormat) {
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!!logFormat) {
|
2017-02-28 18:20:31 +01:00
|
|
|
std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat),
|
|
|
|
free);
|
|
|
|
char* sv = nullptr; // protect against -ENOMEM above
|
|
|
|
char* arg = formats.get();
|
|
|
|
while (!!(arg = strtok_r(arg, delimiters, &sv))) {
|
|
|
|
err = setLogFormat(context, arg);
|
|
|
|
// environment should not cause crash of logcat
|
|
|
|
if ((err < 0) && context->error) {
|
|
|
|
fprintf(context->error,
|
|
|
|
"invalid format in ANDROID_PRINTF_LOG '%s'\n", arg);
|
|
|
|
}
|
|
|
|
arg = nullptr;
|
|
|
|
if (err > 0) hasSetLogFormat = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-02-28 18:20:31 +01:00
|
|
|
}
|
|
|
|
if (!hasSetLogFormat) {
|
2017-02-10 22:09:07 +01:00
|
|
|
setLogFormat(context, "threadtime");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if (forceFilters.size()) {
|
2017-02-10 22:09:07 +01:00
|
|
|
err = android_log_addFilterString(context->logformat,
|
|
|
|
forceFilters.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
if (err < 0) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE,
|
2016-10-18 20:30:11 +02:00
|
|
|
"Invalid filter expression in logcat args\n");
|
2017-02-10 22:09:07 +01:00
|
|
|
goto exit;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-06-16 00:16:20 +02:00
|
|
|
} else if (argc == optind) {
|
2009-03-04 04:32:55 +01:00
|
|
|
// Add from environment variable
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!!env_tags_orig) {
|
2017-02-10 22:09:07 +01:00
|
|
|
err = android_log_addFilterString(context->logformat,
|
|
|
|
env_tags_orig);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
if (err < 0) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_TRUE,
|
|
|
|
"Invalid filter expression in ANDROID_LOG_TAGS\n");
|
|
|
|
goto exit;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add from commandline
|
2018-06-16 00:16:20 +02:00
|
|
|
for (int i = optind ; i < argc ; i++) {
|
2017-02-10 22:09:07 +01:00
|
|
|
// skip stderr redirections of _all_ kinds
|
|
|
|
if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
|
2017-02-17 22:15:51 +01:00
|
|
|
// skip stdout redirections of _all_ kinds
|
|
|
|
if (argv[i][0] == '>') continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
err = android_log_addFilterString(context->logformat, argv[i]);
|
2013-11-22 19:55:48 +01:00
|
|
|
if (err < 0) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_TRUE,
|
|
|
|
"Invalid filter expression '%s'\n", argv[i]);
|
|
|
|
goto exit;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:56:04 +01:00
|
|
|
dev = context->devices;
|
2014-02-15 01:05:05 +01:00
|
|
|
if (tail_time != log_time::EPOCH) {
|
2015-06-05 23:10:12 +02:00
|
|
|
logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
|
2014-02-15 01:05:05 +01:00
|
|
|
} else {
|
2015-06-05 23:10:12 +02:00
|
|
|
logger_list = android_logger_list_alloc(mode, tail_lines, pid);
|
2014-02-15 01:05:05 +01:00
|
|
|
}
|
2015-09-17 00:34:00 +02:00
|
|
|
// We have three orthogonal actions below to clear, set log size and
|
|
|
|
// get log size. All sharing the same iteration loop.
|
2010-02-26 19:04:23 +01:00
|
|
|
while (dev) {
|
2013-11-22 19:55:48 +01:00
|
|
|
dev->logger_list = logger_list;
|
|
|
|
dev->logger = android_logger_open(logger_list,
|
|
|
|
android_name_to_log_id(dev->device));
|
|
|
|
if (!dev->logger) {
|
2017-01-20 23:07:29 +01:00
|
|
|
reportErrorName(&openDeviceFail, dev->device, allSelected);
|
2015-09-17 00:34:00 +02:00
|
|
|
dev = dev->next;
|
|
|
|
continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-08-03 23:20:41 +02:00
|
|
|
if (clearLog || setId) {
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->outputFileName) {
|
2016-06-06 23:56:00 +02:00
|
|
|
int maxRotationCountDigits =
|
2017-02-10 22:09:07 +01:00
|
|
|
(context->maxRotatedLogs > 0) ?
|
|
|
|
(int)(floor(log10(context->maxRotatedLogs) + 1)) :
|
|
|
|
0;
|
2016-06-06 23:56:00 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
|
2016-08-03 23:20:41 +02:00
|
|
|
std::string file;
|
2016-06-06 23:56:00 +02:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!i) {
|
2017-02-10 22:09:07 +01:00
|
|
|
file = android::base::StringPrintf(
|
2017-02-10 22:09:07 +01:00
|
|
|
"%s", context->outputFileName);
|
|
|
|
} else {
|
|
|
|
file = android::base::StringPrintf("%s.%.*d",
|
|
|
|
context->outputFileName, maxRotationCountDigits, i);
|
2016-06-06 23:56:00 +02:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!file.length()) {
|
2016-06-06 23:56:00 +02:00
|
|
|
perror("while clearing log files");
|
2017-01-20 23:07:29 +01:00
|
|
|
reportErrorName(&clearFail, dev->device, allSelected);
|
2016-06-06 23:56:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-03 23:20:41 +02:00
|
|
|
err = unlink(file.c_str());
|
2016-06-06 23:56:00 +02:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (err < 0 && errno != ENOENT && !clearFail) {
|
2016-06-06 23:56:00 +02:00
|
|
|
perror("while clearing log files");
|
2017-01-20 23:07:29 +01:00
|
|
|
reportErrorName(&clearFail, dev->device, allSelected);
|
2016-06-06 23:56:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (android_logger_clear(dev->logger)) {
|
2017-01-20 23:07:29 +01:00
|
|
|
reportErrorName(&clearFail, dev->device, allSelected);
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-09-17 00:34:00 +02:00
|
|
|
if (setLogSize) {
|
|
|
|
if (android_logger_set_log_size(dev->logger, setLogSize)) {
|
2017-01-20 23:07:29 +01:00
|
|
|
reportErrorName(&setSizeFail, dev->device, allSelected);
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
if (getLogSize) {
|
2015-09-17 00:34:00 +02:00
|
|
|
long size = android_logger_get_log_size(dev->logger);
|
|
|
|
long readable = android_logger_get_log_readable_size(dev->logger);
|
|
|
|
|
|
|
|
if ((size < 0) || (readable < 0)) {
|
2017-01-20 23:07:29 +01:00
|
|
|
reportErrorName(&getSizeFail, dev->device, allSelected);
|
2015-09-17 00:34:00 +02:00
|
|
|
} else {
|
2018-09-05 20:05:57 +02:00
|
|
|
auto size_format = format_of_size(size);
|
|
|
|
auto readable_format = format_of_size(readable);
|
2017-02-10 22:09:07 +01:00
|
|
|
std::string str = android::base::StringPrintf(
|
2018-09-05 20:05:57 +02:00
|
|
|
"%s: ring buffer is %lu %sB (%lu %sB consumed),"
|
|
|
|
" max entry is %d B, max payload is %d B\n",
|
2017-02-10 22:09:07 +01:00
|
|
|
dev->device,
|
2018-09-05 20:05:57 +02:00
|
|
|
size_format.first, size_format.second,
|
|
|
|
readable_format.first, readable_format.second,
|
2017-02-10 22:09:07 +01:00
|
|
|
(int)LOGGER_ENTRY_MAX_LEN,
|
|
|
|
(int)LOGGER_ENTRY_MAX_PAYLOAD);
|
|
|
|
TEMP_FAILURE_RETRY(write(context->output_fd,
|
|
|
|
str.data(), str.length()));
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
dev = dev->next;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
|
|
|
|
context->retval = EXIT_SUCCESS;
|
|
|
|
|
2015-09-17 00:34:00 +02:00
|
|
|
// report any errors in the above loop and exit
|
|
|
|
if (openDeviceFail) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE,
|
|
|
|
"Unable to open log device '%s'\n", openDeviceFail);
|
|
|
|
goto close;
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
|
|
|
if (clearFail) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE,
|
|
|
|
"failed to clear the '%s' log\n", clearFail);
|
|
|
|
goto close;
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
|
|
|
if (setSizeFail) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE,
|
|
|
|
"failed to set the '%s' log size\n", setSizeFail);
|
|
|
|
goto close;
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
|
|
|
if (getSizeFail) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE,
|
|
|
|
"failed to get the readable '%s' log size", getSizeFail);
|
|
|
|
goto close;
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
2010-02-26 19:04:23 +01:00
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
if (setPruneList) {
|
2015-04-10 14:51:39 +02:00
|
|
|
size_t len = strlen(setPruneList);
|
2017-02-10 22:09:07 +01:00
|
|
|
// extra 32 bytes are needed by android_logger_set_prune_list
|
2015-04-10 14:51:39 +02:00
|
|
|
size_t bLen = len + 32;
|
2017-03-01 17:30:06 +01:00
|
|
|
char* buf = nullptr;
|
2015-04-10 14:51:39 +02:00
|
|
|
if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
|
|
|
|
buf[len] = '\0';
|
|
|
|
if (android_logger_set_prune_list(logger_list, buf, bLen)) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE,
|
|
|
|
"failed to set the prune list");
|
2015-04-10 14:51:39 +02:00
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
} else {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE,
|
|
|
|
"failed to set the prune list (alloc)");
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
goto close;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
|
2014-04-02 02:19:47 +02:00
|
|
|
if (printStatistics || getPruneList) {
|
2014-02-06 23:48:50 +01:00
|
|
|
size_t len = 8192;
|
2017-02-10 22:09:07 +01:00
|
|
|
char* buf;
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
|
2017-03-01 17:30:06 +01:00
|
|
|
delete[] buf, buf = nullptr, --retry) {
|
2014-02-11 21:29:31 +01:00
|
|
|
if (getPruneList) {
|
|
|
|
android_logger_get_prune_list(logger_list, buf, len);
|
|
|
|
} else {
|
|
|
|
android_logger_get_statistics(logger_list, buf, len);
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
buf[len - 1] = '\0';
|
2015-04-10 14:51:39 +02:00
|
|
|
if (atol(buf) < 3) {
|
2017-02-10 22:09:07 +01:00
|
|
|
delete[] buf;
|
2017-03-01 17:30:06 +01:00
|
|
|
buf = nullptr;
|
2014-02-06 23:48:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
size_t ret = atol(buf) + 1;
|
|
|
|
if (ret <= len) {
|
|
|
|
len = ret;
|
2014-02-06 23:48:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
len = ret;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!buf) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE, "failed to read data");
|
|
|
|
goto close;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove trailing FF
|
2017-02-10 22:09:07 +01:00
|
|
|
char* cp = buf + len - 1;
|
2014-02-06 23:48:50 +01:00
|
|
|
*cp = '\0';
|
|
|
|
bool truncated = *--cp != '\f';
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!truncated) *cp = '\0';
|
2014-02-06 23:48:50 +01:00
|
|
|
|
|
|
|
// squash out the byte count
|
|
|
|
cp = buf;
|
|
|
|
if (!truncated) {
|
2017-03-01 17:30:06 +01:00
|
|
|
while (isdigit(*cp)) ++cp;
|
|
|
|
if (*cp == '\n') ++cp;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
len = strlen(cp);
|
|
|
|
TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
|
2017-02-10 22:09:07 +01:00
|
|
|
delete[] buf;
|
2017-02-10 22:09:07 +01:00
|
|
|
goto close;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (getLogSize || setLogSize || clearLog) goto close;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK));
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->stop) goto close;
|
2016-08-03 23:20:41 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
// LOG_EVENT_INT(10, 12345);
|
|
|
|
// LOG_EVENT_LONG(11, 0x1122334455667788LL);
|
|
|
|
// LOG_EVENT_STRING(0, "whassup, doc?");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
dev = nullptr;
|
2016-03-17 22:04:52 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
while (!context->stop &&
|
|
|
|
(!context->maxCount || (context->printCount < context->maxCount))) {
|
2013-11-22 19:55:48 +01:00
|
|
|
struct log_msg log_msg;
|
|
|
|
int ret = android_logger_list_read(logger_list, &log_msg);
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!ret) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
|
|
|
|
break;
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2017-03-01 17:30:06 +01:00
|
|
|
if (ret == -EAGAIN) break;
|
2013-11-22 19:55:48 +01:00
|
|
|
|
|
|
|
if (ret == -EIO) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
|
|
|
|
break;
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
if (ret == -EINVAL) {
|
2017-02-10 22:09:07 +01:00
|
|
|
logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
|
|
|
|
break;
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
2017-07-08 17:48:00 +02:00
|
|
|
logcat_panic(context, HELP_FALSE, "logcat read failure\n");
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
log_device_t* d;
|
2017-03-06 23:56:04 +01:00
|
|
|
for (d = context->devices; d; d = d->next) {
|
2017-03-01 17:30:06 +01:00
|
|
|
if (android_name_to_log_id(d->device) == log_msg.id()) break;
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
2015-01-26 22:41:33 +01:00
|
|
|
if (!d) {
|
2017-02-10 22:09:07 +01:00
|
|
|
context->devCount = 2; // set to Multiple
|
2015-02-26 23:33:35 +01:00
|
|
|
d = &unexpected;
|
|
|
|
d->binary = log_msg.id() == LOG_ID_EVENTS;
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 22:41:33 +01:00
|
|
|
if (dev != d) {
|
|
|
|
dev = d;
|
2017-02-10 22:09:07 +01:00
|
|
|
maybePrintStart(context, dev, printDividers);
|
|
|
|
if (context->stop) break;
|
2015-01-26 22:41:33 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->printBinary) {
|
|
|
|
printBinary(context, &log_msg);
|
2013-11-22 19:55:48 +01:00
|
|
|
} else {
|
2017-02-10 22:09:07 +01:00
|
|
|
processBuffer(context, dev, &log_msg);
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
close:
|
2017-03-06 23:56:04 +01:00
|
|
|
// Short and sweet. Implemented generic version in android_logcat_destroy.
|
|
|
|
while (!!(dev = context->devices)) {
|
|
|
|
context->devices = dev->next;
|
|
|
|
delete dev;
|
|
|
|
}
|
2013-11-22 19:55:48 +01:00
|
|
|
android_logger_list_free(logger_list);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
exit:
|
2017-02-10 22:09:07 +01:00
|
|
|
// close write end of pipe to help things along
|
|
|
|
if (context->output_fd == context->fds[1]) {
|
|
|
|
android::close_output(context);
|
|
|
|
}
|
|
|
|
if (context->error_fd == context->fds[1]) {
|
|
|
|
android::close_error(context);
|
|
|
|
}
|
|
|
|
if (context->fds[1] >= 0) {
|
|
|
|
// NB: should be closed by the above
|
|
|
|
int save_errno = errno;
|
|
|
|
close(context->fds[1]);
|
|
|
|
errno = save_errno;
|
|
|
|
context->fds[1] = -1;
|
|
|
|
}
|
|
|
|
context->thread_stopped = true;
|
2017-02-10 22:09:07 +01:00
|
|
|
return context->retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can block
|
|
|
|
int android_logcat_run_command(android_logcat_context ctx,
|
|
|
|
int output, int error,
|
|
|
|
int argc, char* const* argv,
|
|
|
|
char* const* envp) {
|
|
|
|
android_logcat_context_internal* context = ctx;
|
|
|
|
|
|
|
|
context->output_fd = output;
|
|
|
|
context->error_fd = error;
|
|
|
|
context->argc = argc;
|
|
|
|
context->argv = argv;
|
|
|
|
context->envp = envp;
|
|
|
|
context->stop = false;
|
2017-02-10 22:09:07 +01:00
|
|
|
context->thread_stopped = false;
|
2017-02-10 22:09:07 +01:00
|
|
|
return __logcat(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finished with context
|
|
|
|
int android_logcat_destroy(android_logcat_context* ctx) {
|
|
|
|
android_logcat_context_internal* context = *ctx;
|
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!context) return -EBADF;
|
|
|
|
|
|
|
|
*ctx = nullptr;
|
2017-02-10 22:09:07 +01:00
|
|
|
|
|
|
|
context->stop = true;
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
while (context->thread_stopped == false) {
|
2017-03-01 17:30:06 +01:00
|
|
|
// Makes me sad, replace thread_stopped with semaphore. Short lived.
|
2017-02-10 22:09:07 +01:00
|
|
|
sched_yield();
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
delete context->regex;
|
2017-02-10 22:09:07 +01:00
|
|
|
context->argv_hold.clear();
|
|
|
|
context->args.clear();
|
|
|
|
context->envp_hold.clear();
|
|
|
|
context->envs.clear();
|
|
|
|
if (context->fds[0] >= 0) {
|
|
|
|
close(context->fds[0]);
|
|
|
|
context->fds[0] = -1;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
android::close_output(context);
|
|
|
|
android::close_error(context);
|
2017-10-19 00:42:00 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if (context->fds[1] >= 0) {
|
2017-10-19 00:42:00 +02:00
|
|
|
// NB: this should be closed by close_output, but just in case...
|
2017-02-10 22:09:07 +01:00
|
|
|
close(context->fds[1]);
|
|
|
|
context->fds[1] = -1;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
|
|
|
|
android_closeEventTagMap(context->eventTagMap);
|
|
|
|
|
2017-03-06 23:56:04 +01:00
|
|
|
// generic cleanup of devices list to handle all possible dirty cases
|
|
|
|
log_device_t* dev;
|
|
|
|
while (!!(dev = context->devices)) {
|
|
|
|
struct logger_list* logger_list = dev->logger_list;
|
|
|
|
if (logger_list) {
|
|
|
|
for (log_device_t* d = dev; d; d = d->next) {
|
|
|
|
if (d->logger_list == logger_list) d->logger_list = nullptr;
|
|
|
|
}
|
|
|
|
android_logger_list_free(logger_list);
|
|
|
|
}
|
|
|
|
context->devices = dev->next;
|
|
|
|
delete dev;
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
int retval = context->retval;
|
|
|
|
|
|
|
|
free(context);
|
|
|
|
|
|
|
|
return retval;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|