record log
This commit is contained in:
parent
1d9119df87
commit
5367ca7f2a
|
@ -127,12 +127,6 @@ typedef struct {
|
||||||
SMonDiskDesc tempdir;
|
SMonDiskDesc tempdir;
|
||||||
} SMonDiskInfo;
|
} SMonDiskInfo;
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int64_t ts;
|
|
||||||
ELogLevel level;
|
|
||||||
char content[MON_LOG_LEN];
|
|
||||||
} SMonLogItem;
|
|
||||||
|
|
||||||
typedef struct SMonInfo SMonInfo;
|
typedef struct SMonInfo SMonInfo;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -143,7 +137,7 @@ typedef struct {
|
||||||
|
|
||||||
int32_t monInit(const SMonCfg *pCfg);
|
int32_t monInit(const SMonCfg *pCfg);
|
||||||
void monCleanup();
|
void monCleanup();
|
||||||
void monAddLogItem(SMonLogItem *pItem);
|
void monRecordLog(int64_t ts, ELogLevel level, const char *content);
|
||||||
|
|
||||||
SMonInfo *monCreateMonitorInfo();
|
SMonInfo *monCreateMonitorInfo();
|
||||||
void monSetBasicInfo(SMonInfo *pMonitor, SMonBasicInfo *pInfo);
|
void monSetBasicInfo(SMonInfo *pMonitor, SMonBasicInfo *pInfo);
|
||||||
|
|
|
@ -34,10 +34,13 @@ typedef enum {
|
||||||
DEBUG_FILE = 128
|
DEBUG_FILE = 128
|
||||||
} ELogLevel;
|
} ELogLevel;
|
||||||
|
|
||||||
|
typedef void (*LogFp)(int64_t ts, ELogLevel level, const char *content);
|
||||||
|
|
||||||
extern bool tsLogEmbedded;
|
extern bool tsLogEmbedded;
|
||||||
extern bool tsAsyncLog;
|
extern bool tsAsyncLog;
|
||||||
extern int32_t tsNumOfLogLines;
|
extern int32_t tsNumOfLogLines;
|
||||||
extern int32_t tsLogKeepDays;
|
extern int32_t tsLogKeepDays;
|
||||||
|
extern LogFp tsLogFp;
|
||||||
extern int64_t tsNumOfErrorLogs;
|
extern int64_t tsNumOfErrorLogs;
|
||||||
extern int64_t tsNumOfInfoLogs;
|
extern int64_t tsNumOfInfoLogs;
|
||||||
extern int64_t tsNumOfDebugLogs;
|
extern int64_t tsNumOfDebugLogs;
|
||||||
|
|
|
@ -23,18 +23,24 @@
|
||||||
#include "tjson.h"
|
#include "tjson.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SRWLatch lock;
|
int64_t ts;
|
||||||
SArray *logs; // array of SMonLogItem
|
ELogLevel level;
|
||||||
int32_t maxLogs;
|
char content[MON_LOG_LEN];
|
||||||
const char *server;
|
} SMonLogItem;
|
||||||
uint16_t port;
|
|
||||||
} SMonitor;
|
|
||||||
|
|
||||||
typedef struct SMonInfo {
|
typedef struct SMonInfo {
|
||||||
SArray *logs; // array of SMonLogItem
|
SArray *logs; // array of SMonLogItem
|
||||||
SJson *pJson;
|
SJson *pJson;
|
||||||
} SMonInfo;
|
} SMonInfo;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
pthread_rwlock_t rwlock;
|
||||||
|
SArray *logs; // array of SMonLogItem
|
||||||
|
int32_t maxLogs;
|
||||||
|
const char *server;
|
||||||
|
uint16_t port;
|
||||||
|
} SMonitor;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -22,6 +22,21 @@
|
||||||
|
|
||||||
static SMonitor tsMonitor = {0};
|
static SMonitor tsMonitor = {0};
|
||||||
|
|
||||||
|
void monRecordLog(int64_t ts, ELogLevel level, const char *content) {
|
||||||
|
pthread_rwlock_rdlock(&tsMonitor.rwlock);
|
||||||
|
int32_t size = taosArrayGetSize(tsMonitor.logs);
|
||||||
|
if (size >= tsMonitor.maxLogs) {
|
||||||
|
uInfo("too many logs for monitor");
|
||||||
|
} else {
|
||||||
|
SMonLogItem item = {.ts = ts, .level = level};
|
||||||
|
SMonLogItem *pItem = taosArrayPush(tsMonitor.logs, &item);
|
||||||
|
if (pItem != NULL) {
|
||||||
|
tstrncpy(pItem->content, content, sizeof(item.content));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pthread_rwlock_unlock(&tsMonitor.rwlock);
|
||||||
|
}
|
||||||
|
|
||||||
int32_t monInit(const SMonCfg *pCfg) {
|
int32_t monInit(const SMonCfg *pCfg) {
|
||||||
tsMonitor.logs = taosArrayInit(16, sizeof(SMonLogItem));
|
tsMonitor.logs = taosArrayInit(16, sizeof(SMonLogItem));
|
||||||
if (tsMonitor.logs == NULL) {
|
if (tsMonitor.logs == NULL) {
|
||||||
|
@ -32,24 +47,15 @@ int32_t monInit(const SMonCfg *pCfg) {
|
||||||
tsMonitor.maxLogs = pCfg->maxLogs;
|
tsMonitor.maxLogs = pCfg->maxLogs;
|
||||||
tsMonitor.server = pCfg->server;
|
tsMonitor.server = pCfg->server;
|
||||||
tsMonitor.port = pCfg->port;
|
tsMonitor.port = pCfg->port;
|
||||||
taosInitRWLatch(&tsMonitor.lock);
|
tsLogFp = monRecordLog;
|
||||||
|
pthread_rwlock_init(&tsMonitor.rwlock, NULL);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void monCleanup() {
|
void monCleanup() {
|
||||||
taosArrayDestroy(tsMonitor.logs);
|
taosArrayDestroy(tsMonitor.logs);
|
||||||
tsMonitor.logs = NULL;
|
tsMonitor.logs = NULL;
|
||||||
}
|
pthread_rwlock_wrlock(&tsMonitor.rwlock);
|
||||||
|
|
||||||
void monAddLogItem(SMonLogItem *pItem) {
|
|
||||||
taosWLockLatch(&tsMonitor.lock);
|
|
||||||
int32_t size = taosArrayGetSize(tsMonitor.logs);
|
|
||||||
if (size >= tsMonitor.maxLogs) {
|
|
||||||
uInfo("too many logs for monitor");
|
|
||||||
} else {
|
|
||||||
taosArrayPush(tsMonitor.logs, pItem);
|
|
||||||
}
|
|
||||||
taosWUnLockLatch(&tsMonitor.lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SMonInfo *monCreateMonitorInfo() {
|
SMonInfo *monCreateMonitorInfo() {
|
||||||
|
@ -59,10 +65,10 @@ SMonInfo *monCreateMonitorInfo() {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
taosWLockLatch(&tsMonitor.lock);
|
pthread_rwlock_wrlock(&tsMonitor.rwlock);
|
||||||
pMonitor->logs = taosArrayDup(tsMonitor.logs);
|
pMonitor->logs = taosArrayDup(tsMonitor.logs);
|
||||||
taosArrayClear(tsMonitor.logs);
|
taosArrayClear(tsMonitor.logs);
|
||||||
taosWUnLockLatch(&tsMonitor.lock);
|
pthread_rwlock_unlock(&tsMonitor.rwlock);
|
||||||
|
|
||||||
pMonitor->pJson = tjsonCreateObject();
|
pMonitor->pJson = tjsonCreateObject();
|
||||||
if (pMonitor->pJson == NULL || pMonitor->logs == NULL) {
|
if (pMonitor->pJson == NULL || pMonitor->logs == NULL) {
|
||||||
|
|
|
@ -193,37 +193,14 @@ void MonitorTest::GetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void MonitorTest::AddLogInfo1() {
|
void MonitorTest::AddLogInfo1() {
|
||||||
SMonLogItem log1 = {0};
|
monRecordLog(taosGetTimestampMs(), DEBUG_INFO, "1 -------------------------- a");
|
||||||
log1.ts = taosGetTimestampMs();
|
monRecordLog(taosGetTimestampMs(), DEBUG_ERROR, "1 ------------------------ b");
|
||||||
log1.level = DEBUG_INFO;
|
monRecordLog(taosGetTimestampMs(), DEBUG_DEBUG, "1 ------- c");
|
||||||
strcpy(log1.content, "1 -------------------------- a");
|
|
||||||
monAddLogItem(&log1);
|
|
||||||
|
|
||||||
SMonLogItem log2 = {0};
|
|
||||||
log2.ts = taosGetTimestampMs();
|
|
||||||
log2.level = DEBUG_ERROR;
|
|
||||||
strcpy(log2.content, "1 ------------------------ b");
|
|
||||||
monAddLogItem(&log2);
|
|
||||||
|
|
||||||
SMonLogItem log3 = {0};
|
|
||||||
log3.ts = taosGetTimestampMs();
|
|
||||||
log3.level = DEBUG_DEBUG;
|
|
||||||
strcpy(log3.content, "1 ------- c");
|
|
||||||
monAddLogItem(&log3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MonitorTest::AddLogInfo2() {
|
void MonitorTest::AddLogInfo2() {
|
||||||
SMonLogItem log1;
|
monRecordLog(taosGetTimestampMs(), DEBUG_ERROR, "2 ------- a");
|
||||||
log1.ts = taosGetTimestampMs();
|
monRecordLog(taosGetTimestampMs(), DEBUG_ERROR, "2 ------- b");
|
||||||
log1.level = DEBUG_ERROR;
|
|
||||||
strcpy(log1.content, "2 ------- a");
|
|
||||||
monAddLogItem(&log1);
|
|
||||||
|
|
||||||
SMonLogItem log2;
|
|
||||||
log2.ts = taosGetTimestampMs();
|
|
||||||
log2.level = DEBUG_ERROR;
|
|
||||||
strcpy(log2.content, "2 ------- b");
|
|
||||||
monAddLogItem(&log2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(MonitorTest, 01_Full) {
|
TEST_F(MonitorTest, 01_Full) {
|
||||||
|
|
|
@ -73,6 +73,7 @@ bool tsLogEmbedded = 0;
|
||||||
bool tsAsyncLog = true;
|
bool tsAsyncLog = true;
|
||||||
int32_t tsNumOfLogLines = 10000000;
|
int32_t tsNumOfLogLines = 10000000;
|
||||||
int32_t tsLogKeepDays = 0;
|
int32_t tsLogKeepDays = 0;
|
||||||
|
LogFp tsLogFp = NULL;
|
||||||
int64_t tsNumOfErrorLogs = 0;
|
int64_t tsNumOfErrorLogs = 0;
|
||||||
int64_t tsNumOfInfoLogs = 0;
|
int64_t tsNumOfInfoLogs = 0;
|
||||||
int64_t tsNumOfDebugLogs = 0;
|
int64_t tsNumOfDebugLogs = 0;
|
||||||
|
|
Loading…
Reference in New Issue