首页服务器Web服务器 linux c下log输出代码模板示例代码

linux c下log输出代码模板示例代码

前言 本文主要介绍了关于linux c下log输出代码模板的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 模板 模本分为两个文件:log.c和log.h. log.c /*…

前言

本文主要介绍了关于linux c下log输出代码模板的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧

模板

模本分为两个文件:log.c和log.h.

log.c

/** log.c **/#include #include "log.h"// log文件路径#define filepath "./ps_com_log.log" //设定时间static char * settime(char * time_s){ time_t timer=time(NULL); strftime(time_s, 20, "%Y-%m-%d %H:%M:%S",localtime(&timer)); return time_s;} /* *打印 * */static int PrintfLog(char * logText, char * string){ FILE * fd = NULL; char s[1024]; char tmp[256]; //使用追加方式打开文件 fd = fopen(filepath,"a+"); if(fd == NULL){  return -1; }  memset(s, 0, sizeof(s)); memset(tmp, 0,sizeof(tmp));  sprintf(tmp, "*****[pid=%d]:[", getpid()); strcpy(s, tmp);  memset(tmp, 0,sizeof(tmp)); settime(tmp); strcat(s, tmp); strcat(s, "]*****"); fprintf(fd, "%s", s); fprintf(fd, "*[%s]*****:/n",logText);  fprintf(fd, "%s/n",string);  fclose(fd);}  /* *日志写入 * */void LogWrite(char *logText,char *string){ //[为支持多线程需要加锁] pthread_mutex_lock(&mutex_log); //lock.  //打印日志信息 PrintfLog(logText, string);                   //[为支持多线程需要加锁] pthread_mutex_unlock(&mutex_log); //unlock.            }
本文来自网络,不代表1号站长-站长学院|资讯交流平台立场。转载请注明出处: https://www.1cn.cc/fwq/web/2318.html
上一篇Docker容器的创建、启动、和停止的方法
下一篇 增强Linux内核中访问控制安全的方法
admin

作者: admin

这里可以再内容模板定义一些文字和说明,也可以调用对应作者的简介!或者做一些网站的描述之类的文字或者HTML!

为您推荐

评论列表()

    联系我们

    联系我们

    0898-88888888

    在线咨询: QQ交谈

    邮箱: email@wangzhan.com

    工作时间:周一至周五,9:00-17:30,节假日休息

    关注微信
    微信扫一扫关注我们

    微信扫一扫关注我们

    关注微博
    返回顶部