mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
Improve status code.
This commit is contained in:
parent
45a7b9e764
commit
df3ddf6cd5
|
@ -366,15 +366,51 @@ v_media_handle -->> -hal:return
|
|||
3. sd卡的日志动态,根据实际售后维护,可以是实时打印log/保存本地log/云log的多态实例库;
|
||||
4. 多态日志功能,可以忽略debug和release版本的区别,只发布一个版本即可;
|
||||
|
||||
##### 1.4.2.4.3. 返回码管理库
|
||||
|
||||
###### 1.4.2.4.3.1. 返回码管理库概述
|
||||
##### 1.4.2.4.3. 状态码管理库
|
||||
|
||||
   提供整个应用程序的返回码管理功能,例如:打印返回码的字符串含义。提供C语言接口,纯C语言开发的模块,形成项目内部唯一返回码标准。
|
||||
|
||||
###### 1.4.2.4.3.1. 状态码功能
|
||||
|
||||
1. 创建返回码操作“句柄”;
|
||||
2. 打印返回码/获取返回码(字符串);
|
||||
3. 不同的模块可继承实现各自的返回码处理接口;
|
||||
3. 不同模块可继承实现各自的返回码处理接口;
|
||||
4. 不同模块之间可透传状态码,避免错误码的转换麻烦;
|
||||
5. 状态码的定义是跟着模块走的,独立模块的状态码定义在模块内部,不链接时不占用程序空间;
|
||||
|
||||
###### 1.4.2.4.3.2. 基础状态码定义
|
||||
|
||||
   基础状态码是全局的基础状态码,枚举值全局唯一,其它独立模块必须继承基础状态码累加枚举值,但是,独立模块之间的枚举值可重复,状态码在使用时,日志只关注状态码的字符串,不关心状态码枚举值,代码逻辑使用枚举值。
|
||||
|
||||
```
|
||||
enum STATUS_CODE
|
||||
{
|
||||
STATUS_CODE_OK = 0,
|
||||
STATUS_CODE_NOT_OK,
|
||||
STATUS_CODE_VIRTUAL_FUNCTION,
|
||||
STATUS_CODE_INVALID_PARAMENTER,
|
||||
STATUS_CODE_END
|
||||
};
|
||||
```
|
||||
| 枚举名 | 字符串 | 描述 |
|
||||
| ------ | :------------------------------- | --------- |
|
||||
| STATUS_CODE_OK | STATUS_CODE_OK | 成功 |
|
||||
| STATUS_CODE_NOT_OK | STATUS_CODE_NOT_OK | 失败 |
|
||||
| STATUS_CODE_VIRTUAL_FUNCTION | STATUS_CODE_VIRTUAL_FUNCTION | 抽象接口,提示抽象接口未实例化 |
|
||||
| STATUS_CODE_INVALID_PARAMENTER | STATUS_CODE_INVALID_PARAMENTER | 无效的参数 |
|
||||
| STATUS_CODE_END | STATUS_CODE_END | 结束,无意义 |
|
||||
| 其它 | ? | 其它状态码在各自模块定义 |
|
||||
|
||||
###### 1.4.2.4.3.3. 已知漏洞
|
||||
|
||||
   状态码在代码层面是存在重复的可能性的,代码逻辑在使用状态码枚举值时,可能会出现逻辑错误。
