Bladeren bron

Merge pull request #4601 from armink/fix_ulog

[ulog] Fix thread info show when kernel not startup.
Bernard Xiong 4 jaren geleden
bovenliggende
commit
e3c19a8463
1 gewijzigde bestanden met toevoegingen van 36 en 14 verwijderingen
  1. 36 14
      components/utilities/ulog/ulog.c

+ 36 - 14
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;
@@ -318,9 +335,14 @@ 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())
+            {
+                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