linux写日志文件

来源:互联网 发布:windows中的剪切板是 编辑:程序博客网 时间:2024/05/21 07:04

/****************************************************************函数说明:获取当前系统时间入口参数:返回类型:fmt   0-返回:yyyy-mm-dd hh24:mi:ss       1-返回:yyyy-mm-dd       2-返回:hh24:mi:ss初始时间:2012-5-2Lebaishi修改记录:用途功能:获取当前系统时间,用做写Log日志****************************************************************/int getTime(char *out, int fmt){if(out == NULL){    return -1;}time_t t;struct tm *tp;t = time(NULL);tp = localtime(&t);if(fmt == 0){    sprintf(out, "%2.2d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", tp->tm_year+2000, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);}else if(fmt == 1){    sprintf(out, "%2.2d-%2.2d-%2.2d", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday);}else if(fmt == 2){    sprintf(out, "%2.2d:%2.2d:%2.2d", tp->tm_hour, tp->tm_min, tp->tm_sec);}return 0;}/****************************************************************函数说明:写入Log  日志入口参数:bLog  表明是否为日志文件,place 代码位置返回类型:初始时间:2012-5-2Lebaishi修改记录:用途功能:如果是日志文件,将会在str前加上当前时间(格式如:2011-04-12 12:10:20)****************************************************************/int write_errlog(Uchar bLog,char *str,char *place){    char curTime[100] = {0,};    int ret = -1;    memset(curTime,0,100);    if(NULL == file_log || NULL == str)//file_log 是FILE *file_log 打开的文件描述符    {return -1;    }    if(0 < bLog) // 获取当前系统时间    {        getTime(curTime, 0);        ret = fprintf(file_log, "[%s] : %s, the err is in %s\n", curTime, str, place);    }    else    {    ret = fprintf(file_log, "%s, the err is in %s\n", str, place);    }    if(ret >= 0)    {    fflush(file_log);        return 0;    }    else    {return -1;    }}




1. 建立工作目录

  mkdir -p /usr/local/virus

  2. 下载鸟哥写的Log分析脚本

  cd /usr/local/virus

  wget linux.vbird.org/download/vbird/logfile-0.1-4-2.tgz">http://linux.vbird.org/download/vbird/logfile-0.1-4-2.tgz

  3. 解压缩

  tar -zxvf logfile-0.1-4-2.tgz

  目录下会出现logfile次目录

  4.  修改logfile.sh

  cd logfile

  chown root:root logfile.sh

  chmod 755 logfile.sh

  vi logfile.sh

  修改email的值成真实email地址

  5. 试运行logfile.sh

  sh logfile.sh



//调用errorlog 函数可以向指定的logfile文件中写入自定义的结构体error_message类型的数据

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/signal.h>
#include <time.h>
#include <errno.h>
struct error_message
{
    time_t occur_time;//错误发生的时间
    const char * filename;//产生错误的文件
    int occur_no;//错误发生的行号
    const char *error_content;//错误的内容
};
typedef struct error_message error_message_t;
void errorlog(const char* logfile, error_message_t *emt)
{
    FILE *fp;
    struct tm ts;
    char time[20];
    int exist=0;
    localtime_r(&(emt->occur_time),&ts);

    sprintf(time,"%04d.%02d.%02d %02d:%02d:%02d",ts.tm_year+1900,ts.tm_mon+1,ts.tm_mday,ts.tm_hour,ts.tm_min,ts.tm_sec);

    exist=access(logfile,F_OK);//判断文件是否存在,不存在返回-1

    fp=fopen(logfile,"a+");
    if(fp!=NULL)
    {
        if(exist<0)
        {//文件不存在
            fprintf(fp,"%20s/t%20s/t%5s/t%s/n","错误时间","文件名","行号","错误内容");
        }

        fprintf(fp,"%s/t%s/t%d/t%s/n",time,emt->filename,emt->occur_no,emt->error_content);
    }
    else
    {
        printf("error:%s/n",strerror(errno));
    }
    signal(SIGXFSZ, SIG_DFL);
       fclose(fp);
}

int main(int argc,char **argv)
{
   error_message_t emt;
   bzero(&emt,sizeof(emt));
   time(&(emt.occur_time));
   emt.filename=__FILE__;
   emt.occur_no=__LINE__;
   emt.error_content="test";
   errorlog("/home/henry/stat/logfile.txt",&emt);
}

有时我们有这样的需求,那就是删除某个目录下过期的文件,可以由下面的函数实现:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <string.h>
#include <errno.h>

void clean_file(char* cleandir,int days)
{//删除cleandir目录下days天前的文件
    DIR *dir;
    struct dirent *de;
    struct stat buf;
    time_t tim;
    int i;
    dir=opendir(cleandir);
    if(dir==NULL)
    {
        printf("open directory %s error:%s/n",cleandir,strerror(errno));
        return;
    }
    chdir(cleandir);
    while((de=readdir(dir))!=NULL)
    {
        bzero(&buf,sizeof(buf));
        i=lstat(de->d_name,&buf);
        if(i<0)
        {
            printf("i=%d,de->d_name=%s/n",i,de->d_name);
            break;
        }
        else
        {
            if(S_ISDIR(buf.st_mode))
            {
                if((strcmp(de->d_name,".")==0)||(strcmp(de->d_name,".."))==0)
                    continue;
                clean_file(de->d_name,days);
            }
            else
            {
                time(&tim);
                if(tim-days*24*60*60>buf.st_mtime)
                {
                    printf("delete file:%s/n",de->d_name);
                    unlink(de->d_name);//delete file
                }
            }
        }
    }
    closedir(dir);
}

int main()
{
    clean_file("/home/henry/programtest",30);
}

//该程序有个缺陷,就是对于programtest  下不能有更多的子目录了。

可以从shell脚本中启动某个程序,脚本内容:

#!/bin/sh

/home/henry/programtest/cleanfile

exit

其中/home/henry/programtest/cleanfile为可执行文件的路径

原创粉丝点击