From b07bd6c5156a8c60b845bd34c4eff596a47c00d3 Mon Sep 17 00:00:00 2001 From: bernard Date: Fri, 29 Dec 2017 22:52:38 +0800 Subject: [PATCH] [Utilities] Add more options for logtrace. --- components/utilities/Kconfig | 38 ++++++++++++++ components/utilities/logtrace/SConscript | 10 +++- components/utilities/logtrace/log_trace.c | 31 +++++++++--- components/utilities/logtrace/log_trace.h | 16 +++++- components/utilities/logtrace/memlog.c | 61 +++++++++++++++++++++++ 5 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 components/utilities/logtrace/memlog.c diff --git a/components/utilities/Kconfig b/components/utilities/Kconfig index 667e0f5bc..09ab2cfe8 100644 --- a/components/utilities/Kconfig +++ b/components/utilities/Kconfig @@ -4,6 +4,44 @@ config RT_USING_LOGTRACE bool "Enable log trace" default n +if RT_USING_LOGTRACE + config LOG_TRACE_MAX_SESSION + int "Maximal number of session" + default 16 + + choice + prompt "The default level of log" + default LOG_TRACE_USING_LEVEL_INFO + + config LOG_TRACE_USING_LEVEL_NOTRACE + bool "No trace" + + config LOG_TRACE_USING_LEVEL_ERROR + bool "Only error log" + + config LOG_TRACE_USING_LEVEL_WARNING + bool "Warning log" + + config LOG_TRACE_USING_LEVEL_INFO + bool "Information log" + + config LOG_TRACE_USING_LEVEL_VERBOSE + bool "Verbose log" + + config LOG_TRACE_USING_LEVEL_DEBUG + bool "All debug log" + endchoice + + config LOG_TRACE_USING_MEMLOG + bool "Enable memory log for logtrace" + default n + help + Enable memory log for logtrace, then the logs in log_trace + will be printed out in idle thread hook function. + + Please make sure the idle hook is not used. +endif + config RT_USING_RYM bool "Enable Ymodem" default n diff --git a/components/utilities/logtrace/SConscript b/components/utilities/logtrace/SConscript index 4a70005a4..afc60792b 100644 --- a/components/utilities/logtrace/SConscript +++ b/components/utilities/logtrace/SConscript @@ -1,9 +1,17 @@ from building import * cwd = GetCurrentDir() -src = Glob('*.c') +src = Split(''' +log_trace.c +''') CPPPATH = [cwd] +if GetDepend('LOG_TRACE_USING_MEMLOG'): + src += ['memlog.c'] + +if GetDepend('RT_USING_DFS'): + src += ['log_file.c'] + group = DefineGroup('Utilities', src, depend = ['RT_USING_LOGTRACE'], CPPPATH = CPPPATH) Return('group') diff --git a/components/utilities/logtrace/log_trace.c b/components/utilities/logtrace/log_trace.c index aa2e71985..79bcde207 100644 --- a/components/utilities/logtrace/log_trace.c +++ b/components/utilities/logtrace/log_trace.c @@ -41,7 +41,7 @@ static struct rt_device _log_device; static rt_device_t _traceout_device = RT_NULL; /* define a default lg session. The name is empty. */ -static struct log_trace_session _def_session = {{"\0"}, LOG_TRACE_LEVEL_INFO}; +static struct log_trace_session _def_session = {{"\0"}, LOG_TRACE_LEVEL_DEFAULT}; static const struct log_trace_session *_the_sessions[LOG_TRACE_MAX_SESSION] = {&_def_session}; /* there is a default session at least */ static rt_uint16_t _the_sess_nr = 1; @@ -267,16 +267,31 @@ void __logtrace_vfmtout(const struct log_trace_session *session, RT_ASSERT(session); RT_ASSERT(fmt); - rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x][", rt_tick_get()); - if (_traceout_device != RT_NULL) + /* it's default session */ + if (session->id.name[0] == '\0') { - rt_device_write(_traceout_device, -1, _trace_buf, 11); - rt_device_write(_traceout_device, -1, - session->id.name, _idname_len(session->id.num)); + rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x]", rt_tick_get()); + if (_traceout_device != RT_NULL) + { + rt_device_write(_traceout_device, -1, _trace_buf, 10); + } + + ptr = &_trace_buf[0]; + } + else + { + rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x][", rt_tick_get()); + if (_traceout_device != RT_NULL) + { + rt_device_write(_traceout_device, -1, _trace_buf, 11); + rt_device_write(_traceout_device, -1, + session->id.name, _idname_len(session->id.num)); + } + + _trace_buf[0] = ']'; + ptr = &_trace_buf[1]; } - _trace_buf[0] = ']'; - ptr = &_trace_buf[1]; length = rt_vsnprintf(ptr, LOG_TRACE_BUFSZ, fmt, argptr); if (length >= LOG_TRACE_BUFSZ) diff --git a/components/utilities/logtrace/log_trace.h b/components/utilities/logtrace/log_trace.h index dc9aafd3c..ca8bebb9c 100644 --- a/components/utilities/logtrace/log_trace.h +++ b/components/utilities/logtrace/log_trace.h @@ -37,8 +37,20 @@ #define LOG_TRACE_LEVEL_DEBUG 0x09 #define LOG_TRACE_LEVEL_ALL 0x0f -#ifndef LOG_TRACE_LEVEL_DEFAULT -#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_INFO +#if defined(LOG_TRACE_USING_LEVEL_NOTRACE) +#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_NOTRACE +#elif defined(LOG_TRACE_USING_LEVEL_ERROR) +#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_ERROR +#elif defined(LOG_TRACE_USING_LEVEL_WARNING) +#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_WARNING +#elif defined(LOG_TRACE_USING_LEVEL_INFO) +#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_INFO +#elif defined(LOG_TRACE_USING_LEVEL_VERBOSE) +#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_VERBOSE +#elif defined(LOG_TRACE_USING_LEVEL_DEBUG) +#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_DEBUG +#else +#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_INFO #endif #define LOG_TRACE_ERROR "<1>" diff --git a/components/utilities/logtrace/memlog.c b/components/utilities/logtrace/memlog.c new file mode 100644 index 000000000..103373ce6 --- /dev/null +++ b/components/utilities/logtrace/memlog.c @@ -0,0 +1,61 @@ +/* + * File : memlog.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2013, RT-Thread Development Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Change Logs: + * Date Author Notes + * 2013-06-26 Grissiom the first version + */ + +#include +#include +#include + +#define PIPE_SZ 2048 +#define PIPE_NAME "memlog" + +static rt_pipe_t *_log_pipe = NULL; +static rt_uint8_t outbuf[1024]; +void memlog_flush(void) +{ + rt_size_t readsz; + rt_device_t console; + + console = rt_console_get_device(); + if (!console) return; + + readsz = rt_device_read((rt_device_t)&(_log_pipe->parent), 0, outbuf, sizeof(outbuf)); + if (readsz) + rt_device_write(console, 0, outbuf, readsz); +} + +int memlog_init(void) +{ + _log_pipe = rt_pipe_create(PIPE_NAME, PIPE_SZ); + if (_log_pipe == RT_NULL) + { + rt_kprintf("init pipe device failed.\n"); + return -1; + } + + log_trace_set_device(PIPE_NAME); + rt_thread_idle_sethook(memlog_flush); + + return 0; +} +INIT_APP_EXPORT(memlog_init);