|
||||
|
||||
* 解决方案
|
||||
在使用状态码进行逻辑判断时,使用状态码枚举值的字符串。
|
||||
Example:
|
||||
```
|
||||
static inline bool StatusCodeEqual(const StatusCode code, const char *value)
|
||||
```
|
||||
|
||||
##### 1.4.2.4.4. 系统标准接口库
|
||||
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
#ifndef I_HAL_H
|
||||
#define I_HAL_H
|
||||
#include "StatusCode.h"
|
||||
/*
|
||||
** Make sure we can call this stuff from C++.
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
|
|
|
@ -6,7 +6,7 @@ StatusCode create_hal_module(void)
|
|||
{
|
||||
IHal *hal = NULL;
|
||||
StatusCode code = HalMakePtr::GetInstance()->CreateHalPtr(&hal);
|
||||
if (STATUS_CODE_OK == code.mStatusCode)
|
||||
if (IsCodeOK(code))
|
||||
{
|
||||
LogInfo("Create Hal instance ok.\n");
|
||||
ResetHalImpl((IHal *)hal);
|
||||
|
@ -17,7 +17,7 @@ StatusCode create_hal_module(void)
|
|||
}
|
||||
auto instance = std::make_shared<IHalCpp>();
|
||||
StatusCode code2 = HalMakePtr::GetInstance()->CreateHalSharePtr(instance);
|
||||
if (STATUS_CODE_OK == code2.mStatusCode)
|
||||
if (IsCodeOK(code2))
|
||||
{
|
||||
LogInfo("IHal manager instance is ok.\n");
|
||||
IHalCpp::GetInstance(&instance);
|
||||
|
|
|
@ -10,7 +10,6 @@ namespace IHalTest
|
|||
{
|
||||
InitLog(LOG_EASYLOGGING, nullptr);
|
||||
create_hal_module();
|
||||
LogInfo("===============\n");
|
||||
IHalInit();
|
||||
IHalUnInit();
|
||||
destroy_hal_module();
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#ifndef STATUS_CODE_H
|
||||
#define STATUS_CODE_H
|
||||
#include <stdbool.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
|
@ -15,10 +16,19 @@ extern "C"
|
|||
typedef struct status_code StatusCode;
|
||||
typedef struct status_code
|
||||
{
|
||||
const char *(*printStringCode)(const StatusCode);
|
||||
const char *(*mPrintStringCode)(const StatusCode);
|
||||
const bool (*mCodeEqual)(const StatusCode, const char *);
|
||||
const long int mStatusCode;
|
||||
} StatusCode;
|
||||
const StatusCode CreateStatusCode(const long int code);
|
||||
static inline bool IsCodeOK(const StatusCode code)
|
||||
{
|
||||
return STATUS_CODE_OK == code.mStatusCode ? true : false;
|
||||
}
|
||||
static inline bool StatusCodeEqual(const StatusCode code, const char *value)
|
||||
{
|
||||
return code.mCodeEqual(code, value);
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
#include "StatusCode.h"
|
||||
#include "Log.h"
|
||||
#include <stdio.h>
|
||||
static const char *StatusCodeString[STATUS_CODE_END + 1]={
|
||||
#include <string.h>
|
||||
static const char *StatusCodeString[STATUS_CODE_END + 1] = {
|
||||
"STATUS_CODE_OK",
|
||||
"STATUS_CODE_NOT_OK",
|
||||
"STATUS_CODE_INVALID_PARAMENTER",
|
||||
"STATUS_CODE_END"
|
||||
};
|
||||
"STATUS_CODE_END"};
|
||||
static const char *PrintStringCode(const StatusCode this)
|
||||
{
|
||||
if (STATUS_CODE_OK <= this.mStatusCode && this.mStatusCode <= STATUS_CODE_END)
|
||||
|
@ -17,19 +17,31 @@ static const char *PrintStringCode(const StatusCode this)
|
|||
LogError("Status code = [ %s ]\n", StatusCodeString[STATUS_CODE_INVALID_PARAMENTER]);
|
||||
return StatusCodeString[STATUS_CODE_INVALID_PARAMENTER];
|
||||
}
|
||||
static bool CodeEqual(const StatusCode code, const char *value)
|
||||
{
|
||||
if (memcmp(value, StatusCodeString[code.mStatusCode], strlen(value)) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static StatusCode NewStatusCode(const long int code)
|
||||
{
|
||||
StatusCode result = {
|
||||
PrintStringCode,
|
||||
CodeEqual,
|
||||
code};
|
||||
return result;
|
||||
}
|
||||
const StatusCode CreateStatusCode(const long int code)
|
||||
{
|
||||
if (STATUS_CODE_OK <= code && code <= STATUS_CODE_END)
|
||||
{
|
||||
StatusCode result = {NULL, code};
|
||||
result.printStringCode = PrintStringCode;
|
||||
return result;
|
||||
return NewStatusCode(code);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogError("undefined code.\n");
|
||||
StatusCode result = {NULL, STATUS_CODE_INVALID_PARAMENTER};
|
||||
result.printStringCode = PrintStringCode;
|
||||
return result;
|
||||
return NewStatusCode(STATUS_CODE_END);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user