diff options
author | Mark Salyzyn | 2015-10-06 10:59:02 -0500 |
---|---|---|
committer | Kevin Ma | 2015-11-12 13:51:41 -0600 |
commit | 5b0d59dde85162fa9ef3a32464c0f1b81d7e05cb (patch) | |
tree | 126d85c1aed8b2d0e33cfacc32cd84f8cb468efc | |
parent | 99cd21ef5c3548613d8a614551b858a5c47985fc (diff) | |
download | platform-system-core-5b0d59dde85162fa9ef3a32464c0f1b81d7e05cb.tar.gz platform-system-core-5b0d59dde85162fa9ef3a32464c0f1b81d7e05cb.tar.xz platform-system-core-5b0d59dde85162fa9ef3a32464c0f1b81d7e05cb.zip |
logcat: continue where we left off
Issue introduced as part of new logcatd functionality in
commit f3555d9427425c2cba9600ceffb49305c440aa4a
Faulty logic, add a gTest to confirm.
Bug: 19608716
Change-Id: Ic1b97def25e03e69faae4398a3dff2ff0f88545e
-rw-r--r-- | logcat/logcat.cpp | 2 | ||||
-rw-r--r-- | logcat/tests/logcat_test.cpp | 134 |
2 files changed, 120 insertions, 16 deletions
diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp index e598bb810..3d05d8ffc 100644 --- a/logcat/logcat.cpp +++ b/logcat/logcat.cpp | |||
@@ -649,7 +649,7 @@ int main(int argc, char **argv) | |||
649 | break; | 649 | break; |
650 | 650 | ||
651 | case 'f': | 651 | case 'f': |
652 | if ((tail_time == log_time::EPOCH) && (tail_lines != 0)) { | 652 | if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) { |
653 | tail_time = lastLogTime(optarg); | 653 | tail_time = lastLogTime(optarg); |
654 | } | 654 | } |
655 | // redirect output to a file | 655 | // redirect output to a file |
diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp index de2db674d..9455d870e 100644 --- a/logcat/tests/logcat_test.cpp +++ b/logcat/tests/logcat_test.cpp | |||
@@ -15,10 +15,12 @@ | |||
15 | */ | 15 | */ |
16 | 16 | ||
17 | #include <ctype.h> | 17 | #include <ctype.h> |
18 | #include <dirent.h> | ||
18 | #include <signal.h> | 19 | #include <signal.h> |
19 | #include <stdio.h> | 20 | #include <stdio.h> |
20 | #include <stdlib.h> | 21 | #include <stdlib.h> |
21 | #include <string.h> | 22 | #include <string.h> |
23 | #include <sys/types.h> | ||
22 | 24 | ||
23 | #include <gtest/gtest.h> | 25 | #include <gtest/gtest.h> |
24 | #include <log/log.h> | 26 | #include <log/log.h> |
@@ -284,7 +286,7 @@ TEST(logcat, get_size) { | |||
284 | 286 | ||
285 | while (fgets(buffer, sizeof(buffer), fp)) { | 287 | while (fgets(buffer, sizeof(buffer), fp)) { |
286 | int size, consumed, max, payload; | 288 | int size, consumed, max, payload; |
287 | char size_mult[2], consumed_mult[2]; | 289 | char size_mult[3], consumed_mult[3]; |
288 | long full_size, full_consumed; | 290 | long full_size, full_consumed; |
289 | 291 | ||
290 | size = consumed = max = payload = 0; | 292 | size = consumed = max = payload = 0; |
@@ -489,12 +491,12 @@ TEST(logcat, logrotate) { | |||
489 | static const char comm[] = "logcat -b radio -b events -b system -b main" | 491 | static const char comm[] = "logcat -b radio -b events -b system -b main" |
490 | " -d -f %s/log.txt -n 7 -r 1"; | 492 | " -d -f %s/log.txt -n 7 -r 1"; |
491 | char command[sizeof(buf) + sizeof(comm)]; | 493 | char command[sizeof(buf) + sizeof(comm)]; |
492 | sprintf(command, comm, buf); | 494 | snprintf(command, sizeof(command), comm, buf); |
493 | 495 | ||
494 | int ret; | 496 | int ret; |
495 | EXPECT_FALSE((ret = system(command))); | 497 | EXPECT_FALSE((ret = system(command))); |
496 | if (!ret) { | 498 | if (!ret) { |
497 | sprintf(command, "ls -s %s 2>/dev/null", buf); | 499 | snprintf(command, sizeof(command), "ls -s %s 2>/dev/null", buf); |
498 | 500 | ||
499 | FILE *fp; | 501 | FILE *fp; |
500 | EXPECT_TRUE(NULL != (fp = popen(command, "r"))); | 502 | EXPECT_TRUE(NULL != (fp = popen(command, "r"))); |
@@ -503,16 +505,12 @@ TEST(logcat, logrotate) { | |||
503 | int count = 0; | 505 | int count = 0; |
504 | 506 | ||
505 | while (fgets(buffer, sizeof(buffer), fp)) { | 507 | while (fgets(buffer, sizeof(buffer), fp)) { |
506 | static const char match_1[] = "4 log.txt"; | ||
507 | static const char match_2[] = "8 log.txt"; | ||
508 | static const char match_3[] = "12 log.txt"; | ||
509 | static const char match_4[] = "16 log.txt"; | ||
510 | static const char total[] = "total "; | 508 | static const char total[] = "total "; |
509 | int num; | ||
510 | char c; | ||
511 | 511 | ||
512 | if (!strncmp(buffer, match_1, sizeof(match_1) - 1) | 512 | if ((2 == sscanf(buffer, "%d log.tx%c", &num, &c)) && |
513 | || !strncmp(buffer, match_2, sizeof(match_2) - 1) | 513 | (num <= 24)) { |
514 | || !strncmp(buffer, match_3, sizeof(match_3) - 1) | ||
515 | || !strncmp(buffer, match_4, sizeof(match_4) - 1)) { | ||
516 | ++count; | 514 | ++count; |
517 | } else if (strncmp(buffer, total, sizeof(total) - 1)) { | 515 | } else if (strncmp(buffer, total, sizeof(total) - 1)) { |
518 | fprintf(stderr, "WARNING: Parse error: %s", buffer); | 516 | fprintf(stderr, "WARNING: Parse error: %s", buffer); |
@@ -522,7 +520,7 @@ TEST(logcat, logrotate) { | |||
522 | EXPECT_TRUE(count == 7 || count == 8); | 520 | EXPECT_TRUE(count == 7 || count == 8); |
523 | } | 521 | } |
524 | } | 522 | } |
525 | sprintf(command, "rm -rf %s", buf); | 523 | snprintf(command, sizeof(command), "rm -rf %s", buf); |
526 | EXPECT_FALSE(system(command)); | 524 | EXPECT_FALSE(system(command)); |
527 | } | 525 | } |
528 | 526 | ||
@@ -534,12 +532,12 @@ TEST(logcat, logrotate_suffix) { | |||
534 | static const char logcat_cmd[] = "logcat -b radio -b events -b system -b main" | 532 | static const char logcat_cmd[] = "logcat -b radio -b events -b system -b main" |
535 | " -d -f %s/log.txt -n 10 -r 1"; | 533 | " -d -f %s/log.txt -n 10 -r 1"; |
536 | char command[sizeof(tmp_out_dir) + sizeof(logcat_cmd)]; | 534 | char command[sizeof(tmp_out_dir) + sizeof(logcat_cmd)]; |
537 | sprintf(command, logcat_cmd, tmp_out_dir); | 535 | snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir); |
538 | 536 | ||
539 | int ret; | 537 | int ret; |
540 | EXPECT_FALSE((ret = system(command))); | 538 | EXPECT_FALSE((ret = system(command))); |
541 | if (!ret) { | 539 | if (!ret) { |
542 | sprintf(command, "ls %s 2>/dev/null", tmp_out_dir); | 540 | snprintf(command, sizeof(command), "ls %s 2>/dev/null", tmp_out_dir); |
543 | 541 | ||
544 | FILE *fp; | 542 | FILE *fp; |
545 | EXPECT_TRUE(NULL != (fp = popen(command, "r"))); | 543 | EXPECT_TRUE(NULL != (fp = popen(command, "r"))); |
@@ -575,7 +573,113 @@ TEST(logcat, logrotate_suffix) { | |||
575 | pclose(fp); | 573 | pclose(fp); |
576 | EXPECT_EQ(11, log_file_count); | 574 | EXPECT_EQ(11, log_file_count); |
577 | } | 575 | } |
578 | sprintf(command, "rm -rf %s", tmp_out_dir); | 576 | snprintf(command, sizeof(command), "rm -rf %s", tmp_out_dir); |
577 | EXPECT_FALSE(system(command)); | ||
578 | } | ||
579 | |||
580 | TEST(logcat, logrotate_continue) { | ||
581 | static const char tmp_out_dir_form[] = "/data/local/tmp/logcat.logrotate.XXXXXX"; | ||
582 | char tmp_out_dir[sizeof(tmp_out_dir_form)]; | ||
583 | ASSERT_TRUE(NULL != mkdtemp(strcpy(tmp_out_dir, tmp_out_dir_form))); | ||
584 | |||
585 | static const char log_filename[] = "log.txt"; | ||
586 | static const char logcat_cmd[] = "logcat -b all -d -f %s/%s -n 256 -r 1024"; | ||
587 | static const char cleanup_cmd[] = "rm -rf %s"; | ||
588 | char command[sizeof(tmp_out_dir) + sizeof(logcat_cmd) + sizeof(log_filename)]; | ||
589 | snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir, log_filename); | ||
590 | |||
591 | int ret; | ||
592 | EXPECT_FALSE((ret = system(command))); | ||
593 | if (ret) { | ||
594 | snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir); | ||
595 | EXPECT_FALSE(system(command)); | ||
596 | return; | ||
597 | } | ||
598 | FILE *fp; | ||
599 | snprintf(command, sizeof(command), "%s/%s", tmp_out_dir, log_filename); | ||
600 | EXPECT_TRUE(NULL != ((fp = fopen(command, "r")))); | ||
601 | if (!fp) { | ||
602 | snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir); | ||
603 | EXPECT_FALSE(system(command)); | ||
604 | return; | ||
605 | } | ||
606 | char *line = NULL; | ||
607 | char *last_line = NULL; // this line is allowed to stutter, one-line overlap | ||
608 | char *second_last_line = NULL; | ||
609 | size_t len = 0; | ||
610 | while (getline(&line, &len, fp) != -1) { | ||
611 | free(second_last_line); | ||
612 | second_last_line = last_line; | ||
613 | last_line = line; | ||
614 | line = NULL; | ||
615 | } | ||
616 | fclose(fp); | ||
617 | free(line); | ||
618 | if (second_last_line == NULL) { | ||
619 | fprintf(stderr, "No second to last line, using last, test may fail\n"); | ||
620 | second_last_line = last_line; | ||
621 | last_line = NULL; | ||
622 | } | ||
623 | free(last_line); | ||
624 | EXPECT_TRUE(NULL != second_last_line); | ||
625 | if (!second_last_line) { | ||
626 | snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir); | ||
627 | EXPECT_FALSE(system(command)); | ||
628 | return; | ||
629 | } | ||
630 | // re-run the command, it should only add a few lines more content if it | ||
631 | // continues where it left off. | ||
632 | snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir, log_filename); | ||
633 | EXPECT_FALSE((ret = system(command))); | ||
634 | if (ret) { | ||
635 | snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir); | ||
636 | EXPECT_FALSE(system(command)); | ||
637 | return; | ||
638 | } | ||
639 | DIR *dir; | ||
640 | EXPECT_TRUE(NULL != (dir = opendir(tmp_out_dir))); | ||
641 | if (!dir) { | ||
642 | snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir); | ||
643 | EXPECT_FALSE(system(command)); | ||
644 | return; | ||
645 | } | ||
646 | struct dirent *entry; | ||
647 | unsigned count = 0; | ||
648 | while ((entry = readdir(dir))) { | ||
649 | if (strncmp(entry->d_name, log_filename, sizeof(log_filename) - 1)) { | ||
650 | continue; | ||
651 | } | ||
652 | snprintf(command, sizeof(command), "%s/%s", tmp_out_dir, entry->d_name); | ||
653 | EXPECT_TRUE(NULL != ((fp = fopen(command, "r")))); | ||
654 | if (!fp) { | ||
655 | fprintf(stderr, "%s ?\n", command); | ||
656 | continue; | ||
657 | } | ||
658 | line = NULL; | ||
659 | size_t number = 0; | ||
660 | while (getline(&line, &len, fp) != -1) { | ||
661 | ++number; | ||
662 | if (!strcmp(line, second_last_line)) { | ||
663 | EXPECT_TRUE(++count <= 1); | ||
664 | fprintf(stderr, "%s(%zu):\n", entry->d_name, number); | ||
665 | } | ||
666 | } | ||
667 | fclose(fp); | ||
668 | free(line); | ||
669 | unlink(command); | ||
670 | } | ||
671 | closedir(dir); | ||
672 | if (count > 1) { | ||
673 | char *brk = strpbrk(second_last_line, "\r\n"); | ||
674 | if (!brk) { | ||
675 | brk = second_last_line + strlen(second_last_line); | ||
676 | } | ||
677 | fprintf(stderr, "\"%.*s\" occured %u times\n", | ||
678 | (int)(brk - second_last_line), second_last_line, count); | ||
679 | } | ||
680 | free(second_last_line); | ||
681 | |||
682 | snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir); | ||
579 | EXPECT_FALSE(system(command)); | 683 | EXPECT_FALSE(system(command)); |
580 | } | 684 | } |
581 | 685 | ||