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