From 4522c026bbc80d9a165704baec3fb6c400238337 Mon Sep 17 00:00:00 2001 From: Letter Date: Sat, 22 May 2021 21:50:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20log=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=E4=BC=98=E5=8C=96=20x86=20demo=E4=BD=BF?= =?UTF-8?q?=E7=94=A8cmake=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++++ README.md | 5 +++- demo/x86-gcc/CmakeLists.txt | 25 +++++++++++++++++++ demo/x86-gcc/makefile | 41 ------------------------------- demo/x86-gcc/shell_cfg.h | 6 +++++ demo/x86-gcc/shell_port.c | 46 +++++++++++++++++++++++++++++++++++ extensions/log/log.c | 48 +++++-------------------------------- 7 files changed, 91 insertions(+), 84 deletions(-) create mode 100644 .gitignore create mode 100644 demo/x86-gcc/CmakeLists.txt delete mode 100644 demo/x86-gcc/makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7eaf369 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +CMakeFiles +cmake_install.cmake +CMakeCache.txt +demo/x86-gcc/Makefile \ No newline at end of file diff --git a/README.md b/README.md index ef96644..54decbf 100644 --- a/README.md +++ b/README.md @@ -487,6 +487,8 @@ letter shell 3.x的权限管理同用户定义紧密相关,letter shell 3.x使 letter shell 3.1增加了shell锁,主要目的是为了防止shell输出和其他输入(比如说日志)对终端的竞争,导致输出混乱的现象,如果使用场景中没有出现终端输出混乱的情况,可以不使用shell锁 +注意: 请使用支持嵌套的锁 + 1. 使能宏并实现锁 使能`SHELL_USING_LOCK`宏,实现shell上锁和解锁函数,函数原型如下: @@ -560,6 +562,7 @@ letter shell 3.x提供了一个x86的demo,可以直接编译运行,其中包 ```sh mv src/shell_cfg.h src/shell_cfg.h.bak cd demo/x86-gcc/ +cmake . make -./build/out +./LetterShell ``` diff --git a/demo/x86-gcc/CmakeLists.txt b/demo/x86-gcc/CmakeLists.txt new file mode 100644 index 0000000..c92f295 --- /dev/null +++ b/demo/x86-gcc/CmakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.0.0) +project(LetterShell VERSION 0.1.0) + +add_executable(LetterShell + main.c + shell_port.c + shell_cpp.cpp + ../../src/shell.c + ../../src/shell_companion.c + ../../src/shell_ext.c + ../../extensions/fs_support/shell_fs.c + ../../extensions/log/log.c + ) + +target_include_directories(LetterShell PUBLIC + "${PROJECT_BINARY_DIR}" + ../../src + ../../extensions/fs_support + ../../extensions/cpp_support + ../../extensions/log + ) + +set(CPACK_PROJECT_NAME ${PROJECT_NAME}) +set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T shell.lds") diff --git a/demo/x86-gcc/makefile b/demo/x86-gcc/makefile deleted file mode 100644 index 3ddd046..0000000 --- a/demo/x86-gcc/makefile +++ /dev/null @@ -1,41 +0,0 @@ -all : mkdir out - -out : main.o shell_port.o shell.o shell_ext.o shell_cmd_list.o shell_companion.o shell_fs.o shell_cmd_group.o shell_cpp.o - gcc -o build/out -T shell.lds build/main.o build/shell_port.o \ - build/shell.o build/shell_ext.o build/shell_cmd_list.o \ - build/shell_companion.o build/shell_fs.o build/shell_cmd_group.o \ - build/shell_cpp.o - -main.o : main.c shell_port.h - gcc -c main.c -I ./ -I ../../src -o build/main.o - -shell_port.o : shell_port.c ../../src/shell.h ../../extensions/fs_support/shell_fs.h - gcc -c shell_port.c -I ./ -I ../../src -I ../../extensions/fs_support -o build/shell_port.o - -shell.o : ../../src/shell.c ../../src/shell.h shell_cfg.h - gcc -c ../../src/shell.c -I ./ -I ../../src -o build/shell.o - -shell_ext.o : ../../src/shell_ext.c ../../src/shell_ext.h ../../src/shell.h shell_cfg.h - gcc -c ../../src/shell_ext.c -I ./ -I ../../src -o build/shell_ext.o - -shell_cmd_list.o : ../../src/shell_cmd_list.c ../../src/shell.h shell_cfg.h - gcc -c ../../src/shell_cmd_list.c -I ./ -I ../../src -o build/shell_cmd_list.o - -shell_companion.o : ../../src/shell_companion.c ../../src/shell.h shell_cfg.h - gcc -c ../../src/shell_companion.c -I ./ -I ../../src -o build/shell_companion.o - -shell_fs.o : ../../extensions/fs_support/shell_fs.c ../../extensions/fs_support/shell_fs.h shell_cfg.h ../../src/shell.h - gcc -c ../../extensions/fs_support/shell_fs.c -I ./ -I ../../src -I ../../extensions/fs_support -o build/shell_fs.o - -shell_cmd_group.o : ../../extensions/shell_enhance/shell_cmd_group.c ../../extensions/shell_enhance/shell_cmd_group.h shell_cfg.h ../../src/shell.h - gcc -c ../../extensions/shell_enhance/shell_cmd_group.c -I ./ -I ../../src -I ../../extensions/shell_enhance -o build/shell_cmd_group.o - -shell_cpp.o : shell_cpp.cpp ../../extensions/cpp_support/shell_cpp.h - g++ -c shell_cpp.cpp -I ./ -I ../../src -I ../../extensions/cpp_support -o build/shell_cpp.o - -.PHONY : all clean mkdir -clean : - -rm -r build - -mkdir : - if [ ! -d "build" ]; then (mkdir build) fi \ No newline at end of file diff --git a/demo/x86-gcc/shell_cfg.h b/demo/x86-gcc/shell_cfg.h index 2d35ffb..974f5bf 100644 --- a/demo/x86-gcc/shell_cfg.h +++ b/demo/x86-gcc/shell_cfg.h @@ -126,6 +126,12 @@ */ #define SHELL_GET_TICK() 0 +/** + * @brief 使用锁 + * @note 使用shell锁时,需要对加锁和解锁进行实现 + */ +#define SHELL_USING_LOCK 0 + /** * @brief shell内存分配 * shell本身不需要此接口,若使用shell伴生对象,需要进行定义 diff --git a/demo/x86-gcc/shell_port.c b/demo/x86-gcc/shell_port.c index e2d4c69..d248422 100644 --- a/demo/x86-gcc/shell_port.c +++ b/demo/x86-gcc/shell_port.c @@ -11,6 +11,7 @@ #include "shell.h" #include "shell_fs.h" +#include "log.h" #include #include #include @@ -21,6 +22,25 @@ Shell shell; char shellBuffer[512]; ShellFs shellFs; char shellPathBuffer[512] = "/"; +Log log = { + .active = 1, + .level = LOG_DEBUG +}; + +/** + * @brief 日志写函数实现 + * + * @param buffer 数据 + * @param len 数据长度 + * + */ +void terminalLogWrite(char *buffer, short len) +{ + if (log.shell) + { + shellWriteEndLine(log.shell, buffer, len); + } +} /** * @brief 用户shell写 @@ -57,6 +77,22 @@ unsigned short userShellRead(char *data, unsigned short len) return len; } +#if SHELL_USING_LOCK == 1 +static int lockCount = 0; +int userShellLock(struct shell_def *shell) +{ + printf("lock: %d\r\n", lockCount); + return 0; +} + + +int userShellUnlock(struct shell_def *shell) +{ + printf("unlock: %d\r\n", lockCount); + return 0; +} +#endif + /** * @brief 列出文件 * @@ -94,9 +130,19 @@ void userShellInit(void) shell.write = userShellWrite; shell.read = userShellRead; +#if SHELL_USING_LOCK == 1 + shell.lock = userShellLock; + shell.unlock = userShellUnlock; +#endif shellSetPath(&shell, shellPathBuffer); shellInit(&shell, shellBuffer, 512); shellCompanionAdd(&shell, SHELL_COMPANION_ID_FS, &shellFs); + + log.write = terminalLogWrite; + logRegister(&log, &shell); + + logDebug("hello world"); + logHexDump(LOG_ALL_OBJ, LOG_DEBUG, (void *)&shell, sizeof(shell)); } diff --git a/extensions/log/log.c b/extensions/log/log.c index 51174da..9ee7364 100644 --- a/extensions/log/log.c +++ b/extensions/log/log.c @@ -126,25 +126,11 @@ void logWrite(Log *log, LogLevel level, char *fmt, ...) va_list vargs; short len; -#if SHELL_USING_LOCK == 1 - if (log->shell) - { - SHELL_LOCK(log->shell); - } -#endif /* SHELL_USING_LOCK == 1 */ - va_start(vargs, fmt); len = vsnprintf(logBuffer, LOG_BUFFER_SIZE - 1, fmt, vargs); va_end(vargs); logWriteBuffer(log, level, logBuffer, len); - -#if SHELL_USING_LOCK == 1 - if (log->shell) - { - SHELL_UNLOCK(log->shell); - } -#endif /* SHELL_USING_LOCK == 1 */ } @@ -161,35 +147,20 @@ void logHexDump(Log *log, LogLevel level, void *base, unsigned int length) unsigned char *address; unsigned int len; unsigned int printLen = 0; - unsigned char buf[12]; - if (length == 0 || log->level < level) + if (length == 0 || (log != LOG_ALL_OBJ && log->level < level)) { return; } -#if SHELL_USING_LOCK == 1 - if (log->shell) - { - SHELL_LOCK(log->shell); - } -#endif /* SHELL_USING_LOCK == 1 */ - - logWriteBuffer(log, LOG_NONE, "memory of 0x", sizeof("memory of 0x")); - len = vsnprintf(buf, 11, "08x", (unsigned int)base); - logWriteBuffer(log, LOG_NONE, buf, len); - logWriteBuffer(log, LOG_NONE, ", size: ", sizeof(", size: ")); - len = vsnprintf(buf, 11, "%d", length); - logWriteBuffer(log, LOG_NONE, buf, len); - logWriteBuffer(log, LOG_NONE, ":\r\n", sizeof(":\r\n")); - + len = snprintf(logBuffer, LOG_BUFFER_SIZE - 1, "memory of 0x%08x, size: %d:\r\n%s", + (unsigned int)base, length, memPrintHead); + logWriteBuffer(log, level, logBuffer, len); address = (unsigned char *)((unsigned int)base & (~0x0000000F)); length += (unsigned int)base - (unsigned int)address; length = (length + 15) & (~0x0000000F); - logWriteBuffer(log, LOG_NONE, memPrintHead, sizeof(memPrintHead)); - len = length; while (length) @@ -233,24 +204,17 @@ void logHexDump(Log *log, LogLevel level, void *base, unsigned int length) logBuffer[printLen ++] = '|'; logBuffer[printLen ++] = '\r'; logBuffer[printLen ++] = '\n'; - logWriteBuffer(log, LOG_NONE, logBuffer, printLen); + logWriteBuffer(log, level, logBuffer, printLen); address += 16; length -= 16; printLen = 0; } - -#if SHELL_USING_LOCK == 1 - if (log->shell) - { - SHELL_UNLOCK(log->shell); - } -#endif /* SHELL_USING_LOCK == 1 */ } #if SHELL_USING_COMPANION == 1 SHELL_EXPORT_CMD_AGENCY( SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_DISABLE_RETURN, hexdump, logHexDump, hex dump\r\n hexdump [base] [len], -(void *)shellCompanionGet(shellGetCurrent(), SHELL_COMPANION_ID_LOG), (void *)LOG_NONE, p1, p2); +(void *)shellCompanionGet(shellGetCurrent(), SHELL_COMPANION_ID_LOG), LOG_NONE, p1, p2); #else SHELL_EXPORT_CMD( SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_DISABLE_RETURN,