linux 往文件中写日志函数 C语言

函数的参数:想往日志里写什么就加什么参数

改完参数记得更改sprintf()中的参数

下面代码写在日志中的格式:

时间|进程绝对路径|IP|端口|结果

void writeRecord(const char* bufTime,const char* processPath,const char* ip,const char* port,const char* ret)
{
    char* recordFilePath = "/home/projects/protectFile/recordDaily.txt";
    char record[1024]={0};
    
    FILE* fp=fopen(recordFilePath,"a+");
    if(fp==NULL)
    {
        printf("fp is NULL and open file fail\n");
        return;
    }   
    //printf("fp!=NULL\n");
    if(sprintf(record,"%.*s|%.*s|%.*s|%.*s|%.*s",(int)strlen(bufTime),bufTime,(int)strlen(processPath),processPath,(int)strlen(ip),ip,(int)strlen(port),port,(int)strlen(ret),ret)<0)
      printf("sprintf() error!\n");
    if(record)
    {
      fwrite(record,strlen(record),1,fp);
      char* next= "\n";
      fwrite(next,strlen(next),1,fp);
    }
    fclose(fp);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值