求LINUX下,C语言编写的日志输出源码~

求LINUX下,C语言编写的日志输出源码~,第1张

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

#include <dirent.h>

#include <time.h>

#define LOGFILE "./dir_log_0"

int g_Count

//#define MAXLEN  1024

void WriteDebugLog(char *str)

int main(int argc, char **argv)

{

    char str[1024]={0}

    strcpy(str,"file no find")

    int i=0,j=0

    for (i=0i<10i++)

    {

        for (j=0j<50j++)

        {

            WriteDebugLog(str)    

        }

            

    }

    return 0

}

void WriteDebugLog(char *str)

{

    char buf[2048]={0}

    char logFileName[50]={0}

    //long MAXLEN = 50*1024*1024//50MB

    int iMax = 1024//1K

    time_t timep

    FILE *fp = NULL

    struct tm *p

    time(&timep)

    p = localtime(&timep)

    memset(buf,0,sizeof(buf))

    sprintf(buf,"[%d-%d-%d %d:%d:%d][DEBUG]",(1900+p->tm_year),(1+p->tm_mon), p->tm_mday,p->tm_hour, p->tm_min, p->tm_sec)//星期p->tm_wday

    strcat(buf,str)

    strcat(buf,"\r\n")

    strcpy(logFileName,LOGFILE)

    int len = strlen(logFileName)

    logFileName[len-1] = '0'+g_Count

    fp = fopen(logFileName,"r")

    if(fp==NULL)

    {

        fp = fopen(logFileName,"w+")

    }

    else

    {

        fseek(fp,0,2)//SEEK_END值为2

        if( ftell(fp) >= iMax)

        {

            fclose(fp)

                

            if (g_Count >= 9) 

            {

                logFileName[len-1] = '0'

                g_Count=0

            }

            else

            {

                g_Count++

                logFileName[len-1] = '0'+g_Count

            //  printf("\n%c",'0'+g_Count)

            }

            fp = fopen(logFileName,"w+")

        }

        else

        { 

            fclose(fp)

            fp = fopen(logFileName,"a")

        }

    }

    fwrite(buf,1,strlen(buf),fp)

    fclose(fp)

}

一般安装的信息会自动保存在/var/log/dpkg.log跟dpkg.log1文件中,你vi一下就知道了。

一般信息都会保存在/var/log/目录下,好多类别的信息都在


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7559233.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-07
下一篇 2023-04-07

发表评论

登录后才能评论

评论列表(0条)

保存