做外貿(mào)有那些網(wǎng)站平臺(tái)北京優(yōu)化核酸檢測(cè)
目錄
一、描述
二、實(shí)現(xiàn)效果
三、使用案例
四、內(nèi)存檢測(cè)
一、描述
? ? ? ? 最近實(shí)現(xiàn)一個(gè) WS 服務(wù)器,內(nèi)部需要一個(gè)日志打印記錄服務(wù)器程序的運(yùn)行過程,故自己實(shí)現(xiàn)了一個(gè)輕量級(jí)的 logger,主要包含如下特征:
-
可輸出 debug、info、warn、error、fatal 級(jí)別日志
-
每行日志包含時(shí)間戳、進(jìn)程號(hào)、線程號(hào)、文件名、函數(shù)、行數(shù)、日志級(jí)別等重要信息
-
可按日志文件大小自動(dòng)拆分日志文件(以 MB 為單位)
-
可限制日志文件數(shù)量(自動(dòng)覆蓋舊的日志文件)
-
日志文件命名可設(shè)置固定開頭,以時(shí)間戳進(jìn)行區(qū)分
-
線程安全,可多線程讀寫
-
通過宏參數(shù)控制日志行為
? ? 具體的代碼已上傳至 gitcode和github,鏈接如下:
項(xiàng)目首頁(yè) - GitCode
??????AtaoistPriest/c_logger: A Lightweight Logger Implemented with C (github.com)
二、實(shí)現(xiàn)效果
? ? ? ? 主要打印時(shí)間戳、進(jìn)程號(hào)、線程號(hào)、文件名、函數(shù)、行數(shù)、日志級(jí)別和錯(cuò)誤描述等重要信息。
May 21 2024 23:45:01 [1661][1663] [./test.c print_log:19] [ERROR] this an error thread 2 54246
May 21 2024 23:45:01 [1661][1663] [./test.c print_log:20] [ERROR] this an error thread 2 54246
May 21 2024 23:45:01 [1661][1663] [./test.c print_log:13] [INFO] this an info thread 2 54247
May 21 2024 23:45:01 [1661][1663] [./test.c print_log:14] [INFO] this an info thread 2 54247
May 21 2024 23:45:01 [1661][1663] [./test.c print_log:16] [WARN] this an warn thread 2 54247
May 21 2024 23:45:01 [1661][1662] [./test.c print_log:13] [INFO] this an info thread 1 59045
May 21 2024 23:45:01 [1661][1662] [./test.c print_log:14] [INFO] this an info thread 1 59045
May 21 2024 23:45:01 [1661][1662] [./test.c print_log:16] [WARN] this an warn thread 1 59045
May 21 2024 23:45:01 [1661][1662] [./test.c print_log:17] [WARN] this an warn thread 1 59045
May 21 2024 23:45:01 [1661][1662] [./test.c print_log:19] [ERROR] this an error thread 1 59045
May 21 2024 23:45:01 [1661][1662] [./test.c print_log:20] [ERROR] this an error thread 1 59045
三、使用案例
#include <unistd.h>
#include <pthread.h>
#include "./logger.h"void *print_log(void *arg)
{char *str = (char *)arg;for ( int i = 0; i < 100000; i++ ){logger_level_printf(LOGGER_DEBUG_LEVEL, "this an debug %s %d", str, 525 + i);logger_level_printf(LOGGER_INFO_LEVEL, "this an info %s %d", str, 525 + i);logger_level_printf(LOGGER_WARN_LEVEL, "this an warn %s %d", str, 525 + i);logger_level_printf(LOGGER_ERROR_LEVEL, "this an error %s %d", str, 525 + i);if ( i % 10000 == 0 ){sleep(1);}}return NULL;
}int main(void)
{logger_init("./log");pthread_t tid1, tid2;pthread_create(&tid1, NULL, print_log, "thread 1");pthread_create(&tid2, NULL, print_log, "thread 2");pthread_join(tid1, NULL);pthread_join(tid2, NULL);logger_destroy();return 0;
}
四、內(nèi)存檢測(cè)
? ? ? ? 使用?valgrind 對(duì)該日志器進(jìn)行了內(nèi)存泄漏檢測(cè),檢測(cè)結(jié)果如下,沒有任何問題。
==14863== Memcheck, a memory error detector
==14863== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==14863== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==14863== Command: ./test
==14863== Parent PID: 10266
==14863==
==14863==
==14863== HEAP SUMMARY:
==14863== in use at exit: 0 bytes in 0 blocks
==14863== total heap usage: 27 allocs, 27 frees, 368,920 bytes allocated
==14863==
==14863== All heap blocks were freed -- no leaks are possible
==14863==
==14863== For lists of detected and suppressed errors, rerun with: -s
==14863== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
????????valgrind是一個(gè)開源的,檢測(cè)內(nèi)存泄漏的工具,通常在linux下使用,除此之外,他還能檢測(cè)內(nèi)存管理錯(cuò)誤,線程bug等錯(cuò)誤。valgrind相當(dāng)于一個(gè)沙盒,指定的程序會(huì)在他模擬的cpu和內(nèi)核中去運(yùn)行。
????????粗淺的來講,valgrind由兩部分構(gòu)成,一部分用來模擬cpu和內(nèi)核,被稱為framework(框架),一部分是他用來檢測(cè)各種錯(cuò)誤信息的插件工具。其中的插件包括:
????????memcheck:是valgrind最重要的工具之一,是一個(gè)重量級(jí)的內(nèi)存檢查器,它可以幫我們檢測(cè)是否有使用未初始化的內(nèi)存,以及內(nèi)存泄漏,訪問越界之類的問題。
????????callgrind:可以用來統(tǒng)計(jì)函數(shù)的互相調(diào)用情況??梢杂脕斫y(tǒng)計(jì)某個(gè)函數(shù)被調(diào)用了幾次,以及他們的調(diào)用關(guān)系。
????????cachegrind:cache分析器,可以精準(zhǔn)的指出cache的丟失與命中。還可以統(tǒng)計(jì)cache命中與丟失的次數(shù),讓我們了解我們程序堆棧的使用情況。
????????helgrind:主要用來檢測(cè)線程資源的競(jìng)爭(zhēng)問題,比如 POSIX pthreads API 的誤用。由鎖排序?qū)е碌乃梨i。以及數(shù)據(jù)的競(jìng)爭(zhēng)問題等。
????????massif:堆棧分析器。它可以統(tǒng)計(jì)堆和棧使用的內(nèi)存大小是多少。
????????extension:可以利用core的特性自己編輯的內(nèi)存調(diào)試工具。
valgrind --tool=memcheck --leak-check=full --show-reachable=yes --log-file=check.log ./host_monitor
????????在程序執(zhí)行結(jié)束后,valgrind 將打印檢測(cè)結(jié)果。
????????valgrind 將內(nèi)存泄漏分為 4 類:并且默認(rèn)情況下,只會(huì)打印明確泄漏 和可能泄漏,如果需要打印 間接泄漏,需要加上選項(xiàng) --show-reachable=yes.
????????a. 明確泄漏(definitely lost):表示某段內(nèi)存還沒有被釋放,但是已經(jīng)沒有指針指向它了,所以明確了這里一定發(fā)生了內(nèi)存釋放。
????????b. 間接泄漏(indirectly lost):某一段內(nèi)存沒有被釋放,指向它的指針仍然存在,但是,存放這個(gè)指針的內(nèi)存已經(jīng)被釋放了。
????????c. 可能泄漏(possibly lost):指針存在,但并不指向內(nèi)存頭地址,而是指向內(nèi)存內(nèi)部的位置。
????????d. 仍可訪達(dá)(still reachable):指針一直存在并指向首地址,直至程序退出時(shí)內(nèi)存還沒被釋放。例如指針 ptr=malloc() 了一段內(nèi)存,直至程序結(jié)束,ptr仍然指向malloc分配的內(nèi)存的首地址,并且該內(nèi)存仍然沒有沒釋放。