diff --git a/include/libs/monitor/monitor.h b/include/libs/monitor/monitor.h index 262861000de5db5f3a016dedf7bf4c3dd3873b6d..0041f3ac5fdb42ad7ac9337e32be265fa8a541ac 100644 --- a/include/libs/monitor/monitor.h +++ b/include/libs/monitor/monitor.h @@ -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); diff --git a/include/util/tlog.h b/include/util/tlog.h index ff1c0044a4a85be79cbdb052099f1a54df57ea6a..d3ab9b0bfb76b566ee91101c2fc8fb7298ace2c4 100644 --- a/include/util/tlog.h +++ b/include/util/tlog.h @@ -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; diff --git a/source/libs/monitor/inc/monInt.h b/source/libs/monitor/inc/monInt.h index 61f9980e4e3133469869e1fb7f649b2aa33ec1cc..ca5aaf0a388826d0ba4acd329296337d5e491a0e 100644 --- a/source/libs/monitor/inc/monInt.h +++ b/source/libs/monitor/inc/monInt.h @@ -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 diff --git a/source/libs/monitor/src/monitor.c b/source/libs/monitor/src/monitor.c index 6882c66e7d31183ae534fbcea3085a3d49c304a4..a50b8c3e90b56db1acbe8cfad9d2ab9c238b72d7 100644 --- a/source/libs/monitor/src/monitor.c +++ b/source/libs/monitor/src/monitor.c @@ -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) { diff --git a/source/libs/monitor/test/monTest.cpp b/source/libs/monitor/test/monTest.cpp index 91a502e8e2030ef06eeb3fc005611dc70888f77f..3eaab45e3e6d21af7a74e4bfb6735f250a023577 100644 --- a/source/libs/monitor/test/monTest.cpp +++ b/source/libs/monitor/test/monTest.cpp @@ -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) { diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index bbc5b6e5130e71babb066a5a77247f5762f52d19..f3457da5b0f733cabab89e91043638a2a9fd905b 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -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;