From 3ae6ca1fb09a1ec254285f8e5522e8d233f1c85c Mon Sep 17 00:00:00 2001 From: armink Date: Mon, 12 Apr 2021 22:16:17 +0800 Subject: [PATCH 1/5] [ulog] Fix thread info show when kernel not startup. --- components/utilities/ulog/ulog.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index e903a707b2..4952da4007 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -318,9 +318,12 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta /* is not in interrupt context */ if (rt_interrupt_get_nest() == 0) { - rt_size_t name_len = rt_strnlen(rt_thread_self()->name, RT_NAME_MAX); - - rt_strncpy(log_buf + log_len, rt_thread_self()->name, name_len); + 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; } else From 6eb6752398901671ae7d7076d8f363196724b991 Mon Sep 17 00:00:00 2001 From: armink Date: Thu, 15 Apr 2021 14:12:02 +0800 Subject: [PATCH 2/5] [libc/time] Fix MDK build error when using gmtime_r. --- components/libc/compilers/common/sys/time.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index 2a40b7b805..b3adaedfab 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -57,6 +57,10 @@ time_t timegm(struct tm * const t); int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); +#if defined(__ARMCC_VERSION) || defined (__ICCARM__) +struct tm *gmtime_r(const time_t *timep, struct tm *r); +#endif + #ifdef RT_USING_POSIX #include /* posix clock and timer */ From 2ea39b4f054239704f7e9176a09c6681d3d3975f Mon Sep 17 00:00:00 2001 From: armink Date: Thu, 15 Apr 2021 14:15:39 +0800 Subject: [PATCH 3/5] [ulog] Using gettimeofday for timestamp get. --- components/utilities/ulog/ulog.c | 41 +++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 4952da4007..602385a200 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -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 - 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 */ + 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); + /* 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; From fc97cd23d80bf7d036b7d3afe359380b2b6aca23 Mon Sep 17 00:00:00 2001 From: armink Date: Fri, 16 Apr 2021 09:21:01 +0800 Subject: [PATCH 4/5] [libc/time] Revert the time.h --- components/libc/compilers/common/sys/time.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index b3adaedfab..2a40b7b805 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -57,10 +57,6 @@ time_t timegm(struct tm * const t); int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); -#if defined(__ARMCC_VERSION) || defined (__ICCARM__) -struct tm *gmtime_r(const time_t *timep, struct tm *r); -#endif - #ifdef RT_USING_POSIX #include /* posix clock and timer */ From 3fa35a398bac2976ffbc7bfd3040c5686abed91c Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Mon, 19 Apr 2021 06:50:58 +0800 Subject: [PATCH 5/5] Update ulog.c Use `rt_thread_self()` to determine whether the scheduler is started or not. --- components/utilities/ulog/ulog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 602385a200..9dd2ed2fd0 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -337,7 +337,7 @@ 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) + if (rt_thread_self()) { thread_name = rt_thread_self()->name; }