1
0
mirror of https://github.com/NevermindZZT/letter-shell.git synced 2025-01-21 10:02:54 +08:00

Merge pull request #113 from jinchunxing/master

新增 log组件适配shell锁
This commit is contained in:
Letter 2021-12-30 08:33:06 +08:00 committed by GitHub
commit ec8699e774
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 4 deletions

View File

@ -27,6 +27,58 @@
Log *logList[LOG_MAX_NUMBER] = {0};
static char logBuffer[LOG_BUFFER_SIZE];
#if LOG_USING_LOCK == 1
/**
* @brief log对象
* @param log log对象
*/
static void logLock(Log *log)
{
if (log == LOG_ALL_OBJ)
{
for (short i = 0; i < LOG_MAX_NUMBER; i++)
{
if (logList[i] && logList[i]->active)
{
if (logList[i]->lock)
{
LOG_LOCK(logList[i]);
}
}
}
}
else if (log->lock)
{
LOG_LOCK(log);
}
}
/**
* @brief log对象
* @param log log对象
*/
static void logUnlock(Log *log)
{
if (log == LOG_ALL_OBJ)
{
for (short i = 0; i < LOG_MAX_NUMBER; i++)
{
if (logList[i] && logList[i]->active)
{
if (logList[i]->unlock)
{
LOG_UNLOCK(logList[i]);
}
}
}
}
else if (log->unlock)
{
LOG_UNLOCK(log);
}
}
#endif /* LOG_USING_LOCK == 1 */
/**
* @brief log对象
*
@ -102,6 +154,9 @@ logSetLevel, logSetLevel, set log level\r\n logSetLevel [log] [level]);
*/
static void logWriteBuffer(Log *log, LogLevel level, char *buffer, short len)
{
#if LOG_USING_LOCK == 1
logLock(log);
#endif /* LOG_USING_LOCK == 1 */
if (log == LOG_ALL_OBJ)
{
for (short i = 0; i < LOG_MAX_NUMBER; i++)
@ -118,6 +173,9 @@ static void logWriteBuffer(Log *log, LogLevel level, char *buffer, short len)
{
log->write(logBuffer, len);
}
#if LOG_USING_LOCK == 1
logUnlock(log);
#endif /* LOG_USING_LOCK == 1 */
}
/**
@ -132,15 +190,20 @@ void logWrite(Log *log, LogLevel level, char *fmt, ...)
{
va_list vargs;
short len;
#if LOG_USING_LOCK == 1
logLock(log);
#endif /* LOG_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 LOG_USING_LOCK == 1
logUnlock(log);
#endif /* LOG_USING_LOCK == 1 */
}
/**
* @brief 16
*
@ -159,7 +222,9 @@ void logHexDump(Log *log, LogLevel level, void *base, unsigned int length)
{
return;
}
#if LOG_USING_LOCK == 1
logLock(log);
#endif /* LOG_USING_LOCK == 1 */
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);
@ -216,6 +281,9 @@ void logHexDump(Log *log, LogLevel level, void *base, unsigned int length)
length -= 16;
printLen = 0;
}
#if LOG_USING_LOCK == 1
logUnlock(log);
#endif /* LOG_USING_LOCK == 1 */
}
#if SHELL_USING_COMPANION == 1
SHELL_EXPORT_CMD_AGENCY(

View File

@ -17,6 +17,7 @@
#define SHELL_COMPANION_ID_LOG -2
#define LOG_USING_LOCK 0
#define LOG_BUFFER_SIZE 256 /**< log输出缓冲大小 */
#define LOG_USING_COLOR 1 /**< 是否使用颜色 */
#define LOG_MAX_NUMBER 5 /**< 允许注册的最大log对象数量 */
@ -29,6 +30,14 @@
#define LOG_ENABLE 1 /**< 使能log */
#endif
#if LOG_USING_LOCK == 1
#define LOG_LOCK(log) log->lock(log)
#define LOG_UNLOCK(log) log->unlock(log)
#else
#define LOG_LOCK(log)
#define LOG_UNLOCK(log)
#endif /* LOG_USING_LOCK == 1 */
#define LOG_ALL_OBJ ((Log *)-1) /**< 所有已注册的log对象 */
/**
@ -92,11 +101,15 @@ typedef enum
* @brief log对象定义
*
*/
typedef struct
typedef struct log_def
{
void (*write)(char *, short); /**< 写buffer */
char active; /**< 是否激活 */
LogLevel level; /**< 日志级别 */
#if LOG_USING_LOCK == 1
int (*lock)(struct log_def *); /**< log 加锁 */
int (*unlock)(struct log_def *); /**< log 解锁 */
#endif /** LOG_USING_LOCK == 1 */
Shell *shell; /**< 关联shell对象 */
} Log;