// Copyright 2006-2015 The Android Open Source Project #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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) 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 = NULL; printed = false; logger = NULL; logger_list = NULL; } }; namespace android { /* Global Variables */ static const char * g_outputFileName = NULL; // 0 means "no log rotation" static size_t g_logRotateSizeKBytes = 0; // 0 means "unbounded" static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; static int g_outFD = -1; static size_t g_outByteCount = 0; static int g_printBinary = 0; static int g_devCount = 0; // >1 means multiple __noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3); static int openLogFile (const char *pathname) { return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); } 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); // 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; for (int i = g_maxRotatedLogs ; i > 0 ; i--) { char *file0, *file1; asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i); if (i - 1 == 0) { asprintf(&file0, "%s", g_outputFileName); } else { asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1); } if (!file0 || !file1) { perror("while rotating log files"); break; } err = rename(file0, file1); if (err < 0 && errno != ENOENT) { perror("while rotating log files"); } free(file1); free(file0); } g_outFD = openLogFile(g_outputFileName); if (g_outFD < 0) { logcat_panic(false, "couldn't open output file"); } g_outByteCount = 0; } void printBinary(struct log_msg *buf) { size_t size = buf->len(); TEMP_FAILURE_RETRY(write(g_outFD, buf, size)); } static void processBuffer(log_device_t* dev, struct log_msg *buf) { int bytesWritten = 0; int err; AndroidLogEntry entry; char binaryMsgBuf[1024]; if (dev->binary) { static bool hasOpenedEventTagMap = false; static EventTagMap *eventTagMap = NULL; if (!eventTagMap && !hasOpenedEventTagMap) { eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE); hasOpenedEventTagMap = true; } err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry, eventTagMap, binaryMsgBuf, sizeof(binaryMsgBuf)); //printf(">>> pri=%d len=%d msg='%s'\n", // entry.priority, entry.messageLen, entry.message); } else { err = android_log_processLogBuffer(&buf->entry_v1, &entry); } if (err < 0) { goto error; } if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) { bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry); if (bytesWritten < 0) { logcat_panic(false, "output error"); } } g_outByteCount += bytesWritten; if (g_logRotateSizeKBytes > 0 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes ) { rotateLogs(); } error: //fprintf (stderr, "Error processing record\n"); return; } static void maybePrintStart(log_device_t* dev, bool printDividers) { if (!dev->printed || printDividers) { if (g_devCount > 1 && !g_printBinary) { char buf[1024]; snprintf(buf, sizeof(buf), "--------- %s %s\n", dev->printed ? "switch to" : "beginning of", dev->device); if (write(g_outFD, buf, strlen(buf)) < 0) { logcat_panic(false, "output error"); } } dev->printed = true; } } static void setupOutput() { if (g_outputFileName == NULL) { g_outFD = STDOUT_FILENO; } else { 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"); } if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { fprintf(stderr, "failed set to priority\n"); } g_outFD = openLogFile (g_outputFileName); if (g_outFD < 0) { logcat_panic(false, "couldn't open output file"); } struct stat statbuf; 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"); } 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" " Like specifying filterspec '*:S'\n" " -f Log to file. Default is stdout\n" " -r Rotate log every kbytes. Requires -f\n" " -n Sets max number of rotated logs to , default 4\n" " -v Sets the log print format, where is:\n\n" " brief color epoch long monotonic printable process raw\n" " tag thread threadtime time usec UTC year zone\n\n" " -D print dividers between each log buffer\n" " -c clear (flush) the entire log and exit\n" " -d dump the log and then exit (don't block)\n" " -t print only the most recent lines (implies -d)\n" " -t '