提交 392d3df9 编写于 作者: S Shengliang Guan

config

上级 18634533
......@@ -23,6 +23,8 @@
extern "C" {
#endif
#define CFG_NAME_MAX_LEN 128
typedef enum {
CFG_STYPE_DEFAULT,
CFG_STYPE_CFG_FILE,
......@@ -48,12 +50,12 @@ typedef enum {
typedef enum {
CFG_UTYPE_NONE,
CFG_UTYPE_PERCENT,
CFG_UTYPE_GB,
CFG_UTYPE_MB,
CFG_UTYPE_BYTE,
CFG_UTYPE_SECOND,
CFG_UTYPE_MS
CFG_UTYPE_MS,
CFG_UTYPE_PERCENT
} ECfgUnitType;
typedef struct SConfigItem {
......
......@@ -24,7 +24,7 @@ void taosRemoveDir(const char *dirname);
int32_t taosDirExist(char *dirname);
int32_t taosMkDir(const char *dirname);
void taosRemoveOldFiles(char *dirname, int32_t keepDays);
int32_t taosExpandDir(char *dirname, char *outname, int32_t maxlen);
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen);
int32_t taosRealPath(char *dirname, int32_t maxlen);
#ifdef __cplusplus
......
......@@ -55,7 +55,7 @@ void taosSetMaskSIGPIPE();
int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen);
int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t *optlen);
uint32_t taosInetAddr(char *ipAddr);
uint32_t taosInetAddr(const char *ipAddr);
const char *taosInetNtoa(struct in_addr ipInt);
#if (defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32))
......
......@@ -20,6 +20,7 @@
#include "config.h"
#include "taoserror.h"
#include "thash.h"
#include "tutil.h"
#include "ulog.h"
#ifdef __cplusplus
......
......@@ -64,8 +64,7 @@ SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashI
void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashCancelIterate(pConfig->hash, pIter); }
int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
bool tmp = false;
if (strcasecmp(value, "true") == 0) {
tmp = true;
......@@ -73,12 +72,13 @@ int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
if (atoi(value) > 0) {
tmp = true;
}
pItem->boolVal = tmp;
pItem->stype = stype;
return 0;
}
int32_t cfgSetInt8(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
static int32_t cfgSetInt8(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
int8_t ival = (int8_t)atoi(value);
if (ival < pItem->minIntVal || ival > pItem->maxIntVal) {
uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d",
......@@ -87,12 +87,13 @@ int32_t cfgSetInt8(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
pItem->int8Val = ival;
pItem->stype = stype;
return 0;
}
int32_t cfgSetUInt16(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
static int32_t cfgSetUInt16(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
uint16_t ival = (uint16_t)atoi(value);
if (ival < pItem->minIntVal || ival > pItem->maxIntVal) {
uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d",
......@@ -101,12 +102,13 @@ int32_t cfgSetUInt16(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
pItem->uint16Val = ival;
pItem->stype = stype;
return 0;
}
int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
static int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
int32_t ival = (int32_t)atoi(value);
if (ival < pItem->minIntVal || ival > pItem->maxIntVal) {
uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d",
......@@ -115,26 +117,29 @@ int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
pItem->int32Val = ival;
pItem->stype = stype;
return 0;
}
int32_t cfgSetInt64(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
static int32_t cfgSetInt64(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
int64_t ival = (int64_t)atoi(value);
if (ival < pItem->minIntVal || ival > pItem->maxIntVal) {
uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d",
uError("cfg:%s, type:%s src:%s value:%" PRId64 " out of range[%" PRId64 ", %" PRId64
"], use last src:%s value:%" PRId64,
pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
cfgStypeStr(pItem->stype), pItem->int64Val);
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
pItem->int64Val = ival;
pItem->stype = stype;
return 0;
}
int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
static int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
float fval = (float)atof(value);
if (fval < pItem->minFloatVal || fval > pItem->maxFloatVal) {
uError("cfg:%s, type:%s src:%s value:%f out of range[%f, %f], use last src:%s value:%f", pItem->name,
......@@ -143,47 +148,51 @@ int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
pItem->floatVal = fval;
pItem->stype = stype;
return 0;
}
int32_t cfgSetString(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
static int32_t cfgSetString(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
char *tmp = strdup(value);
if (tmp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s, use last src:%s value:%s", pItem->name,
cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), terrstr(), cfgStypeStr(pItem->stype), pItem->floatVal);
cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal);
return -1;
}
free(pItem->strVal);
pItem->strVal = tmp;
pItem->stype = stype;
return 0;
}
int32_t cfgSetIpStr(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
static int32_t cfgSetIpStr(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
char *tmp = strdup(value);
if (tmp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s, use last src:%s value:%s", pItem->name,
cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), terrstr(), cfgStypeStr(pItem->stype), pItem->floatVal);
cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal);
return -1;
}
free(pItem->strVal);
pItem->strVal = tmp;
pItem->stype = stype;
return 0;
}
int32_t cfgSetDir(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
static int32_t cfgSetDir(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
char *tmp = strdup(value);
if (tmp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s, use last src:%s value:%s", pItem->name,
cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), terrstr(), cfgStypeStr(pItem->stype), pItem->floatVal);
cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal);
return -1;
}
free(pItem->strVal);
pItem->strVal = tmp;
pItem->stype = stype;
......@@ -214,7 +223,7 @@ int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSr
case CFG_DTYPE_IPSTR:
return cfgSetIpStr(pItem, value, stype);
case CFG_DTYPE_DIR:
return cfgSetFqdn(pItem, value, stype);
return cfgSetDir(pItem, value, stype);
case CFG_DTYPE_NONE:
default:
break;
......@@ -225,8 +234,9 @@ int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSr
}
SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) {
char lowcaseName[128] = 0;
memcpy(lowcaseName, name, 127);
char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
memcpy(lowcaseName, name, CFG_NAME_MAX_LEN);
strntolower(lowcaseName, name, CFG_NAME_MAX_LEN);
SConfigItem *pItem = taosHashGet(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1);
if (pItem == NULL) {
......@@ -245,8 +255,10 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name
return -1;
}
char lowcaseName[128] = 0;
memcpy(lowcaseName, name, 127);
char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
memcpy(lowcaseName, name, CFG_NAME_MAX_LEN);
strntolower(lowcaseName, name, CFG_NAME_MAX_LEN);
if (taosHashPut(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) {
if (pItem->dtype == CFG_DTYPE_STRING) {
free(pItem->strVal);
......@@ -412,6 +424,10 @@ const char *cfgStypeStr(ECfgSrcType type) {
return "env_var";
case CFG_STYPE_APOLLO_URL:
return "apollo_url";
case CFG_STYPE_ARG_LIST:
return "arg_list";
case CFG_STYPE_API_OPTION:
return "api_option";
default:
return "invalid";
}
......
......@@ -34,12 +34,13 @@ TEST_F(CfgTest, 02_Str) {
EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_FILE), "env_file");
EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_VAR), "env_var");
EXPECT_STREQ(cfgStypeStr(CFG_STYPE_APOLLO_URL), "apollo_url");
EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ARG_LIST), "arg_list");
EXPECT_STREQ(cfgStypeStr(CFG_STYPE_API_OPTION), "api_option");
EXPECT_STREQ(cfgStypeStr(ECfgSrcType(1024)), "invalid");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_NONE), "none");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_BOOL), "bool");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT8), "int8");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_UINT16), "uint16");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT32), "int32");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT64), "int64");
......@@ -50,12 +51,12 @@ TEST_F(CfgTest, 02_Str) {
EXPECT_STREQ(cfgDtypeStr(ECfgDataType(1024)), "invalid");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_NONE), "");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_PERCENT), "(%)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_GB), "(GB)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_MB), "(Mb)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_BYTE), "(byte)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_SECOND), "(s)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_MS), "(ms)");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_PERCENT), "(%)");
EXPECT_STREQ(cfgUtypeStr(ECfgUnitType(1024)), "invalid");
}
......@@ -64,11 +65,11 @@ TEST_F(CfgTest, 02_Basic) {
ASSERT_NE(pConfig, nullptr);
EXPECT_EQ(cfgAddBool(pConfig, "test_bool", 0, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddInt8(pConfig, "test_int8", 1, CFG_UTYPE_GB), 0);
EXPECT_EQ(cfgAddUInt16(pConfig, "test_uint16", 2, CFG_UTYPE_SECOND), 0);
EXPECT_EQ(cfgAddInt32(pConfig, "test_int32", 3, CFG_UTYPE_MS), 0);
EXPECT_EQ(cfgAddInt64(pConfig, "test_int64", 4, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddFloat(pConfig, "test_float", 5, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddInt8(pConfig, "test_int8", 1, 0, 16, CFG_UTYPE_GB), 0);
EXPECT_EQ(cfgAddUInt16(pConfig, "test_uint16", 2, 0, 16, CFG_UTYPE_MB), 0);
EXPECT_EQ(cfgAddInt32(pConfig, "test_int32", 3, 0, 16, CFG_UTYPE_BYTE), 0);
EXPECT_EQ(cfgAddInt64(pConfig, "test_int64", 4, 0, 16, CFG_UTYPE_SECOND), 0);
EXPECT_EQ(cfgAddFloat(pConfig, "test_float", 5, 0, 16, CFG_UTYPE_MS), 0);
EXPECT_EQ(cfgAddString(pConfig, "test_string", "6", CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddIpStr(pConfig, "test_ipstr", "192.168.0.1", CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddDir(pConfig, "test_dir", "/tmp", CFG_UTYPE_NONE), 0);
......@@ -83,16 +84,16 @@ TEST_F(CfgTest, 02_Basic) {
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->boolVal);
break;
case CFG_DTYPE_INT8:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint8Val);
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int8Val);
break;
case CFG_DTYPE_UINT16:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int16Val);
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint16Val);
break;
case CFG_DTYPE_INT32:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint32Val);
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int32Val);
break;
case CFG_DTYPE_INT64:
printf("index:%d, cfg:%s value:%" PRIu64 "\n", size, pItem->name, pItem->uint64Val);
printf("index:%d, cfg:%s value:%" PRId64 "\n", size, pItem->name, pItem->int64Val);
break;
case CFG_DTYPE_FLOAT:
printf("index:%d, cfg:%s value:%f\n", size, pItem->name, pItem->floatVal);
......@@ -133,28 +134,28 @@ TEST_F(CfgTest, 02_Basic) {
pItem = cfgGetItem(pConfig, "test_uint16");
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_SECOND);
EXPECT_EQ(pItem->utype, CFG_UTYPE_MB);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_UINT16);
EXPECT_STREQ(pItem->name, "test_uint16");
EXPECT_EQ(pItem->uint16Val, 2);
pItem = cfgGetItem(pConfig, "test_int32");
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_MS);
EXPECT_EQ(pItem->utype, CFG_UTYPE_BYTE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT32);
EXPECT_STREQ(pItem->name, "test_int32");
EXPECT_EQ(pItem->int32Val, 3);
pItem = cfgGetItem(pConfig, "test_int64");
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->utype, CFG_UTYPE_SECOND);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT64);
EXPECT_STREQ(pItem->name, "test_int64");
EXPECT_EQ(pItem->int64Val, 4);
pItem = cfgGetItem(pConfig, "test_float");
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->utype, CFG_UTYPE_MS);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_FLOAT);
EXPECT_STREQ(pItem->name, "test_float");
EXPECT_EQ(pItem->floatVal, 5);
......
......@@ -114,7 +114,7 @@ void taosRemoveOldFiles(char *dirname, int32_t keepDays) {
rmdir(dirname);
}
int32_t taosExpandDir(char *dirname, char *outname, int32_t maxlen) {
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) {
wordexp_t full_path;
if (0 != wordexp(dirname, &full_path, 0)) {
//printf("failed to expand path:%s since %s", dirname, strerror(errno));
......
......@@ -150,7 +150,7 @@ int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *op
#if !((defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) && defined(_MSC_VER))
uint32_t taosInetAddr(char *ipAddr) { return inet_addr(ipAddr); }
uint32_t taosInetAddr(const char *ipAddr) { return inet_addr(ipAddr); }
const char *taosInetNtoa(struct in_addr ipInt) { return inet_ntoa(ipInt); }
......@@ -240,7 +240,7 @@ int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *op
#ifdef _MSC_VER
//#if _MSC_VER >= 1900
uint32_t taosInetAddr(char *ipAddr) {
uint32_t taosInetAddr(const char *ipAddr) {
uint32_t value;
int32_t ret = inet_pton(AF_INET, ipAddr, &value);
if (ret <= 0) {
......
......@@ -217,7 +217,7 @@ static void *taosThreadToOpenNewFile(void *param) {
uInfo(" new log file:%d is opened", tsLogObj.flag);
uInfo("==================================");
taosPrintCfg();
// taosPrintCfg();
taosKeepOldLog(keepName);
return NULL;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册