mirror of
https://github.com/NevermindZZT/letter-shell.git
synced 2025-01-01 09:58:41 +08:00
修复 log工具问题
优化 x86 demo使用cmake构建
This commit is contained in:
parent
80ec62eb4e
commit
4522c026bb
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
CMakeFiles
|
||||
cmake_install.cmake
|
||||
CMakeCache.txt
|
||||
demo/x86-gcc/Makefile
|
@ -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
|
||||
```
|
||||
|
25
demo/x86-gcc/CmakeLists.txt
Normal file
25
demo/x86-gcc/CmakeLists.txt
Normal file
@ -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")
|
@ -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
|
@ -126,6 +126,12 @@
|
||||
*/
|
||||
#define SHELL_GET_TICK() 0
|
||||
|
||||
/**
|
||||
* @brief 使用锁
|
||||
* @note 使用shell锁时,需要对加锁和解锁进行实现
|
||||
*/
|
||||
#define SHELL_USING_LOCK 0
|
||||
|
||||
/**
|
||||
* @brief shell内存分配
|
||||
* shell本身不需要此接口,若使用shell伴生对象,需要进行定义
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
#include "shell.h"
|
||||
#include "shell_fs.h"
|
||||
#include "log.h"
|
||||
#include <stdio.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
@ -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));
|
||||
}
|
||||
|
||||
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user