Browse Source

[component][ulog] Reduce thread stack usage when using ulog.

armink 6 years ago
parent
commit
c336099a93

+ 1 - 1
components/utilities/Kconfig

@@ -162,7 +162,7 @@ config RT_USING_ULOG
 
         menu "log format"
             config ULOG_OUTPUT_FLOAT
-                bool "Enable float number support."
+                bool "Enable float number support. It will using more thread stack."
                 select RT_USING_LIBC
                 default n
                 help

+ 40 - 5
components/utilities/ulog/ulog.c

@@ -170,6 +170,26 @@ size_t ulog_strcpy(size_t cur_len, char *dst, const char *src)
     return src - src_old;
 }
 
+size_t ulog_ultoa(char *s, unsigned long int n)
+{
+    size_t i = 0, j = 0, len = 0;
+    char swap;
+
+    do
+    {
+        s[len++] = n % 10 + '0';
+    } while (n /= 10);
+    s[len] = '\0';
+    /* reverse string */
+    for (i = 0, j = len - 1; i < j; ++i, --j)
+    {
+        swap = s[i];
+        s[i] = s[j];
+        s[j] = swap;
+    }
+    return len;
+}
+
 static void output_unlock(void)
 {
     /* is in thread context */
@@ -221,14 +241,18 @@ static char *get_log_buf(void)
 RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *tag, rt_bool_t newline,
         const char *format, va_list args)
 {
-    rt_size_t log_len = 0, newline_len = rt_strlen(ULOG_NEWLINE_SIGN);
-    int fmt_result;
+    /* the caller has locker, so it can use static variable for reduce stack usage */
+    static rt_size_t log_len, newline_len;
+    static int fmt_result;
 
     RT_ASSERT(log_buf);
     RT_ASSERT(level <= LOG_LVL_DBG);
     RT_ASSERT(tag);
     RT_ASSERT(format);
 
+    log_len = 0;
+    newline_len = rt_strlen(ULOG_NEWLINE_SIGN);
+
 #ifdef ULOG_USING_COLOR
     /* add CSI start sign and color info */
     if (color_output_info[level])
@@ -242,9 +266,10 @@ 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
-        time_t now = time(NULL);
-        struct tm *tm, tm_tmp;
+        static time_t now;
+        static struct tm *tm, tm_tmp;
 
+        now = time(NULL);
         tm = gmtime_r(&now, &tm_tmp);
 
 #ifdef RT_USING_SOFT_RTC
@@ -256,7 +281,12 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta
 #endif /* RT_USING_SOFT_RTC */
 
 #else
-        rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "[%d]", rt_tick_get());
+        static rt_size_t tick_len = 0;
+
+        log_buf[log_len] = '[';
+        tick_len = ulog_ultoa(log_buf + log_len + 1, rt_tick_get());
+        log_buf[log_len + 1 + tick_len] = ']';
+        log_buf[log_len + 1 + tick_len + 1] = '\0';
 #endif /* ULOG_TIME_USING_TIMESTAMP */
 
         log_len += rt_strlen(log_buf + log_len);
@@ -988,6 +1018,11 @@ rt_err_t ulog_backend_unregister(ulog_backend_t backend)
     RT_ASSERT(backend);
     RT_ASSERT(ulog.init_ok);
 
+    if (backend->deinit)
+    {
+        backend->deinit(backend);
+    }
+
     level = rt_hw_interrupt_disable();
     rt_slist_remove(&ulog.backend_list, &backend->list);
     rt_hw_interrupt_enable(level);

+ 1 - 1
components/utilities/ulog/ulog.h

@@ -82,7 +82,7 @@ void ulog_async_waiting_log(rt_int32_t time);
 void ulog_hexdump(const char *name, rt_size_t width, rt_uint8_t *buf, rt_size_t size);
 
 /*
- * Another log output API. This API is difficult to use than LOG_X API.
+ * Another log output API. This API is more difficult to use than LOG_X API.
  */
 void ulog_voutput(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, va_list args);
 void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, ...);

+ 3 - 2
components/utilities/ulog/ulog_def.h

@@ -7,6 +7,7 @@
  * Date           Author       Notes
  * 2018-08-25     armink       the first version
  */
+
 #ifndef _ULOG_DEF_H_
 #define _ULOG_DEF_H_
 
@@ -63,9 +64,9 @@ extern "C" {
 #if !defined(LOG_LVL)
     /* compatible for rtdbg */
     #if defined(DBG_LEVEL)
-        #define LOG_LVL                    DBG_LEVEL
+        #define LOG_LVL                DBG_LEVEL
     #else
-        #define LOG_LVL                    LOG_LVL_DBG
+        #define LOG_LVL                LOG_LVL_DBG
     #endif
 #endif /* !defined(LOG_LVL) */