Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eba7344f authored by Aristidis Papaioannou's avatar Aristidis Papaioannou Committed by Mark Salyzyn
Browse files

Made suffix length of rotated logcat files constant.

The suffix now uses as many digits as needed to cover the maximum number
of rotated files.

eg, for 20 files it would use 2 digits: log_file.{01,02,03,...,20}

Change-Id: I4709b3cf4bf88c209db3c4d9e24a3a4731c98bae
parent 64989126
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
@@ -80,15 +81,20 @@ static void rotateLogs()

    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, i);
        asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);

        if (i - 1 == 0) {
            asprintf(&file0, "%s", g_outputFileName);
        } else {
            asprintf(&file0, "%s.%d", g_outputFileName, i - 1);
            asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
        }

        err = rename (file0, file1);
+54 −0
Original line number Diff line number Diff line
@@ -524,6 +524,60 @@ TEST(logcat, logrotate) {
    EXPECT_FALSE(system(command));
}

TEST(logcat, logrotate_suffix) {
    static const char tmp_out_dir_form[] = "/data/local/tmp/logcat.logrotate.XXXXXX";
    char tmp_out_dir[sizeof(tmp_out_dir_form)];
    ASSERT_TRUE(NULL != mkdtemp(strcpy(tmp_out_dir, tmp_out_dir_form)));

    static const char logcat_cmd[] = "logcat -b radio -b events -b system -b main"
                                     " -d -f %s/log.txt -n 10 -r 1";
    char command[sizeof(tmp_out_dir) + sizeof(logcat_cmd)];
    sprintf(command, logcat_cmd, tmp_out_dir);

    int ret;
    EXPECT_FALSE((ret = system(command)));
    if (!ret) {
        sprintf(command, "ls %s 2>/dev/null", tmp_out_dir);

        FILE *fp;
        EXPECT_TRUE(NULL != (fp = popen(command, "r")));
        char buffer[5120];
        int log_file_count = 0;

        while (fgets(buffer, sizeof(buffer), fp)) {
            static const char rotated_log_filename_prefix[] = "log.txt.";
            static const size_t rotated_log_filename_prefix_len =
                strlen(rotated_log_filename_prefix);
            static const char total[] = "total ";
            static const char log_filename[] = "log.txt";

            if (!strncmp(buffer, rotated_log_filename_prefix, rotated_log_filename_prefix_len)) {
              // Rotated file should have form log.txt.##
              char* rotated_log_filename_suffix = buffer + rotated_log_filename_prefix_len;
              char* endptr;
              const long int suffix_value = strtol(rotated_log_filename_suffix, &endptr, 10);
              EXPECT_EQ(rotated_log_filename_suffix + 2, endptr);
              EXPECT_LE(suffix_value, 10);
              EXPECT_GT(suffix_value, 0);
              ++log_file_count;
              continue;
            }

            if (!strncmp(buffer, log_filename, strlen(log_filename))) {
              ++log_file_count;
              continue;
            }

            fprintf(stderr, "ERROR: Unexpected file: %s", buffer);
            ADD_FAILURE();
        }
        pclose(fp);
        EXPECT_EQ(11, log_file_count);
    }
    sprintf(command, "rm -rf %s", tmp_out_dir);
    EXPECT_FALSE(system(command));
}

static void caught_blocking_clear(int /*signum*/)
{
    unsigned long long v = 0xDEADBEEFA55C0000ULL;