Skip to content
Snippets Groups Projects
Commit 33f77039 authored by Vladimir Davydov's avatar Vladimir Davydov
Browse files

say: add helper function to get current time

We have three places in say.c where we peform exactly the same steps to
obtain the current time. Let's add a helper function to avoid code and
comments duplication.

NO_DOC=refactoring
NO_TEST=refactoring
NO_CHANGELOG=refactoring

(cherry picked from commit dd825873)
parent 916bc445
No related branches found
No related tags found
No related merge requests found
......@@ -175,6 +175,21 @@ level_to_syslog_priority(int level)
}
}
/**
* Helper function that fills the given tm struct with the current local time.
* tm_sec is set to the number of seconds that passed since the last minute
* (0-60). Note, in contrast to tm.tm_sec, it has a fractional part.
*/
static void
get_current_time(struct tm *tm, double *tm_sec)
{
/* Don't use ev_now() since it requires a working event loop. */
ev_tstamp now = ev_time();
time_t now_seconds = (time_t)now;
localtime_r(&now_seconds, tm);
*tm_sec = now - now_seconds + tm->tm_sec;
}
enum say_logger_type
log_type(void)
{
......@@ -846,17 +861,14 @@ say_format_plain(struct log *log, char *buf, int len, int level, const char *fil
const char *error, const char *format, va_list ap)
{
(void) log;
/* Don't use ev_now() since it requires a working event loop. */
ev_tstamp now = ev_time();
time_t now_seconds = (time_t) now;
struct tm tm;
localtime_r(&now_seconds, &tm);
double tm_sec;
get_current_time(&tm, &tm_sec);
/* Print time in format 2012-08-07 18:30:00.634 */
int total = strftime(buf, len, "%F %H:%M", &tm);
buf += total, len -= total;
SNPRINT(total, snprintf, buf, len, ":%06.3f",
now - now_seconds + tm.tm_sec);
SNPRINT(total, snprintf, buf, len, ":%06.3f", tm_sec);
/* Print pid */
SNPRINT(total, snprintf, buf, len, " [%i]", getpid());
......@@ -883,15 +895,12 @@ say_format_json(struct log *log, char *buf, int len, int level, const char *file
SNPRINT(total, snprintf, buf, len, "{\"time\": \"");
/* Don't use ev_now() since it requires a working event loop. */
ev_tstamp now = ev_time();
time_t now_seconds = (time_t) now;
struct tm tm;
localtime_r(&now_seconds, &tm);
double tm_sec;
get_current_time(&tm, &tm_sec);
int written = strftime(buf, len, "%FT%H:%M", &tm);
buf += written, len -= written, total += written;
SNPRINT(total, snprintf, buf, len, ":%06.3f",
now - now_seconds + tm.tm_sec);
SNPRINT(total, snprintf, buf, len, ":%06.3f", tm_sec);
written = strftime(buf, len, "%z", &tm);
buf += written, len -= written, total += written;
SNPRINT(total, snprintf, buf, len, "\", ");
......@@ -973,11 +982,9 @@ static int
say_format_syslog(struct log *log, char *buf, int len, int level, const char *filename,
int line, const char *error, const char *format, va_list ap)
{
/* Don't use ev_now() since it requires a working event loop. */
ev_tstamp now = ev_time();
time_t now_seconds = (time_t) now;
struct tm tm;
localtime_r(&now_seconds, &tm);
double tm_sec;
get_current_time(&tm, &tm_sec);
int total = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment