2015-01-26 22:41:33 +01:00
|
|
|
// Copyright 2006-2015 The Android Open Source Project
|
2009-03-04 04:32:55 +01:00
|
|
|
|
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>
|
2014-10-17 07:19:55 +02:00
|
|
|
#include <math.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <signal.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
|
|
|
|
2015-05-27 16:39:56 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <base/file.h>
|
|
|
|
#include <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/log.h>
|
2014-02-15 01:05:05 +01:00
|
|
|
#include <log/log_read.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <log/logd.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <log/logger.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <log/logprint.h>
|
2015-06-09 08:36:34 +02:00
|
|
|
#include <utils/threads.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#define DEFAULT_MAX_ROTATED_LOGS 4
|
|
|
|
|
|
|
|
static AndroidLogFormat * g_logformat;
|
|
|
|
|
|
|
|
/* logd prefixes records with a length field */
|
|
|
|
#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
struct log_device_t {
|
2013-11-22 19:55:48 +01:00
|
|
|
const char* device;
|
2010-02-26 19:04:23 +01:00
|
|
|
bool binary;
|
2013-11-22 19:55:48 +01:00
|
|
|
struct logger *logger;
|
|
|
|
struct logger_list *logger_list;
|
2010-02-26 19:04:23 +01:00
|
|
|
bool printed;
|
|
|
|
|
|
|
|
log_device_t* next;
|
|
|
|
|
2015-02-27 22:41:34 +01:00
|
|
|
log_device_t(const char* d, bool b) {
|
2010-02-26 19:04:23 +01:00
|
|
|
device = d;
|
|
|
|
binary = b;
|
|
|
|
next = NULL;
|
|
|
|
printed = false;
|
2015-04-10 14:51:39 +02:00
|
|
|
logger = NULL;
|
|
|
|
logger_list = NULL;
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
|
|
|
};
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
/* Global Variables */
|
|
|
|
|
|
|
|
static const char * g_outputFileName = NULL;
|
2015-04-10 14:51:39 +02:00
|
|
|
// 0 means "no log rotation"
|
|
|
|
static size_t g_logRotateSizeKBytes = 0;
|
|
|
|
// 0 means "unbounded"
|
|
|
|
static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
|
2009-03-04 04:32:55 +01:00
|
|
|
static int g_outFD = -1;
|
2015-04-10 14:51:39 +02:00
|
|
|
static size_t g_outByteCount = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
static int g_printBinary = 0;
|
2015-02-26 23:33:35 +01:00
|
|
|
static int g_devCount = 0; // >1 means multiple
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
|
|
|
|
|
2009-03-04 04:32:55 +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
|
|
|
}
|
|
|
|
|
|
|
|
static void rotateLogs()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
// Can't rotate logs if we're not outputting to a file
|
|
|
|
if (g_outputFileName == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(g_outFD);
|
|
|
|
|
2014-10-17 07:19:55 +02:00
|
|
|
// Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
|
|
|
|
// eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
|
|
|
|
int maxRotationCountDigits =
|
|
|
|
(g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
|
|
|
|
char *file0, *file1;
|
|
|
|
|
2014-10-17 07:19:55 +02:00
|
|
|
asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (i - 1 == 0) {
|
|
|
|
asprintf(&file0, "%s", g_outputFileName);
|
|
|
|
} else {
|
2014-10-17 07:19:55 +02:00
|
|
|
asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
if (!file0 || !file1) {
|
|
|
|
perror("while rotating log files");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rename(file0, file1);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (err < 0 && errno != ENOENT) {
|
|
|
|
perror("while rotating log files");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(file1);
|
|
|
|
free(file0);
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
g_outFD = openLogFile(g_outputFileName);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (g_outFD < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "couldn't open output file");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_outByteCount = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
void printBinary(struct log_msg *buf)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2013-11-22 19:55:48 +01:00
|
|
|
size_t size = buf->len();
|
|
|
|
|
|
|
|
TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
static void processBuffer(log_device_t* dev, struct log_msg *buf)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
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) {
|
2015-02-26 23:33:35 +01:00
|
|
|
static bool hasOpenedEventTagMap = false;
|
|
|
|
static EventTagMap *eventTagMap = NULL;
|
|
|
|
|
|
|
|
if (!eventTagMap && !hasOpenedEventTagMap) {
|
|
|
|
eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
|
|
|
|
hasOpenedEventTagMap = true;
|
|
|
|
}
|
2013-11-22 19:55:48 +01:00
|
|
|
err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
|
2015-02-26 23:33:35 +01:00
|
|
|
eventTagMap,
|
2013-11-22 19:55:48 +01:00
|
|
|
binaryMsgBuf,
|
|
|
|
sizeof(binaryMsgBuf));
|
2009-03-04 04:32:55 +01:00
|
|
|
//printf(">>> pri=%d len=%d msg='%s'\n",
|
|
|
|
// 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
|
|
|
}
|
2010-03-01 18:11:54 +01:00
|
|
|
if (err < 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
goto error;
|
2010-03-01 18:11:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
|
|
|
|
bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-03-01 18:11:54 +01:00
|
|
|
if (bytesWritten < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "output error");
|
2010-03-01 18:11:54 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_outByteCount += bytesWritten;
|
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
if (g_logRotateSizeKBytes > 0
|
2009-03-04 04:32:55 +01:00
|
|
|
&& (g_outByteCount / 1024) >= g_logRotateSizeKBytes
|
|
|
|
) {
|
|
|
|
rotateLogs();
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
//fprintf (stderr, "Error processing record\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-26 22:41:33 +01:00
|
|
|
static void maybePrintStart(log_device_t* dev, bool printDividers) {
|
|
|
|
if (!dev->printed || printDividers) {
|
2010-03-12 05:32:17 +01:00
|
|
|
if (g_devCount > 1 && !g_printBinary) {
|
|
|
|
char buf[1024];
|
2015-01-26 22:41:33 +01:00
|
|
|
snprintf(buf, sizeof(buf), "--------- %s %s\n",
|
|
|
|
dev->printed ? "switch to" : "beginning of",
|
2013-11-22 19:55:48 +01:00
|
|
|
dev->device);
|
2010-03-12 05:32:17 +01:00
|
|
|
if (write(g_outFD, buf, strlen(buf)) < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "output error");
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
static void setupOutput()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (g_outputFileName == NULL) {
|
|
|
|
g_outFD = STDOUT_FILENO;
|
|
|
|
|
|
|
|
} else {
|
2015-06-02 16:57:16 +02:00
|
|
|
if (set_sched_policy(0, SP_BACKGROUND) < 0) {
|
|
|
|
fprintf(stderr, "failed to set background scheduling policy\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sched_param param;
|
|
|
|
memset(¶m, 0, sizeof(param));
|
|
|
|
if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) {
|
|
|
|
fprintf(stderr, "failed to set to batch scheduler\n");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-06-09 08:36:34 +02:00
|
|
|
if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
|
|
|
|
fprintf(stderr, "failed set to priority\n");
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
g_outFD = openLogFile (g_outputFileName);
|
|
|
|
|
|
|
|
if (g_outFD < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "couldn't open output file");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-06-02 16:57:16 +02:00
|
|
|
struct stat statbuf;
|
2015-04-10 14:51:39 +02:00
|
|
|
if (fstat(g_outFD, &statbuf) == -1) {
|
|
|
|
close(g_outFD);
|
|
|
|
logcat_panic(false, "couldn't get output file stat\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
|
|
|
|
close(g_outFD);
|
|
|
|
logcat_panic(false, "invalid output file stat\n");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
g_outByteCount = statbuf.st_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_help(const char *cmd)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
|
|
|
|
|
|
|
|
fprintf(stderr, "options include:\n"
|
|
|
|
" -s Set default filter to silent.\n"
|
2015-03-09 17:32:56 +01:00
|
|
|
" Like specifying filterspec '*:S'\n"
|
2015-05-27 16:39:56 +02:00
|
|
|
" -f <filename> Log to file. Default is stdout\n"
|
2015-04-10 14:51:39 +02:00
|
|
|
" -r <kbytes> Rotate log every kbytes. Requires -f\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" -n <count> Sets max number of rotated logs to <count>, default 4\n"
|
2010-10-17 22:39:37 +02:00
|
|
|
" -v <format> Sets the log print format, where <format> is:\n\n"
|
2015-05-15 18:01:58 +02:00
|
|
|
" brief color long printable process raw tag thread\n"
|
|
|
|
" threadtime time usec\n\n"
|
2015-01-26 22:41:33 +01:00
|
|
|
" -D print dividers between each log buffer\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" -c clear (flush) the entire log and exit\n"
|
|
|
|
" -d dump the log and then exit (don't block)\n"
|
2010-03-12 05:32:17 +01:00
|
|
|
" -t <count> print only the most recent <count> lines (implies -d)\n"
|
2014-09-01 19:41:33 +02:00
|
|
|
" -t '<time>' print most recent lines since specified time (implies -d)\n"
|
2013-12-09 22:47:00 +01:00
|
|
|
" -T <count> print only the most recent <count> lines (does not imply -d)\n"
|
2014-09-01 19:41:33 +02:00
|
|
|
" -T '<time>' print most recent lines since specified time (not imply -d)\n"
|
|
|
|
" count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" -g get the size of the log's ring buffer and exit\n"
|
2014-12-15 19:01:31 +01:00
|
|
|
" -L dump logs from prior to last reboot\n"
|
2014-02-06 23:48:50 +01:00
|
|
|
" -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
|
2014-04-07 23:58:08 +02:00
|
|
|
" 'events', 'crash' or 'all'. Multiple -b parameters are\n"
|
|
|
|
" allowed and results are interleaved. The default is\n"
|
|
|
|
" -b main -b system -b crash.\n"
|
2014-02-06 23:48:50 +01:00
|
|
|
" -B output the log in binary.\n"
|
2014-04-11 22:49:43 +02:00
|
|
|
" -S output statistics.\n"
|
|
|
|
" -G <size> set size of log ring buffer, may suffix with K or M.\n"
|
|
|
|
" -p print prune white and ~black list. Service is specified as\n"
|
|
|
|
" UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
|
|
|
|
" 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"
|
|
|
|
" -P '<list> ...' set prune white and ~black list, using same format as\n"
|
|
|
|
" printed above. Must be quoted.\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
fprintf(stderr,"\nfilterspecs are a series of \n"
|
|
|
|
" <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
|
|
|
}
|
|
|
|
|
|
|
|
static int setLogFormat(const char * formatString)
|
|
|
|
{
|
|
|
|
static AndroidLogPrintFormat format;
|
|
|
|
|
|
|
|
format = android_log_formatFromString(formatString);
|
|
|
|
|
|
|
|
if (format == FORMAT_OFF) {
|
|
|
|
// FORMAT_OFF means invalid string
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-06 17:40:40 +02:00
|
|
|
return android_log_setPrintFormat(g_logformat, format);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
static const char multipliers[][2] = {
|
|
|
|
{ "" },
|
|
|
|
{ "K" },
|
|
|
|
{ "M" },
|
|
|
|
{ "G" }
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long value_of_size(unsigned long value)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0;
|
|
|
|
(i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
|
|
|
|
value /= 1024, ++i) ;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *multiplier_of_size(unsigned long value)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0;
|
|
|
|
(i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
|
|
|
|
value /= 1024, ++i) ;
|
|
|
|
return multipliers[i];
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
/*String to unsigned int, returns -1 if it fails*/
|
|
|
|
static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
|
|
|
|
size_t max = SIZE_MAX)
|
|
|
|
{
|
|
|
|
char *endp;
|
|
|
|
errno = 0;
|
|
|
|
size_t ret = (size_t) strtoll(ptr, &endp, 0);
|
|
|
|
|
|
|
|
if (endp[0] != '\0' || errno != 0 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret > max || ret < min) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*val = ret;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void logcat_panic(bool showHelp, const char *fmt, ...)
|
|
|
|
{
|
2015-04-13 18:27:57 +02:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
va_end(args);
|
2015-04-10 14:51:39 +02:00
|
|
|
|
|
|
|
if (showHelp) {
|
|
|
|
show_help(getprogname());
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:39:56 +02:00
|
|
|
static const char g_defaultTimeFormat[] = "%m-%d %H:%M:%S.%q";
|
|
|
|
|
|
|
|
// Find last logged line in gestalt of all matching existing output files
|
|
|
|
static log_time lastLogTime(char *outputFileName) {
|
|
|
|
log_time retval(log_time::EPOCH);
|
|
|
|
if (!outputFileName) {
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_time now(CLOCK_REALTIME);
|
|
|
|
|
|
|
|
std::string directory;
|
|
|
|
char *file = strrchr(outputFileName, '/');
|
|
|
|
if (!file) {
|
|
|
|
directory = ".";
|
|
|
|
file = outputFileName;
|
|
|
|
} else {
|
|
|
|
*file = '\0';
|
|
|
|
directory = outputFileName;
|
|
|
|
*file = '/';
|
|
|
|
++file;
|
|
|
|
}
|
|
|
|
size_t len = strlen(file);
|
|
|
|
log_time modulo(0, NS_PER_SEC);
|
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
|
|
|
|
struct dirent *dp;
|
|
|
|
while ((dp = readdir(dir.get())) != NULL) {
|
|
|
|
if ((dp->d_type != DT_REG)
|
|
|
|
|| strncmp(dp->d_name, file, len)
|
|
|
|
|| (dp->d_name[len]
|
|
|
|
&& ((dp->d_name[len] != '.')
|
|
|
|
|| !isdigit(dp->d_name[len+1])))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_name = directory;
|
|
|
|
file_name += "/";
|
|
|
|
file_name += dp->d_name;
|
|
|
|
std::string file;
|
|
|
|
if (!android::base::ReadFileToString(file_name, &file)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
for (const auto& line : android::base::Split(file, "\n")) {
|
|
|
|
log_time t(log_time::EPOCH);
|
|
|
|
char *ep = t.strptime(line.c_str(), g_defaultTimeFormat);
|
|
|
|
if (!ep || (*ep != ' ')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// 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.
|
|
|
|
if (!dp->d_name[len] && found) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (retval == log_time::EPOCH) {
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
} /* namespace android */
|
|
|
|
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
int main(int argc, char **argv)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-04-10 14:51:39 +02:00
|
|
|
using namespace android;
|
2009-03-04 04:32:55 +01:00
|
|
|
int err;
|
|
|
|
int hasSetLogFormat = 0;
|
|
|
|
int clearLog = 0;
|
|
|
|
int getLogSize = 0;
|
2014-02-11 21:29:31 +01:00
|
|
|
unsigned long setLogSize = 0;
|
|
|
|
int getPruneList = 0;
|
|
|
|
char *setPruneList = NULL;
|
2014-02-06 23:48:50 +01:00
|
|
|
int printStatistics = 0;
|
2015-01-26 19:46:44 +01:00
|
|
|
int mode = ANDROID_LOG_RDONLY;
|
2009-03-04 04:32:55 +01:00
|
|
|
const char *forceFilters = NULL;
|
2010-02-26 19:04:23 +01:00
|
|
|
log_device_t* devices = NULL;
|
|
|
|
log_device_t* dev;
|
2015-01-26 22:41:33 +01:00
|
|
|
bool printDividers = false;
|
2013-11-22 19:55:48 +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);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-12-13 20:10:11 +01:00
|
|
|
signal(SIGPIPE, exit);
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
g_logformat = android_log_format_new();
|
|
|
|
|
|
|
|
if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
|
2015-04-10 14:51:39 +02:00
|
|
|
show_help(argv[0]);
|
|
|
|
return EXIT_SUCCESS;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int ret;
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
ret = getopt(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(ret) {
|
2013-11-22 19:55:48 +01:00
|
|
|
case 's':
|
2009-03-04 04:32:55 +01:00
|
|
|
// default to all silent
|
|
|
|
android_log_addFilterRule(g_logformat, "*:s");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
clearLog = 1;
|
2015-01-26 19:46:44 +01:00
|
|
|
mode |= ANDROID_LOG_WRONLY;
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
|
2014-12-15 19:01:31 +01:00
|
|
|
case 'L':
|
|
|
|
mode |= ANDROID_LOG_PSTORE;
|
|
|
|
break;
|
|
|
|
|
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;
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
|
2010-03-12 05:32:17 +01:00
|
|
|
case 't':
|
2015-01-26 19:46:44 +01:00
|
|
|
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
|
2013-12-09 22:47:00 +01:00
|
|
|
/* FALLTHRU */
|
|
|
|
case 'T':
|
2014-02-15 01:05:05 +01:00
|
|
|
if (strspn(optarg, "0123456789") != strlen(optarg)) {
|
2015-05-27 16:39:56 +02:00
|
|
|
char *cp = tail_time.strptime(optarg, g_defaultTimeFormat);
|
2014-02-15 01:05:05 +01:00
|
|
|
if (!cp) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false,
|
|
|
|
"-%c \"%s\" not in \"%s\" time format\n",
|
2015-05-27 16:39:56 +02:00
|
|
|
ret, optarg, g_defaultTimeFormat);
|
2014-02-15 01:05:05 +01:00
|
|
|
}
|
|
|
|
if (*cp) {
|
|
|
|
char c = *cp;
|
|
|
|
*cp = '\0';
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
|
|
|
|
ret, optarg, c, cp + 1);
|
|
|
|
*cp = c;
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-10 14:51:39 +02:00
|
|
|
if (!getSizeTArg(optarg, &tail_lines, 1)) {
|
2014-02-15 01:05:05 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: -%c %s invalid, setting to 1\n",
|
|
|
|
ret, optarg);
|
|
|
|
tail_lines = 1;
|
|
|
|
}
|
|
|
|
}
|
2010-03-12 05:32:17 +01:00
|
|
|
break;
|
|
|
|
|
2015-01-26 22:41:33 +01:00
|
|
|
case 'D':
|
|
|
|
printDividers = true;
|
|
|
|
break;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
case 'g':
|
|
|
|
getLogSize = 1;
|
|
|
|
break;
|
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
case 'G': {
|
2015-04-10 14:51:39 +02:00
|
|
|
char *cp;
|
|
|
|
if (strtoll(optarg, &cp, 0) > 0) {
|
|
|
|
setLogSize = strtoll(optarg, &cp, 0);
|
|
|
|
} else {
|
|
|
|
setLogSize = 0;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(*cp) {
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
setLogSize *= 1024;
|
|
|
|
/* FALLTHRU */
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
setLogSize *= 1024;
|
|
|
|
/* FALLTHRU */
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
setLogSize *= 1024;
|
|
|
|
/* FALLTHRU */
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
setLogSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setLogSize) {
|
|
|
|
fprintf(stderr, "ERROR: -G <num><multiplier>\n");
|
2015-04-10 14:51:39 +02:00
|
|
|
return EXIT_FAILURE;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
getPruneList = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
|
|
|
setPruneList = optarg;
|
|
|
|
break;
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
case 'b': {
|
2014-02-06 23:48:50 +01:00
|
|
|
if (strcmp(optarg, "all") == 0) {
|
|
|
|
while (devices) {
|
|
|
|
dev = devices;
|
|
|
|
devices = dev->next;
|
|
|
|
delete dev;
|
|
|
|
}
|
|
|
|
|
2014-10-11 00:25:38 +02:00
|
|
|
devices = dev = NULL;
|
2015-04-10 14:51:39 +02:00
|
|
|
g_devCount = 0;
|
2014-10-11 00:25:38 +02:00
|
|
|
for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
|
|
|
|
const char *name = android_log_id_to_name((log_id_t)i);
|
|
|
|
log_id_t log_id = android_name_to_log_id(name);
|
|
|
|
|
|
|
|
if (log_id != (log_id_t)i) {
|
|
|
|
continue;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
2014-10-11 00:25:38 +02:00
|
|
|
|
|
|
|
bool binary = strcmp(name, "events") == 0;
|
2015-02-27 22:41:34 +01:00
|
|
|
log_device_t* d = new log_device_t(name, binary);
|
2014-10-11 00:25:38 +02:00
|
|
|
|
|
|
|
if (dev) {
|
|
|
|
dev->next = d;
|
|
|
|
dev = d;
|
|
|
|
} else {
|
|
|
|
devices = dev = d;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
g_devCount++;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
bool binary = strcmp(optarg, "events") == 0;
|
|
|
|
|
|
|
|
if (devices) {
|
|
|
|
dev = devices;
|
|
|
|
while (dev->next) {
|
|
|
|
dev = dev->next;
|
|
|
|
}
|
2015-02-27 22:41:34 +01:00
|
|
|
dev->next = new log_device_t(optarg, binary);
|
2010-02-26 19:04:23 +01:00
|
|
|
} else {
|
2015-02-27 22:41:34 +01:00
|
|
|
devices = new log_device_t(optarg, binary);
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
g_devCount++;
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
2015-04-10 14:51:39 +02:00
|
|
|
g_printBinary = 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2015-05-27 16:39:56 +02:00
|
|
|
if ((tail_time == log_time::EPOCH) && (tail_lines != 0)) {
|
|
|
|
tail_time = lastLogTime(optarg);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
// redirect output to a file
|
2015-04-10 14:51:39 +02:00
|
|
|
g_outputFileName = optarg;
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
2015-04-10 14:51:39 +02:00
|
|
|
if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
|
|
|
|
logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
2015-04-10 14:51:39 +02:00
|
|
|
if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
|
|
|
|
logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
err = setLogFormat (optarg);
|
|
|
|
if (err < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-05-06 17:40:40 +02:00
|
|
|
hasSetLogFormat |= err;
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Q':
|
|
|
|
/* 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 run the program. If nothing is found, the program should */
|
|
|
|
/* quit immediately */
|
|
|
|
#define KERNEL_OPTION "androidboot.logcat="
|
|
|
|
#define CONSOLE_OPTION "androidboot.console="
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char* logcat;
|
|
|
|
char* console;
|
|
|
|
int force_exit = 1;
|
|
|
|
static char cmdline[1024];
|
|
|
|
|
|
|
|
fd = open("/proc/cmdline", O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
int n = read(fd, cmdline, sizeof(cmdline)-1 );
|
|
|
|
if (n < 0) n = 0;
|
|
|
|
cmdline[n] = 0;
|
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
cmdline[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
logcat = strstr( cmdline, KERNEL_OPTION );
|
|
|
|
console = strstr( cmdline, CONSOLE_OPTION );
|
|
|
|
if (logcat != NULL) {
|
|
|
|
char* p = logcat + sizeof(KERNEL_OPTION)-1;;
|
|
|
|
char* q = strpbrk( p, " \t\n\r" );;
|
|
|
|
|
|
|
|
if (q != NULL)
|
|
|
|
*q = 0;
|
|
|
|
|
|
|
|
forceFilters = p;
|
|
|
|
force_exit = 0;
|
|
|
|
}
|
|
|
|
/* if nothing found or invalid filters, exit quietly */
|
2015-04-10 14:51:39 +02:00
|
|
|
if (force_exit) {
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
/* redirect our output to the emulator console */
|
|
|
|
if (console) {
|
|
|
|
char* p = console + sizeof(CONSOLE_OPTION)-1;
|
|
|
|
char* q = strpbrk( p, " \t\n\r" );
|
|
|
|
char devname[64];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (q != NULL) {
|
|
|
|
len = q - p;
|
|
|
|
} else
|
|
|
|
len = strlen(p);
|
|
|
|
|
|
|
|
len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
|
|
|
|
fprintf(stderr, "logcat using %s (%d)\n", devname, len);
|
|
|
|
if (len < (int)sizeof(devname)) {
|
|
|
|
fd = open( devname, O_WRONLY );
|
|
|
|
if (fd >= 0) {
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-02-06 23:48:50 +01:00
|
|
|
case 'S':
|
|
|
|
printStatistics = 1;
|
|
|
|
break;
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
case ':':
|
|
|
|
logcat_panic(true, "Option -%c needs an argument\n", optopt);
|
|
|
|
break;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
default:
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(true, "Unrecognized Option %c\n", optopt);
|
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
if (!devices) {
|
2015-02-27 22:41:34 +01:00
|
|
|
dev = devices = new log_device_t("main", false);
|
2015-04-10 14:51:39 +02:00
|
|
|
g_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);
|
2015-04-10 14:51:39 +02:00
|
|
|
g_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);
|
2015-04-10 14:51:39 +02:00
|
|
|
g_devCount++;
|
2014-05-14 21:37:22 +02:00
|
|
|
}
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
|
|
|
|
logcat_panic(true, "-r requires -f as well\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
setupOutput();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (hasSetLogFormat == 0) {
|
|
|
|
const char* logFormat = getenv("ANDROID_PRINTF_LOG");
|
|
|
|
|
|
|
|
if (logFormat != NULL) {
|
|
|
|
err = setLogFormat(logFormat);
|
|
|
|
if (err < 0) {
|
2013-11-22 19:55:48 +01:00
|
|
|
fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
|
2009-03-04 04:32:55 +01:00
|
|
|
logFormat);
|
|
|
|
}
|
2014-09-16 18:15:15 +02:00
|
|
|
} else {
|
|
|
|
setLogFormat("threadtime");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forceFilters) {
|
|
|
|
err = android_log_addFilterString(g_logformat, forceFilters);
|
|
|
|
if (err < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "Invalid filter expression in logcat args\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
} else if (argc == optind) {
|
|
|
|
// Add from environment variable
|
|
|
|
char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
|
|
|
|
|
|
|
|
if (env_tags_orig != NULL) {
|
|
|
|
err = android_log_addFilterString(g_logformat, env_tags_orig);
|
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
if (err < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(true,
|
|
|
|
"Invalid filter expression in ANDROID_LOG_TAGS\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add from commandline
|
|
|
|
for (int i = optind ; i < argc ; i++) {
|
|
|
|
err = android_log_addFilterString(g_logformat, argv[i]);
|
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
if (err < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
dev = devices;
|
2014-02-15 01:05:05 +01:00
|
|
|
if (tail_time != log_time::EPOCH) {
|
|
|
|
logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
|
|
|
|
} else {
|
|
|
|
logger_list = android_logger_list_alloc(mode, tail_lines, 0);
|
|
|
|
}
|
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) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "Unable to open log device '%s'\n",
|
|
|
|
dev->device);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
if (clearLog) {
|
|
|
|
int ret;
|
2013-11-22 19:55:48 +01:00
|
|
|
ret = android_logger_clear(dev->logger);
|
2010-02-26 19:04:23 +01:00
|
|
|
if (ret) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "failed to clear the log");
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "failed to set the log size");
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
if (getLogSize) {
|
2014-02-11 21:29:31 +01:00
|
|
|
long size, readable;
|
2010-02-26 19:04:23 +01:00
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
size = android_logger_get_log_size(dev->logger);
|
2010-02-26 19:04:23 +01:00
|
|
|
if (size < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "failed to get the log size");
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
readable = android_logger_get_log_readable_size(dev->logger);
|
2010-02-26 19:04:23 +01:00
|
|
|
if (readable < 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "failed to get the readable log size");
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
|
2010-02-26 19:04:23 +01:00
|
|
|
"max entry is %db, max payload is %db\n", dev->device,
|
2014-05-06 16:34:59 +02:00
|
|
|
value_of_size(size), multiplier_of_size(size),
|
|
|
|
value_of_size(readable), multiplier_of_size(readable),
|
2010-02-26 19:04:23 +01:00
|
|
|
(int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
dev = dev->next;
|
|
|
|
}
|
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
if (setPruneList) {
|
2015-04-10 14:51:39 +02:00
|
|
|
size_t len = strlen(setPruneList);
|
|
|
|
/*extra 32 bytes are needed by android_logger_set_prune_list */
|
|
|
|
size_t bLen = len + 32;
|
|
|
|
char *buf = NULL;
|
|
|
|
if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
|
|
|
|
buf[len] = '\0';
|
|
|
|
if (android_logger_set_prune_list(logger_list, buf, bLen)) {
|
|
|
|
logcat_panic(false, "failed to set the prune list");
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
} else {
|
|
|
|
logcat_panic(false, "failed to set the prune list (alloc)");
|
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;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
for(int retry = 32;
|
|
|
|
(retry >= 0) && ((buf = new char [len]));
|
2015-04-10 14:51:39 +02:00
|
|
|
delete [] buf, buf = NULL, --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);
|
|
|
|
}
|
2014-02-06 23:48:50 +01:00
|
|
|
buf[len-1] = '\0';
|
2015-04-10 14:51:39 +02:00
|
|
|
if (atol(buf) < 3) {
|
2014-02-06 23:48:50 +01:00
|
|
|
delete [] buf;
|
|
|
|
buf = NULL;
|
|
|
|
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) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "failed to read data");
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove trailing FF
|
|
|
|
char *cp = buf + len - 1;
|
|
|
|
*cp = '\0';
|
|
|
|
bool truncated = *--cp != '\f';
|
|
|
|
if (!truncated) {
|
|
|
|
*cp = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// squash out the byte count
|
|
|
|
cp = buf;
|
|
|
|
if (!truncated) {
|
2014-03-21 21:12:16 +01:00
|
|
|
while (isdigit(*cp)) {
|
|
|
|
++cp;
|
|
|
|
}
|
|
|
|
if (*cp == '\n') {
|
2014-02-06 23:48:50 +01:00
|
|
|
++cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s", cp);
|
|
|
|
delete [] buf;
|
2015-04-10 14:51:39 +02:00
|
|
|
return EXIT_SUCCESS;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
if (getLogSize) {
|
2015-04-10 14:51:39 +02:00
|
|
|
return EXIT_SUCCESS;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-02-11 21:29:31 +01:00
|
|
|
if (setLogSize || setPruneList) {
|
2015-04-10 14:51:39 +02:00
|
|
|
return EXIT_SUCCESS;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2010-03-01 18:11:54 +01:00
|
|
|
if (clearLog) {
|
2015-04-10 14:51:39 +02:00
|
|
|
return EXIT_SUCCESS;
|
2010-03-01 18:11:54 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
//LOG_EVENT_INT(10, 12345);
|
|
|
|
//LOG_EVENT_LONG(11, 0x1122334455667788LL);
|
|
|
|
//LOG_EVENT_STRING(0, "whassup, doc?");
|
|
|
|
|
2015-01-26 22:41:33 +01:00
|
|
|
dev = NULL;
|
2015-02-27 22:41:34 +01:00
|
|
|
log_device_t unexpected("unexpected", false);
|
2013-11-22 19:55:48 +01:00
|
|
|
while (1) {
|
|
|
|
struct log_msg log_msg;
|
2015-01-26 22:41:33 +01:00
|
|
|
log_device_t* d;
|
2013-11-22 19:55:48 +01:00
|
|
|
int ret = android_logger_list_read(logger_list, &log_msg);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "read: unexpected EOF!\n");
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == -EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == -EIO) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "read: unexpected EOF!\n");
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
if (ret == -EINVAL) {
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "read: unexpected length.\n");
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
logcat_panic(false, "logcat read failure");
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 22:41:33 +01:00
|
|
|
for(d = devices; d; d = d->next) {
|
|
|
|
if (android_name_to_log_id(d->device) == log_msg.id()) {
|
2013-11-22 19:55:48 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-26 22:41:33 +01:00
|
|
|
if (!d) {
|
2015-04-10 14:51:39 +02:00
|
|
|
g_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;
|
2015-04-10 14:51:39 +02:00
|
|
|
maybePrintStart(dev, printDividers);
|
2015-01-26 22:41:33 +01:00
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
if (g_printBinary) {
|
|
|
|
printBinary(&log_msg);
|
2013-11-22 19:55:48 +01:00
|
|
|
} else {
|
2015-04-10 14:51:39 +02:00
|
|
|
processBuffer(dev, &log_msg);
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
android_logger_list_free(logger_list);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
return EXIT_SUCCESS;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|