linux下的日志编写

1、日志初始化创建

2、日志写入

3、日志关闭

log.c

cpp 复制代码
#include "log.h"

static log_t LOG;


//初始化日志文件,在当前目录创建日志文件
int log_init(char *pdirname)
{
    time_t t;
    struct tm *ptm = NULL;
    char filepath[64] = {0};
    int ret = 0;

    time(&t);
    ptm = localtime(&t);

    ret = access(pdirname, F_OK);
    if (0 != ret)
    {
        mkdir(pdirname, 0777);
    }

    sprintf(filepath, "%s/log_%4d%02d%02d", pdirname, ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);

    LOG.flog = fopen(filepath, "a");
    if (NULL == LOG.flog)
    {
        fprintf(stderr, "logfile open faileed, can not record software information");
        return -1;
    }

    LOG.curtime = ptm;

    pthread_mutex_init(&LOG.lock, NULL);

    return 0;
}


//写入日志文件
int log_write(LEVEL_T level,const char *pfile, const int line, const char *pfunc, const char *pstr, ...)
{
    time_t t;
    struct tm *ptm = NULL;
    char tmpinfo[1024] = {0};
    va_list pnext;

    pthread_mutex_lock(&LOG.lock);
    if (level < LOG.curlevel)
    {
        pthread_mutex_unlock(&LOG.lock);
        return 0;
    }

    va_start(pnext, pstr);

    time(&t);
    ptm = localtime(&t);

    if (ptm->tm_year != LOG.curtime->tm_year | ptm->tm_mon != LOG.curtime->tm_mon | ptm->tm_mday != LOG.curtime->tm_mday)
    {   
        pthread_mutex_unlock(&LOG.lock);
        log_deinit();
        log_init("LOG_FILE");
        pthread_mutex_lock(&LOG.lock);
        LOG.curtime = ptm;
    }

    sprintf(tmpinfo, "%s\n", pstr);
    fprintf(LOG.flog, "[%4d-%02d-%02d %02d-%02d-%02d] [%s %d %s] ", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, pfile, line, pfunc);
    vfprintf(LOG.flog, tmpinfo, pnext);

    va_end(pnext);

    pthread_mutex_unlock(&LOG.lock);

    return 0;
}



//销毁日志文件
int log_deinit(void)
{
    //关闭日志文件
    if (NULL != LOG.flog)
    {
        pthread_mutex_lock(&LOG.lock);
        fclose(LOG.flog);
        LOG.flog = NULL;
        pthread_mutex_unlock(&LOG.lock);
        pthread_mutex_destroy(&LOG.lock);
    }

    return 0;
}


//设置日志级别
void log_setlevel(LEVEL_T level)
{
    LOG.curlevel = level;

    return;
}

log.h

cpp 复制代码
#ifndef __LOG_H__
#define __LOG_H__

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <pthread.h>

typedef struct LOG_DATE 
{
    int year;
    int mon;
    int day;
}DATE_T;

typedef enum LOG_LEVEL
{
    LOG_MASSAGE,
    LOG_WORNING,
    LOG_ERROR,
}LEVEL_T;

typedef struct log
{
    FILE *flog;
    LEVEL_T curlevel;
    struct tm *curtime;  
    pthread_mutex_t lock;
}log_t;

extern int log_init(char *pdirname);
extern int log_write(LEVEL_T level,const char *pfile, const int line, const char *pfunc, const char *pstr, ...);
extern int log_deinit(void);


#endif

main.c

cpp 复制代码
#include "log.h"

int main(void)
{
    int num = 11451;

    log_init("LOG_FILE");

    log_write(LOG_MASSAGE, __FILE__,__LINE__,__func__, "===================================================");
    log_write(LOG_MASSAGE, __FILE__,__LINE__,__func__, "             software log record systerm           ");
    log_write(LOG_MASSAGE, __FILE__,__LINE__,__func__, "===================================================");
    log_write(LOG_MASSAGE, __FILE__,__LINE__,__func__, "hello world %d", num);
    log_write(LOG_MASSAGE, __FILE__,__LINE__,__func__, "hello world %d", num);

    log_deinit();

    return 0;
}

效果:

相关推荐
春日见3 小时前
如何入门端到端自动驾驶?
linux·人工智能·算法·机器学习·自动驾驶
dys_Codemonkey3 小时前
如何在树莓派上用 VS Code 优雅直连内部的 Ubuntu 子系统/容器用来访问容器内的文件和代码?
linux·运维·ubuntu·树莓派
炸膛坦客4 小时前
Linux - Ubuntu - PC端:(五)shell 操作(终端命令,2026)→ 3)基础命令,27 个常用命令
linux·ubuntu
努力中的编程者4 小时前
栈和队列(C语言底层实现环形队列)
c语言·开发语言
·醉挽清风·4 小时前
学习笔记—Linux—文件IO
linux·服务器·学习
一叶落4385 小时前
题目:15. 三数之和
c语言·数据结构·算法·leetcode
宁波阿成5 小时前
OpenClaw 在 Ubuntu 22.04.5 LTS 上的安装与问题处理记录
java·linux·ubuntu·openclaw·龙虾
上海合宙LuatOS5 小时前
LuatOS核心库API——【 string】字符串操作
运维·服务器·物联网·junit·硬件工程·信息与通信·嵌入式实时数据库
码不停蹄Zzz5 小时前
C语言——神奇的static
java·c语言·开发语言
徐子元竟然被占了!!5 小时前
Linux的cat
linux·运维·服务器