log_format_log_line: Use timespecs directly 52/237052/3
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 24 Jun 2020 10:21:32 +0000 (12:21 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 24 Jun 2020 13:56:31 +0000 (15:56 +0200)
This Occamifies the code, ie. "entities should not be
multiplied without necessity."

Change-Id: I1d64b0563c5e921d0aa99fe0c91e3a0647232ed9

src/shared/logprint.c

index 6bdb74f..f845e9c 100644 (file)
@@ -821,7 +821,6 @@ char *log_format_log_line(
 {
        struct tm tmBuf;
        struct tm* ptm;
-       time_t time_t_temp;
        char timeBuf[32], tzBuf[16];
        char recvTimeBuf[32];
        char prefixBuf[128], suffixBuf[128];
@@ -860,26 +859,16 @@ char *log_format_log_line(
         * in the time stamp.  Don't use forward slashes, parenthesis,
         * brackets, asterisks, or other special chars here.
         */
+       struct timespec ts_sent = entry_get_ts(entry, p_format.format, TS_SENT);
+       struct timespec ts_recv = entry_get_ts(entry, p_format.format, TS_RECV);
 
-       struct timespec ts;
-
-       long int sec_sent, nsec_sent;
-       ts = entry_get_ts(entry, p_format.format, TS_SENT);
-       sec_sent = ts.tv_sec;
-       nsec_sent = ts.tv_nsec;
-
-       long int sec_recv, nsec_recv;
-       ts = entry_get_ts(entry, p_format.format, TS_RECV);
-       sec_recv = ts.tv_sec;
-       nsec_recv = ts.tv_nsec;
-
-       time_t_temp = sec_sent;
+       time_t time_t_temp = ts_sent.tv_sec;
        ptm = localtime_r(&time_t_temp, &tmBuf);
        if (!ptm)
                return NULL;
        strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
 
-       time_t_temp = sec_recv;
+       time_t_temp = ts_recv.tv_sec;
        ptm = localtime_r(&time_t_temp, &tmBuf);
        if (!ptm)
                return NULL;
@@ -925,7 +914,7 @@ char *log_format_log_line(
                break;
        case FORMAT_TIME:
                prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
-                               "%s.%03ld%s %c/%-8s(%5d): ", timeBuf, nsec_sent / 1000000,
+                               "%s.%03ld%s %c/%-8s(%5d): ", timeBuf, ts_sent.tv_nsec / 1000000,
                                tzBuf, priChar, tag, (int)entry->pid);
                strncpy(suffixBuf, "\n", 2);
                suffixLen = 1;
@@ -934,7 +923,7 @@ char *log_format_log_line(
        case FORMAT_THREADTIME:
                prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
                                "%s.%03ld%s %c/%-8s(P%5d, T%5d): ",
-                               timeBuf, nsec_sent / 1000000,
+                               timeBuf, ts_sent.tv_nsec / 1000000,
                                tzBuf, priChar, tag, (int)entry->pid, (int)entry->tid);
                strncpy(suffixBuf, "\n", 2);
                suffixLen = 1;
@@ -943,7 +932,7 @@ char *log_format_log_line(
        case FORMAT_KERNELTIME:
                prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
                                "%5lu.%03ld %c/%-8s(P%5d, T%5d): ",
-                               sec_sent, nsec_sent / 1000000,
+                               ts_sent.tv_sec, ts_sent.tv_nsec / 1000000,
                                priChar, tag, (int)entry->pid, (int)entry->tid);
                strncpy(suffixBuf, "\n", 2);
                suffixLen = 1;
@@ -952,17 +941,17 @@ char *log_format_log_line(
        case FORMAT_RECV_REALTIME:
                prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
                                "%s.%03ld %c/%-8s(P%5d, T%5d): ",
-                               recvTimeBuf, nsec_recv / 1000000,
+                               recvTimeBuf, ts_recv.tv_nsec / 1000000,
                                priChar, tag, (int)entry->pid, (int)entry->tid);
                strncpy(suffixBuf, "\n", 2);
                suffixLen = 1;
                colorSuffix = false;
                break;
-       case FORMAT_RWTIME:
+       case FORMAT_RWTIME: {
                /* This shouldn't be needed. However, sadly, the rwtime format contains two different timestamps.
                 * Such a situation is impossible to describe as is. Therefore, it calls for an ugly hack :P
                 * Again, the planned refactor of the format system will fix this. */
-               ts = entry_get_ts(entry, FORMAT_KERNELTIME, TS_SENT);
+               struct timespec ts = entry_get_ts(entry, FORMAT_KERNELTIME, TS_SENT);
                prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
                                "%s [%5lu.%03ld] %c/%-8s(P%5d, T%5d): ",
                                recvTimeBuf, ts.tv_sec, ts.tv_nsec / 1000000,
@@ -971,10 +960,11 @@ char *log_format_log_line(
                suffixLen = 1;
                colorSuffix = false;
                break;
+       }
        case FORMAT_LONG:
                prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
                                "[ %s.%03ld %c/%-8s P%5d, T%5d]\n",
-                               timeBuf, nsec_sent / 1000000, priChar, tag,
+                               timeBuf, ts_sent.tv_nsec / 1000000, priChar, tag,
                                (int)entry->pid, (int)entry->tid);
                strncpy(suffixBuf, "\n\n", 3);
                suffixLen = 2;