[ulog] Using gettimeofday for timestamp get.

This commit is contained in:
armink 2021-04-15 14:15:39 +08:00
parent 6eb6752398
commit 2ea39b4f05
1 changed files with 30 additions and 11 deletions

View File

@ -260,19 +260,36 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta
/* add time info */
{
#ifdef ULOG_TIME_USING_TIMESTAMP
static time_t now;
static struct timeval now;
static struct tm *tm, tm_tmp;
static rt_bool_t check_usec_support = RT_FALSE, usec_is_support = RT_FALSE;
now = time(NULL);
tm = gmtime_r(&now, &tm_tmp);
#ifdef RT_USING_SOFT_RTC
rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "%02d-%02d %02d:%02d:%02d.%03d", tm->tm_mon + 1,
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, rt_tick_get() % 1000);
#else
if (gettimeofday(&now, NULL) >= 0)
{
time_t t = now.tv_sec;
tm = localtime_r(&t, &tm_tmp);
/* show the time format MM-DD HH:MM:SS */
rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "%02d-%02d %02d:%02d:%02d", tm->tm_mon + 1,
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
#endif /* RT_USING_SOFT_RTC */
/* check the microseconds support when kernel is startup */
if (!check_usec_support && rt_thread_self() != RT_NULL)
{
long old_usec = now.tv_usec;
/* delay some time for wait microseconds changed */
rt_thread_delay(2);
gettimeofday(&now, NULL);
check_usec_support = RT_TRUE;
/* the microseconds is not equal between two gettimeofday calls */
if (now.tv_usec != old_usec)
usec_is_support = RT_TRUE;
}
if (usec_is_support)
{
/* show the millisecond */
log_len += rt_strlen(log_buf + log_len);
rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, ".%03d", now.tv_usec / 1000);
}
}
#else
static rt_size_t tick_len = 0;
@ -321,7 +338,9 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta
rt_size_t name_len = 0;
const char *thread_name = "N/A";
if (rt_thread_self()->name)
{
thread_name = rt_thread_self()->name;
}
name_len = rt_strnlen(thread_name, RT_NAME_MAX);
rt_strncpy(log_buf + log_len, thread_name, name_len);
log_len += name_len;