2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
31 #include <arpa/inet.h>
37 #define DEFAULT_LOG_ROTATE_SIZE_KBYTES 16
38 #define DEFAULT_MAX_ROTATED_LOGS 4
40 #define LOG_FILE_DIR "/dev/log_"
42 static log_format* g_logformat;
43 static bool g_nonblock = false;
44 static int g_tail_lines = 0;
46 static const char * g_output_filename = NULL;
47 static int g_log_rotate_size_kbytes = 0; // 0 means "no log rotation"
48 static int g_max_rotated_logs = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded"
49 static int g_outfd = -1;
50 static off_t g_out_byte_count = 0;
51 static int g_dev_count = 0;
53 struct queued_entry_t {
55 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1] __attribute__((aligned(4)));
56 struct logger_entry entry __attribute__((aligned(4)));
58 struct queued_entry_t* next;
61 static int cmp(struct queued_entry_t* a, struct queued_entry_t* b)
63 int n = a->entry.sec - b->entry.sec;
68 return a->entry.nsec - b->entry.nsec;
76 struct queued_entry_t* queue;
77 struct log_device_t* next;
80 static void enqueue(struct log_device_t* device, struct queued_entry_t* entry)
82 if( device->queue == NULL)
84 device->queue = entry;
88 struct queued_entry_t** e = &device->queue;
89 while(*e && cmp(entry, *e) >= 0 )
98 static int open_logfile (const char *pathname)
100 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
103 static void rotate_logs()
110 // Can't rotate logs if we're not outputting to a file
111 if (g_output_filename == NULL) {
117 for (i = g_max_rotated_logs ; i > 0 ; i--)
119 snprintf(file1, 255, "%s.%d", g_output_filename, i);
122 snprintf(file0, 255, "%s", g_output_filename);
124 snprintf(file0, 255, "%s.%d", g_output_filename, i - 1);
127 err = rename (file0, file1);
129 if (err < 0 && errno != ENOENT) {
130 perror("while rotating log files");
134 g_outfd = open_logfile (g_output_filename);
137 perror ("couldn't open output file");
141 g_out_byte_count = 0;
146 static void processBuffer(struct log_device_t* dev, struct logger_entry *buf)
148 int bytes_written = 0;
153 err = log_process_log_buffer(buf, &entry);
159 if (log_should_print_line(g_logformat, entry.tag, entry.priority)) {
160 if (false && g_dev_count > 1) {
162 mgs_buf[0] = dev->device[0];
164 bytes_written = write(g_outfd, mgs_buf, 2);
165 if (bytes_written < 0)
167 perror("output error");
172 bytes_written = log_print_log_line(g_logformat, g_outfd, &entry);
174 if (bytes_written < 0)
176 perror("output error");
181 g_out_byte_count += bytes_written;
183 if (g_log_rotate_size_kbytes > 0 && (g_out_byte_count / 1024) >= g_log_rotate_size_kbytes)
189 //fprintf (stderr, "Error processing record\n");
193 static void chooseFirst(struct log_device_t* dev, struct log_device_t** firstdev)
195 for (*firstdev = NULL; dev != NULL; dev = dev->next) {
196 if (dev->queue != NULL && (*firstdev == NULL || cmp(dev->queue, (*firstdev)->queue) < 0))
203 static void maybePrintStart(struct log_device_t* dev) {
206 if (g_dev_count > 1 ) {
208 snprintf(buf, sizeof(buf), "--------- beginning of %s\n", dev->device);
209 if (write(g_outfd, buf, strlen(buf)) < 0) {
210 perror("output error");
217 static void skipNextEntry(struct log_device_t* dev) {
218 maybePrintStart(dev);
219 struct queued_entry_t* entry = dev->queue;
220 dev->queue = entry->next;
224 static void printNextEntry(struct log_device_t* dev)
226 maybePrintStart(dev);
227 processBuffer(dev, &dev->queue->entry);
232 static void read_log_lines(struct log_device_t* devices)
234 struct log_device_t* dev;
237 int queued_lines = 0;
238 bool sleep = false; // for exit immediately when log buffer is empty and g_nonblock value is true.
243 for (dev=devices; dev; dev = dev->next) {
251 struct timeval timeout = { 0, 5000 /* 5ms */ }; // If we oversleep it's ok, i.e. ignore EINTR.
253 for (dev=devices; dev; dev = dev->next) {
254 FD_SET(dev->fd, &readset);
256 result = select(max + 1, &readset, NULL, NULL, sleep ? NULL : &timeout);
257 } while (result == -1 && errno == EINTR);
260 for (dev=devices; dev; dev = dev->next) {
261 if (FD_ISSET(dev->fd, &readset)) {
262 struct queued_entry_t* entry = (struct queued_entry_t *)malloc(sizeof( struct queued_entry_t));
264 fprintf(stderr,"Can't malloc queued_entry\n");
269 /* NOTE: driver guarantees we read exactly one full entry */
270 ret = read(dev->fd, entry->buf, LOGGER_ENTRY_MAX_LEN);
272 if (errno == EINTR) {
276 if (errno == EAGAIN) {
280 perror("dlogutil read");
284 fprintf(stderr, "read: Unexpected EOF!\n");
288 entry->entry.msg[entry->entry.len] = '\0';
296 // we did our short timeout trick and there's nothing new
297 // print everything we have and wait for more data
300 chooseFirst(devices, &dev);
304 if (g_tail_lines == 0 || queued_lines <= g_tail_lines) {
312 // the caller requested to just dump the log and exit
317 // print all that aren't the last in their list
319 while (g_tail_lines == 0 || queued_lines > g_tail_lines) {
320 chooseFirst(devices, &dev);
321 if (dev == NULL || dev->queue->next == NULL) {
324 if (g_tail_lines == 0) {
339 static int clear_log(int logfd)
341 return ioctl(logfd, LOGGER_FLUSH_LOG);
344 /* returns the total size of the log's ring buffer */
345 static int get_log_size(int logfd)
347 return ioctl(logfd, LOGGER_GET_LOG_BUF_SIZE);
350 /* returns the readable size of the log's ring buffer (that is, amount of the log consumed) */
351 static int get_log_readable_size(int logfd)
353 return ioctl(logfd, LOGGER_GET_LOG_LEN);
356 static void setup_output()
359 if (g_output_filename == NULL) {
360 g_outfd = STDOUT_FILENO;
365 g_outfd = open_logfile (g_output_filename);
368 perror ("couldn't open output file");
372 fstat(g_outfd, &statbuf);
374 g_out_byte_count = statbuf.st_size;
378 static int set_log_format(const char * formatString)
380 static log_print_format format;
382 format = log_format_from_string(formatString);
384 if (format == FORMAT_OFF) {
385 // FORMAT_OFF means invalid string
389 log_set_print_format(g_logformat, format);
394 static void show_help(const char *cmd)
396 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
398 fprintf(stderr, "options include:\n"
399 " -s Set default filter to silent.\n"
400 " Like specifying filterspec '*:s'\n"
401 " -f <filename> Log to file. Default to stdout\n"
402 " -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n"
403 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
404 " -v <format> Sets the log print format, where <format> is one of:\n\n"
405 " brief process tag thread raw time threadtime long\n\n"
406 " -c clear (flush) the entire log and exit\n"
407 " -d dump the log and then exit (don't block)\n"
408 " -t <count> print only the most recent <count> lines (implies -d)\n"
409 " -g get the size of the log's ring buffer and exit\n"
410 " -b <buffer> request alternate ring buffer\n"
411 " ('main' (default), 'radio', 'system')");
414 fprintf(stderr,"\nfilterspecs are a series of \n"
415 " <tag>[:priority]\n\n"
416 "where <tag> is a log component tag (or * for all) and priority is:\n"
423 " S Silent (supress all output)\n"
424 "\n'*' means '*:D' and <tag> by itself means <tag>:V\n"
425 "If no filterspec is found, filter defaults to '*:I'\n"
426 "\nIf not specified with -v, format is set defaults to \"brief\"\n\n");
434 * free one log_device_t and it doesn't take care of chain so it
435 * may break the chain list
437 static void log_devices_free(struct log_device_t *dev)
446 while (dev->queue->next) {
447 struct queued_entry_t *tmp = dev->queue->next;
448 dev->queue->next = tmp->next;
460 * free all the nodes after the "dev" and includes itself
462 static void log_devices_chain_free(struct log_device_t *dev)
468 struct log_device_t *tmp = dev->next;
469 dev->next = tmp->next;
470 log_devices_free(tmp);
473 log_devices_free(dev);
479 * create a new log_device_t instance but don't care about
480 * the device node accessable or not
482 static struct log_device_t *log_devices_new(const char *path)
484 struct log_device_t *new;
486 if (!path || strlen(path) <= 0)
489 new = malloc(sizeof(*new));
491 fprintf(stderr, "out of memory\n");
495 new->device = strdup(path);
497 new->printed = false;
506 * add a new device to the tail of chain
508 static int log_devices_add_to_tail(struct log_device_t *devices, struct log_device_t *new)
510 struct log_device_t *tail = devices;
512 if (!devices || !new)
524 int main(int argc, char **argv)
527 int has_set_log_format = 0;
528 int is_clear_log = 0;
532 // const char *forceFilters = NULL;
533 struct log_device_t* devices = NULL;
534 struct log_device_t* dev;
536 g_logformat = (log_format *)log_format_new();
538 if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
539 logprint_run_tests();
543 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
551 ret = getopt(argc, argv, "cdt:gsf:r:n:v:b:D");
559 // default to all silent
560 log_add_filter_rule(g_logformat, "*:s");
574 g_tail_lines = atoi(optarg);
584 if (asprintf(&buf, LOG_FILE_DIR "/%s", optarg) == -1) {
585 asprintf(stderr,"Can't malloc LOG_FILE_DIR\n");
589 dev = log_devices_new(buf);
591 if (log_devices_add_to_tail(devices, dev)) {
592 fprintf(stderr, "Open log device %s failed\n", buf);
603 // redirect output to a file
605 g_output_filename = optarg;
610 // if (optarg == NULL) {
611 // fprintf(stderr,"optarg == null\n");
612 // g_log_rotate_size_kbytes = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
614 //long logRotateSize;
617 if (!isdigit(optarg[0])) {
618 fprintf(stderr,"Invalid parameter to -r\n");
622 g_log_rotate_size_kbytes = atoi(optarg);
627 if (!isdigit(optarg[0])) {
628 fprintf(stderr,"Invalid parameter to -r\n");
633 g_max_rotated_logs = atoi(optarg);
637 err = set_log_format (optarg);
639 fprintf(stderr,"Invalid parameter to -v\n");
644 has_set_log_format = 1;
648 fprintf(stderr,"Unrecognized Option\n");
656 devices = log_devices_new("/dev/"LOGGER_LOG_MAIN);
657 if (devices == NULL) {
658 fprintf(stderr,"Can't add log device: %s\n", LOGGER_LOG_MAIN);
664 (mode & O_RDONLY) ? R_OK : 0
665 | (mode & O_WRONLY) ? W_OK : 0;
667 // only add this if it's available
668 if (0 == access("/dev/"LOGGER_LOG_SYSTEM, accessmode)) {
669 if (log_devices_add_to_tail(devices, log_devices_new("/dev/"LOGGER_LOG_SYSTEM))) {
670 fprintf(stderr,"Can't add log device: %s\n", LOGGER_LOG_SYSTEM);
675 if (0 == access("/dev/"LOGGER_LOG_APPS, accessmode)) {
676 if (log_devices_add_to_tail(devices, log_devices_new("/dev/"LOGGER_LOG_APPS))) {
677 fprintf(stderr,"Can't add log device: %s\n", LOGGER_LOG_APPS);
682 // only add this if it's available
684 if ((fd = open("/dev/"LOGGER_LOG_SYSTEM, mode)) != -1) {
685 devices->next = (struct log_device_t *)malloc( sizeof(struct log_device_t));
686 devices->next->device = strdup("/dev/"LOGGER_LOG_SYSTEM);
687 devices->next->fd = -1;
688 devices->next->printed = false;
689 devices->next->queue = NULL;
690 devices->next->next = NULL;
698 if (g_log_rotate_size_kbytes != 0 && g_output_filename == NULL)
700 fprintf(stderr,"-r requires -f as well\n");
708 if (has_set_log_format == 0) {
709 err = set_log_format("brief");
712 const char* logFormat = getenv("DLOG_PRINTF_LOG");
714 if (logFormat != NULL) {
715 err = set_log_format("brief");
718 fprintf(stderr, "invalid format in DLOG_PRINTF_LOG '%s'\n", logFormat);
723 err = log_add_filter_string(g_logformat, forceFilters);
725 fprintf (stderr, "Invalid filter expression in -logcat option\n");
728 } else if (argc == optind) {
729 // Add from environment variable
730 char *env_tags_orig = getenv("DLOG_LOG_TAGS");
732 if (env_tags_orig != NULL) {
733 err = log_add_filter_string(g_logformat, env_tags_orig);
736 fprintf(stderr, "Invalid filter expression in DLOG_LOG_TAGS\n");
742 // Add from commandline
744 fprintf(stderr,"arc = %d, optind = %d ,Kb %d, rotate %d\n", argc, optind,g_log_rotate_size_kbytes,g_max_rotated_logs);
748 // Add from environment variable
749 //char *env_tags_orig = getenv("DLOG_TAGS");
750 log_add_filter_string(g_logformat, "*:d");
755 for (i = optind ; i < argc ; i++) {
756 err = log_add_filter_string(g_logformat, argv[i]);
759 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
770 dev->fd = open(dev->device, mode);
772 fprintf(stderr, "Unable to open log device '%s': %s\n",
773 dev->device, strerror(errno));
779 ret = clear_log(dev->fd);
789 size = get_log_size(dev->fd);
795 readable = get_log_readable_size(dev->fd);
801 printf("%s: ring buffer is %dKb (%dKb consumed), "
802 "max entry is %db, max payload is %db\n", dev->device,
803 size / 1024, readable / 1024,
804 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
818 read_log_lines(devices);
820 log_devices_chain_free(devices);