TEST(logcat, tail_time) {
    FILE *fp;

    ASSERT_TRUE(NULL != (fp = popen("logcat -v long -b all -t 10 2>&1", "r")));

    char buffer[BIG_BUFFER];
    char *last_timestamp = NULL;
    char *first_timestamp = NULL;
    int count = 0;

    char *cp;
    while ((cp = fgetLongTime(buffer, sizeof(buffer), fp))) {
        ++count;
        if (!first_timestamp) {
            first_timestamp = strdup(cp);
        }
        free(last_timestamp);
        last_timestamp = strdup(cp);
    }
    pclose(fp);

    EXPECT_EQ(10, count);
    EXPECT_TRUE(last_timestamp != NULL);
    EXPECT_TRUE(first_timestamp != NULL);

    snprintf(buffer, sizeof(buffer), "logcat -v long -b all -t '%s' 2>&1",
             first_timestamp);
    ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));

    int second_count = 0;
    int last_timestamp_count = -1;

    while ((cp = fgetLongTime(buffer, sizeof(buffer), fp))) {
        ++second_count;
        if (first_timestamp) {
            // we can get a transitory *extremely* rare failure if hidden
            // underneath the time is *exactly* XX-XX XX:XX:XX.XXX000000
            EXPECT_STREQ(cp, first_timestamp);
            free(first_timestamp);
            first_timestamp = NULL;
        }
        if (!strcmp(cp, last_timestamp)) {
            last_timestamp_count = second_count;
        }
    }
    pclose(fp);

    free(last_timestamp);
    last_timestamp = NULL;
    free(first_timestamp);

    EXPECT_TRUE(first_timestamp == NULL);
    EXPECT_LE(count, second_count);
    EXPECT_LE(count, last_timestamp_count);
}
void do_tail(int num) {
    int tries = 3; // in case run too soon after system start or buffer clear
    int count;

    do {
        char buffer[BIG_BUFFER];

        snprintf(buffer, sizeof(buffer),
          "logcat -v long -b radio -b events -b system -b main -t %d 2>/dev/null",
          num);

        FILE *fp;
        ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));

        count = 0;

        while (fgetLongTime(buffer, sizeof(buffer), fp)) {
            ++count;
        }

        pclose(fp);

    } while ((count < num) && --tries && (sleep(1), true));

    ASSERT_EQ(num, count);
}
TEST(logcat, tz) {

    if (android_log_clockid() == CLOCK_MONOTONIC) {
        fprintf(stderr, "Skipping test, logd is monotonic time\n");
        return;
    }

    int tries = 3; // in case run too soon after system start or buffer clear
    int count;

    do {
        FILE *fp;

        ASSERT_TRUE(NULL != (fp = popen(
          "logcat -v long -v America/Los_Angeles -b all -t 3 2>/dev/null",
          "r")));

        char buffer[BIG_BUFFER];

        count = 0;

        while (fgetLongTime(buffer, sizeof(buffer), fp)) {
            if (strstr(buffer, " -0700") || strstr(buffer, " -0800")) {
                ++count;
            }
        }

        pclose(fp);

    } while ((count < 3) && --tries && (sleep(1), true));

    ASSERT_EQ(3, count);
}
TEST(logcat, tz) {

    if (android_log_clockid() == CLOCK_MONOTONIC) {
        fprintf(stderr, "Skipping test, logd is monotonic time\n");
        return;
    }

    FILE *fp;

    ASSERT_TRUE(NULL != (fp = popen(
      "logcat -v long -v America/Los_Angeles -b all -t 3 2>/dev/null",
      "r")));

    char buffer[5120];

    int count = 0;

    while (fgetLongTime(buffer, sizeof(buffer), fp)) {
        if (strstr(buffer, " -0700") || strstr(buffer, " -0800")) {
            ++count;
        }
    }

    pclose(fp);

    ASSERT_EQ(3, count);
}
TEST(logcat, tail_1000) {
    FILE *fp;

    ASSERT_TRUE(NULL != (fp = popen(
      "logcat -v long -b radio -b events -b system -b main -t 1000 2>/dev/null",
      "r")));

    char buffer[5120];

    int count = 0;

    while (fgetLongTime(buffer, sizeof(buffer), fp)) {
        ++count;
    }

    pclose(fp);

    ASSERT_EQ(1000, count);
}
TEST(logcat, ntz) {
    FILE *fp;

    ASSERT_TRUE(NULL != (fp = popen(
      "logcat -v long -v America/Los_Angeles -v zone -b all -t 3 2>/dev/null",
      "r")));

    char buffer[BIG_BUFFER];

    int count = 0;

    while (fgetLongTime(buffer, sizeof(buffer), fp)) {
        if (strstr(buffer, " -0700") || strstr(buffer, " -0800")) {
            ++count;
        }
    }

    pclose(fp);

    ASSERT_EQ(0, count);
}