From: Aleksei Vereshchagin Date: Wed, 29 Aug 2018 18:31:52 +0000 (+0300) Subject: show milliseconds in log X-Git-Tag: submit/tizen/20180911.125435~20 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6693dcc6d4341f2ad6e9a18a8b470eaac7fb4ed8;p=sdk%2Ftools%2Fprofctl.git show milliseconds in log --- diff --git a/profctl.c b/profctl.c index 82c5677..fc6fc16 100644 --- a/profctl.c +++ b/profctl.c @@ -76,12 +76,40 @@ static struct option long_options[] = { {0, 0, 0, 0} }; +static int get_current_time(time_t *et, struct tm *tm, int *millisec) +{ + struct timeval timeval; + int result = gettimeofday(&timeval, NULL); + time_t t; + if (result == 0) { + t = timeval.tv_sec; + } + else { + t = time(NULL); + timeval.tv_usec = 0; + } + int msec = (timeval.tv_usec + 500) / 1000; + if (msec >= 1000) { + ++t; + msec = 0; + } + if (et != NULL) { + *et = t; + } + if (tm != NULL) { + *tm = *localtime(&t); + } + *millisec = msec; + return result; +} + static void log_current_time() { - time_t t = time(NULL); - struct tm tm = *localtime(&t); - fprintf(stderr, "%04d-%02d-%02d %02d:%02d:%02d ", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); + struct tm tm; + int millisec; + get_current_time(NULL, &tm, &millisec); + fprintf(stderr, "%02d-%02d-%02d %02d:%02d:%02d.%03d ", + tm.tm_year + 1900 - 2000, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec); } static pthread_mutex_t stderr_lock; @@ -331,24 +359,11 @@ static void *outstat(void *arg) long pages; long available; - struct timeval timeval; - int err, millisec; time_t t; - err = gettimeofday(&timeval, NULL); - if (err == 0) { - t = timeval.tv_sec; - } - else { - t = time(NULL); - timeval.tv_usec = 0; + int millisec; + if (get_current_time(&t, NULL, &millisec) != 0) { // gettimeofday failed (no milliseconds) timeoutMicrosec = 1000000; // 1 sec } - millisec = (timeval.tv_usec + 500) / 1000; - if (millisec >= 1000) - { - ++t; - millisec = 0; - } fseek(sstat, 0, SEEK_SET); fflush(sstat);