linux下C語言實現(xiàn)寫日志功能
先上程序,該程序經(jīng)過測試能夠很好的實現(xiàn)寫日志要求
/*************************************************************************
> File Name: log.c
> Author:
************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <time.h>
#include <pthread.h>
int safe_asprintf(char **strp, const char *fmt, ...);
int safe_vasprintf(char **strp, const char *fmt, va_list ap);
void plog(const char *format, ...) ;
void pinfo(const char *format, ...) ;
#define DEBUG
#ifdef DEBUG
void plog(const char *format, ...);
void pinfo(const char *format, ...);
#define debug(fmt, args...) plog(fmt, ##args)
#else
#define debug(fmt, args...) do{}while(0)
#endif
static pthread_mutex_t fileMutex = PTHREAD_MUTEX_INITIALIZER;
int main(int argc, char *argv)
{
return 0;
}
/*
* safe_asprintf();
*/
int safe_asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
int retval;
va_start(ap, fmt);
retval = safe_vasprintf(strp, fmt, ap);
va_end(ap);
return retval;
}
/*
* safe_vasprintf();
*/
int safe_vasprintf(char **strp, const char *fmt, va_list ap)
{
int retval;
retval = vasprintf(strp, fmt, ap);
if (retval == -1)
{
printf("Failed to vasprintf: %s. Bailing out\n", strerror(errno));
return 1;
}
return retval;
}
/*
* plog();
*/
void plog(const char *format, ...)
{
pthread_mutex_lock(&fileMutex);
FILE *fp = NULL;
va_list vlist;
char *fmt = NULL;
// Open debug info output file.
if (!(fp = fopen("log.txt", "a+"))) {
pthread_mutex_unlock(&fileMutex);
return;
}
va_start(vlist, format);
safe_vasprintf(&fmt, format, vlist);
va_end(vlist);
if (!fmt) {
pthread_mutex_unlock(&fileMutex);
return;
}
time_t timep;
struct tm *ptm = NULL;
time(&timep);
ptm = localtime(&timep);
fprintf(fp, "[%04d-%02d-%02d-%02d-%02d-%02d] %s",
ptm->tm_year + 1900,
ptm->tm_mon + 1,
ptm->tm_mday,
ptm->tm_hour,
ptm->tm_min,
ptm->tm_sec,
fmt);
free(fmt);
fsync(fileno(fp));
fclose(fp);
pthread_mutex_unlock(&fileMutex);
}
/*
* pinfo();
*/
void pinfo(const char *format, ...)
{
pthread_mutex_lock(&fileMutex);
FILE *fp = NULL;
va_list vlist;
char *fmt = NULL;
// Open debug info output file.
if (!(fp = fopen("log.txt", "a+"))) {
pthread_mutex_unlock(&fileMutex);
return;
}
va_start(vlist, format);
safe_vasprintf(&fmt, format, vlist);
va_end(vlist);
if (!fmt) {
pthread_mutex_unlock(&fileMutex);
return;
}
fprintf(fp, "%s", fmt);
free(fmt);
fsync(fileno(fp));
fclose(fp);
pthread_mutex_unlock(&fileMutex);
}
程序?qū)崿F(xiàn)的日志格式為:
時間 + 空格 + 具體實現(xiàn)(自己的調(diào)試內(nèi)容)
本段程序值得學(xué)習(xí)的地方:
- va_list 結(jié)構(gòu)體的使用
- linux 的格式化輸出字符串
- 文件操作過程中pthread_mutex鎖的使用,以及他的優(yōu)點
- linux DEBUG 的應(yīng)用,方便調(diào)試
linux如何查看日志:
使用tail 命令可以實現(xiàn)日志的查詢,以及其他功能,不了解的話,自行查資料解決。
對上面應(yīng)用不明白的請自行查資料解決。
相關(guān)文章
Linux deepin 刪除多余內(nèi)核的實現(xiàn)方法
這篇文章主要介紹了Linux deepin 刪除多余內(nèi)核的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Centos7.5配置java環(huán)境安裝tomcat的講解
今天小編就為大家分享一篇關(guān)于Centos7.5配置java環(huán)境安裝tomcat的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Linux printf如何將十進制轉(zhuǎn)換為十六進制
文章總結(jié):介紹了十進制、十六進制和八進制之間的轉(zhuǎn)換方法,包括使用\b命令和bc工具進行轉(zhuǎn)換的技巧2024-12-12
CentOS8出現(xiàn)-bash:亂碼問題及解決方法
這篇文章主要介紹了CentOS8出現(xiàn)-bash:亂碼問題及解決方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
centos7利用yum安裝lnmp的教程(linux+nginx+php7.1+mysql5.7)
lnmp相信不用多介紹了,大家應(yīng)該都知道,下面這篇文章主要給大家介紹了關(guān)于centos7利用yum安裝lnmp(linux+nginx+php7.1+mysql5.7)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2018-03-03

