Linux Write log file __linux

Source: Internet
Author: User

Call the Errorlog function to write the custom struct error_message type of data to the specified logfile file

#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;//when the error occurred
const char * filename;//files that produce errors
int occur_no;//The line number where the error occurred
const char *error_content;//the contents of the error
};
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.%0 2d.%0 2d%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)//To determine whether the file exists, does not exist return-1

Fp=fopen (LogFile, "A +");
if (fp!=null)
{
if (exist<0)
{//file does not exist
fprintf (FP, "%20s/t%20s/t%5s/t%s/n", "Error Time", "filename", "line number", "Error content");
}

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);
}

Sometimes we have the need to delete a file that has expired in a directory that can be implemented by the following function:

#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)
{//delete files for days before the Cleandir directory
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-&GT;D_NAME,&AMP;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);
}

The program has a flaw, that is, for programtest can not have more subdirectories.

You can start a program from a shell script, script content:

#!/bin/sh

/home/henry/programtest/cleanfile

Exit

Where/home/henry/programtest/cleanfile is the path to the executable file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.