提交 18634533 编写于 作者: S Shengliang Guan

config

上级 1244bbe4
......@@ -24,32 +24,26 @@ extern "C" {
#endif
typedef enum {
CFG_TYPE_DEFAULT,
CFG_TYPE_CFG_FILE,
CFG_TYPE_ENV_FILE,
CFG_TYPE_ENV_VAR,
CFG_TYPE_APOLLO_URL,
CFG_TYPE_CONSOLE_PARA
CFG_STYPE_DEFAULT,
CFG_STYPE_CFG_FILE,
CFG_STYPE_ENV_FILE,
CFG_STYPE_ENV_VAR,
CFG_STYPE_APOLLO_URL,
CFG_STYPE_ARG_LIST,
CFG_STYPE_API_OPTION
} ECfgSrcType;
typedef enum {
CFG_DTYPE_NONE,
CFG_DTYPE_BOOL,
CFG_DTYPE_INT8,
CFG_DTYPE_UINT8,
CFG_DTYPE_INT16,
CFG_DTYPE_UINT16,
CFG_DTYPE_INT32,
CFG_DTYPE_UINT32,
CFG_DTYPE_INT64,
CFG_DTYPE_UINT64,
CFG_DTYPE_FLOAT,
CFG_DTYPE_DOUBLE,
CFG_DTYPE_STRING,
CFG_DTYPE_FQDN,
CFG_DTYPE_IPSTR,
CFG_DTYPE_DIR,
CFG_DTYPE_FILE
} ECfgDataType;
typedef enum {
......@@ -63,28 +57,30 @@ typedef enum {
} ECfgUnitType;
typedef struct SConfigItem {
ECfgSrcType stype;
ECfgSrcType stype;
ECfgUnitType utype;
ECfgDataType dtype;
char *name;
union {
bool boolVal;
uint8_t uint8Val;
int8_t int8Val;
uint16_t uint16Val;
int16_t int16Val;
uint32_t uint32Val;
int32_t int32Val;
uint64_t uint64Val;
int64_t int64Val;
float floatVal;
double doubleVal;
char *strVal;
char *fqdnVal;
char *ipstrVal;
char *dirVal;
char *fileVal;
};
union {
int64_t minIntVal;
double minFloatVal;
};
union {
int64_t maxIntVal;
double maxFloatVal;
};
} SConfigItem;
typedef struct SConfig SConfig;
......@@ -99,21 +95,19 @@ void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter);
SConfigItem *cfgGetItem(SConfig *pConfig, const char *name);
int32_t cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal, ECfgUnitType utype);
int32_t cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, ECfgUnitType utype);
int32_t cfgAddUInt8(SConfig *pConfig, const char *name, uint8_t defaultVal, ECfgUnitType utype);
int32_t cfgAddInt16(SConfig *pConfig, const char *name, int16_t defaultVal, ECfgUnitType utype);
int32_t cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, ECfgUnitType utype);
int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, ECfgUnitType utype);
int32_t cfgAddUInt32(SConfig *pConfig, const char *name, uint32_t defaultVal, ECfgUnitType utype);
int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, ECfgUnitType utype);
int32_t cfgAddUInt64(SConfig *pConfig, const char *name, uint64_t defaultVal, ECfgUnitType utype);
int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, ECfgUnitType utype);
int32_t cfgAddDouble(SConfig *pConfig, const char *name, double defaultVal, ECfgUnitType utype);
int32_t cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, int64_t minval, int64_t maxval,
ECfgUnitType utype);
int32_t cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, int64_t minval, int64_t maxval,
ECfgUnitType utype);
int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval,
ECfgUnitType utype);
int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval,
ECfgUnitType utype);
int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, double minval, double maxval,
ECfgUnitType utype);
int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
int32_t cfgAddFqdn(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
int32_t cfgAddFile(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
const char *cfgStypeStr(ECfgSrcType type);
const char *cfgDtypeStr(ECfgDataType type);
......
......@@ -71,6 +71,8 @@ int32_t* taosGetErrno();
#define TSDB_CODE_MSG_NOT_PROCESSED TAOS_DEF_ERROR_CODE(0, 0x0109)
#define TSDB_CODE_INVALID_PARA TAOS_DEF_ERROR_CODE(0, 0x010A)
#define TSDB_CODE_REPEAT_INIT TAOS_DEF_ERROR_CODE(0, 0x010B)
#define TSDB_CODE_CFG_NOT_FOUND TAOS_DEF_ERROR_CODE(0, 0x010C)
#define TSDB_CODE_INVALID_CFG TAOS_DEF_ERROR_CODE(0, 0x010D)
#define TSDB_CODE_REF_NO_MEMORY TAOS_DEF_ERROR_CODE(0, 0x0110)
#define TSDB_CODE_REF_FULL TAOS_DEF_ERROR_CODE(0, 0x0111)
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_UTIL_CONFIG_H
#define _TD_UTIL_CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
#define TSDB_CFG_MAX_NUM 115
#define TSDB_CFG_PRINT_LEN 23
#define TSDB_CFG_OPTION_LEN 24
#define TSDB_CFG_VALUE_LEN 41
#define TSDB_CFG_CTYPE_B_CONFIG 1U // can be configured from file
#define TSDB_CFG_CTYPE_B_SHOW 2U // can displayed by "show configs" commands
#define TSDB_CFG_CTYPE_B_LOG 4U // is a log type configuration
#define TSDB_CFG_CTYPE_B_CLIENT 8U // can be displayed in the client log
#define TSDB_CFG_CTYPE_B_OPTION 16U // can be configured by taos_options function
#define TSDB_CFG_CTYPE_B_NOT_PRINT 32U // such as password
#define MAX_FLOAT 100000
#define MIN_FLOAT 0
enum {
TAOS_CFG_CSTATUS_NONE, // not configured
TAOS_CFG_CSTATUS_DEFAULT, // use system default value
TAOS_CFG_CSTATUS_FILE, // configured from file
TAOS_CFG_CSTATUS_OPTION, // configured by taos_options function
TAOS_CFG_CSTATUS_ARG, // configured by program argument
};
enum {
TAOS_CFG_VTYPE_INT8,
TAOS_CFG_VTYPE_INT16,
TAOS_CFG_VTYPE_INT32,
TAOS_CFG_VTYPE_UINT16,
TAOS_CFG_VTYPE_FLOAT,
TAOS_CFG_VTYPE_STRING,
TAOS_CFG_VTYPE_IPSTR,
TAOS_CFG_VTYPE_DIRECTORY,
TAOS_CFG_VTYPE_DATA_DIRCTORY,
TAOS_CFG_VTYPE_DOUBLE,
};
enum {
TAOS_CFG_UTYPE_NONE,
TAOS_CFG_UTYPE_PERCENT,
TAOS_CFG_UTYPE_GB,
TAOS_CFG_UTYPE_MB,
TAOS_CFG_UTYPE_BYTE,
TAOS_CFG_UTYPE_SECOND,
TAOS_CFG_UTYPE_MS
};
typedef struct {
char * option;
void * ptr;
float minValue;
float maxValue;
int8_t cfgType;
int8_t cfgStatus;
int8_t unitType;
int8_t valType;
int32_t ptrLength;
} SGlobalCfg;
extern SGlobalCfg tsGlobalConfig[];
extern int32_t tsGlobalConfigNum;
extern char * tsCfgStatusStr[];
void taosReadGlobalLogCfg();
int32_t taosReadCfgFromFile();
void taosPrintCfg();
void taosDumpGlobalCfg();
void taosAddConfigOption(SGlobalCfg cfg);
SGlobalCfg *taosGetConfigOption(const char *option);
#ifdef __cplusplus
}
#endif
#endif /*_TD_UTIL_CONFIG_H*/
......@@ -21,7 +21,6 @@
#include "scheduler.h"
#include "tmsg.h"
#include "tcache.h"
#include "tconfig.h"
#include "tglobal.h"
#include "tnote.h"
#include "tref.h"
......
......@@ -19,7 +19,6 @@
#include "taosdef.h"
#include "taoserror.h"
#include "tcompare.h"
#include "tconfig.h"
#include "tep.h"
#include "tglobal.h"
#include "tlocale.h"
......@@ -305,7 +304,7 @@ static void taosCheckDataDirCfg() {
static void doInitGlobalConfig(void) {
osInit();
srand(taosSafeRand());
#if 0
SGlobalCfg cfg = {0};
// ip address
......@@ -1025,7 +1024,7 @@ static void doInitGlobalConfig(void) {
cfg.valType = TAOS_CFG_VTYPE_DOUBLE;
cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG;
cfg.minValue = MIN_FLOAT;
cfg.maxValue = MAX_FLOAT;
cfg.maxValue = 100000;
cfg.ptrLength = 0;
cfg.unitType = TAOS_CFG_UTYPE_NONE;
......@@ -1035,8 +1034,8 @@ static void doInitGlobalConfig(void) {
cfg.ptr = &dPrecision;
cfg.valType = TAOS_CFG_VTYPE_DOUBLE;
cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG;
cfg.minValue = MIN_FLOAT;
cfg.maxValue = MAX_FLOAT;
cfg.minValue = 100000;
cfg.maxValue = 0;
cfg.ptrLength = 0;
cfg.unitType = TAOS_CFG_UTYPE_NONE;
taosAddConfigOption(cfg);
......@@ -1064,11 +1063,15 @@ static void doInitGlobalConfig(void) {
#else
// assert(tsGlobalConfigNum == TSDB_CFG_MAX_NUM - 5);
#endif
#endif
}
void taosInitGlobalCfg() { pthread_once(&tsInitGlobalCfgOnce, doInitGlobalConfig); }
int32_t taosCheckAndPrintCfg() {
#if 0
SEp ep = {0};
if (debugFlag & DEBUG_TRACE || debugFlag & DEBUG_DEBUG || debugFlag & DEBUG_DUMP) {
taosSetAllDebugFlag();
......@@ -1121,7 +1124,7 @@ int32_t taosCheckAndPrintCfg() {
uInfo(" check global cfg completed");
uInfo("==================================");
taosPrintCfg();
#endif
return 0;
}
......
......@@ -17,7 +17,6 @@
#include "os.h"
#include "ulog.h"
#include "tglobal.h"
#include "tconfig.h"
#include "tutil.h"
/**
......
......@@ -17,7 +17,6 @@
#include "os.h"
#include "ulog.h"
#include "tglobal.h"
#include "tconfig.h"
#include "tutil.h"
// TODO refactor to set the tz value through parameter
......
......@@ -19,7 +19,6 @@
#include "config.h"
#include "dnode.h"
#include "tconfig.h"
#include "tglobal.h"
#include "tnote.h"
#include "ulog.h"
......
......@@ -42,6 +42,176 @@ static void dmnInitObjCfg(SDnodeObjCfg *pCfg) {
tstrncpy(pCfg->firstEp, tsFirst, TSDB_EP_LEN);
}
#if 0
void taosReadGlobalLogCfg() {
FILE * fp;
char * line, *option, *value;
int olen, vlen;
char fileName[PATH_MAX] = {0};
taosExpandDir(configDir, configDir, PATH_MAX);
taosReadLogOption("logDir", tsLogDir);
sprintf(fileName, "%s/taos.cfg", configDir);
fp = fopen(fileName, "r");
if (fp == NULL) {
printf("\nconfig file:%s not found, all variables are set to default\n", fileName);
return;
}
ssize_t _bytes = 0;
size_t len = 1024;
line = calloc(1, len);
while (!feof(fp)) {
memset(line, 0, len);
option = value = NULL;
olen = vlen = 0;
_bytes = tgetline(&line, &len, fp);
if (_bytes < 0)
{
break;
}
line[len - 1] = 0;
paGetToken(line, &option, &olen);
if (olen == 0) continue;
option[olen] = 0;
paGetToken(option + olen + 1, &value, &vlen);
if (vlen == 0) continue;
value[vlen] = 0;
taosReadLogOption(option, value);
}
tfree(line);
fclose(fp);
}
void taosPrintCfg() {
uInfo(" taos config & system info:");
uInfo("==================================");
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (tscEmbeddedInUtil == 0 && !(cfg->cfgType & TSDB_CFG_CTYPE_B_CLIENT)) continue;
if (cfg->cfgType & TSDB_CFG_CTYPE_B_NOT_PRINT) continue;
int optionLen = (int)strlen(cfg->option);
int blankLen = TSDB_CFG_PRINT_LEN - optionLen;
blankLen = blankLen < 0 ? 0 : blankLen;
char blank[TSDB_CFG_PRINT_LEN];
memset(blank, ' ', TSDB_CFG_PRINT_LEN);
blank[blankLen] = 0;
switch (cfg->valType) {
case TAOS_CFG_VTYPE_INT8:
uInfo(" %s:%s%d%s", cfg->option, blank, *((int8_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_INT16:
uInfo(" %s:%s%d%s", cfg->option, blank, *((int16_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_INT32:
uInfo(" %s:%s%d%s", cfg->option, blank, *((int32_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_UINT16:
uInfo(" %s:%s%d%s", cfg->option, blank, *((uint16_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_FLOAT:
uInfo(" %s:%s%f%s", cfg->option, blank, *((float *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_DOUBLE:
uInfo(" %s:%s%f%s", cfg->option, blank, *((double *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_STRING:
case TAOS_CFG_VTYPE_IPSTR:
case TAOS_CFG_VTYPE_DIRECTORY:
uInfo(" %s:%s%s%s", cfg->option, blank, (char *)cfg->ptr, tsGlobalUnit[cfg->unitType]);
break;
default:
break;
}
}
taosPrintOsInfo();
uInfo("==================================");
}
#if 0
static void taosDumpCfg(SGlobalCfg *cfg) {
int optionLen = (int)strlen(cfg->option);
int blankLen = TSDB_CFG_PRINT_LEN - optionLen;
blankLen = blankLen < 0 ? 0 : blankLen;
char blank[TSDB_CFG_PRINT_LEN];
memset(blank, ' ', TSDB_CFG_PRINT_LEN);
blank[blankLen] = 0;
switch (cfg->valType) {
case TAOS_CFG_VTYPE_INT8:
printf(" %s:%s%d%s\n", cfg->option, blank, *((int8_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_INT16:
printf(" %s:%s%d%s\n", cfg->option, blank, *((int16_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_INT32:
printf(" %s:%s%d%s\n", cfg->option, blank, *((int32_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_UINT16:
printf(" %s:%s%d%s\n", cfg->option, blank, *((uint16_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_FLOAT:
printf(" %s:%s%f%s\n", cfg->option, blank, *((float *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_STRING:
case TAOS_CFG_VTYPE_IPSTR:
case TAOS_CFG_VTYPE_DIRECTORY:
printf(" %s:%s%s%s\n", cfg->option, blank, (char *)cfg->ptr, tsGlobalUnit[cfg->unitType]);
break;
default:
break;
}
}
void taosDumpGlobalCfg() {
printf("taos global config:\n");
printf("==================================\n");
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (tscEmbeddedInUtil == 0 && !(cfg->cfgType & TSDB_CFG_CTYPE_B_CLIENT)) continue;
if (cfg->cfgType & TSDB_CFG_CTYPE_B_NOT_PRINT) continue;
if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW)) continue;
taosDumpCfg(cfg);
}
printf("\ntaos local config:\n");
printf("==================================\n");
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (tscEmbeddedInUtil == 0 && !(cfg->cfgType & TSDB_CFG_CTYPE_B_CLIENT)) continue;
if (cfg->cfgType & TSDB_CFG_CTYPE_B_NOT_PRINT) continue;
if (cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW) continue;
taosDumpCfg(cfg);
}
}
#endif
#endif
static int32_t dmnInitLog() {
}
int32_t dnmInitCfg(SDnodeEnvCfg *pEnvCfg, SDnodeObjCfg *pObjCfg, const char *configFile, const char *envFile,
const char *apolloUrl) {
dmnInitEnvCfg(pEnvCfg);
......
......@@ -21,7 +21,6 @@
#include "dnode.h"
#include "tmsg.h"
#include "tconfig.h"
#include "tdataformat.h"
#include "tglobal.h"
#include "tnote.h"
......
......@@ -27,8 +27,8 @@ extern "C" {
#endif
typedef struct SConfig {
ECfgSrcType stype;
SHashObj *hash;
ECfgSrcType stype;
SHashObj *hash;
} SConfig;
int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath);
......@@ -36,6 +36,8 @@ int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath);
int32_t cfgLoadFromEnvVar(SConfig *pConfig);
int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url);
int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSrcType stype);
#ifdef __cplusplus
}
#endif
......
......@@ -17,6 +17,54 @@
#include "cfgInt.h"
int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) {
uInfo("load from .cfg file %s", filepath);
char *line, *name, *value, *value2, *value3;
int olen, vlen, vlen2, vlen3;
ssize_t _bytes = 0;
size_t len = 1024;
FILE *fp = fopen(filepath, "r");
if (fp == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
line = malloc(len);
while (!feof(fp)) {
memset(line, 0, len);
name = value = value2 = value3 = NULL;
olen = vlen = vlen2 = vlen3 = 0;
_bytes = tgetline(&line, &len, fp);
if (_bytes < 0) {
break;
}
line[len - 1] = 0;
paGetToken(line, &name, &olen);
if (olen == 0) continue;
name[olen] = 0;
paGetToken(name + olen + 1, &value, &vlen);
if (vlen == 0) continue;
value[vlen] = 0;
paGetToken(value + vlen + 1, &value2, &vlen2);
if (vlen2 != 0) {
value2[vlen2] = 0;
paGetToken(value2 + vlen2 + 1, &value3, &vlen3);
if (vlen3 != 0) value3[vlen3] = 0;
}
cfgSetItem(pConfig, name, value, CFG_STYPE_CFG_FILE);
// taosReadConfigOption(name, value, value2, value3);
}
fclose(fp);
tfree(line);
uInfo("load from cfg file %s success", filepath);
return 0;
}
\ No newline at end of file
......@@ -35,13 +35,13 @@ SConfig *cfgInit() {
int32_t cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr) {
switch (cfgType) {
case CFG_TYPE_CFG_FILE:
case CFG_STYPE_CFG_FILE:
return cfgLoadFromCfgFile(pConfig, sourceStr);
case CFG_TYPE_ENV_FILE:
case CFG_STYPE_ENV_FILE:
return cfgLoadFromEnvFile(pConfig, sourceStr);
case CFG_TYPE_ENV_VAR:
case CFG_STYPE_ENV_VAR:
return cfgLoadFromEnvVar(pConfig);
case CFG_TYPE_APOLLO_URL:
case CFG_STYPE_APOLLO_URL:
return cfgLoadFromApollUrl(pConfig, sourceStr);
default:
return -1;
......@@ -64,10 +64,180 @@ SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashI
void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashCancelIterate(pConfig->hash, pIter); }
SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) { taosHashGet(pConfig->hash, name, strlen(name) + 1); }
int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
bool tmp = false;
if (strcasecmp(value, "true") == 0) {
tmp = true;
}
if (atoi(value) > 0) {
tmp = true;
}
pItem->boolVal = tmp;
pItem->stype = stype;
return 0;
}
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",
pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
cfgStypeStr(pItem->stype), pItem->int8Val);
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) {
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",
pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
cfgStypeStr(pItem->stype), pItem->uint16Val);
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) {
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",
pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
cfgStypeStr(pItem->stype), pItem->int32Val);
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) {
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",
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) {
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,
cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), fval, pItem->minFloatVal, pItem->maxFloatVal,
cfgStypeStr(pItem->stype), pItem->floatVal);
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) {
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);
return -1;
}
free(pItem->strVal);
pItem->strVal = tmp;
pItem->stype = stype;
return 0;
}
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);
return -1;
}
free(pItem->strVal);
pItem->strVal = tmp;
pItem->stype = stype;
return 0;
}
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);
return -1;
}
free(pItem->strVal);
pItem->strVal = tmp;
pItem->stype = stype;
return 0;
}
int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSrcType stype) {
SConfigItem *pItem = cfgGetItem(pConfig, name);
if (pItem == NULL) {
return -1;
}
switch (pItem->dtype) {
case CFG_DTYPE_BOOL:
return cfgSetBool(pItem, value, stype);
case CFG_DTYPE_INT8:
return cfgSetInt8(pItem, value, stype);
case CFG_DTYPE_UINT16:
return cfgSetUInt16(pItem, value, stype);
case CFG_DTYPE_INT32:
return cfgSetInt32(pItem, value, stype);
case CFG_DTYPE_INT64:
return cfgSetInt64(pItem, value, stype);
case CFG_DTYPE_FLOAT:
return cfgSetFloat(pItem, value, stype);
case CFG_DTYPE_STRING:
return cfgSetString(pItem, value, stype);
case CFG_DTYPE_IPSTR:
return cfgSetIpStr(pItem, value, stype);
case CFG_DTYPE_DIR:
return cfgSetFqdn(pItem, value, stype);
case CFG_DTYPE_NONE:
default:
break;
}
terrno = TSDB_CODE_INVALID_CFG;
return -1;
}
SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) {
char lowcaseName[128] = 0;
memcpy(lowcaseName, name, 127);
SConfigItem *pItem = taosHashGet(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1);
if (pItem == NULL) {
terrno = TSDB_CODE_CFG_NOT_FOUND;
}
return pItem;
}
static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name, ECfgUnitType utype) {
pItem->stype = CFG_TYPE_DEFAULT;
pItem->stype = CFG_STYPE_DEFAULT;
pItem->utype = utype;
pItem->name = strdup(name);
if (pItem->name == NULL) {
......@@ -75,18 +245,15 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name
return -1;
}
if (taosHashPut(pConfig->hash, name, strlen(name) + 1, pItem, sizeof(SConfigItem)) != 0) {
char lowcaseName[128] = 0;
memcpy(lowcaseName, name, 127);
if (taosHashPut(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) {
if (pItem->dtype == CFG_DTYPE_STRING) {
free(pItem->strVal);
} else if (pItem->dtype == CFG_DTYPE_FQDN) {
free(pItem->fqdnVal);
} else if (pItem->dtype == CFG_DTYPE_IPSTR) {
free(pItem->ipstrVal);
} else if (pItem->dtype == CFG_DTYPE_DIR) {
free(pItem->dirVal);
} else if (pItem->dtype == CFG_DTYPE_FILE) {
free(pItem->fileVal);
} else {
}
free(pItem->name);
terrno = TSDB_CODE_OUT_OF_MEMORY;
......@@ -101,57 +268,67 @@ int32_t cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal, ECfgUnit
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_INT8, .int8Val = defaultVal};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, int64_t minval, int64_t maxval,
ECfgUnitType utype) {
if (defaultVal < minval || defaultVal > maxval) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
int32_t cfgAddUInt8(SConfig *pConfig, const char *name, uint8_t defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_UINT8, .uint8Val = defaultVal};
SConfigItem item = {.dtype = CFG_DTYPE_INT8, .int8Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddInt16(SConfig *pConfig, const char *name, int16_t defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_INT16, .int16Val = defaultVal};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, int64_t minval, int64_t maxval,
ECfgUnitType utype) {
if (defaultVal < minval || defaultVal > maxval) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
int32_t cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_UINT16, .uint16Val = defaultVal};
SConfigItem item = {.dtype = CFG_DTYPE_UINT16, .uint16Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_INT32, .int32Val = defaultVal};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval,
ECfgUnitType utype) {
if (defaultVal < minval || defaultVal > maxval) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
int32_t cfgAddUInt32(SConfig *pConfig, const char *name, uint32_t defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_UINT32, .uint32Val = defaultVal};
SConfigItem item = {.dtype = CFG_DTYPE_INT32, .int32Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_INT64, .int64Val = defaultVal};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval,
ECfgUnitType utype) {
if (defaultVal < minval || defaultVal > maxval) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
int32_t cfgAddUInt64(SConfig *pConfig, const char *name, uint64_t defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_UINT64, .uint64Val = defaultVal};
SConfigItem item = {.dtype = CFG_DTYPE_INT64, .int64Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, .floatVal = defaultVal};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, double minval, double maxval,
ECfgUnitType utype) {
if (defaultVal < minval || defaultVal > maxval) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
int32_t cfgAddDouble(SConfig *pConfig, const char *name, double defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_DOUBLE, .doubleVal = defaultVal};
SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, .floatVal = defaultVal, .minFloatVal = minval, .maxFloatVal = maxval};
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
if (defaultVal == NULL) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
SConfigItem item = {.dtype = CFG_DTYPE_STRING};
item.strVal = strdup(defaultVal);
if (item.strVal == NULL) {
......@@ -161,17 +338,22 @@ int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal,
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddFqdn(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_FQDN};
item.fqdnVal = strdup(defaultVal);
if (item.fqdnVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
static int32_t cfgCheckIpStr(const char *ip) {
uint32_t value = taosInetAddr(ip);
if (value == INADDR_NONE) {
uError("ip:%s is not a valid ip address", ip);
return -1;
}
return cfgAddItem(pConfig, &item, name, utype);
return 0;
}
int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
if (cfgCheckIpStr(defaultVal) != 0) {
terrno = TSDB_CODE_OUT_OF_RANGE;
return -1;
}
SConfigItem item = {.dtype = CFG_DTYPE_IPSTR};
item.ipstrVal = strdup(defaultVal);
if (item.ipstrVal == NULL) {
......@@ -181,21 +363,38 @@ int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal,
return cfgAddItem(pConfig, &item, name, utype);
}
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_DIR};
item.dirVal = strdup(defaultVal);
if (item.dirVal == NULL) {
static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) {
char fullDir[PATH_MAX] = {0};
if (taosExpandDir(inputDir, fullDir, PATH_MAX) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
uError("failed to expand dir:%s since %s", inputDir, terrstr());
return -1;
}
if (taosRealPath(fullDir, PATH_MAX) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
uError("failed to get realpath of dir:%s since %s", inputDir, terrstr());
return -1;
}
if (taosMkDir(fullDir) != 0) {
uError("failed to create dir:%s realpath:%s since %s", inputDir, fullDir, terrstr());
return -1;
}
tfree(pItem->dirVal);
pItem->dirVal = strdup(fullDir);
if (pItem->dirVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
return cfgAddItem(pConfig, &item, name, utype);
return 0;
}
int32_t cfgAddFile(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_FILE};
item.fileVal = strdup(defaultVal);
if (item.fileVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
SConfigItem item = {.dtype = CFG_DTYPE_DIR};
if (cfgCheckAndSetDir(&item, defaultVal) != 0) {
return -1;
}
return cfgAddItem(pConfig, &item, name, utype);
......@@ -203,15 +402,15 @@ int32_t cfgAddFile(SConfig *pConfig, const char *name, const char *defaultVal, E
const char *cfgStypeStr(ECfgSrcType type) {
switch (type) {
case CFG_TYPE_DEFAULT:
case CFG_STYPE_DEFAULT:
return "default";
case CFG_TYPE_CFG_FILE:
case CFG_STYPE_CFG_FILE:
return "cfg_file";
case CFG_TYPE_ENV_FILE:
case CFG_STYPE_ENV_FILE:
return "env_file";
case CFG_TYPE_ENV_VAR:
case CFG_STYPE_ENV_VAR:
return "env_var";
case CFG_TYPE_APOLLO_URL:
case CFG_STYPE_APOLLO_URL:
return "apollo_url";
default:
return "invalid";
......@@ -226,34 +425,20 @@ const char *cfgDtypeStr(ECfgDataType type) {
return "bool";
case CFG_DTYPE_INT8:
return "int8";
case CFG_DTYPE_UINT8:
return "uint8";
case CFG_DTYPE_INT16:
return "int16";
case CFG_DTYPE_UINT16:
return "uint16";
case CFG_DTYPE_INT32:
return "int32";
case CFG_DTYPE_UINT32:
return "uint32";
case CFG_DTYPE_INT64:
return "int64";
case CFG_DTYPE_UINT64:
return "uint64";
case CFG_DTYPE_FLOAT:
return "float";
case CFG_DTYPE_DOUBLE:
return "double";
case CFG_DTYPE_STRING:
return "string";
case CFG_DTYPE_FQDN:
return "fqdn";
case CFG_DTYPE_IPSTR:
return "ipstr";
case CFG_DTYPE_DIR:
return "dir";
case CFG_DTYPE_FILE:
return "file";
default:
return "invalid";
}
......
......@@ -29,30 +29,24 @@ class CfgTest : public ::testing::Test {
const char *CfgTest::pConfig;
TEST_F(CfgTest, 02_Str) {
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_DEFAULT), "default");
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_CFG_FILE), "cfg_file");
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_ENV_FILE), "env_file");
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_ENV_VAR), "env_var");
EXPECT_STREQ(cfgStypeStr(CFG_TYPE_APOLLO_URL), "apollo_url");
EXPECT_STREQ(cfgStypeStr(CFG_STYPE_DEFAULT), "default");
EXPECT_STREQ(cfgStypeStr(CFG_STYPE_CFG_FILE), "cfg_file");
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(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_UINT8), "uint8");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT16), "int16");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_UINT16), "uint16");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT32), "int32");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_UINT32), "uint32");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT64), "int64");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_UINT64), "uint64");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_FLOAT), "float");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DOUBLE), "double");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_STRING), "string");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_FQDN), "fqdn");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_IPSTR), "ipstr");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_DIR), "dir");
EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_FILE), "file");
EXPECT_STREQ(cfgDtypeStr(ECfgDataType(1024)), "invalid");
EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_NONE), "");
......@@ -71,22 +65,15 @@ TEST_F(CfgTest, 02_Basic) {
EXPECT_EQ(cfgAddBool(pConfig, "test_bool", 0, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddInt8(pConfig, "test_int8", 1, CFG_UTYPE_GB), 0);
EXPECT_EQ(cfgAddUInt8(pConfig, "test_uint8", 2, CFG_UTYPE_MB), 0);
EXPECT_EQ(cfgAddInt16(pConfig, "test_int16", 3, CFG_UTYPE_BYTE), 0);
EXPECT_EQ(cfgAddUInt16(pConfig, "test_uint16", 4, CFG_UTYPE_SECOND), 0);
EXPECT_EQ(cfgAddInt32(pConfig, "test_int32", 5, CFG_UTYPE_MS), 0);
EXPECT_EQ(cfgAddUInt32(pConfig, "test_uint32", 6, CFG_UTYPE_PERCENT), 0);
EXPECT_EQ(cfgAddInt64(pConfig, "test_int64", 7, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddUInt64(pConfig, "test_uint64", 8, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddFloat(pConfig, "test_float", 9, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddDouble(pConfig, "test_double", 10, CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddString(pConfig, "test_string", "11", CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgAddFqdn(pConfig, "test_fqdn", "localhost", CFG_UTYPE_NONE), 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(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);
EXPECT_EQ(cfgAddFile(pConfig, "test_file", "/tmp/file1", CFG_UTYPE_NONE), 0);
EXPECT_EQ(cfgGetSize(pConfig), 16);
EXPECT_EQ(cfgGetSize(pConfig), 9);
int32_t size = 0;
SConfigItem *pItem = cfgIterate(pConfig, NULL);
......@@ -98,48 +85,27 @@ TEST_F(CfgTest, 02_Basic) {
case CFG_DTYPE_INT8:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint8Val);
break;
case CFG_DTYPE_UINT8:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int8Val);
break;
case CFG_DTYPE_INT16:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint16Val);
break;
case CFG_DTYPE_UINT16:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int16Val);
break;
case CFG_DTYPE_INT32:
printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint32Val);
break;
case CFG_DTYPE_UINT32:
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);
break;
case CFG_DTYPE_UINT64:
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);
break;
case CFG_DTYPE_DOUBLE:
printf("index:%d, cfg:%s value:%f\n", size, pItem->name, pItem->doubleVal);
break;
case CFG_DTYPE_STRING:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->strVal);
break;
case CFG_DTYPE_FQDN:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->fqdnVal);
break;
case CFG_DTYPE_IPSTR:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->ipstrVal);
break;
case CFG_DTYPE_DIR:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->dirVal);
break;
case CFG_DTYPE_FILE:
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->fileVal);
break;
default:
printf("index:%d, cfg:%s invalid cfg dtype:%d\n", size, pItem->name, pItem->dtype);
break;
......@@ -149,119 +115,70 @@ TEST_F(CfgTest, 02_Basic) {
}
cfgCancelIterate(pConfig, pItem);
EXPECT_EQ(cfgGetSize(pConfig), 16);
EXPECT_EQ(cfgGetSize(pConfig), 9);
pItem = cfgGetItem(pConfig, "test_bool");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_BOOL);
EXPECT_STREQ(pItem->name, "test_bool");
EXPECT_EQ(pItem->boolVal, 0);
pItem = cfgGetItem(pConfig, "test_int8");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_GB);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT8);
EXPECT_STREQ(pItem->name, "test_int8");
EXPECT_EQ(pItem->int8Val, 1);
pItem = cfgGetItem(pConfig, "test_uint8");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_MB);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_UINT8);
EXPECT_STREQ(pItem->name, "test_uint8");
EXPECT_EQ(pItem->uint8Val, 2);
pItem = cfgGetItem(pConfig, "test_int16");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_BYTE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT16);
EXPECT_STREQ(pItem->name, "test_int16");
EXPECT_EQ(pItem->int16Val, 3);
pItem = cfgGetItem(pConfig, "test_uint16");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_SECOND);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_UINT16);
EXPECT_STREQ(pItem->name, "test_uint16");
EXPECT_EQ(pItem->uint16Val, 4);
EXPECT_EQ(pItem->uint16Val, 2);
pItem = cfgGetItem(pConfig, "test_int32");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_MS);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT32);
EXPECT_STREQ(pItem->name, "test_int32");
EXPECT_EQ(pItem->int32Val, 5);
pItem = cfgGetItem(pConfig, "test_uint32");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_PERCENT);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_UINT32);
EXPECT_STREQ(pItem->name, "test_uint32");
EXPECT_EQ(pItem->uint32Val, 6);
EXPECT_EQ(pItem->int32Val, 3);
pItem = cfgGetItem(pConfig, "test_int64");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT64);
EXPECT_STREQ(pItem->name, "test_int64");
EXPECT_EQ(pItem->int64Val, 7);
pItem = cfgGetItem(pConfig, "test_uint64");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_UINT64);
EXPECT_STREQ(pItem->name, "test_uint64");
EXPECT_EQ(pItem->uint64Val, 8);
EXPECT_EQ(pItem->int64Val, 4);
pItem = cfgGetItem(pConfig, "test_float");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_FLOAT);
EXPECT_STREQ(pItem->name, "test_float");
EXPECT_EQ(pItem->floatVal, 9);
pItem = cfgGetItem(pConfig, "test_double");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_DOUBLE);
EXPECT_STREQ(pItem->name, "test_double");
EXPECT_EQ(pItem->doubleVal, 10);
EXPECT_EQ(pItem->floatVal, 5);
pItem = cfgGetItem(pConfig, "test_string");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_STRING);
EXPECT_STREQ(pItem->name, "test_string");
EXPECT_STREQ(pItem->strVal, "11");
pItem = cfgGetItem(pConfig, "test_fqdn");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_FQDN);
EXPECT_STREQ(pItem->name, "test_fqdn");
EXPECT_STREQ(pItem->strVal, "localhost");
EXPECT_STREQ(pItem->strVal, "6");
pItem = cfgGetItem(pConfig, "test_ipstr");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_IPSTR);
EXPECT_STREQ(pItem->name, "test_ipstr");
EXPECT_STREQ(pItem->ipstrVal, "192.168.0.1");
pItem = cfgGetItem(pConfig, "test_dir");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_DIR);
EXPECT_STREQ(pItem->name, "test_dir");
EXPECT_STREQ(pItem->dirVal, "/tmp");
pItem = cfgGetItem(pConfig, "test_file");
EXPECT_EQ(pItem->stype, CFG_TYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_FILE);
EXPECT_STREQ(pItem->name, "test_file");
EXPECT_STREQ(pItem->fileVal, "/tmp/file1");
cfgCleanup(pConfig);
}
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _DEFAULT_SOURCE
#include "os.h"
#include "tconfig.h"
#include "tutil.h"
#include "ulog.h"
SGlobalCfg tsGlobalConfig[TSDB_CFG_MAX_NUM] = {{0}};
int32_t tsGlobalConfigNum = 0;
static char *tsGlobalUnit[] = {
" ",
"(%)",
"(GB)",
"(Mb)",
"(byte)",
"(s)",
"(ms)"
};
char *tsCfgStatusStr[] = {
"none",
"system default",
"config file",
"taos_options",
"program argument list"
};
static void taosReadFloatConfig(SGlobalCfg *cfg, char *input_value) {
float value = (float)atof(input_value);
float *option = (float *)cfg->ptr;
if (value < cfg->minValue || value > cfg->maxValue) {
uError("config option:%s, input value:%s, out of range[%f, %f], use default value:%f",
cfg->option, input_value, cfg->minValue, cfg->maxValue, *option);
} else {
if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) {
*option = value;
cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE;
} else {
uWarn("config option:%s, input value:%s, is configured by %s, use %f", cfg->option, input_value,
tsCfgStatusStr[cfg->cfgStatus], *option);
}
}
}
static void taosReadDoubleConfig(SGlobalCfg *cfg, char *input_value) {
double value = atof(input_value);
double *option = (double *)cfg->ptr;
if (value < cfg->minValue || value > cfg->maxValue) {
uError("config option:%s, input value:%s, out of range[%f, %f], use default value:%f",
cfg->option, input_value, cfg->minValue, cfg->maxValue, *option);
} else {
if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) {
*option = value;
cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE;
} else {
uWarn("config option:%s, input value:%s, is configured by %s, use %f", cfg->option, input_value,
tsCfgStatusStr[cfg->cfgStatus], *option);
}
}
}
static void taosReadInt32Config(SGlobalCfg *cfg, char *input_value) {
int32_t value = atoi(input_value);
int32_t *option = (int32_t *)cfg->ptr;
if (value < cfg->minValue || value > cfg->maxValue) {
uError("config option:%s, input value:%s, out of range[%f, %f], use default value:%d",
cfg->option, input_value, cfg->minValue, cfg->maxValue, *option);
} else {
if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) {
*option = value;
cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE;
} else {
uWarn("config option:%s, input value:%s, is configured by %s, use %d", cfg->option, input_value,
tsCfgStatusStr[cfg->cfgStatus], *option);
}
}
}
static void taosReadInt16Config(SGlobalCfg *cfg, char *input_value) {
int32_t value = atoi(input_value);
int16_t *option = (int16_t *)cfg->ptr;
if (value < cfg->minValue || value > cfg->maxValue) {
uError("config option:%s, input value:%s, out of range[%f, %f], use default value:%d",
cfg->option, input_value, cfg->minValue, cfg->maxValue, *option);
} else {
if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) {
*option = (int16_t)value;
cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE;
} else {
uWarn("config option:%s, input value:%s, is configured by %s, use %d", cfg->option, input_value,
tsCfgStatusStr[cfg->cfgStatus], *option);
}
}
}
static void taosReadUInt16Config(SGlobalCfg *cfg, char *input_value) {
int32_t value = atoi(input_value);
uint16_t *option = (uint16_t *)cfg->ptr;
if (value < cfg->minValue || value > cfg->maxValue) {
uError("config option:%s, input value:%s, out of range[%f, %f], use default value:%d",
cfg->option, input_value, cfg->minValue, cfg->maxValue, *option);
} else {
if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) {
*option = (uint16_t)value;
cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE;
} else {
uWarn("config option:%s, input value:%s, is configured by %s, use %d", cfg->option, input_value,
tsCfgStatusStr[cfg->cfgStatus], *option);
}
}
}
static void taosReadInt8Config(SGlobalCfg *cfg, char *input_value) {
int32_t value = atoi(input_value);
int8_t *option = (int8_t *)cfg->ptr;
if (value < cfg->minValue || value > cfg->maxValue) {
uError("config option:%s, input value:%s, out of range[%f, %f], use default value:%d",
cfg->option, input_value, cfg->minValue, cfg->maxValue, *option);
} else {
if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) {
*option = (int8_t)value;
cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE;
} else {
uWarn("config option:%s, input value:%s, is configured by %s, use %d", cfg->option, input_value,
tsCfgStatusStr[cfg->cfgStatus], *option);
}
}
}
static bool taosReadDirectoryConfig(SGlobalCfg *cfg, char *input_value) {
int length = (int)strlen(input_value);
char *option = (char *)cfg->ptr;
if (length <= 0 || length > cfg->ptrLength) {
uError("config option:%s, input value:%s, length out of range[0, %d], use default value:%s", cfg->option,
input_value, cfg->ptrLength, option);
return false;
} else {
if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) {
taosExpandDir(input_value, option, cfg->ptrLength);
taosRealPath(option, cfg->ptrLength);
if (taosMkDir(option) != 0) {
uError("config option:%s, input value:%s, directory not exist, create fail:%s", cfg->option, input_value,
strerror(errno));
return false;
}
cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE;
} else {
uWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, input_value,
tsCfgStatusStr[cfg->cfgStatus], option);
}
}
return true;
}
static void taosReadIpStrConfig(SGlobalCfg *cfg, char *input_value) {
uint32_t value = taosInetAddr(input_value);
char * option = (char *)cfg->ptr;
if (value == INADDR_NONE) {
uError("config option:%s, input value:%s, is not a valid ip address, use default value:%s",
cfg->option, input_value, option);
} else {
if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) {
strncpy(option, input_value, cfg->ptrLength);
cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE;
} else {
uWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, input_value,
tsCfgStatusStr[cfg->cfgStatus], option);
}
}
}
static void taosReadStringConfig(SGlobalCfg *cfg, char *input_value) {
int length = (int) strlen(input_value);
char *option = (char *)cfg->ptr;
if (length <= 0 || length > cfg->ptrLength) {
uError("config option:%s, input value:%s, length out of range[0, %d], use default value:%s",
cfg->option, input_value, cfg->ptrLength, option);
} else {
if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_FILE) {
strncpy(option, input_value, cfg->ptrLength);
cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE;
} else {
uWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, input_value,
tsCfgStatusStr[cfg->cfgStatus], option);
}
}
}
static void taosReadLogOption(char *option, char *value) {
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_CONFIG) || !(cfg->cfgType & TSDB_CFG_CTYPE_B_LOG)) continue;
if (strcasecmp(cfg->option, option) != 0) continue;
switch (cfg->valType) {
case TAOS_CFG_VTYPE_INT32:
taosReadInt32Config(cfg, value);
// if (strcasecmp(cfg->option, "debugFlag") == 0) {
// taosSetAllDebugFlag();
// }
break;
case TAOS_CFG_VTYPE_DIRECTORY:
taosReadDirectoryConfig(cfg, value);
break;
default:
break;
}
break;
}
}
SGlobalCfg *taosGetConfigOption(const char *option) {
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (strcasecmp(cfg->option, option) != 0) continue;
return cfg;
}
return NULL;
}
static void taosReadConfigOption(const char *option, char *value, char *value2, char *value3) {
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_CONFIG)) continue;
if (strcasecmp(cfg->option, option) != 0) continue;
switch (cfg->valType) {
case TAOS_CFG_VTYPE_INT8:
taosReadInt8Config(cfg, value);
break;
case TAOS_CFG_VTYPE_INT16:
taosReadInt16Config(cfg, value);
break;
case TAOS_CFG_VTYPE_INT32:
taosReadInt32Config(cfg, value);
break;
case TAOS_CFG_VTYPE_UINT16:
taosReadUInt16Config(cfg, value);
break;
case TAOS_CFG_VTYPE_FLOAT:
taosReadFloatConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_DOUBLE:
taosReadDoubleConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_STRING:
taosReadStringConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_IPSTR:
taosReadIpStrConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_DIRECTORY:
taosReadDirectoryConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_DATA_DIRCTORY:
if (taosReadDirectoryConfig(cfg, value)) {
// taosReadDataDirCfg(value, value2, value3);
}
break;
default:
uError("config option:%s, input value:%s, can't be recognized", option, value);
break;
}
break;
}
}
void taosAddConfigOption(SGlobalCfg cfg) {
tsGlobalConfig[tsGlobalConfigNum++] = cfg;
}
void taosReadGlobalLogCfg() {
FILE * fp;
char * line, *option, *value;
int olen, vlen;
char fileName[PATH_MAX] = {0};
taosExpandDir(configDir, configDir, PATH_MAX);
taosReadLogOption("logDir", tsLogDir);
sprintf(fileName, "%s/taos.cfg", configDir);
fp = fopen(fileName, "r");
if (fp == NULL) {
printf("\nconfig file:%s not found, all variables are set to default\n", fileName);
return;
}
ssize_t _bytes = 0;
size_t len = 1024;
line = calloc(1, len);
while (!feof(fp)) {
memset(line, 0, len);
option = value = NULL;
olen = vlen = 0;
_bytes = tgetline(&line, &len, fp);
if (_bytes < 0)
{
break;
}
line[len - 1] = 0;
paGetToken(line, &option, &olen);
if (olen == 0) continue;
option[olen] = 0;
paGetToken(option + olen + 1, &value, &vlen);
if (vlen == 0) continue;
value[vlen] = 0;
taosReadLogOption(option, value);
}
tfree(line);
fclose(fp);
}
int32_t taosReadCfgFromFile() {
char * line, *option, *value, *value2, *value3;
int olen, vlen, vlen2, vlen3;
char fileName[PATH_MAX] = {0};
sprintf(fileName, "%s/taos.cfg", configDir);
FILE *fp = fopen(fileName, "r");
if (fp == NULL) {
fp = fopen(configDir, "r");
if (fp == NULL) {
return -1;
}
}
ssize_t _bytes = 0;
size_t len = 1024;
line = calloc(1, len);
while (!feof(fp)) {
memset(line, 0, len);
option = value = value2 = value3 = NULL;
olen = vlen = vlen2 = vlen3 = 0;
_bytes = tgetline(&line, &len, fp);
if (_bytes < 0)
{
break;
}
line[len - 1] = 0;
paGetToken(line, &option, &olen);
if (olen == 0) continue;
option[olen] = 0;
paGetToken(option + olen + 1, &value, &vlen);
if (vlen == 0) continue;
value[vlen] = 0;
paGetToken(value + vlen + 1, &value2, &vlen2);
if (vlen2 != 0) {
value2[vlen2] = 0;
paGetToken(value2 + vlen2 + 1, &value3, &vlen3);
if (vlen3 != 0) value3[vlen3] = 0;
}
taosReadConfigOption(option, value, value2, value3);
}
fclose(fp);
tfree(line);
// if (debugFlag & DEBUG_TRACE || debugFlag & DEBUG_DEBUG || debugFlag & DEBUG_DUMP) {
// taosSetAllDebugFlag();
// }
return 0;
}
void taosPrintCfg() {
uInfo(" taos config & system info:");
uInfo("==================================");
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (tscEmbeddedInUtil == 0 && !(cfg->cfgType & TSDB_CFG_CTYPE_B_CLIENT)) continue;
if (cfg->cfgType & TSDB_CFG_CTYPE_B_NOT_PRINT) continue;
int optionLen = (int)strlen(cfg->option);
int blankLen = TSDB_CFG_PRINT_LEN - optionLen;
blankLen = blankLen < 0 ? 0 : blankLen;
char blank[TSDB_CFG_PRINT_LEN];
memset(blank, ' ', TSDB_CFG_PRINT_LEN);
blank[blankLen] = 0;
switch (cfg->valType) {
case TAOS_CFG_VTYPE_INT8:
uInfo(" %s:%s%d%s", cfg->option, blank, *((int8_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_INT16:
uInfo(" %s:%s%d%s", cfg->option, blank, *((int16_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_INT32:
uInfo(" %s:%s%d%s", cfg->option, blank, *((int32_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_UINT16:
uInfo(" %s:%s%d%s", cfg->option, blank, *((uint16_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_FLOAT:
uInfo(" %s:%s%f%s", cfg->option, blank, *((float *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_DOUBLE:
uInfo(" %s:%s%f%s", cfg->option, blank, *((double *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_STRING:
case TAOS_CFG_VTYPE_IPSTR:
case TAOS_CFG_VTYPE_DIRECTORY:
uInfo(" %s:%s%s%s", cfg->option, blank, (char *)cfg->ptr, tsGlobalUnit[cfg->unitType]);
break;
default:
break;
}
}
taosPrintOsInfo();
uInfo("==================================");
}
static void taosDumpCfg(SGlobalCfg *cfg) {
int optionLen = (int)strlen(cfg->option);
int blankLen = TSDB_CFG_PRINT_LEN - optionLen;
blankLen = blankLen < 0 ? 0 : blankLen;
char blank[TSDB_CFG_PRINT_LEN];
memset(blank, ' ', TSDB_CFG_PRINT_LEN);
blank[blankLen] = 0;
switch (cfg->valType) {
case TAOS_CFG_VTYPE_INT8:
printf(" %s:%s%d%s\n", cfg->option, blank, *((int8_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_INT16:
printf(" %s:%s%d%s\n", cfg->option, blank, *((int16_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_INT32:
printf(" %s:%s%d%s\n", cfg->option, blank, *((int32_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_UINT16:
printf(" %s:%s%d%s\n", cfg->option, blank, *((uint16_t *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_FLOAT:
printf(" %s:%s%f%s\n", cfg->option, blank, *((float *)cfg->ptr), tsGlobalUnit[cfg->unitType]);
break;
case TAOS_CFG_VTYPE_STRING:
case TAOS_CFG_VTYPE_IPSTR:
case TAOS_CFG_VTYPE_DIRECTORY:
printf(" %s:%s%s%s\n", cfg->option, blank, (char *)cfg->ptr, tsGlobalUnit[cfg->unitType]);
break;
default:
break;
}
}
void taosDumpGlobalCfg() {
printf("taos global config:\n");
printf("==================================\n");
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (tscEmbeddedInUtil == 0 && !(cfg->cfgType & TSDB_CFG_CTYPE_B_CLIENT)) continue;
if (cfg->cfgType & TSDB_CFG_CTYPE_B_NOT_PRINT) continue;
if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW)) continue;
taosDumpCfg(cfg);
}
printf("\ntaos local config:\n");
printf("==================================\n");
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (tscEmbeddedInUtil == 0 && !(cfg->cfgType & TSDB_CFG_CTYPE_B_CLIENT)) continue;
if (cfg->cfgType & TSDB_CFG_CTYPE_B_NOT_PRINT) continue;
if (cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW) continue;
taosDumpCfg(cfg);
}
}
......@@ -81,6 +81,8 @@ TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_MSG, "Invalid message")
TAOS_DEFINE_ERROR(TSDB_CODE_MSG_NOT_PROCESSED, "Message not processed")
TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_PARA, "Invalid parameters")
TAOS_DEFINE_ERROR(TSDB_CODE_REPEAT_INIT, "Repeat initialization")
TAOS_DEFINE_ERROR(TSDB_CODE_CFG_NOT_FOUND, "Config not found")
TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_CFG, "Invalid config option")
TAOS_DEFINE_ERROR(TSDB_CODE_REF_NO_MEMORY, "Ref out of memory")
TAOS_DEFINE_ERROR(TSDB_CODE_REF_FULL, "too many Ref Objs")
......
......@@ -15,7 +15,6 @@
#include "os.h"
#include "shell.h"
#include "tconfig.h"
#include "tglobal.h"
pthread_t pid;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册